View.php 5.9 KB

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