Ngu ngơ học làm web (x50) - CakePHP2 - postLink trong form, updateAll

Tiếp theo của: Ngu ngơ học làm web (x49) - CakePHP2 – Lưu trên nhiều bảng
-----

Phần x50. CakePHP2 – postLink trong form, updateAll


Xem (clip số 48 – chickenrainshop):

- update nhiều mẩu tin cùng lúc (48)

- checkbox (2":17)

- select box (3":06)

- form chứa các checkbox và select box không nằm cạnh nhau (5":04)

- Xử lý tình trạng vừa có postLink vừa có submit (6":31) 

Trong OrdersController, sửa action index() thành admin_index()

[OrdersController.php]

public function admin_index() {
                        $this->Order->recursive = 0;
                        $this->set('orders', $this->Paginator->paginate());
            }

Trong View\Orders, sửa view index.ctp thành admin_index.ctp.

[admin_index.ctp]

<div class="orders index">
            <h2><?php echo __('Orders'); ?></h2>
            <table cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                                    <th>Select</th>
                                    <th><?php echo $this->Paginator->sort('id', 'Mã đơn hàng'); ?></th>
                                    <th><?php echo $this->Paginator->sort('user_id', 'User'); ?></th>
                                    <th><?php echo $this->Paginator->sort('customer_info', 'Email'); ?></th>
                                    <th><?php echo $this->Paginator->sort('payment_info', 'Giá trị đơn hàng'); ?></th>
                                    <th><?php echo $this->Paginator->sort('status', 'Tình trạng'); ?></th>
                                    <th><?php echo $this->Paginator->sort('created', 'Ngày tạo'); ?></th>
                                    <th><?php echo $this->Paginator->sort('modified', 'Ngày xử lý'); ?></th>
                                    <th class="actions"><?php echo __('Actions'); ?></th>
            </tr>
            </thead>
            <tbody>
            <?php echo $this->Form->create('Order', ['url'=>['action' => 'process']]); ?>
            <?php foreach ($orders as $order): ?>
            <tr>
                        <td>
                                    <?php echo $this->Form->checkbox('Order.id.'.$order['Order']['id']); ?>
                        </td>
                        <td><?php echo h($order['Order']['id']); ?>&nbsp;</td>
                        <td>
                                    <?php echo $this->Html->link($order['User']['fullname'], array('controller' => 'users', 'action' => 'view', $order['User']['id'])); ?>
                        </td>
                        <td><?php echo json_decode($order['Order']['customer_info'])->email; ?>&nbsp;</td>
                        <td><?php echo $this->Number->currency(json_decode($order['Order']['payment_info'])->total, ' VND', ['places' => 0, 'wholePosition' => 'after']); ?>&nbsp;</td>
                        <td>
                                    <?php if ($order['Order']['status'] == 0): ?>
                                                Chưa xử lý
                                    <?php elseif ($order['Order']['status'] == 1): ?>
                                                Đã xử lý
                                    <?php else: ?>
                                                Hủy
                                    <?php endif ?>
                        </td>
                        <td><?php echo $this->Time->format('d-m-Y', $order['Order']['created'], null, 'Asia/Ho_Chi_Minh'); ?>&nbsp;</td>
                        <td><?php echo $this->Time->format('d-m-Y', $order['Order']['modified'], null, 'Asia/Ho_Chi_Minh'); ?>&nbsp;</td>
                        <td class="actions">
                                    <?php echo $this->Html->link(__('View'), array('action' => 'view', $order['Order']['id'])); ?>
                                    <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $order['Order']['id'])); ?>
                                    <?php //echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $order['Order']['id']), array('confirm' => __('Are you sure you want to delete # %s?', $order['Order']['id']))); ?>
                        </td>
            </tr>
<?php endforeach; ?>
            </tbody>
            </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 class="paging">
            <?php
                        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
                        echo $this->Paginator->numbers(array('separator' => ''));
                        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
            ?>
            </div>
            <?php echo $this->Form->select('action', ['1' => 'Chấp nhận đơn hàng', '2' => 'Tạm ngưng đơn hàng', '3' => 'Hủy đơn hàng'], ['empty' => false]); ?>
            <?php echo $this->Form->end('Submit'); ?>
</div>
<div class="actions">
            <h3><?php echo __('Actions'); ?></h3>
            <ul>
                        <li><?php echo $this->Html->link(__('New Order'), array('action' => 'add')); ?></li>
                        <li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
                        <li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
            </ul>
</div>

[OrdersController.php action admin_ process()]

            public function admin_process() {
                        foreach ($this->request->data['Order']['id'] as $id => $value) {
                                    if ($value == 1) {
                                                $ids[] = $id;
                                    }
                        }
                        if (!empty($ids)) {
                                    switch ($this->request->data['Order']['action']) {
                                                case 1:
                                                if ($this->Order->updateAll(['Order.status' => 1], ['Order.id' => $ids])) {
                                                            $this->Flash->success(__('Đã xử lý đơn hàng thành công!'));
                                                } else {
                                                            $this->Flash->error(__('Có lỗi xảy ra, vui lòng thử lại!'));
                                                }
                                                break;
                                                case 2:
                                                if ($this->Order->updateAll(['Order.status' => 0], ['Order.id' => $ids])) {
                                                            $this->Flash->success(__('Đã tạm ngưng đơn hàng thành công!'));
                                                } else {
                                                            $this->Flash->error(__('Có lỗi xảy ra, vui lòng thử lại!'));
                                                }
                                                break;
                                                case 3:
                                                if ($this->Order->updateAll(['Order.status' => 2], ['Order.id' => $ids])) {
                                                            $this->Flash->success(__('Đã hủy đơn hàng thành công!'));
                                                } else {
                                                            $this->Flash->error(__('Có lỗi xảy ra, vui lòng thử lại!'));
                                                }
                                                break;
                                                default:
                                                            $this->Flash->error(__('Không có xử lý này, vui lòng thử lại!'));
                                                break;
                                    }          
                        } else {
                                    $this->Flash->error(__('Bạn chưa chọn đơn hàng để xử lý!'));
                        }
                        $this->redirect(['action' => 'index']);

            }
-----------
Cập nhật 2/8/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web