View.php 5.4 KB

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