PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 231 lines | 123 code | 37 blank | 71 comment | 17 complexity | 571891a851684c48df00bcec7d11257c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: template.php 39 2009-05-21 07:32:36Z 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. jimport('joomla.application.component.controller');
  11. /**
  12. * @package TAOJ.ContentManager
  13. * @subpackage com_contentmanager
  14. */
  15. class ContentManagerControllerTemplate extends JController
  16. {
  17. /**
  18. * Constructor
  19. */
  20. function __construct()
  21. {
  22. parent::__construct();
  23. $this->registerTask('save2copy', 'save');
  24. $this->registerTask('save2new', 'save');
  25. $this->registerTask('apply', 'save');
  26. }
  27. /**
  28. * Display the view
  29. */
  30. function display()
  31. {
  32. }
  33. /**
  34. * Proxy for getModel
  35. */
  36. function &getModel()
  37. {
  38. return parent::getModel('Template', '', array('ignore_request' => true));
  39. }
  40. /**
  41. * Method to edit a object
  42. *
  43. * Sets object ID in the session from the request, checks the item out, and then redirects to the edit page.
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function edit()
  49. {
  50. $cid = JRequest::getVar('cid', array(), '', 'array');
  51. $id = JRequest::getInt('id', @$cid[0]);
  52. $session = &JFactory::getSession();
  53. $session->set('contentmanager.edit.template.id', $id);
  54. // Checkout item
  55. $model = $this->getModel();
  56. if ($id) {
  57. $model->checkout($id);
  58. }
  59. $this->setRedirect(JRoute::_('index.php?option=com_contentmanager&view=template&layout=edit', false));
  60. }
  61. /**
  62. * Method to cancel an edit
  63. *
  64. * Checks the item in, sets item ID in the session to null, and then redirects to the list page.
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. function cancel()
  70. {
  71. // Checkin item if checked out
  72. $session = &JFactory::getSession();
  73. if ($id = (int) $session->get('contentmanager.edit.template.id')) {
  74. $model = $this->getModel();
  75. $model->checkin($id);
  76. }
  77. // Clear the session of the item
  78. $session->set('contentmanager.edit.template.id', null);
  79. $this->setRedirect(JRoute::_('index.php?option=com_contentmanager&view=templates', false));
  80. }
  81. /**
  82. * Save the record
  83. */
  84. function save()
  85. {
  86. // Check for request forgeries.
  87. JRequest::checkToken();
  88. // Get posted form variables.
  89. $values = JRequest::getVar('jxform', array(), 'post', 'array');
  90. // Get the id of the item out of the session.
  91. $session = &JFactory::getSession();
  92. $id = (int) $session->get('contentmanager.edit.template.id');
  93. $values['id'] = $id;
  94. // Get the extensions model and set the post request in its state.
  95. $model = &$this->getModel();
  96. $post = JRequest::get('post');
  97. $model->setState('request', $post);
  98. // Get the form model object and validate it.
  99. $form = &$model->getForm('model');
  100. $form->filter($values);
  101. $result = $form->validate($values);
  102. if (JError::isError($result)) {
  103. JError::raiseError(500, $result->message);
  104. }
  105. $result = $model->save($values);
  106. $msg = JError::isError($result) ? $result->message : 'Saved';
  107. if ($this->_task == 'apply')
  108. {
  109. $session->set('contentmanager.edit.template.id', $model->getState('id'));
  110. $this->setRedirect(JRoute::_('index.php?option=com_contentmanager&view=template&layout=edit', false), JText::_($msg));
  111. }
  112. else if ($this->_task == 'save2new')
  113. {
  114. $session->set('contentmanager.edit.template.id', null);
  115. $model->checkin($id);
  116. $this->setRedirect(JRoute::_('index.php?option=com_contentmanager&view=template&layout=edit', false), JText::_($msg));
  117. }
  118. else
  119. {
  120. $session->set('contentmanager.edit.template.id', null);
  121. $model->checkin($id);
  122. $this->setRedirect(JRoute::_('index.php?option=com_contentmanager&view=templates', false), JText::_($msg));
  123. }
  124. }
  125. /**
  126. * Removes an item
  127. */
  128. function delete()
  129. {
  130. // Check for request forgeries
  131. JRequest::checkToken() or die('Invalid Token');
  132. // Get items to remove from the request.
  133. $cid = JRequest::getVar('cid', array(), '', 'array');
  134. if (!is_array($cid) || count($cid) < 1) {
  135. JError::raiseWarning(500, JText::_('Select an item to delete'));
  136. }
  137. else {
  138. // Get the model.
  139. $model = $this->getModel();
  140. // Make sure the item ids are integers
  141. jimport('joomla.utilities.arrayhelper');
  142. JArrayHelper::toInteger($cid);
  143. // Remove the items.
  144. if (!$model->delete($cid)) {
  145. JError::raiseWarning(500, $model->getError());
  146. }
  147. }
  148. $this->setRedirect('index.php?option=com_contentmanager&view=templates');
  149. }
  150. /**
  151. * Method to publish a list of taxa
  152. *
  153. * @access public
  154. * @return void
  155. * @since 1.0
  156. */
  157. function publish()
  158. {
  159. // Check for request forgeries
  160. JRequest::checkToken() or die('Invalid Token');
  161. // Get items to publish from the request.
  162. $cid = JRequest::getVar('cid', array(), '', 'array');
  163. $values = array('publish' => 1, 'unpublish' => 0, 'trash' => -2);
  164. $task = $this->getTask();
  165. $value = JArrayHelper::getValue($values, $task, 0, 'int');
  166. if (!is_array($cid) || count($cid) < 1) {
  167. JError::raiseWarning(500, JText::_('Select an item to publish'));
  168. }
  169. else {
  170. // Get the model.
  171. $model = $this->getModel();
  172. // Make sure the item ids are integers
  173. JArrayHelper::toInteger($cid);
  174. // Publish the items.
  175. if (!$model->publish($cid, $value)) {
  176. JError::raiseWarning(500, $model->getError());
  177. }
  178. }
  179. $this->setRedirect('index.php?option=com_contentmanager&view=templates');
  180. }
  181. /**
  182. * Changes the order of an item
  183. */
  184. function ordering()
  185. {
  186. // Check for request forgeries
  187. JRequest::checkToken() or die('Invalid Token');
  188. $cid = JRequest::getVar('cid', null, 'post', 'array');
  189. $inc = $this->getTask() == 'orderup' ? -1 : +1;
  190. $model = & $this->getModel();
  191. $model->ordering($cid, $inc);
  192. $this->setRedirect('index.php?option=com_contentmanager&view=templates');
  193. }
  194. }