PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/models/template.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 256 lines | 125 code | 43 blank | 88 comment | 16 complexity | 99f6d048b23b54b6314245837101f989 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: template.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
  5. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  6. * @link http://www.theartofjoomla.com
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die;
  10. jximport2('jxtended.application.component.modelitem');
  11. /**
  12. * @package TAOJ.ContentManager
  13. * @subpackage com_contentmanager
  14. */
  15. class ContentManagerModelTemplate extends JxModelItem
  16. {
  17. /**
  18. * Override to get the template table
  19. */
  20. public function &getTable()
  21. {
  22. return JTable::getInstance('Template', 'ContentManagerTable');
  23. }
  24. /**
  25. */
  26. public function _populateState()
  27. {
  28. $session = &JFactory::getSession();
  29. $params = JComponentHelper::getParams('com_contentmanager');
  30. // Load the object state.
  31. $id = (int) $session->get('contentmanager.edit.template.id');
  32. $this->setState('template.id', $id);
  33. // Load the parameters.
  34. $this->setState('params', $params);
  35. }
  36. /**
  37. * Method to checkin a row.
  38. *
  39. * @access public
  40. * @param integer $id The numeric id of a row
  41. * @return boolean True on success/false on failure
  42. */
  43. public function checkin($id = null)
  44. {
  45. // Initialize variables.
  46. $id = (int) $id;
  47. if ($id === 0) {
  48. $id = $this->getState('template.id');
  49. }
  50. if (!$id) {
  51. return true;
  52. }
  53. $table = $this->getTable();
  54. // Attempt to check-in the row.
  55. $return = $table->checkin($id);
  56. // Check for a database error.
  57. if ($return === false) {
  58. $this->setError($table->getError());
  59. return false;
  60. }
  61. return true;
  62. }
  63. /**
  64. * Method to check-out a object for editing.
  65. *
  66. * @access public
  67. * @param int $id The numeric id of the object to check-out.
  68. * @return bool False on failure or error, success otherwise.
  69. */
  70. public function checkout($id)
  71. {
  72. // Initialize variables.
  73. $userId = (int) JFactory::getUser()->get('id');
  74. $id = (int) $id;
  75. // Check for a new object id.
  76. if (empty($id)) {
  77. return true;
  78. }
  79. $table = $this->getTable();
  80. // Attempt to check-out the row.
  81. $return = $table->checkout($userId, $id);
  82. // Check for a database error.
  83. if ($return === false) {
  84. $this->setError($table->_db->getErrorMsg());
  85. return false;
  86. }
  87. // Check if the row is checked-out by someone else.
  88. if ($return === null) {
  89. $this->setError(JText::_('Notes_Note_checked_out'));
  90. return false;
  91. }
  92. return true;
  93. }
  94. /**
  95. * Method to get an ojbect.
  96. *
  97. * @access public
  98. * @param integer The id of the object to get.
  99. * @return mixed Object on success, false on failure.
  100. */
  101. public function &getItem($id = null)
  102. {
  103. // Initialize variables.
  104. $id = (!empty($id)) ? $id : (int)$this->getState('template.id');
  105. // Get a level row instance.
  106. $table = &$this->getTable();
  107. // Attempt to load the row.
  108. $table->load($id);
  109. // Check for a table object error.
  110. if ($error = $table->getError()) {
  111. $this->setError($error);
  112. $false = false;
  113. return $false;
  114. }
  115. // Convert the JTable to a clean JObject.
  116. $result = JArrayHelper::toObject($table->getProperties(1), 'JObject');
  117. return $result;
  118. }
  119. /**
  120. * Method to get the form object.
  121. *
  122. * @access public
  123. * @param string $type The type of form to load (view, model);
  124. * @return mixed JXForm object on success, false on failure.
  125. */
  126. public function &getForm($type = 'view')
  127. {
  128. jximport2('jxtended.form.helper');
  129. JXFormHelper::addIncludePath(JPATH_COMPONENT.DS.'models');
  130. if ($type == 'model') {
  131. $result = &JXFormHelper::getModel($this->getName());
  132. }
  133. else {
  134. $result = &JXFormHelper::getView($this->getName());
  135. }
  136. if (JError::isError($result)) {
  137. $this->setError($result->message);
  138. return false;
  139. }
  140. return $result;
  141. }
  142. /**
  143. * Method to save the form data.
  144. *
  145. * @access public
  146. * @param array The form data.
  147. * @return boolean True on success.
  148. */
  149. public function save($data)
  150. {
  151. $id = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('template.id');
  152. $isNew = true;
  153. // Get a group row instance.
  154. $table = &$this->getTable();
  155. // Load the row if saving an existing item.
  156. if ($id > 0) {
  157. $table->load($id);
  158. $isNew = false;
  159. }
  160. // Bind the data.
  161. if (!$table->bind($data)) {
  162. $this->setError($table->getError());
  163. return false;
  164. }
  165. // Check the data.
  166. if (!$table->check()) {
  167. $this->setError($table->getError());
  168. return false;
  169. }
  170. // Store the data.
  171. if (!$table->store()) {
  172. $this->setError($this->_db->getErrorMsg());
  173. return false;
  174. }
  175. return $table->id;
  176. }
  177. /**
  178. * Method to delete groups.
  179. *
  180. * @access public
  181. * @param array An array of group ids.
  182. * @return boolean Returns true on success, false on failure.
  183. */
  184. public function delete($ids)
  185. {
  186. // Sanitize the ids.
  187. $ids = (array) $ids;
  188. JArrayHelper::toInteger($ids);
  189. // Get a group row instance.
  190. $table = &$this->getTable();
  191. // Iterate the items to delete each one.
  192. foreach ($ids as $id) {
  193. $table->delete($id);
  194. }
  195. return true;
  196. }
  197. /**
  198. * Method to change the publish state of an item
  199. *
  200. * @param array $ids The IDs of the taxonomy rows to publish.
  201. * @param int $value The value to set
  202. * @return mixed True on success or JExeception object on failure
  203. */
  204. public function publish($ids, $value = 1)
  205. {
  206. $table = &$this->getTable();
  207. if (!$table->publish($ids, $value, JFactory::getUser()->get('id'))) {
  208. $this->setError($table->getError());
  209. return false;
  210. }
  211. return true;
  212. }
  213. }