/libraries/molajo/component/controllers/redirect.php

https://github.com/ot2sen/Molajo · PHP · 300 lines · 131 code · 36 blank · 133 comment · 62 complexity · 514f0fb5d6dac1b8f12267a9a12acfce MD5 · raw file

  1. <?php
  2. /**
  3. * @version controller.php
  4. * @package Molajo
  5. * @subpackage Display Controller
  6. * @copyright Copyright (C) 2011 Amy Stephen. All rights reserved.
  7. * @license GNU General Public License Version 2, or later http://www.gnu.org/licenses/gpl.html
  8. */
  9. defined('MOLAJO') or die;
  10. /**
  11. * Redurect Controller
  12. *
  13. * @package Molajo
  14. * @subpackage Controller
  15. * @since 1.0
  16. */
  17. class MolajoControllerRedirect extends MolajoController
  18. {
  19. /**
  20. * $redirect
  21. *
  22. * @var object
  23. */
  24. protected $redirect = null;
  25. /**
  26. * $redirectAction
  27. *
  28. * @var boolean
  29. */
  30. protected $redirectAction = null;
  31. /**
  32. * $successIndicator
  33. *
  34. * @var boolean
  35. */
  36. protected $successIndicator = null;
  37. /**
  38. * $redirectMessage
  39. *
  40. * @var boolean
  41. */
  42. protected $redirectMessage = null;
  43. /**
  44. * $redirectMessageType
  45. *
  46. * @var boolean
  47. */
  48. protected $redirectMessageType = null;
  49. /**
  50. * $redirectReturn
  51. *
  52. * @var string
  53. */
  54. protected $redirectReturn = null;
  55. /**
  56. * $redirectSuccess
  57. *
  58. * @var string
  59. */
  60. protected $redirectSuccess = null;
  61. /**
  62. * $datakey
  63. *
  64. * @var string
  65. */
  66. protected $datakey = null;
  67. /**
  68. * $return_page
  69. *
  70. * @var string
  71. */
  72. protected $return_page = null;
  73. /**
  74. * initialize
  75. *
  76. * Establish the Link needed for redirecting after the task is complete (or fails)
  77. *
  78. * @return boolean
  79. * @since 1.0
  80. */
  81. public function initialize ($task=null)
  82. {
  83. /** no redirect: */
  84. /** 1. ajax and non-html output **/
  85. $format = JRequest::getCmd('format', 'html');
  86. if ($format == null) {
  87. $format = 'html';
  88. }
  89. if (!$format == 'html') {
  90. $this->setRedirectAction(false);
  91. return;
  92. }
  93. /** 2. display, add, edit tasks **/
  94. if ($task == 'display' || $task == 'add' || $task == 'edit') {
  95. $this->setRedirectAction(false);
  96. return;
  97. }
  98. /** remaining: tasks that will redirect to a display/add/edit task upon completion **/
  99. $this->redirectAction = true;
  100. /** extension: category uses this parameter **/
  101. $extension = JRequest::getCmd('extension', '');
  102. if ($extension == '' || $extension == null) {
  103. $extension = '';
  104. } else {
  105. $extension = '&extension='.$extension;
  106. }
  107. /** component_specific: to add parameter pairs needed in addition to standard **/
  108. $component_specific = JRequest::getCmd('component_specific', '');
  109. if ($component_specific == '' || $component_specific == null) {
  110. $component_specific = '';
  111. } elseif (substr($component_specific, 1, 1) == '&') {
  112. } else {
  113. $component_specific .= '&'.$component_specific;
  114. }
  115. /** cancel **/
  116. if ($task == 'cancel') {
  117. if (MolajoFactory::getApplication()->getName() == 'site') {
  118. if ($this->id == 0) {
  119. $this->redirectSuccess = 'index.php';
  120. } else {
  121. $this->redirectSuccess = 'index.php?option='.JRequest::getCmd('option').'&view='.JRequest::getCmd('DefaultView').'&id='.$this->id.$extension.$component_specific;
  122. }
  123. } else {
  124. $this->redirectSuccess = 'index.php?option='.JRequest::getCmd('option').'&view='.JRequest::getCmd('DefaultView').'&id='.$this->id.$extension.$component_specific;
  125. }
  126. $this->redirectReturn = $this->redirectSuccess;
  127. return true;
  128. }
  129. /** multiple controller tasks **/
  130. if (JRequest::getCmd('controller') == JRequest::getCmd('DefaultView')) {
  131. $this->redirectSuccess = 'index.php?option='.JRequest::getCmd('option').'&view='.JRequest::getCmd('DefaultView').$extension.$component_specific;
  132. $this->redirectReturn = $this->redirectSuccess;
  133. return;
  134. }
  135. /** single controller tasks - item url with and without the layout=editor **/
  136. $this->redirectSuccess = 'index.php?option='.JRequest::getCmd('option').'&view='.JRequest::getCmd('DefaultView').$extension.$component_specific;
  137. $this->redirectReturn = 'index.php?option='.JRequest::getCmd('option').'&view='.JRequest::getCmd('EditView').$extension.$component_specific;
  138. return;
  139. }
  140. /**
  141. * setDatakey
  142. *
  143. * unique random value and $datakey parameter used for storing and retrieving form contents from session
  144. * instead of $context for returning due to an error
  145. *
  146. * @return string The return URL.
  147. * @since 1.0
  148. */
  149. protected function setDatakey()
  150. {
  151. $this->datakey = mt_rand ();
  152. JRequest::setVar('datakey', $this->datakey);
  153. return;
  154. }
  155. /**
  156. * setRedirectAction
  157. *
  158. * Indicator of whether or not a redirect should be issued
  159. *
  160. * @return boolean
  161. * @since 1.0
  162. */
  163. public function setRedirectAction ($action)
  164. {
  165. $this->redirectAction = $action;
  166. return;
  167. }
  168. /**
  169. * setRedirectMessageType
  170. *
  171. * Message Type of Message: message, warning, or error
  172. *
  173. * @return boolean
  174. * @since 1.0
  175. */
  176. public function setRedirectMessageType ($messagetype)
  177. {
  178. $this->redirectMessageType = $messagetype;
  179. return;
  180. }
  181. /**
  182. * setRedirectMessage
  183. *
  184. * User Message regarding Task conclusion
  185. *
  186. * @return boolean
  187. * @since 1.0
  188. */
  189. public function setRedirectMessage ($message)
  190. {
  191. $this->redirectMessage = $message;
  192. return;
  193. }
  194. /**
  195. * setSuccessIndicator
  196. *
  197. * Indicator as to whether or not the task succeeded or failed
  198. *
  199. * @return boolean
  200. * @since 1.0
  201. */
  202. public function setSuccessIndicator ($indicator = true)
  203. {
  204. $this->successIndicator = (boolean) $indicator;
  205. $this->redirect();
  206. }
  207. /**
  208. * redirect
  209. *
  210. * Redirects the browser or returns false if no redirect is set.
  211. *
  212. * @return boolean False if no redirect exists.
  213. * @since 1.0
  214. */
  215. public function redirect ($task=null)
  216. {
  217. /** Display tasks and non-HTML format tasks do not redirect **/
  218. if ($this->redirectAction === false) {
  219. return false;
  220. }
  221. /** task **/
  222. if ($task == null) {
  223. $task = $this->data['task'];
  224. }
  225. /** message and message type **/
  226. if ($this->successIndicator === false) {
  227. if ($this->redirectMessage == null || $this->redirectMessage == '') {
  228. $this->redirectMessage = MolajoText::_('MOLAJO_STANDARD_FAILURE_MESSAGE');
  229. }
  230. if ($this->redirectMessageType == null) {
  231. $this->redirectMessageType = 'error';
  232. }
  233. } else {
  234. /** defaults to success **/
  235. if ($this->redirectMessage == null) {
  236. $this->redirectMessage = MolajoText::_('MOLAJO_STANDARD_SUCCESS_MESSAGE');
  237. }
  238. if ($this->redirectMessageType == null) {
  239. $this->redirectMessageType = 'message';
  240. }
  241. }
  242. /** list **/
  243. if (JRequest::getCmd('controller') == JRequest::getCmd('DefaultView')) {
  244. $link = $this->redirectSuccess;
  245. /** redirect url **/
  246. } else if ($this->successIndicator === false || $task == 'apply' || $task == 'save2new') {
  247. $link = $this->redirectReturn;
  248. $id = $this->data['id'];
  249. if ((int) $id == 0 || $task == 'save2new') {
  250. $link .= '&task='.JRequest::getCmd('EditView').'.add'.'&datakey='.$this->datakey;
  251. } else {
  252. $link .= '&task='.JRequest::getCmd('EditView').'.edit&id='.$this->id.'&datakey='.$this->datakey;
  253. }
  254. } else {
  255. $link = $this->redirectSuccess.'&id='.$this->id;
  256. }
  257. /** should not be needed */
  258. if ($link == '') {
  259. $link = 'index.php';
  260. }
  261. /** redirect **/
  262. MolajoFactory::getApplication()->redirect(MolajoRoute::_($link, false), $this->redirectMessage, $this->redirectMessageType);
  263. }
  264. }