Exemplo n.º 1
0
 /**
  * Default implementation for patch function.
  * Patch requests are routed here by the main Sync function.
  * These requests are usually coming from lists togglers and state buttons.
  * Usually there will be no need to override this function.
  *
  * @return void
  */
 protected function patch()
 {
     // Check for token
     JSession::checkToken() or K2Response::throwError(JText::_('JINVALID_TOKEN'));
     // Batch update
     $ids = $this->input->get('id', array(), 'array');
     JArrayHelper::toInteger($ids);
     $states = $this->input->get('states', array(), 'array');
     $mode = $this->input->get('mode', 'apply', 'string');
     // Ensure we have ids
     $ids = array_filter($ids);
     if (!count($ids)) {
         K2Response::throwError(JText::_('K2_NO_ROWS_SELECTED'), 401);
     }
     foreach ($ids as $key => $id) {
         $data = array();
         $data['id'] = $id;
         foreach ($states as $state => $values) {
             $value = is_array($values) ? $values[$key] : $values;
             if ($value != '') {
                 $data[$state] = $value;
             }
         }
         if ($mode == 'clone') {
             $sourceData = $this->model->getCopyData($id);
             $data = array_merge($sourceData, $data);
             $data['id'] = null;
             $this->model->setState('patch', false);
         } else {
             $this->model->setState('patch', true);
         }
         $this->model->setState('data', $data);
         $result = $this->model->save();
         if (!$result) {
             K2Response::throwError($this->model->getError());
         }
     }
     // Trigger change state event for items and categories
     if ($mode != 'clone' && isset($states['state']) && in_array($this->resourceType, array('items', 'categories'))) {
         // Get dispatcher
         $dispatcher = JDispatcher::getInstance();
         // Import content plugins
         JPluginHelper::importPlugin('content');
         if ($this->resourceType == 'items') {
             $eventName = 'onContentChangeState';
             $context = 'com_k2.' . $this->resourceType;
         } else {
             if ($this->resourceType == 'categories') {
                 $eventName = 'onCategoryChangeState';
                 $context = 'com_k2';
             }
         }
         $dispatcher->trigger($eventName, array($context, $ids, $states['state']));
     }
     K2Response::setResponse($result);
 }