View.php 9.4 KB

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