View.php 5.9 KB

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