Example #1
0
 /**
  * Process action.
  *
  * @param Table $table
  * @param $action
  * @param array $ids
  * @param array $options
  * @return \Cake\Network\Response|void
  */
 public function process(Table $table, $action, array $ids = [], array $options = [])
 {
     $idsCount = count($ids);
     $options = Hash::merge($this->_config, $options);
     $_options = ['messages' => $this->__setupMessages($idsCount)];
     $options = Hash::merge($_options, $options);
     $redirect = $options['redirect'];
     $messages = $options['messages'];
     if (!$action) {
         $this->Flash->error($messages['noAction']);
         return $this->_controller->redirect($redirect);
     }
     if ($idsCount == 0) {
         $this->Flash->error($messages['noChose']);
         return $this->_controller->redirect($redirect);
     }
     $behaviors = $table->behaviors();
     if (!in_array('BulkProcess', $behaviors->loaded())) {
         $behaviors->load('Union/Core.BulkProcess');
     }
     $event = Event::dispatch($this->_getEventName($action, 'bulkBefore'), $this->_controller, ['ids' => $ids]);
     if (is_array($event->result)) {
         $ids = $event->result;
         if (count($ids) == 0) {
             $this->Flash->error($messages['noChose']);
             return $this->_controller->redirect($redirect);
         }
         $messages = $this->__setupMessages(count($ids));
     }
     $process = $table->processAction($action, $ids);
     if ($process) {
         if (!empty($messages[$action])) {
             $message = $messages[$action];
         } else {
             $message = __d('union', '{0} processed', Inflector::humanize($this->_controller->name));
         }
         Event::dispatch($this->_getEventName($action), $this->_controller, ['ids' => $ids]);
         $this->Flash->success($message);
         return $this->_controller->redirect($redirect);
     }
     $this->Flash->error(__d('union', 'An error occurred'));
     return $this->_controller->redirect($redirect);
 }