View.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Minz_View represents a view in the MVC paradigm
  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 string $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 string $filename the name of the file to include.
  70. * @return boolean 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. public function renderToString(): string {
  102. ob_start();
  103. $this->render();
  104. return ob_get_clean();
  105. }
  106. /**
  107. * Ajoute un élément du layout
  108. * @param string $part l'élément partial à ajouter
  109. */
  110. public function partial ($part) {
  111. $fic_partial = self::LAYOUT_PATH_NAME . '/' . $part . '.phtml';
  112. if (!$this->includeFile($fic_partial)) {
  113. Minz_Log::warning('File not found: `' . $fic_partial . '`');
  114. }
  115. }
  116. /**
  117. * Affiche un élément graphique situé dans APP./views/helpers/
  118. * @param string $helper l'élément à afficher
  119. */
  120. public function renderHelper ($helper) {
  121. $fic_helper = '/views/helpers/' . $helper . '.phtml';
  122. if (!$this->includeFile($fic_helper)) {
  123. Minz_Log::warning('File not found: `' . $fic_helper . '`');
  124. }
  125. }
  126. /**
  127. * Retourne renderHelper() dans une chaîne
  128. * @param string $helper l'élément à traîter
  129. */
  130. public function helperToString($helper) {
  131. ob_start();
  132. $this->renderHelper($helper);
  133. return ob_get_clean();
  134. }
  135. /**
  136. * Choose the current view layout.
  137. * @param string|false $layout the layout name to use, false to use no layouts.
  138. */
  139. public function _layout($layout) {
  140. if ($layout) {
  141. $this->layout_filename = self::LAYOUT_PATH_NAME . $layout . '.phtml';
  142. } else {
  143. $this->layout_filename = '';
  144. }
  145. }
  146. /**
  147. * Choose if we want to use the layout or not.
  148. * @deprecated Please use the `_layout` function instead.
  149. * @param bool $use true if we want to use the layout, false else
  150. */
  151. public function _useLayout ($use) {
  152. Minz_Log::warning('Minz_View::_useLayout is deprecated, it will be removed in a future version. Please use Minz_View::_layout instead.');
  153. if ($use) {
  154. $this->_layout(self::LAYOUT_DEFAULT);
  155. } else {
  156. $this->_layout(false);
  157. }
  158. }
  159. /**
  160. * Gestion du titre
  161. */
  162. public static function title () {
  163. return self::$title;
  164. }
  165. public static function headTitle () {
  166. return '<title>' . self::$title . '</title>' . "\n";
  167. }
  168. public static function _title ($title) {
  169. self::$title = $title;
  170. }
  171. public static function prependTitle ($title) {
  172. self::$title = $title . self::$title;
  173. }
  174. public static function appendTitle ($title) {
  175. self::$title = self::$title . $title;
  176. }
  177. /**
  178. * Gestion des feuilles de style
  179. */
  180. public static function headStyle () {
  181. $styles = '';
  182. foreach(self::$styles as $style) {
  183. $cond = $style['cond'];
  184. if ($cond) {
  185. $styles .= '<!--[if ' . $cond . ']>';
  186. }
  187. $styles .= '<link rel="stylesheet" ' .
  188. ($style['media'] === 'all' ? '' : 'media="' . $style['media'] . '" ') .
  189. 'href="' . $style['url'] . '" />';
  190. if ($cond) {
  191. $styles .= '<![endif]-->';
  192. }
  193. $styles .= "\n";
  194. }
  195. return $styles;
  196. }
  197. public static function prependStyle ($url, $media = 'all', $cond = false) {
  198. array_unshift (self::$styles, array (
  199. 'url' => $url,
  200. 'media' => $media,
  201. 'cond' => $cond
  202. ));
  203. }
  204. public static function appendStyle ($url, $media = 'all', $cond = false) {
  205. self::$styles[] = array (
  206. 'url' => $url,
  207. 'media' => $media,
  208. 'cond' => $cond
  209. );
  210. }
  211. /**
  212. * Gestion des scripts JS
  213. */
  214. public static function headScript () {
  215. $scripts = '';
  216. foreach (self::$scripts as $script) {
  217. $cond = $script['cond'];
  218. if ($cond) {
  219. $scripts .= '<!--[if ' . $cond . ']>';
  220. }
  221. $scripts .= '<script src="' . $script['url'] . '"';
  222. if ($script['defer']) {
  223. $scripts .= ' defer="defer"';
  224. }
  225. if ($script['async']) {
  226. $scripts .= ' async="async"';
  227. }
  228. $scripts .= '></script>';
  229. if ($cond) {
  230. $scripts .= '<![endif]-->';
  231. }
  232. $scripts .= "\n";
  233. }
  234. return $scripts;
  235. }
  236. public static function prependScript ($url, $cond = false, $defer = true, $async = true) {
  237. array_unshift(self::$scripts, array (
  238. 'url' => $url,
  239. 'cond' => $cond,
  240. 'defer' => $defer,
  241. 'async' => $async,
  242. ));
  243. }
  244. public static function appendScript ($url, $cond = false, $defer = true, $async = true) {
  245. self::$scripts[] = array (
  246. 'url' => $url,
  247. 'cond' => $cond,
  248. 'defer' => $defer,
  249. 'async' => $async,
  250. );
  251. }
  252. /**
  253. * Gestion des paramètres ajoutés à la vue
  254. */
  255. public static function _param ($key, $value) {
  256. self::$params[$key] = $value;
  257. }
  258. public function attributeParams () {
  259. foreach (Minz_View::$params as $key => $value) {
  260. $this->$key = $value;
  261. }
  262. }
  263. }