/contentmanager/code/trunk/administrator/components/com_contentmanager/models/template.php
PHP | 256 lines | 125 code | 43 blank | 88 comment | 16 complexity | 99f6d048b23b54b6314245837101f989 MD5 | raw file
- <?php
- /**
- * @version $Id: template.php 160 2009-07-09 00:06:09Z eddieajau $
- * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
- * @link http://www.theartofjoomla.com
- */
-
- // no direct access
- defined('_JEXEC') or die;
-
- jximport2('jxtended.application.component.modelitem');
-
- /**
- * @package TAOJ.ContentManager
- * @subpackage com_contentmanager
- */
- class ContentManagerModelTemplate extends JxModelItem
- {
- /**
- * Override to get the template table
- */
- public function &getTable()
- {
- return JTable::getInstance('Template', 'ContentManagerTable');
- }
-
- /**
- */
- public function _populateState()
- {
- $session = &JFactory::getSession();
- $params = JComponentHelper::getParams('com_contentmanager');
-
- // Load the object state.
- $id = (int) $session->get('contentmanager.edit.template.id');
- $this->setState('template.id', $id);
-
- // Load the parameters.
- $this->setState('params', $params);
- }
-
- /**
- * Method to checkin a row.
- *
- * @access public
- * @param integer $id The numeric id of a row
- * @return boolean True on success/false on failure
- */
- public function checkin($id = null)
- {
- // Initialize variables.
- $id = (int) $id;
-
- if ($id === 0) {
- $id = $this->getState('template.id');
- }
-
- if (!$id) {
- return true;
- }
-
- $table = $this->getTable();
-
- // Attempt to check-in the row.
- $return = $table->checkin($id);
-
- // Check for a database error.
- if ($return === false) {
- $this->setError($table->getError());
- return false;
- }
-
- return true;
- }
-
- /**
- * Method to check-out a object for editing.
- *
- * @access public
- * @param int $id The numeric id of the object to check-out.
- * @return bool False on failure or error, success otherwise.
- */
- public function checkout($id)
- {
- // Initialize variables.
- $userId = (int) JFactory::getUser()->get('id');
- $id = (int) $id;
-
- // Check for a new object id.
- if (empty($id)) {
- return true;
- }
-
- $table = $this->getTable();
-
- // Attempt to check-out the row.
- $return = $table->checkout($userId, $id);
-
- // Check for a database error.
- if ($return === false) {
- $this->setError($table->_db->getErrorMsg());
- return false;
- }
-
- // Check if the row is checked-out by someone else.
- if ($return === null) {
- $this->setError(JText::_('Notes_Note_checked_out'));
- return false;
- }
-
- return true;
- }
-
- /**
- * Method to get an ojbect.
- *
- * @access public
- * @param integer The id of the object to get.
- * @return mixed Object on success, false on failure.
- */
- public function &getItem($id = null)
- {
- // Initialize variables.
- $id = (!empty($id)) ? $id : (int)$this->getState('template.id');
-
- // Get a level row instance.
- $table = &$this->getTable();
-
- // Attempt to load the row.
- $table->load($id);
-
- // Check for a table object error.
- if ($error = $table->getError()) {
- $this->setError($error);
- $false = false;
- return $false;
- }
-
- // Convert the JTable to a clean JObject.
- $result = JArrayHelper::toObject($table->getProperties(1), 'JObject');
-
- return $result;
- }
-
- /**
- * Method to get the form object.
- *
- * @access public
- * @param string $type The type of form to load (view, model);
- * @return mixed JXForm object on success, false on failure.
- */
- public function &getForm($type = 'view')
- {
- jximport2('jxtended.form.helper');
- JXFormHelper::addIncludePath(JPATH_COMPONENT.DS.'models');
-
- if ($type == 'model') {
- $result = &JXFormHelper::getModel($this->getName());
- }
- else {
- $result = &JXFormHelper::getView($this->getName());
- }
-
- if (JError::isError($result)) {
- $this->setError($result->message);
- return false;
- }
-
- return $result;
- }
-
- /**
- * Method to save the form data.
- *
- * @access public
- * @param array The form data.
- * @return boolean True on success.
- */
- public function save($data)
- {
- $id = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('template.id');
- $isNew = true;
-
- // Get a group row instance.
- $table = &$this->getTable();
-
- // Load the row if saving an existing item.
- if ($id > 0) {
- $table->load($id);
- $isNew = false;
- }
-
- // Bind the data.
- if (!$table->bind($data)) {
- $this->setError($table->getError());
- return false;
- }
-
- // Check the data.
- if (!$table->check()) {
- $this->setError($table->getError());
- return false;
- }
-
- // Store the data.
- if (!$table->store()) {
- $this->setError($this->_db->getErrorMsg());
- return false;
- }
-
- return $table->id;
- }
-
- /**
- * Method to delete groups.
- *
- * @access public
- * @param array An array of group ids.
- * @return boolean Returns true on success, false on failure.
- */
- public function delete($ids)
- {
- // Sanitize the ids.
- $ids = (array) $ids;
- JArrayHelper::toInteger($ids);
-
- // Get a group row instance.
- $table = &$this->getTable();
-
- // Iterate the items to delete each one.
- foreach ($ids as $id) {
- $table->delete($id);
- }
-
- return true;
- }
-
- /**
- * Method to change the publish state of an item
- *
- * @param array $ids The IDs of the taxonomy rows to publish.
- * @param int $value The value to set
- * @return mixed True on success or JExeception object on failure
- */
- public function publish($ids, $value = 1)
- {
- $table = &$this->getTable();
-
- if (!$table->publish($ids, $value, JFactory::getUser()->get('id'))) {
- $this->setError($table->getError());
- return false;
- }
- return true;
- }
- }