Cake PHP 3.5 multiple pagination - php

I have a media library where i load images and videos, both should have a pagination. So far it works and i can load e.g. the page 2,3... with ajax and replace the content for each entity.
Now the problem is the first page + the first- and prev- buttons if they point to the first page from each entity. Why?
The created links look like :
working one : http://www.xyz.de/media?image[page]=2
not working : http://www.xyz.de/.media
This occurs in the image- and video-paginaton for the first page.
What I am doing wrong? I read the Paginator-Component and the Paginator-Helper Doku 100 times and can firure it out.
I have my MediaController where I set up my paginator as follows :
public $paginate = [
'Images' => ['limit' => 10, 'scope' => 'image'],
'Videos' => ['limit' => 8, 'scope' => 'video'],
'limit' => 12
];
than in my controller-action i set the values :
$images = $this->paginate($imageTable->find('all',$image_options), ['scope' => 'image']);
$videos = $this->paginate($videosTable->find('all', $video_options), ['scope' => 'video']);
$this->set(compact('images', 'videos'));
in my view I use the PaginatorHelper to set e.g. the numbers like so
$paginator->numbers(['model' => 'Images'])

Related

Upload a File to a Task on Cerb API

At the moment I am adding a task to Cerb using the API (Docs: https://cerb.ai/docs/api/). I can add and edit comments on the task.
An extension is to add file uploads. The API documentation seems to be extremely limited in terms of file uploads and isn't helping much. I have based my current code on https://cerb.ai/docs/api/endpoints/records/#update, using a custom field linked to the task and posting directly to the API.
The process is as follows:
1 - User inserts information on a Laravel form
2 - Form is submitted, task is created
3 - If the user entered a comment or description, the controller then uses the ID from the newly created task toa dd the comment
4 - If the user adds a file, the controller should then use the same ID from the task to attach the file
Task and comment adding has been done through models, which were previously done directly through the API and reformatted once they worked.
Add Task:
$params = [
'title' => $request->input('title'),
'links' => ['project:'.$request->input('project')],
'priority' => $request->input('priority'),
'status_id' => 2
];
$due_date = $request->input('due');
if(!empty($due_date)){
$params['due'] = strtotime($due_date);
}
$success_task = $task->addUpdateRecord('tasks.add', $params);
Add Comment:
$params = [
'author__context' => 'bot',
'author_id' => 3,
'comment' => empty($comment_input) ? request()->comment : $comment_input,
'target__context' => 'task',
'target_id' => $task_id,
'author' => Auth::user()->name // custom field
];
$success = $comment->addUpdateRecord('comments.add', $params);
The addUpdateRecord() essentially does the same thing as the file upload as below:
$cerb = new CerbApi();
$out = $cerb->put($root.'records/file_bundle/1054.json?expand=custom_', $putfields);
$putfields contains a normal file upload object.
I'm getting the following error on request:
array:2 [▼
"__status" => "error"
"message" => "Unknown command (root/records/task/1054.json)"
]
(root removed for confidentiality)
Has anyone worked with this? Anyone able to assist?
Ended up getting it right by using the attachments record type https://cerb.ai/docs/records/types/attachment/ and creating a model for it with its own addUpdateRecord() method.
I then used the records API to do the post for the file as the first step instead of the last one:
$file_upload = new File;
// Get file contents
$data = file_get_contents($file);
// Base64 encode contents
$data = base64_encode($data);
$file_params = [
'name' => $file->getClientOriginalName(),
'content' => $data
];
// Do upload
$file_success = $file_upload->addUpdateRecord('files.add', $file_params);
// Only need to return the file ID to link it to the task
return $file_success["data"]["id"];
And then using the ID that was being returned to link it to the task:
$task = new Task();
$params = [
'title' => $request->input('title'),
'links' => [
'project:'.$request->input('project'),
'file:'.$file_id
],
'priority' => $request->input('priority'),
'status_id' => 2
];
I hope this helps someone in the future.
Note: Cerb API Version 9.2.0

Yii - Pass model ID into a view from CButtonColumn image

Alright, still new to Yii I am attempting to create a button in a column of CGridView. This button, when clicked, will take the user to the "view.php" page and display the information of the node they clicked on by passing in its ID.
I am frustrated that I cannot figure out how to simply add a link to the image that will direct my users. Here are some snippets of the code I have been working on for the past couple of days.
index.php
<?php echo CHtml::beginForm(); ?>
<?php
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'nodes-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'class' => 'CButtonColumn',
'template' => '{restart}',
'buttons' => array
(
'restart' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array('id'=>'NB_bounce_Button'),
'url'=>array('view', 'id'=>$model->id),
)
),
),
/* 'id', */
'name',
'url',
'description',
'node_type',
'last_bounced',
//..
NodeBouncerController.php (View action)
/**
* Displays a particular model.
* #param integer $id the ID of the model to be displayed
*/
public function actionView($id) {
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
The button is on the left. (Refresh green arrow)
What am I doing wrong? More specifically, am I using the buttons of CButtonColumn incorrectly? If so, how may I fix this?
My company uses: Yii v1.1.8.r3324
EDIT: [8/10/15]
It seems I may have been overcomplicating it. What I needed was to simply have an image-link that when clicked went to the view for that particular node that was clicked. Well, Gii auto-generates the particular view I needed and the code associated with it (as seen above).
My fix was to do away with the over complicated mess that I had and keep it simple. Use the template already provided for "view" and just alter it to my needs.
Like so:
/* 'id', */
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons' => array
(
'view' => array
(
'label'=>'Restart this node',
'imageUrl'=>Yii::app()->baseUrl.'/images/refresh.png',
'options'=>array
(
'id'=>'NB_bounce_Button'
)
)
),
),
'name',
'url',
'description',
'node_type',
'last_bounced',
//....
It would be nice to know how to do this manually but I needed something to simply work for now and expand on it later for work.
I think the problem is url of the button. Because you did not include controllerID in the url. You should use this:
'url' => 'Yii::app()->createUrl("nodeBouncer/view", "id"=>$model->id)';
For creating url in yii, usually you should follow this pattern:
contollerID/actionID

