View.php 9.4 KB

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