View.php 5.5 KB

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