Different current page in one controller

I am using pagination on my CakePHP page and it works well, but I have a few pages generated by different functions in one controller file and there is an cumbersome issue.
Let's assume I've got two tables with hundreds of entries, which my paginators divide and display 15 of entries on one page:
public $paginate = array(
'Table1' => Array(
'order' => array('Table1.id' => 'desc'),
'limit' => 15
),
'Table2' => Array(
'order' => array('Table2.id' => 'asc'),
'limit' => 15
)
);
And functions in this controller file:
public function showTable1() {
$this->set('table', $this->paginate('Table1'));
}
public function showTable2() {
$this->set('table', $this->paginate('Table2));
}
When I go to the next page while displaying "Table 1", I go to the next page and it works well. But when I open another page with "Table 2" generated by another function from the same controller file, it unfortunately also goes to the second page.
What should I do to have different "pages indicators"? I mean, when I go to the third page on "Table 1", I don't want to go to the third page when I go to "Table 2".
Thanks in advance. I hope I presented my problem clearly.
instead of setting the pagination options on the top of the controller, try deleting that public $paginate and setting all the options inside action, like this (and in the similar way for the second action)
public function showTable1() {
$options = array(
'conditions' => array(
'Table1.id' => whatever
),
'fields' => array(
'Table1.id'
),
'order' => array(
'Table1.id' => 'DESC'
),
'limit' => 15
);
$this->Paginator->settings = $options;
$table = $this->Paginator->paginate('Table1');
$this->set(compact('table'));
}

CakePHP pagination ignoring page number

I have some simple pagination set up in my controller like so:
public function index() {
$this->set('items', $this->paginate());
}
In my view I'm using the Paginator helper to output numbered links:
echo $this->Paginator->numbers(array(
'separator' => '',
'tag' => 'li',
'currentTag' => 'a',
'currentClass' => 'active'
));
This all works fine, however I want to use a custom URL for the paginated links. I added this to my routes.php file:
Router::connect('/things/:page', array('controller' => 'things', 'action' => 'index'), array('page' => '[0-9]+'));
Now the links outputted by the Paginator helper are the way I want. They look like http://mysite.com/things/2, http://mysite.com/things/3 etc.
But when I click the links the Paginator in my controller doesn't seem to recognize it's on a certain page, as I just get the first page's results shown. I'm guessing I need to somehow pass the page number to $this->paginate(), but I don't know what the best method is. Is there a way for Cake to get the page number automatically if I modify my route?
Thanks!
Since the Pagination component by default expect named parameter 'page:1' you need somehow to pass same variable in index.
if you make print of $this->request->params in your controller, you will see that it's missing.
See that example how you can pass named parameters in the Router:
Router::connect(
'/:controller/:action/*',
array(),
array(
'named' => array(
'wibble',
'fish' => array('action' => 'index'),
'fizz' => array('controller' => array('comments', 'other')),
'buzz' => 'val-[\d]+'
)
)
);
For more info see this section in Cakephp book

cakephp - view with data from multiple tables

i am working on a Cakephp 2.x. I have two tables in my app: 'Images' and 'Audios'... i have a view in which i want to show the images and videos both sorted by date ... dont know how to do this ... because the main problem is in Cakephp if you want to show image we do this
echo $data['Image']['filename'];
and for audio i do this
echo $data['Audio']['filename'];
we have to specify the modal name specifically.. but now i dont want to do this.. i want to show in random sorted by date.. for example if today someone uploaded an image and yesterday an audio was uploaded ... so the data has to look like this on the view page
audio.mp3 ,image.jpg .....so on
like this ..
i dont want to write like this
foreach($datas as $data){
echo $data['Image']['filename'];
echo $data['Audio']['filename'];
}
here i am explicitly telling that image comes first and then the audio which i dont want ...
my Images Model
function getImagesAndAudio($userid){
$this->bindModel(array(
'belongsTo' => array(
'Audio' => array(
'className' => 'Audio',
'foreignKey' => false,
'conditions' => array(
'Image.user_id = Audio.user_id',
),
'type' => 'LEFT',
)
)
), false);
return $this->find('all', array('conditions' => array('Image.User_id' => $userid),
'contain' => array('Audio' ),
));
}
passing data from controller like this
$data = $this->Image->getImagesAndAudio($userid);
$this->set('datas', $data);

Categories