PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/redirect/code/trunk/administrator/components/com_redirect/controllers/link.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 328 lines | 142 code | 55 blank | 131 comment | 12 complexity | ec4946a194620dd807da25c61ba87784 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: link.php 391 2010-11-05 11:40:55Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_redirect
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://www.theartofjoomla.com
  9. */
  10. defined('_JEXEC') or die('Invalid Request.');
  11. jimport('joomla.application.component.controller');
  12. jimport('joomla.application.component.helper');
  13. /**
  14. * The Redirect Link Controller
  15. *
  16. * @package Redirect
  17. * @subpackage com_redirect
  18. * @version 1.0
  19. */
  20. class RedirectControllerLink extends JController
  21. {
  22. /**
  23. * Overridden constructor to register alternate tasks
  24. *
  25. * @access protected
  26. * @return void
  27. * @since 1.0
  28. */
  29. function __construct()
  30. {
  31. parent::__construct();
  32. // Map the save tasks.
  33. $this->registerTask('save2new', 'save');
  34. $this->registerTask('apply', 'save');
  35. // Map the publishing state tasks.
  36. $this->registerTask('unpublish', 'publish');
  37. $this->registerTask('archive', 'publish');
  38. }
  39. /**
  40. * Dummy method to redirect back to standard controller
  41. *
  42. * @access public
  43. * @return void
  44. * @since 1.0
  45. */
  46. function display()
  47. {
  48. $this->setRedirect('index.php?option=com_redirect');
  49. }
  50. /**
  51. * Method to add a new redirect link.
  52. *
  53. * @access public
  54. * @return void
  55. * @since 1.0
  56. */
  57. function add()
  58. {
  59. // Initialize variables.
  60. $app = &JFactory::getApplication();
  61. // Clear the link id from the session.
  62. $app->setUserState('redirect.edit.link.id', null);
  63. // Redirect to the edit screen.
  64. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  65. }
  66. /**
  67. * Method to setup a redirect link for editing and redirect to the edit form.
  68. *
  69. * @access public
  70. * @return void
  71. * @since 1.0
  72. */
  73. function edit()
  74. {
  75. // Initialize variables.
  76. $app = &JFactory::getApplication();
  77. $cid = JRequest::getVar('cid', array(), '', 'array');
  78. // Get the previous link id (if any) and the current link id.
  79. $previousId = (int) $app->getUserState('redirect.edit.link.id');
  80. $linkId = (int) (count($cid) ? $cid[0] : JRequest::getInt('l_id'));
  81. // Set the category id for the category to edit in the session.
  82. $app->setUserState('redirect.edit.link.id', $linkId);
  83. // Get the model.
  84. $model = &$this->getModel('Link', 'RedirectModel');
  85. // Redirect to the edit screen.
  86. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  87. }
  88. /**
  89. * Method to cancel an edit
  90. *
  91. * Sets item id in the session to null, and then redirects to the list screen.
  92. *
  93. * @access public
  94. * @return void
  95. * @since 1.0
  96. */
  97. function cancel()
  98. {
  99. // Initialize variables.
  100. $app = &JFactory::getApplication();
  101. // Clear the link id from the session.
  102. $app->setUserState('redirect.edit.link.id', null);
  103. // Redirect to the list screen.
  104. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=links', false));
  105. }
  106. /**
  107. * Method to delete redirect links.
  108. *
  109. * @access public
  110. * @return void
  111. * @since 1.0
  112. */
  113. function delete()
  114. {
  115. // Check for request forgeries.
  116. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  117. // Get and sanitize the items to delete.
  118. $cid = JRequest::getVar('cid', null, 'post', 'array');
  119. JArrayHelper::toInteger($cid);
  120. // Get the model.
  121. $model = &$this->getModel('Link', 'RedirectModel');
  122. // Attempt to delete the item(s).
  123. if (!$model->delete($cid)) {
  124. $this->setMessage(JText::sprintf('REDIRECT_LINK_DELETE_FAILED', $model->getError()), 'notice');
  125. }
  126. else {
  127. $this->setMessage(JText::sprintf('REDIRECT_LINK_DELETE_SUCCESS', count($cid)));
  128. }
  129. // Redirect to the list screen.
  130. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=links', false));
  131. }
  132. /**
  133. * Method to save a redirect link.
  134. *
  135. * @access public
  136. * @return void
  137. * @since 1.0
  138. */
  139. function save()
  140. {
  141. // Check for request forgeries.
  142. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  143. // Initialize variables.
  144. $app = &JFactory::getApplication();
  145. // Get the posted values from the request.
  146. $data = JRequest::getVar('jxform', array(), 'post', 'array');
  147. // Populate the row id from the session.
  148. $data['id'] = (int) $app->getUserState('redirect.edit.link.id');
  149. // Get the model and attempt to validate the posted data.
  150. $model = &$this->getModel('Link', 'RedirectModel');
  151. $return = $model->validate($data);
  152. // Check for validation errors.
  153. if ($return === false)
  154. {
  155. // Get the validation messages.
  156. $errors = $model->getErrors();
  157. // Push up to three validation messages out to the user.
  158. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
  159. {
  160. if (JError::isError($errors[$i])) {
  161. $app->enqueueMessage($errors[$i]->getMessage(), 'notice');
  162. } else {
  163. $app->enqueueMessage($errors[$i], 'notice');
  164. }
  165. }
  166. // Save the data in the session.
  167. $app->setUserState('redirect.edit.link.data', $data);
  168. // Redirect back to the edit screen.
  169. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  170. return false;
  171. }
  172. // Attempt to save the data.
  173. $return = $model->save($data);
  174. // Check for errors.
  175. if ($return === false)
  176. {
  177. // Save the data in the session.
  178. $app->setUserState('redirect.edit.link.data', $data);
  179. // Redirect back to the edit screen.
  180. $this->setMessage(JText::sprintf('REDIRECT_LINK_SAVE_FAILED', $model->getError()), 'notice');
  181. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  182. return false;
  183. }
  184. // Redirect the user and adjust session state based on the chosen task.
  185. switch ($this->_task)
  186. {
  187. case 'apply':
  188. // Redirect back to the edit screen.
  189. $this->setMessage(JText::_('REDIRECT_LINK_SAVE_SUCCESS'));
  190. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  191. break;
  192. case 'save2new':
  193. // Clear the link id from the session.
  194. $app->setUserState('redirect.edit.link.id', null);
  195. // Redirect back to the edit screen.
  196. $this->setMessage(JText::_('REDIRECT_LINK_SAVE_SUCCESS'));
  197. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=link&layout=edit', false));
  198. break;
  199. default:
  200. // Clear the link id from the session.
  201. $app->setUserState('redirect.edit.link.id', null);
  202. // Redirect to the list screen.
  203. $this->setMessage(JText::_('REDIRECT_LINK_SAVE_SUCCESS'));
  204. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=links', false));
  205. break;
  206. }
  207. // Flush the data from the session.
  208. $app->setUserState('redirect.edit.link.data', null);
  209. }
  210. /**
  211. * Method to activate a list of links
  212. *
  213. * @access public
  214. * @return void
  215. * @since 1.0
  216. */
  217. function activate()
  218. {
  219. // Check for request forgeries.
  220. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  221. // Get and sanitize the items to delete.
  222. $cid = JRequest::getVar('cid', null, 'post', 'array');
  223. JArrayHelper::toInteger($cid);
  224. // Get the model.
  225. $model = &$this->getModel('Link', 'RedirectModel', array('ignore_request' => true));
  226. $new_url = JRequest::getString('new_url');
  227. $comment = JRequest::getString('comment');
  228. // Attempt to activate the links
  229. $result = $model->activate($cid, $new_url, $comment);
  230. // If there is a problem, set the error message.
  231. if (!$result) {
  232. $this->setMessage($model->getError());
  233. }
  234. // Redirect to the list screen.
  235. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=links', false));
  236. }
  237. /**
  238. * Method to set the published state of links.
  239. *
  240. * @access public
  241. * @return void
  242. * @since 1.0
  243. */
  244. function publish()
  245. {
  246. // Check for request forgeries.
  247. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  248. // Get and sanitize the items to delete.
  249. $cid = JRequest::getVar('cid', null, 'post', 'array');
  250. JArrayHelper::toInteger($cid);
  251. // Get the model.
  252. $model = &$this->getModel('Link', 'RedirectModel', array('ignore_request' => true));
  253. // Attempt to set the publishing state for the links.
  254. switch ($this->getTask())
  255. {
  256. case 'publish':
  257. $result = $model->publish($cid);
  258. break;
  259. case 'unpublish':
  260. $result = $model->unpublish($cid);
  261. break;
  262. case 'archive':
  263. $result = $model->archive($cid);
  264. break;
  265. }
  266. // If there is a problem, set the error message.
  267. if (!$result) {
  268. $this->setMessage($model->getError());
  269. }
  270. // Redirect to the list screen.
  271. $this->setRedirect(JRoute::_('index.php?option=com_redirect&view=links', false));
  272. }
  273. }