Example #1
0
 /**
  * Save a type
  *
  * @return     void
  */
 public function saveTask($redirect = false)
 {
     // Check for request forgeries
     Request::checkToken();
     $prop = Request::getVar('prop', array(), 'post');
     $url = 'index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=edit&id[]=' . $prop['id'];
     // Initiate extended database class
     $row = new \Components\Publications\Tables\Category($this->database);
     if (!$row->bind($prop)) {
         $this->addComponentMessage($row->getError(), 'error');
         App::redirect($url);
         return;
     }
     // Get the custom fields
     $fields = Request::getVar('fields', array(), 'post');
     if (is_array($fields)) {
         $elements = new stdClass();
         $elements->fields = array();
         foreach ($fields as $val) {
             if ($val['title']) {
                 $element = new stdClass();
                 $element->default = isset($val['default']) ? $val['default'] : '';
                 $element->name = isset($val['name']) && trim($val['name']) != '' ? $val['name'] : $this->_normalize(trim($val['title']));
                 $element->label = $val['title'];
                 $element->type = isset($val['type']) && trim($val['type']) != '' ? $val['type'] : 'text';
                 $element->required = isset($val['required']) ? $val['required'] : '0';
                 foreach ($val as $key => $v) {
                     if (!in_array($key, array('default', 'type', 'title', 'name', 'required', 'options'))) {
                         $element->{$key} = $v;
                     }
                 }
                 if (isset($val['options'])) {
                     $element->options = array();
                     foreach ($val['options'] as $option) {
                         if (trim($option['label'])) {
                             $opt = new stdClass();
                             $opt->label = $option['label'];
                             $opt->value = $option['label'];
                             $element->options[] = $opt;
                         }
                     }
                 }
                 $elements->fields[] = $element;
             }
         }
         include_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'models' . DS . 'elements.php';
         $re = new \Components\Publications\Models\Elements($elements);
         $row->customFields = $re->toString();
     }
     // Get parameters
     $params = Request::getVar('params', '', 'post');
     if (is_array($params)) {
         $txt = array();
         foreach ($params as $k => $v) {
             $txt[] = "{$k}={$v}";
         }
         $row->params = implode("\n", $txt);
     }
     // Check content
     if (!$row->check()) {
         App::redirect($url, $row->getError(), 'error');
         return;
     }
     // Store new content
     if (!$row->store()) {
         App::redirect($url, $row->getError(), 'error');
         return;
     }
     // Redirect to edit view?
     if ($redirect) {
         App::redirect($url, Lang::txt('COM_PUBLICATIONS_CATEGORY_SAVED'));
     } else {
         App::redirect('index.php?option=' . $this->_option . '&controller=' . $this->_controller, Lang::txt('COM_PUBLICATIONS_CATEGORY_SAVED'));
     }
     return;
 }