View.php 5.5 KB

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