PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/carleton/reason_package
PHP | 302 lines | 215 code | 17 blank | 70 comment | 41 complexity | 2a063042bf8f61beeaca4205a73ed855 MD5 | raw file
  1. <?php
  2. /**
  3. * @package reason
  4. * @subpackage minisite_modules
  5. */
  6. /**
  7. * Include parent class & dependencies
  8. */
  9. reason_include_once( 'minisite_templates/modules/default.php' );
  10. reason_include_once( 'function_libraries/url_utils.php' );
  11. /**
  12. * Register module with Reason
  13. */
  14. $GLOBALS[ '_module_class_names' ][ basename( __FILE__, '.php' ) ] = 'FormMinisiteModule';
  15. /**
  16. * Form 2.0
  17. *
  18. * Reason Form Module - intended to be used to build interfaces around thor or custom forms, while maintaining backwards
  19. * compatibility with Reason's old thor form module.
  20. *
  21. * Common usage would involve instantiation of a model, view (optional), admin_view (optional) and controller.
  22. *
  23. * If no parameters are provided, then the default thor form model, view, and admin_view, and controller will be used.
  24. *
  25. * The model is passed a reference to the module at the time of instantiation, so that head items, the cur_page object,
  26. * or other items available to the module can be localized into the model. The controller handles cleanup rules and request
  27. * variables, just like a module would. Essentially, the controller serves as a sub-module.
  28. *
  29. * The controller itself will be provided the view and admin view if these are provided parameter, but they are optional.
  30. *
  31. * @author Nathan White
  32. */
  33. class FormMinisiteModule extends DefaultMinisiteModule
  34. {
  35. public $form_controller;
  36. public $form_model;
  37. public $form_view;
  38. public $acceptable_params = array('form_model' => false,
  39. 'form_controller' => false,
  40. 'form_view' => false,
  41. 'form_admin_view' => false,
  42. 'force_login' => false,
  43. 'force_secure' => true);
  44. function _init_legacy()
  45. {
  46. // prep items to always do for backwards compatibility with old form module
  47. $this->_check_force_secure_parameter();
  48. $this->_check_force_login_parameter();
  49. $this->_redirect_old_style_url();
  50. }
  51. /**
  52. * Invokes the controller init method
  53. */
  54. function init( $args=array() )
  55. {
  56. $this->_init_legacy();
  57. if ($this->model_is_usable())
  58. {
  59. $controller =& $this->get_form_controller();
  60. $controller->init();
  61. }
  62. else parent::init();
  63. }
  64. /**
  65. * Invokes the controller run method
  66. */
  67. function run()
  68. {
  69. if (reason_maintenance_mode() && !reason_check_privs('db_maintenance'))
  70. {
  71. echo '<div id="form">';
  72. echo '<p><em>This web site is currently in maintenance mode, so forms are temporarily disabled. Please try again later.</em></p>';
  73. echo '</div>';
  74. }
  75. else if ($this->model_is_usable())
  76. {
  77. $controller = $this->get_form_controller();
  78. $controller->run();
  79. }
  80. else // present a somewhat friendly error message
  81. {
  82. echo '<div id="form">';
  83. echo '<p>This page should display a form, but is not set up correctly. Please try again later.</p>';
  84. echo '</div>';
  85. }
  86. }
  87. function model_is_usable()
  88. {
  89. if (!isset($this->model_is_usable))
  90. {
  91. $model =& $this->get_form_model();
  92. $this->model_is_usable = $model->is_usable();
  93. }
  94. return $this->model_is_usable;
  95. }
  96. /**
  97. * Get the form model - this must be specified as a page type parameter, otherwise the thor model is used.
  98. */
  99. function &get_form_model()
  100. {
  101. if (!isset($this->form_model))
  102. {
  103. $default_model_filename = (defined('REASON_FORMS_THOR_DEFAULT_MODEL')) ? REASON_FORMS_THOR_DEFAULT_MODEL : 'thor.php';
  104. if (!empty($this->params['form_model']) && is_array($this->params['form_model'])) $model_filename = $this->params['form_model']['model'];
  105. elseif (!empty($this->params['form_model'])) $model_filename = $this->params['form_model'];
  106. else $model_filename = $default_model_filename;
  107. if (reason_file_exists('minisite_templates/modules/form/models/'.$model_filename))
  108. {
  109. reason_include_once('minisite_templates/modules/form/models/'.$model_filename);
  110. }
  111. elseif (reason_file_exists($model_filename))
  112. {
  113. reason_include_once($model_filename);
  114. }
  115. elseif (file_exists($model_filename))
  116. {
  117. include_once($model_filename);
  118. }
  119. else trigger_error('The forms module was unable to load a model - the model_filename in get_form_model is ' . $model_filename, FATAL);
  120. $model_name = $GLOBALS[ '_form_model_class_names' ][ basename($model_filename, '.php')];
  121. $model = new $model_name();
  122. $model->init_from_module($this);
  123. $this->form_model =& $model;
  124. }
  125. return $this->form_model;
  126. }
  127. /**
  128. * Get the form controller - if a thor model is being used we use our REASON_FORMS_THOR_DEFAULT_CONTROLLER by default.
  129. */
  130. function &get_form_controller()
  131. {
  132. if (!isset($this->form_controller))
  133. {
  134. // lets check if the model is (or is based on) thor.
  135. $model =& $this->get_form_model();
  136. if ((get_class($model) == 'ThorFormModel') || (is_subclass_of($model, 'ThorFormModel')))
  137. {
  138. $default_controller_filename = (defined('REASON_FORMS_THOR_DEFAULT_CONTROLLER')) ? REASON_FORMS_THOR_DEFAULT_CONTROLLER : 'thor.php';
  139. }
  140. else $default_controller_filename = 'default.php';
  141. $controller_filename = (!empty($this->params['form_controller'])) ? $this->params['form_controller'] : $default_controller_filename;
  142. if (reason_file_exists('minisite_templates/modules/form/controllers/'.$controller_filename))
  143. {
  144. reason_include_once('minisite_templates/modules/form/controllers/'.$controller_filename);
  145. }
  146. elseif (reason_file_exists($controller_filename))
  147. {
  148. reason_include_once($controller_filename);
  149. }
  150. elseif (file_exists($controller_filename))
  151. {
  152. include_once($controller_filename);
  153. }
  154. else trigger_error('The forms module was unable to load a controller - the controller_filename in get_form_controller is ' . $controller_filename, FATAL);
  155. $model =& $this->get_form_model();
  156. $view =& $this->get_form_view();
  157. $admin_view =& $this->get_form_admin_view();
  158. $controller_name = $GLOBALS['_form_controller_class_names'][basename($controller_filename, '.php')];
  159. $controller = new $controller_name();
  160. $controller->set_model($model);
  161. if ($view) $controller->set_view($view);
  162. if ($admin_view) $controller->set_admin_view($admin_view);
  163. $this->form_controller =& $controller;
  164. }
  165. return $this->form_controller;
  166. }
  167. /**
  168. * Get the form view from page type parameter if it exists - in some cases (like thor) the controller may handle view selection.
  169. */
  170. function &get_form_view()
  171. {
  172. if (!isset($this->form_view))
  173. {
  174. $view_filename = (!empty($this->params['form_view'])) ? $this->params['form_view'] : false;
  175. if ($view_filename)
  176. {
  177. if (reason_file_exists('minisite_templates/modules/form/views/'.$view_filename))
  178. {
  179. reason_include_once('minisite_templates/modules/form/views/'.$view_filename);
  180. }
  181. elseif (reason_file_exists($view_filename))
  182. {
  183. reason_include_once($view_filename);
  184. }
  185. elseif (file_exists($view_filename))
  186. {
  187. include_once($view_filename);
  188. }
  189. else trigger_error('The forms module was unable to load the view specified (' . $view_filename . ')', FATAL);
  190. $view_name = $GLOBALS['_form_view_class_names'][basename($view_filename, '.php')];
  191. $view = new $view_name();
  192. $model =& $this->get_form_model();
  193. $view->set_model($model);
  194. }
  195. else $view = false;
  196. $this->form_view =& $view;
  197. }
  198. return $this->form_view;
  199. }
  200. /**
  201. * Get the form view from page type parameter if it exists - in some cases (like thor) the controller may handle view selection.
  202. */
  203. function &get_form_admin_view()
  204. {
  205. if (!isset($this->form_admin_view))
  206. {
  207. $admin_view_filename = (!empty($this->params['form_admin_view'])) ? $this->params['form_admin_view'] : false;
  208. if ($admin_view_filename)
  209. {
  210. if (reason_file_exists('minisite_templates/modules/form/admin_views/'.$admin_view_filename))
  211. {
  212. reason_include_once('minisite_templates/modules/form/admin_views/'.$admin_view_filename);
  213. }
  214. elseif (reason_file_exists($admin_view_filename))
  215. {
  216. reason_include_once($admin_view_filename);
  217. }
  218. elseif (file_exists($admin_view_filename))
  219. {
  220. include_once($admin_view_filename);
  221. }
  222. else trigger_error('The forms module was unable to load the admin view specified (' . $admin_view_filename . ')', FATAL);
  223. $admin_view_name = $GLOBALS['_form_admin_view_class_names'][basename($admin_view_filename, '.php')];
  224. $admin_view = new $admin_view_name();
  225. $model =& $this->get_form_model();
  226. $admin_view->set_model($model);
  227. }
  228. else $admin_view = false;
  229. $this->form_admin_view =& $admin_view;
  230. }
  231. return $this->form_admin_view;
  232. }
  233. /**
  234. * People may have admin URLs bookmarked from the old form module ...
  235. *
  236. * ... in this case a permanent redirect is sent to the correct admin URL
  237. *
  238. * This method is included only for backwards compatibility -
  239. * the query string parameter data_view should now be form_admin_view
  240. *
  241. * @access private
  242. */
  243. function _redirect_old_style_url()
  244. {
  245. if (isset($_REQUEST['mode']) && $_REQUEST['mode'] == 'data_view')
  246. {
  247. $redirect = carl_make_redirect(array('mode' => '', 'form_admin_view' => 'true'));
  248. header("Location: " . $redirect);
  249. exit;
  250. }
  251. if (isset($_REQUEST['thor_success']) && $_REQUEST['thor_success'] == 'true')
  252. {
  253. $redirect = carl_make_redirect(array('thor_success' => '', 'submission_key' => 'true'));
  254. header("Location: " . $redirect);
  255. exit;
  256. }
  257. }
  258. /**
  259. * The old form module supported a force_login parameter - we will continue to support it though really the models
  260. * are probably a better place to force login.
  261. *
  262. * @access private
  263. */
  264. function _check_force_login_parameter()
  265. {
  266. if ($this->params['force_login'])
  267. {
  268. reason_require_authentication('form_login_msg');
  269. }
  270. }
  271. /**
  272. * The old form module always would force a secure connection if available. We will maintain this as the default,
  273. * but allow the force_secure parameters to also be set to false, thus disabling force_secure.
  274. *
  275. * @access private
  276. */
  277. function _check_force_secure_parameter()
  278. {
  279. if ($this->params['force_secure'])
  280. {
  281. force_secure_if_available();
  282. }
  283. }
  284. }
  285. ?>