View.php 8.8 KB

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