controller.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Controller
  4. *
  5. *
  6. *
  7. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  8. * @license see /license.txt
  9. */
  10. class Controller
  11. {
  12. const PARAM_ACTION = 'action';
  13. protected $access;
  14. protected function __construct($access = null)
  15. {
  16. $access = $access ? $access : Access::all();
  17. $this->access = $access;
  18. }
  19. /**
  20. *
  21. * @return \Access
  22. */
  23. public function access()
  24. {
  25. return $this->access;
  26. }
  27. /**
  28. * List of actions accepted by the controller.
  29. *
  30. * @return array
  31. */
  32. public function get_actions()
  33. {
  34. $reflector = new ReflectionClass($this);
  35. $constants = $reflector->getConstants();
  36. $result = array();
  37. foreach ($constants as $key => $value) {
  38. if (strpos($key, 'ACTION') !== false) {
  39. $result[$key] = $value;
  40. }
  41. }
  42. return $result;
  43. }
  44. /**
  45. * Action to perform.
  46. * Returns the request parameter.
  47. *
  48. * @return string
  49. */
  50. public function get_action()
  51. {
  52. $result = Request::get(self::PARAM_ACTION);
  53. $actions = $this->get_actions();
  54. $result = in_array($result, $actions) ? $result : '';
  55. return $result;
  56. }
  57. /**
  58. * Set up the environment. Set up breadcrumps, raise tracking event, etc.
  59. */
  60. protected function prolog()
  61. {
  62. }
  63. /**
  64. * Whether the call is authorized or not.
  65. *
  66. * @return boolean
  67. */
  68. public function authorize()
  69. {
  70. return $this->access()->authorize();
  71. }
  72. /**
  73. * Returns a string containing dynamic javascript to be included in the template.
  74. * This requires a {{javascript}} tag in a twigg template to appear.
  75. *
  76. * Note:
  77. *
  78. * A better approach to this method is to create a twigg "javascript"
  79. * template and to include it where required.
  80. *
  81. * @return string
  82. */
  83. public function javascript()
  84. {
  85. return '';
  86. }
  87. // public function check_token()
  88. // {
  89. // return (bool) Security::check_token('get');
  90. // }
  91. /**
  92. * Run the controller. Dispatch action and execute requested tasks.
  93. */
  94. public function run()
  95. {
  96. if (!$this->authorize()) {
  97. $this->forbidden();
  98. return false;
  99. }
  100. $this->prolog();
  101. $action = $this->get_action();
  102. if (empty($action)) {
  103. $this->unknown();
  104. return;
  105. }
  106. $f = array($this, $action);
  107. if (is_callable($f)) {
  108. call_user_func($f);
  109. } else {
  110. $this->missing();
  111. }
  112. }
  113. /**
  114. * Unknown action. I.e. the action has not been registered.
  115. * Possibly missing action declaration:
  116. *
  117. * const ACTION_XXX = 'XXX';
  118. *
  119. * @return boolean
  120. */
  121. public function unknown()
  122. {
  123. return false;
  124. }
  125. /**
  126. *
  127. * @return boolean
  128. */
  129. public function forbidden()
  130. {
  131. api_not_allowed();
  132. return false;
  133. }
  134. /**
  135. * Action exists but implementation is missing.
  136. */
  137. public function missing()
  138. {
  139. echo 'No implementation';
  140. return false;
  141. }
  142. /**
  143. * Render a template using data. Adds a few common parameters to data.
  144. *
  145. * @see /main/template/default/course_description/
  146. * @param string $template
  147. * @param array $data
  148. */
  149. protected function render($template_name, $data)
  150. {
  151. $data = (object) $data;
  152. $data->www = \Chamilo::url();
  153. $data->messages = isset($data->messages) ? $data->messages : array();
  154. $javascript = $this->javascript();
  155. if ($javascript) {
  156. $data->javascript = $javascript;
  157. }
  158. $tpl = new Template();
  159. foreach ($data as $key => $value) {
  160. $tpl->assign($key, $value);
  161. }
  162. $template = $tpl->get_template($template_name);
  163. $content = $tpl->fetch($template);
  164. $tpl->assign('content', $content);
  165. $tpl->display_one_col_template();
  166. }
  167. /**
  168. * Render data as JSON
  169. *
  170. * @param any $data
  171. */
  172. protected function render_json($data)
  173. {
  174. Header::content_type_json();
  175. echo json_encode($data);
  176. }
  177. }