4
0

View.php 6.0 KB

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