View.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe View représente la vue de l'application
  8. */
  9. class Minz_View {
  10. const VIEWS_PATH_NAME = '/views';
  11. const LAYOUT_PATH_NAME = '/layout';
  12. const LAYOUT_FILENAME = '/layout.phtml';
  13. private $view_filename = '';
  14. private $use_layout = null;
  15. private static $base_pathnames = array(APP_PATH);
  16. private static $title = '';
  17. private static $styles = array ();
  18. private static $scripts = array ();
  19. private static $params = array ();
  20. /**
  21. * Constructeur
  22. * Détermine si on utilise un layout ou non
  23. */
  24. public function __construct () {
  25. $this->change_view(Minz_Request::controllerName(),
  26. Minz_Request::actionName());
  27. self::$title = Minz_Configuration::title ();
  28. }
  29. /**
  30. * Change le fichier de vue en fonction d'un controller / action
  31. */
  32. public function change_view($controller_name, $action_name) {
  33. $this->view_filename = self::VIEWS_PATH_NAME . '/'
  34. . $controller_name . '/'
  35. . $action_name . '.phtml';
  36. }
  37. /**
  38. * Add a base pathname to search views.
  39. *
  40. * New pathnames will be added at the beginning of the list.
  41. *
  42. * @param $base_pathname the new base pathname.
  43. */
  44. public static function addBasePathname($base_pathname) {
  45. array_unshift(self::$base_pathnames, $base_pathname);
  46. }
  47. /**
  48. * Construit la vue
  49. */
  50. public function build () {
  51. if ($this->use_layout === null) { //TODO: avoid file_exists and require views to be explicit
  52. $this->use_layout = file_exists (APP_PATH . self::LAYOUT_PATH_NAME . self::LAYOUT_FILENAME);
  53. }
  54. if ($this->use_layout) {
  55. $this->buildLayout ();
  56. } else {
  57. $this->render ();
  58. }
  59. }
  60. /**
  61. * Construit le layout
  62. */
  63. public function buildLayout () {
  64. include (
  65. APP_PATH
  66. . self::LAYOUT_PATH_NAME
  67. . self::LAYOUT_FILENAME
  68. );
  69. }
  70. /**
  71. * Affiche la Vue en elle-même
  72. */
  73. public function render () {
  74. $view_found = false;
  75. // We search the view in the list of base pathnames. Only the first view
  76. // found is considered.
  77. foreach (self::$base_pathnames as $base) {
  78. $filename = $base . $this->view_filename;
  79. if (file_exists($filename)) {
  80. include $filename;
  81. $view_found = true;
  82. break;
  83. }
  84. }
  85. if (!$view_found) {
  86. Minz_Log::notice('File not found: `' . $this->view_filename . '`');
  87. }
  88. }
  89. /**
  90. * Ajoute un élément du layout
  91. * @param $part l'élément partial à ajouter
  92. */
  93. public function partial ($part) {
  94. $fic_partial = APP_PATH
  95. . self::LAYOUT_PATH_NAME . '/'
  96. . $part . '.phtml';
  97. if ((include($fic_partial)) === false) {
  98. Minz_Log::warning('File not found: `' . $fic_partial . '`');
  99. }
  100. }
  101. /**
  102. * Affiche un élément graphique situé dans APP./views/helpers/
  103. * @param $helper l'élément à afficher
  104. */
  105. public function renderHelper ($helper) {
  106. $fic_helper = APP_PATH
  107. . '/views/helpers/'
  108. . $helper . '.phtml';
  109. if ((include($fic_helper)) === false) {;
  110. Minz_Log::warning('File not found: `' . $fic_helper . '`');
  111. }
  112. }
  113. /**
  114. * Retourne renderHelper() dans une chaîne
  115. * @param $helper l'élément à traîter
  116. */
  117. public function helperToString($helper) {
  118. ob_start();
  119. $this->renderHelper($helper);
  120. return ob_get_clean();
  121. }
  122. /**
  123. * Permet de choisir si on souhaite utiliser le layout
  124. * @param $use true si on souhaite utiliser le layout, false sinon
  125. */
  126. public function _useLayout ($use) {
  127. $this->use_layout = $use;
  128. }
  129. /**
  130. * Gestion du titre
  131. */
  132. public static function title () {
  133. return self::$title;
  134. }
  135. public static function headTitle () {
  136. return '<title>' . self::$title . '</title>' . "\n";
  137. }
  138. public static function _title ($title) {
  139. self::$title = $title;
  140. }
  141. public static function prependTitle ($title) {
  142. self::$title = $title . self::$title;
  143. }
  144. public static function appendTitle ($title) {
  145. self::$title = self::$title . $title;
  146. }
  147. /**
  148. * Gestion des feuilles de style
  149. */
  150. public static function headStyle () {
  151. $styles = '';
  152. foreach(self::$styles as $style) {
  153. $cond = $style['cond'];
  154. if ($cond) {
  155. $styles .= '<!--[if ' . $cond . ']>';
  156. }
  157. $styles .= '<link rel="stylesheet" ' .
  158. ($style['media'] === 'all' ? '' : 'media="' . $style['media'] . '" ') .
  159. 'href="' . $style['url'] . '" />';
  160. if ($cond) {
  161. $styles .= '<![endif]-->';
  162. }
  163. $styles .= "\n";
  164. }
  165. return $styles;
  166. }
  167. public static function prependStyle ($url, $media = 'all', $cond = false) {
  168. array_unshift (self::$styles, array (
  169. 'url' => $url,
  170. 'media' => $media,
  171. 'cond' => $cond
  172. ));
  173. }
  174. public static function appendStyle ($url, $media = 'all', $cond = false) {
  175. self::$styles[] = array (
  176. 'url' => $url,
  177. 'media' => $media,
  178. 'cond' => $cond
  179. );
  180. }
  181. /**
  182. * Gestion des scripts JS
  183. */
  184. public static function headScript () {
  185. $scripts = '';
  186. foreach (self::$scripts as $script) {
  187. $cond = $script['cond'];
  188. if ($cond) {
  189. $scripts .= '<!--[if ' . $cond . ']>';
  190. }
  191. $scripts .= '<script src="' . $script['url'] . '"';
  192. if ($script['defer']) {
  193. $scripts .= ' defer="defer"';
  194. }
  195. if ($script['async']) {
  196. $scripts .= ' async="async"';
  197. }
  198. $scripts .= '></script>';
  199. if ($cond) {
  200. $scripts .= '<![endif]-->';
  201. }
  202. $scripts .= "\n";
  203. }
  204. return $scripts;
  205. }
  206. public static function prependScript ($url, $cond = false, $defer = true, $async = true) {
  207. array_unshift(self::$scripts, array (
  208. 'url' => $url,
  209. 'cond' => $cond,
  210. 'defer' => $defer,
  211. 'async' => $async,
  212. ));
  213. }
  214. public static function appendScript ($url, $cond = false, $defer = true, $async = true) {
  215. self::$scripts[] = array (
  216. 'url' => $url,
  217. 'cond' => $cond,
  218. 'defer' => $defer,
  219. 'async' => $async,
  220. );
  221. }
  222. /**
  223. * Gestion des paramètres ajoutés à la vue
  224. */
  225. public static function _param ($key, $value) {
  226. self::$params[$key] = $value;
  227. }
  228. public function attributeParams () {
  229. foreach (Minz_View::$params as $key => $value) {
  230. $this->$key = $value;
  231. }
  232. }
  233. }