/reason_4.0/lib/core/minisite_templates/modules/form/controllers/abstract.php

https://github.com/luthercollege/reason_package · PHP · 321 lines · 193 code · 33 blank · 95 comment · 21 complexity · e601c5da20fe6b36786635c91b3aeebe MD5 · raw file

  1. <?php
  2. /**
  3. * @package reason
  4. * @subpackage minisite_modules
  5. */
  6. /**
  7. * Register controller with Reason
  8. */
  9. $GLOBALS[ '_form_controller_class_names' ][ basename( __FILE__, '.php') ] = 'AbstractFormController';
  10. /**
  11. * Abstract Form Controller
  12. *
  13. * Form controllers need a model in order to function, and optionally may use a view.
  14. *
  15. * A form controller should define an instance variable called supported_modes.
  16. *
  17. * init_$mode methods needs to be defined in the model or controller
  18. * run_$mode methods need to be defined in the view or controller
  19. *
  20. * The model must minimally support is_$mode methods for each supported controller mode.
  21. *
  22. * @author Nathan White
  23. */
  24. class AbstractFormController
  25. {
  26. /**
  27. * @var object form model
  28. */
  29. var $model;
  30. /**
  31. * @var object form view
  32. */
  33. var $view;
  34. /**
  35. * @var object admin view
  36. */
  37. var $admin_view;
  38. /**
  39. * A list of modes supported by the form controller
  40. * @var array supported_modes
  41. */
  42. var $supported_modes;
  43. /**
  44. * Cleanup rules for user input. Input that passes becomes a part of $this->request
  45. * @var array cleanup_rules
  46. */
  47. var $cleanup_rules;
  48. /**
  49. * @var array request
  50. */
  51. var $request;
  52. function AbstractFormController()
  53. {
  54. $request =& $this->get_request();
  55. $cleanup_rules =& $this->get_cleanup_rules();
  56. if (empty($request) && !empty($cleanup_rules))
  57. {
  58. $unclean_request = conditional_stripslashes($_REQUEST);
  59. $request = carl_clean_vars($unclean_request, $cleanup_rules);
  60. $this->set_request($request);
  61. }
  62. }
  63. /**
  64. * Init method does the following:
  65. *
  66. * 1. Runs setup controller
  67. * 2. Runs setup model
  68. * 3. Runs setup view
  69. * 4. Invoke the appropriate init method obtained with the get_init_method method
  70. */
  71. function init( )
  72. {
  73. $this->setup_mvc();
  74. $init_mode = $this->_get_mode_with_prefix('init_');
  75. if ($init_mode) $this->check_model_and_invoke_method($this->_get_mode_with_prefix('init_'));
  76. }
  77. function setup_mvc()
  78. {
  79. $this->setup_model();
  80. $this->setup_view();
  81. $this->setup_admin_view();
  82. $this->setup_controller();
  83. $this->check_view_and_invoke_model_method('validate_request');
  84. }
  85. /**
  86. * Provide the request variables to the model
  87. */
  88. function setup_model()
  89. {
  90. // setup the model according to parameters
  91. $model =& $this->get_model();
  92. $request =& $this->get_request();
  93. $model->handle_request_vars($request);
  94. }
  95. /**
  96. * If we do not have a view - ask the model to setup the view (if $model->setup_view exists)
  97. *
  98. * Otherwise pass a reference to the view if the model has a set_view method.
  99. */
  100. function setup_view()
  101. {
  102. $model =& $this->get_model();
  103. $view =& $this->get_view();
  104. if (!isset($view) || ($view == false))
  105. {
  106. if (method_exists($model, 'setup_view'))
  107. {
  108. $view =& $model->setup_view();
  109. $this->set_view($view);
  110. }
  111. }
  112. elseif (method_exists($model, 'set_view'))
  113. {
  114. $model->set_view($view);
  115. }
  116. }
  117. /**
  118. * If we do not have an admin view - ask the model to setup the admin view (if $model->setup_admin_view exists)
  119. *
  120. * Otherwise pass a reference to the admin view if the model has a set_admin_view method.
  121. */
  122. function setup_admin_view()
  123. {
  124. $model =& $this->get_model();
  125. $admin_view =& $this->get_admin_view();
  126. if (!isset($admin_view) || ($admin_view == false))
  127. {
  128. if (method_exists($model, 'setup_admin_view'))
  129. {
  130. $admin_view =& $model->setup_admin_view();
  131. $this->set_admin_view($admin_view);
  132. }
  133. }
  134. elseif (method_exists($model, 'set_admin_view')) $model->set_admin_view($admin_view);
  135. }
  136. /**
  137. * Hook for extra controller setup prior to our init method
  138. */
  139. function setup_controller()
  140. {
  141. return false;
  142. }
  143. function run()
  144. {
  145. echo $this->check_view_and_invoke_method('pre_run');
  146. $run_mode = $this->_get_mode_with_prefix('run_');
  147. if ($run_mode) echo $this->check_view_and_invoke_method($run_mode);
  148. echo $this->check_view_and_invoke_method('post_run');
  149. }
  150. /**
  151. * Run action that always occurs prior to the run method specific to the mode
  152. */
  153. function pre_run()
  154. {
  155. return false;
  156. }
  157. /**
  158. * Run action that always occurs after the run method specific to the mode
  159. */
  160. function post_run()
  161. {
  162. return false;
  163. }
  164. /**
  165. * Checks if the view has a method with name method - if so, run that method, otherwise run the controller method
  166. * @param string method name to invoke
  167. */
  168. function check_view_and_invoke_method($method, $show_errors = true)
  169. {
  170. $view =& $this->get_view();
  171. if (method_exists($view, $method)) return $view->$method();
  172. elseif (method_exists($this, $method)) return $this->$method();
  173. elseif ($show_errors) trigger_error('The form controller called a method ' . $method . ' that does not exists in the view or controller');
  174. return false;
  175. }
  176. /**
  177. * Checks if the model has a method with name method - if so, run that method, otherwise run the controller method
  178. * @param string method name to invoke
  179. */
  180. function check_model_and_invoke_method($method)
  181. {
  182. $model =& $this->get_model();
  183. if (method_exists($model, $method)) return $model->$method();
  184. elseif (method_exists($this, $method)) return $this->$method();
  185. else trigger_error('The form controller called a method ' . $method . ' that does not exists in the model or controller');
  186. return false;
  187. }
  188. /**
  189. * Checks if the view has a method with name method - if so, run that method, otherwise run the model method
  190. * @param string method name to invoke
  191. */
  192. function check_view_and_invoke_model_method($method, $show_errors = true)
  193. {
  194. $model =& $this->get_model();
  195. $view =& $this->get_view();
  196. if (method_exists($view, $method)) return $view->$method();
  197. elseif (method_exists($model, $method)) return $model->$method();
  198. elseif ($show_errors) trigger_error('The form controller called a method ' . $method . ' that does not exists in the view or model');
  199. return false;
  200. }
  201. /**
  202. * Checks if the view or model has a method with name method - if so, run that method, otherwise run the controller method
  203. * @param string method name to invoke
  204. */
  205. function check_view_and_model_and_invoke_method($method, $show_errors = true)
  206. {
  207. $model =& $this->get_model();
  208. $view =& $this->get_view();
  209. if (method_exists($view, $method)) return $view->$method();
  210. elseif (method_exists($model, $method)) return $model->$method();
  211. elseif (method_exists($this, $method)) return $this->$method();
  212. elseif ($show_errors) trigger_error('The form controller called a method ' . $method . ' that does not exists in the view, model, or controller');
  213. return false;
  214. }
  215. /**
  216. * Runs is_$mode methods to determine what the "mode" of the module is - returns appropriate method with a given prefix.
  217. * @param string mode prefix
  218. * @return string module mode with prefix
  219. * @access private
  220. */
  221. function _get_mode_with_prefix($prefix)
  222. {
  223. $modes =& $this->get_supported_modes();
  224. if (!empty($modes))
  225. {
  226. $model =& $this->get_model();
  227. foreach ($modes as $mode)
  228. {
  229. $is_method = "is_" . $mode;
  230. if (method_exists($model, $is_method))
  231. {
  232. if ($model->$is_method()) return $prefix.$mode;
  233. }
  234. else trigger_error('The form controller supports the mode ' . $mode . ' but the model used does not have an is_' . $mode . ' method defined');
  235. }
  236. }
  237. return false;
  238. }
  239. // GETTERS AND SETTERS
  240. function set_supported_modes(&$supported_modes)
  241. {
  242. $this->supported_modes =& $supported_modes;
  243. }
  244. function &get_supported_modes()
  245. {
  246. return $this->supported_modes;
  247. }
  248. function set_model(&$model)
  249. {
  250. $this->model =& $model;
  251. }
  252. function &get_model()
  253. {
  254. return $this->model;
  255. }
  256. function set_view(&$view)
  257. {
  258. $this->view =& $view;
  259. }
  260. function set_admin_view(&$admin_view)
  261. {
  262. $this->admin_view =& $admin_view;
  263. }
  264. function &get_view()
  265. {
  266. return $this->view;
  267. }
  268. function &get_admin_view()
  269. {
  270. return $this->admin_view;
  271. }
  272. function set_cleanup_rules(&$cleanup_rules)
  273. {
  274. $this->cleanup_rules =& $cleanup_rules;
  275. }
  276. function &get_cleanup_rules()
  277. {
  278. return $this->cleanup_rules;
  279. }
  280. function set_request(&$request)
  281. {
  282. $this->request =& $request;
  283. }
  284. function &get_request()
  285. {
  286. return $this->request;
  287. }
  288. }