CakePHP table sorting with bootstrap icons - cakephp

I want to sort a table by id when clicking on it. It is working but the main thing is that the bootstrap arrow up and down icons should also be attached to the id label.
When the data is displayed in descending order, then the bootstrap arrow-bottom icon should be shown, and when the data is displayed in ascending order, then the arrow-up icon should be shown.
UsersController.php
public $paginate = array('limit'=>4);
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
index.ctp
<div class="users index">
<h2><?php echo __('Users'); ?></h2>
<table class="zebra-striped table-bordered " cellpadding="0" cellspacing="0">
<tr>
<th>
<a href='' >
<?php echo $this->Paginator->sort('id'); ?>
<i class='icon-arrow-up'></i>
</a>
</th>
<th><a href='' >First Name <i class='icon-arrow-down'></i>
</a> </th>
<th>Last Name <i class='icon-resize-full'></i></a></th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo h($user['User']['id']); ?> </td>
<td><?php echo h($user['User']['first_name']); ?> </td>
<td><?php echo h($user['User']['last_name']); ?> </td>
</tr>
<?php endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?> </p>
<div>
<div class="pagination">
<ul>
<?php
echo $this->Paginator->prev(__('prev'), array('tag' => 'li'),
null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
echo $this->Paginator->numbers(array('separator' => '','currentTag'
=> 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
?>
</ul>
</div>

A Simple Solution using Jquery, just add the below 3 lines in your default application.js or in layout/default.ctp
$('th a').append(' <i class="icon-sort"></i>');
$('th a.asc i').attr('class', 'icon-sort-down');
$('th a.desc i').attr('class', 'icon-sort-up');
Another Simple CSS Based Solution, Include Font Awesome's necessary files.
th > a:after {
content: " \f0dc";
font-family: FontAwesome;
}
th > a.asc:after {
content: " \f0dd";
font-family: FontAwesome;
}
th > a.desc:after {
content: " \f0de";
font-family: FontAwesome;
}

Use PaginatorHelper::sortDir() to check the current sort direction and build the class name based on that value:
<i class='icon-arrow-<?php echo $this->Paginator->sortDir() === 'asc' ? 'up' : 'down'; ?>'></i>
To embed this into a sort link, pass the HTML as the second parameter to PaginatorHelper::sort(), and set the escape option to false:
$type = $this->Paginator->sortDir() === 'asc' ? 'up' : 'down';
$icon = "<i class='icon-arrow-" . $type . "'></i>";
echo $this->Paginator->sort('id', $icon, array('escape' => false));
That should result in a link like:
<i class='icon-arrow-up'></i>

You need also check a key, so it would be
<?php echo $this->Paginator->sort('title', 'My great title'); ?>
<?php if ($this->Paginator->sortKey() == 'title'): ?>
<i class='fa fa-sort-<?php echo $this->Paginator->sortDir() === 'asc' ? 'up' : 'down'; ?>'></i>
<?php else: ?>
<i class='fa fa-sort'></i>
<?php endif; ?>

Related

Cakephp 3 Three Column View based on an Id

This is Cakephp 3.5, So I have three businesses 1, 2, 3. I have a view to show Estimated Revenue From Fees.
On the index page I have a three column Div
<div class="row">
<div class="col-md-12">
<h2><?= __('Estimated Revenue From Fees') ?></h2>
<div class="row">
<div class="column">
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromCFeesBiz1 as $estimatedRevenueFromFeeBiz1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz1->year), ['action' => 'view', $estimatedRevenueFromFeeBiz1->id]) ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="column">
<h4>Business Two</h4>
<?php foreach ($estimatedRevenueFromFeesNauBiz2 as $estimatedRevenueFromFeeBiz2): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeNauBiz2->year), ['action' => 'view', $estimatedRevenueFromFeeBiz2->id]) ?>
</li>
<?php endforeach; ?>
</div>
<div class="column">
<h4>Business Three</h4>
<?php foreach ($estimatedRevenueFromCourseFeesUa as $estimatedRevenueFromCourseFeeUa): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz3->year), ['action' => 'view', $estimatedRevenueFromFeeBiz3->id]) ?>
</li>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
In my controller I've set up like this:
public function index()
{
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1));
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFeesBiz2->find('list', array( 'conditions' => 'business_id' == 2));
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFeesBiz3->find('list', array( 'conditions' => 'business_id' == 1));
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
I'm getting a Call to a member function find() on boolean
Error in: ROOT\src\Controller\EstimatedRevenueFromCourseFeesController.php, line 26 which is the
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1)); line.
I figured it out. I changed the controller back to just calling the whole table:
public function index()
{
$this->paginate = [
'contain' => ['Businesses']
];
$estimatedRevenueFromFees = $this->paginate($this->EstimatedRevenueFromFees);
$this->set(compact('estimatedRevenueFromFees'));
}
I then added a condition into the index.ctp file:
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromFees as $estimatedRevenueFromFee): ?>
<?php if ($estimatedRevenueFromFee['business_id'] == 1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFee->year), ['action' => 'view', $estimatedRevenueFromFee->id]) ?>
</li>
<?php else: ?>
<?php continue; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Well, based on what you said and on your own answer, it looks like what you were trying to do was this:
public function index() {
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 2]]);
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
What changes is your condition (business_id). But you are always querying over the same table (EstimatedRevenueFromFees).
Bare in mind that in your solution you are paginating. So you might end up having every record in a page with a certain business_id, or whatever combination.

