View.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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::notice('File not found: `' . $this->view_filename . '`');
  66. }
  67. }
  68. /**
  69. * Ajoute un élément du layout
  70. * @param $part l'élément partial à ajouter
  71. */
  72. public function partial ($part) {
  73. $fic_partial = APP_PATH
  74. . self::LAYOUT_PATH_NAME . '/'
  75. . $part . '.phtml';
  76. if ((include($fic_partial)) === false) {
  77. Minz_Log::warning('File not found: `' . $fic_partial . '`');
  78. }
  79. }
  80. /**
  81. * Affiche un élément graphique situé dans APP./views/helpers/
  82. * @param $helper l'élément à afficher
  83. */
  84. public function renderHelper ($helper) {
  85. $fic_helper = APP_PATH
  86. . '/views/helpers/'
  87. . $helper . '.phtml';
  88. if ((include($fic_helper)) === false) {;
  89. Minz_Log::warning('File not found: `' . $fic_helper . '`');
  90. }
  91. }
  92. /**
  93. * Retourne renderHelper() dans une chaîne
  94. * @param $helper l'élément à traîter
  95. */
  96. public function helperToString($helper) {
  97. ob_start();
  98. $this->renderHelper($helper);
  99. return ob_get_clean();
  100. }
  101. /**
  102. * Permet de choisir si on souhaite utiliser le layout
  103. * @param $use true si on souhaite utiliser le layout, false sinon
  104. */
  105. public function _useLayout ($use) {
  106. $this->use_layout = $use;
  107. }
  108. /**
  109. * Gestion du titre
  110. */
  111. public static function title () {
  112. return self::$title;
  113. }
  114. public static function headTitle () {
  115. return '<title>' . self::$title . '</title>' . "\n";
  116. }
  117. public static function _title ($title) {
  118. self::$title = $title;
  119. }
  120. public static function prependTitle ($title) {
  121. self::$title = $title . self::$title;
  122. }
  123. public static function appendTitle ($title) {
  124. self::$title = self::$title . $title;
  125. }
  126. /**
  127. * Gestion des feuilles de style
  128. */
  129. public static function headStyle () {
  130. $styles = '';
  131. foreach(self::$styles as $style) {
  132. $cond = $style['cond'];
  133. if ($cond) {
  134. $styles .= '<!--[if ' . $cond . ']>';
  135. }
  136. $styles .= '<link rel="stylesheet" ' .
  137. ($style['media'] === 'all' ? '' : 'media="' . $style['media'] . '" ') .
  138. 'href="' . $style['url'] . '" />';
  139. if ($cond) {
  140. $styles .= '<![endif]-->';
  141. }
  142. $styles .= "\n";
  143. }
  144. return $styles;
  145. }
  146. public static function prependStyle ($url, $media = 'all', $cond = false) {
  147. array_unshift (self::$styles, array (
  148. 'url' => $url,
  149. 'media' => $media,
  150. 'cond' => $cond
  151. ));
  152. }
  153. public static function appendStyle ($url, $media = 'all', $cond = false) {
  154. self::$styles[] = array (
  155. 'url' => $url,
  156. 'media' => $media,
  157. 'cond' => $cond
  158. );
  159. }
  160. /**
  161. * Gestion des scripts JS
  162. */
  163. public static function headScript () {
  164. $scripts = '';
  165. foreach (self::$scripts as $script) {
  166. $cond = $script['cond'];
  167. if ($cond) {
  168. $scripts .= '<!--[if ' . $cond . ']>';
  169. }
  170. $scripts .= '<script src="' . $script['url'] . '"';
  171. if ($script['defer']) {
  172. $scripts .= ' defer="defer"';
  173. }
  174. if ($script['async']) {
  175. $scripts .= ' async="async"';
  176. }
  177. $scripts .= '></script>';
  178. if ($cond) {
  179. $scripts .= '<![endif]-->';
  180. }
  181. $scripts .= "\n";
  182. }
  183. return $scripts;
  184. }
  185. public static function prependScript ($url, $cond = false, $defer = true, $async = true) {
  186. array_unshift(self::$scripts, array (
  187. 'url' => $url,
  188. 'cond' => $cond,
  189. 'defer' => $defer,
  190. 'async' => $async,
  191. ));
  192. }
  193. public static function appendScript ($url, $cond = false, $defer = true, $async = true) {
  194. self::$scripts[] = array (
  195. 'url' => $url,
  196. 'cond' => $cond,
  197. 'defer' => $defer,
  198. 'async' => $async,
  199. );
  200. }
  201. /**
  202. * Gestion des paramètres ajoutés à la vue
  203. */
  204. public static function _param ($key, $value) {
  205. self::$params[$key] = $value;
  206. }
  207. public function attributeParams () {
  208. foreach (Minz_View::$params as $key => $value) {
  209. $this->$key = $value;
  210. }
  211. }
  212. }