View.php 5.3 KB

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