public function save() { // check for request forgeries YRequest::checkToken() or jexit('Invalid Token'); // init vars $db = JFactory::getDBO(); $config = JFactory::getConfig(); $now = JFactory::getDate(); $post = YRequest::get('post'); $frontpage = YRequest::getBool('frontpage', false); $categories = YRequest::getArray('categories', null); $details = YRequest::getArray('details', null); $metadata = YRequest::getArray('meta', null); $cid = YRequest::getArray('cid.0', '', 'int'); $tzoffset = $config->getValue('config.offset'); $post = array_merge($post, $details); try { // get item table $table = YTable::getInstance('item'); // get item if ($cid) { $item = $table->get($cid); } else { $item = new Item(); $item->application_id = $this->application->id; $item->type = YRequest::getVar('type'); } // bind item data $item->bind($post, array('elements', 'params', 'created_by')); $created_by = isset($post['created_by']) ? $post['created_by'] : ''; $item->created_by = empty($created_by) ? JFactory::getUser()->id : $created_by == 'NO_CHANGE' ? $item->created_by : $created_by; $tags = isset($post['tags']) ? $post['tags'] : array(); $item->setTags($tags); // bind element data foreach ($item->getElements() as $id => $element) { if (isset($post['elements'][$id])) { $element->bindData($post['elements'][$id]); } else { $element->bindData(); } } // set alias $item->alias = ItemHelper::getUniqueAlias($item->id, YString::sluggify($item->alias)); // set modified $item->modified = $now->toMySQL(); $item->modified_by = $this->user->get('id'); // set created date if ($item->created && strlen(trim($item->created)) <= 10) { $item->created .= ' 00:00:00'; } $date = JFactory::getDate($item->created, $tzoffset); $item->created = $date->toMySQL(); // set publish up date if (strlen(trim($item->publish_up)) <= 10) { $item->publish_up .= ' 00:00:00'; } $date = JFactory::getDate($item->publish_up, $tzoffset); $item->publish_up = $date->toMySQL(); // set publish down date if (trim($item->publish_down) == JText::_('Never') || trim($item->publish_down) == '') { $item->publish_down = $db->getNullDate(); } else { if (strlen(trim($item->publish_down)) <= 10) { $item->publish_down .= ' 00:00:00'; } $date = JFactory::getDate($item->publish_down, $tzoffset); $item->publish_down = $date->toMySQL(); } // get primary category $primary_category = @$post['params']['primary_category']; if (empty($primary_category) && count($categories)) { $primary_category = $categories[0]; } // set params $item->params = $item->getParams()->remove('metadata.')->remove('template.')->set('metadata.', @$post['params']['metadata'])->set('template.', @$post['params']['template'])->set('config.enable_comments', @$post['params']['enable_comments'])->set('config.primary_category', $primary_category)->toString(); // save item $table->save($item); // make sure categories contain primary category if (!empty($primary_category) && !in_array($primary_category, $categories)) { $categories[] = $primary_category; } // save category relations if ($frontpage) { $categories[] = 0; } CategoryHelper::saveCategoryItemRelations($item->id, $categories); // set redirect message $msg = JText::_('Item Saved'); } catch (YException $e) { // raise notice on exception JError::raiseNotice(0, JText::_('Error Saving Item') . ' (' . $e . ')'); $this->_task = 'apply'; $msg = null; } $link = $this->baseurl; switch ($this->getTask()) { case 'apply': $link .= '&task=edit&type=' . $item->type . '&cid[]=' . $item->id; break; case 'saveandnew': $link .= '&task=add'; break; } $this->setRedirect($link, $msg); }