4
0

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