View.php 6.8 KB

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