View.php 6.6 KB

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