cakephp, link to retrieve status variable

Total n00b in CakePhp
Im trying to link to the view retrieved data from variable $status, defined in the controller, given the simple condition "done" or "pending".
Given the model:
<?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'Title of a task cannot be empty'
)
);
}?>
The Task Controller:
function index($status=null) {
if($status == 'done')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1')));
else if($status == 'pending')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0')));
else
$tasks = $this->set('Tasks', $this->Task->find('all'));
$this->set('tasks', $tasks);
$this->set('status', $status);
}
And finally The View:
<h2>Tasks</h2>
<?php if(empty($Tasks)): ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($Tasks as $Task): ?>
<tr>
<td>
<?php echo $Task['Task']['title'] ?>
</td>
<td>
<?php
if($Task['Task']['done']) echo "Done";
else echo "Pending";
?>
</td>
<td>
<?php echo $Task['Task']['created'] ?>
</td>
<td>
<?php echo $Task['Task']['modified'] ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action'=>'edit', $Task['Task']['id'])); ?>
<?php echo $this->Html->link('Delete', array('action'=>'delete', $Task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index','done')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index', 'pending')); ?><br />
Clicking the "Done Task" returns an empty list (There are no tasks in this list). Can anyone see what is wrong on the link? Thanks in advance!
You are mixing up the casing in the variable name $Tasks. In your example you are assigning the array to $tasks, but then in your view, your are trying to check a variable names $Tasks, which will turn up empty because it has never been properly set.
Change this line:
$this->set('tasks', $tasks);
To:
$this->set('Tasks', $tasks);
As mentioned by McWayWeb above to case of the variable is incorrect.
As you are setting lower case vars to use in your view i would make the change there. Change all counts of Task in the view to task.
I would also consider setting all viewVars at the same time using the below
$this->set(compact('tasks','status));

How to correctly move CakePHP tree items up and and down?

Im stuck with moving items of a tree up and down, the item is not being moved to the correct position.
Please check these pictures:
https://docs.google.com/a/wgs.co.id/file/d/0B88jB6a4wxkTdTNtTGl1YjhXdTQ/edit?usp=drivesdk
https://docs.google.com/a/wgs.co.id/file/d/0B88jB6a4wxkTcW5pSFJyUnVhejA/edit?usp=drivesdk
My code for moving items is based on http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveDown
Here is my code:
Controller
function admin_moveup($id = null, $delta = null) {
$this->ProductNeed->id = $id;
if (!$this->ProductNeed->exists()) {
throw new NotFoundException(__('Invalid id'));
}
if ($delta > 0) {
$this->ProductNeed->moveUp($this->ProductNeed->id, abs($delta));
} else {
$this->Session->setFlash(
'Silahkan tentukan posisi nomor order terlebih dahulu'
);
}
return $this->redirect(array('admin' => true, 'controller' => $this->params['controller'], 'action' => 'index'));
}
function admin_movedown($id = null, $delta = null) {
$this->ProductNeed->id = $id;
if (!$this->ProductNeed->exists()) {
throw new NotFoundException(__('Invalid id'));
}
if ($delta > 0) {
$this->ProductNeed->moveDown($this->ProductNeed->id, abs($delta));
} else {
$this->Session->setFlash(
'Silahkan tentukan posisi nomor order terlebih dahulu'
);
}
return $this->redirect(array('admin' => true, 'controller' => $this->params['controller'], 'action' => 'index'));
}
View
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Order</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($data['rows'])) {
$i = 0;
foreach($data['rows'] as $key => $value) {
$i++;
?>
<tr id="row-<?php echo $i; ?>">
<td><?php echo $i; ?></td>
<td><?= $value ?></td>
<td>
<?php
echo $this->Html->link(
'Up',
'/admin/'.$this->params['controller'].'/moveup/'.$key.'/'.$i
);
?>
<?php
echo $this->Html->link(
'Down',
'/admin/'.$this->params['controller'].'/movedown/'.$key.'/'.$i
);
?>
</td>
<td>
<ul class="action">
<li><i class="glyphicon glyphicon-edit" data-toggle="tooltip" title="Ubah"></i></li>
<li><i class="glyphicon glyphicon-ban-circle" data-toggle="tooltip" title="Hapus"></i></li>
</ul>
<?= $html->modalBoxDelete($i, $key) ?>
</td>
</tr>
<?php
}
} else {
echo $html->noData(4);
}
?>
</tbody>
</table>
Re-read the docs that you've linked, the code takes a delta value, ie the numer of positions the item should be moved.
If you'd wanted to move an item by just one row, then you'll have to use a delta of 1, your view template code however passes the items index in the array as the delta value, so for the Kesehan Mata row in your example that would be a 4, hence the item is moved to after Stamina Vitalitas, which is 4 top level items ahead.
So either pass a static value of 1, like
'/admin/'.$this->params['controller'].'/moveup/'.$key.'/1'
'/admin/'.$this->params['controller'].'/movedown/'.$key.'/1'
or if you don't plan to ever move the items by more than 1, simply remove the delta argument alltogether and hardcode the value passed to TreeBehavior::up/down() in your controller methods.

Delete multiple rows by ticking Checkbox

How do I delete multiple rows by checking a checkbox?
I have a table of records, and I'd like to to delete multiple rows at once.
This is my my deleteselected function:
UPDATED
public function order() {
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
and in my view.
<?php
echo $this->Form->create('Order',array('url' => array('controller' => 'admin', 'action' => 'order'))); ?>
<table class="table table-bordered table-hover">
<th>Name</th>
<th>Email</th>
<th>phone</th>
<th>view</th>
<th>delete</th>
<thead>
</tr>
</thead>
<tbody>
<?php foreach ($order as $orders): ?>
<tr>
<td><?php echo $orders['Order']['varfullname']; ?></td>
<td><?php echo $orders['Order']['varemailid']; ?></td>
<td><?php echo $orders['Order']['varphone']; ?></td>
<td>
<?php echo $this->Html->link('',
array('controller' => 'orders', 'action' => 'view', $orders['Order']['id']), array('title'=>'VIEW','class' => 'fa fa-search'));?>
</td>
<td><input type="checkbox" name="order_id[]" value ="<?php echo $orders['Order']['id'];?>" hiddenField = "false">
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</br>
<?
echo $this->Form->end('Deleteselected'); ?>
</div>
</div>
</br>
</div>
<!-- /.row -->
after using below answer my delete selected working fine
Use in ctp
//in ctp Along with EACH record use Checkbox integration . Dont use Checkbox code u used.
<input type="checkbox" name="order_id[]" value ="<?php echo $referenceSingle['Order']['id'];?>" hiddenField = "false">
//In controller
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
UPDATE
Use Above Code in your Index method itself ,Dont create deleteAll method.
OR
Make <form action TO deleteAll
LIKE
echo $this->Form->create('searchCourse',array('url' => array('controller' => 'Courses', 'action' => 'index')));
UPDATE
For proper Workflow refer :
ALWAYS do paperwork and Algorithm ,Before Coding SIR.

CakePHP 2 How to pass variables through the URL when using pagination?

I have a project in CakePHP application and I want to add pagination to it. I have added pagination to it, but when I click on a page number or go to the next page my application breaks.
The page relies on variables being passed to the controller in order to display the correct data. Here is my code for the view:
<table class="table table-striped">
<thead>
<tr>
<th>Job ID</th>
<th>Date</th>
<th>Site</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $jobsheet) { ?>
<tr>
<td>
<?php echo $jobsheet['Jobsheet']['jobnum']; ?>
</td>
<td>
<?php echo date("l, jS F Y", strtotime($jobsheet['Jobsheet']['jobdate'])); ?>
</td>
<td>
<span class="companyname"><strong><?php echo $jobsheet['Siteaddress']['sitename'] ?></strong></span><br />
<?php echo $jobsheet['Siteaddress']['addressline1']; ?>,
<?php echo $jobsheet['Siteaddress']['addressline2']; ?>,
<?php if ( $jobsheet['Siteaddress']['addressline3'] = '') { ?>
<?php echo $jobsheet['Siteaddress']['addressline3']; ?>,
<?php } else { ?>
<?php } ?>
<?php echo $jobsheet['Siteaddress']['city']; ?>,
<?php echo $jobsheet['Siteaddress']['county']; ?>,
<?php echo $jobsheet['Siteaddress']['country']; ?>,
<?php echo $jobsheet['Siteaddress']['postcode']; ?>
</td>
<td><a data-toggle="modal" href="#viewjobsheet<?php echo $jobsheet['Jobsheet']['id']; ?>" onClick="document.getElementById('jbif<?php echo $jobsheet['Jobsheet']['id']; ?>').src='/modal/customers/<?php echo $company['Company']['id']; ?>/jobs/<?php echo $jobsheet['Jobsheet']['id']; ?>/view/';" title="View" class="icon-eye-open"></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="controls">
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<br />
<br />
<?php echo $this->Paginator->counter(array(
'format' => 'Page {:page} of {:pages}, showing {:current} records out of
{:count} total, starting on record {:start}, ending on {:end}'
));
?>
</div>
</div>
This is the controller:
function index(){
$companyid = $this->params['customer'];
$this->set('companyid', $companyid);
$siteid = $this->params['site'];
$this->loadModel('Jobsheet');
// Job Sheets
$jobsheets = $this->Jobsheet->find('all', array('conditions' => array('Jobsheet.company' => $companyid), 'recursive' => 2, 'order' => 'Jobsheet.jobdate DESC', 'limit' => '10'));
$this->set('jobsheets', $jobsheets);
$this->paginate = array(
'limit' => 10
);
$data = $this->paginate('Jobsheet');
$this->set(compact('data'));
}
While the pagination does work, it ends up loosing the variable and this breaks what I'm trying to do. What am i missing?
One method is to use PaginatorHelper::options (see the API)
For example, say we want to pass the variable $pass between Paginator page changes. So first pass the variable to the View.
public function index() {
$pass = 'testString';
$this->set(compact('pass'));
$this->set('posts', $this->paginate());
}
In the View, we can then add this to the parameters that Paginator passes when changing pages using Paginator::options and the url option.
<?php
$this->Paginator->options(array(
'url' => array(
'pass' => $pass
)
));
?>
So if you moved from page 1 to page 2, the resulting url would be something similar to:
http://localhost/cake/posts/index/pass:testString/page:2
This is also how Paginator passes sort orders and sort keys. So while "pass" in pass:testString can be anything you want, be sure not to use sort, direction, and page.
As long as you are changing pages through the Paginator links, the passed variables will remain. Note that you can access the values of the passed arguments in both the View and the Controller by the property:
$this->passedArgs

Resources