Yii 1 Pagination takes every params user inserted - yii

I have my pagination url set to
www...com/category/detail.html?page=2
For which my code is,
$dataProvider = new CActiveDataProvider('Page', array('criteria' => array('condition' => 'status=1', 'condition' => 'category_id=' . $categoryObject -> id, 'order' => 'postDate DESC'), 'pagination' => array('pageSize' => 4,'pageVar'=>'page'), ));
$dataProvider->getData();
var_dump $dataProvider->totalItemCount;
I am getting the exact data counts and my pagination url seems working. I have my URL rule configured as
'index'=>'site/index',
'contact'=>'site/contact',
'privacy'=>'site/privacy',
'sitemap.xml'=>'site/sitemap',
'<category:\w+>' => 'category/detail',
'<category>/page/<page:\d+>' => 'category/detail',
'<category>'=>'category/detail',
'<category:\w+>/<postTitle:.+>' => 'category/post',
'<category:\w.+>/<postTitle:.+>'=>'category/post',
my auto generated pagination URl are working fine but,
If I manually insert url something like
www......com/category/detail.html?Page_page&page=2
www......com/category/detail.html?Page_pizza&page=2
or any stupid things I can put they navigates to the page.
Now, here I want to remove these extra parameters or I want my pagination url to be strict to
www...com/category/detail.html?page=2
and if I put any additional params I want an error page.
I have been working in this for 2 weeks and tried every possible ways I could.

You should set the "params" property of the pagination to an empty array:
'pagination' => array('pageSize' => 4,'pageVar'=>'page', 'params' => array()),
This will avoid exclude any GET parameters from pagination links(if you need any included, add their name there).
To throw an error, you need to check for the parameters in your action.
// remove valid prameters from get array.
$arr = array_diff_key($_GET, array_flip(array('page', 'postTitle', 'category')));
// if still there are parameters left, throw an error.
if(count($arr) > 0) {
throw new CHttpException('invalid parameter');
}

Related

Yii2 - Custom Pagination

What is the right way to have variable pagination in Yii2?
I mean by this, that I want the user to be able to give the number of items in a page while sending an API request.
I know about setting the pageSize in the dataProvider.
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT * FROM user WHERE status=:status',
'params' => [':status' => 1],
'pagination' => [
'pageSize' => 20,
],
]);
But my question is about anything that is built in that allows the user to send the pageSize through the request? Is there anything built-in to perform this function?
Leave empty the pagination field in the dataProvider and just add the per-page GET parameter in your calls:
http://your_url/controlller/action?per-page=20
More info here.

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.

how to edit pagination behaviour in yii

hello i am creating a search module which is taking data from apis..
Now i am getting all result in 1 api call and i am making it as a dataProvider.
this is the code..
$dataProvider = new CArrayDataProvider($result, array(
'sort' => array(
'attributes' => array('name',
),
),
'pagination' => array(
'pageSize' => 10,
),
));
this is working fine and giving pagination. What i want to do is to use limit and ofset of api.
for eg consider the yelp api
http://api.yelp.com/search?term="xxx"&location="xxx"&limit=10&ofset=0;
i want to get only 10 result initially and i need another api call to get next set when i click the pagination [2] or next >.
how can this be done ?
I also need a expert opinion. which one is better.? calling api at single time and fetch all detail once or getting few one by one ? the expected results will be around 200..
Yelp doesn't allow to "cache" its search results in any meaning http://www.yelp.com/developers/getting_started/api_terms (section 6). So I believe you need to do call each time pagination link is clicked.
For this purpose I would create some YelpDataProvider extended from CDataProvider and override required abstract methods.
Pagination:
Not sure I got what kind of problem you faced with, but if you implement your own data provider you will have access to CPagination class instance and its properties pageSize and offset.
pageSize is to be mapped to limit yelp request parameter, offset property - directly to offset request param.
I hope this will help.
http://www.yiiframework.com/doc/api/1.1/CPagination
$dataProvider = new CArrayDataProvider($result, array(
'sort' => array(
'attributes' => array('name'),
),
'pagination' => array(
'pageSize' => 10,
'offset' => 5
),
));

yii CListView is not updating when we delete the records through ajax request

here i am using the ajax request to delete the record from the clistview the record is deleting succesfully but the list view is not updating if we refresh the page then only the record is removing from the clist view can any one help here is my code
<?php
echo CHtml::link(CHtml::encode('Delete'), $this->createUrl('delete', array('id' => $data['id'])), array(// for htmlOptions
'onclick' => ' {' . CHtml::ajax(array(
'type'=>'POST',
'beforeSend' => 'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}',
'url'=>$this->createUrl('delete', array('id' => $data['id'],'ajax'=>'delete')),
'complete'=>'js:function(jqXHR, textStatus){$.fn.yiiListView.update("firstlist");}'
)) .
'return false;}', // returning false prevents the default navigation to another url on a new page
)
);
here is the code for clistview
<?php $this->widget('zii.widgets.CListView', array(
'id' => 'firstlist',
'dataProvider'=>$dataProvider,
'itemView'=>'_beneview',
'enablePagination' => false,
'summaryText' => '',
)); ?>
You could use CHtml::ajaxLink for ajax request, but your way is also fine.
I have filtered CListView results also with paginations enabled, you can just look at the major things for updating list view on this link
Please check view part and updating script part.
Hope his will help.
Use CGridView, you can add CButtonColumn

JCarousel widget on Yii framework don't load image

I'm trying to use JCarousel widget on Yii framework.
My JCarousel must show images of specific user that belong to a specific gallery (for that user).
I retrieve the information about the images from a MySQL DB, in the specific from Photos table, where gallery_id have a determined value. The column from Photos table that contain the name of the photos is "path".
In the code I have written this:
$DataProvider = new CActiveDataProvider('Photos', array('criteria'=>array(
'condition'=>'gallery_id = :id',
'params'=>array(':id'=>$gallery->id),
),));
$this->widget('ext.JCarousel.JCarousel', array(
'dataProvider' => $DataProvider,
'thumbUrl' => '"/images/upload/".$data->id."_".$data->path',
'imageUrl' => '"/images/upload/".$data->id."_".$data->path',
'target' => 'big-gallery-item',
'vertical' => false,
));
With this code I get, on the div, the written message: "No result find"
Where I did i go wrong?
Check your dataProvider. Try get results via $DataProvider->getData() and check that you photos exists.
I do not think that the error in the extension, most likely you do not select data from the database.