Page.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * Page superclass file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package IVV
  10. */
  11. /**
  12. * Page superclass.
  13. *
  14. * Every visitable page type must inherit from this one.
  15. *
  16. * @category Page
  17. */
  18. abstract class Page{
  19. /**
  20. * @var string[] List of CSS files required for the page.
  21. */
  22. private $css = [];
  23. /**
  24. * @var string[] List of js files required for the page.
  25. */
  26. private $js = [];
  27. /**
  28. * @var string Path to the view associated with the page.
  29. */
  30. private $view;
  31. /**
  32. * @var string Title of the page, in the defined language.
  33. */
  34. private $title;
  35. /**
  36. * @var string Site name.
  37. */
  38. private $name;
  39. /**
  40. * @var string Page description, in the defined language.
  41. */
  42. private $description;
  43. /**
  44. * @var string URL to the site favicon.
  45. */
  46. private $favicon;
  47. /**
  48. * @var string URL to the page icon or main image.
  49. */
  50. private $icon;
  51. /**
  52. * @var string Canonical URL.
  53. */
  54. private $canonical;
  55. /**
  56. * @var string Autjhor URL.
  57. */
  58. private $author;
  59. /**
  60. * @var integer HTTP status code.
  61. */
  62. private $code = 0;
  63. /**
  64. * @var string HTTP status message.
  65. */
  66. private $message = "";
  67. /**
  68. * Constructor.
  69. */
  70. public function __construct(){
  71. $this->favicon = URL::IMG["LOGO"] . "logo.svg";
  72. $this->icon = URL::IMG["LOGO"] . "logo.svg";
  73. $this->name = get_context()->get_user()->get_display_name();
  74. $this->author = URL::BASE;
  75. $this->add_css("ui.css");
  76. }
  77. /**
  78. * Adds a CSS file to the list.
  79. *
  80. * @param string $css Css file. Can be the absolute URL, or just the filename.
  81. */
  82. protected function add_css($css){
  83. if (strripos($css, URL::CSS) === false) $file = URL::CSS . $css;
  84. else $file = $css;
  85. array_push($this->css, $file);
  86. }
  87. /**
  88. * Adds a JS file to the list.
  89. *
  90. * @param string $js JS file. Can be the absolute URL, or just the filename.
  91. */
  92. protected function add_js($js){
  93. if (strripos($js, URL::JS) === false) $file = URL::JS . $js;
  94. else $file = $js;
  95. array_push($this->js, $file);
  96. }
  97. /**
  98. * Retrieves the CSS file URLs for the page.
  99. *
  100. * @return string[] CSS files (by absolute URL.)
  101. */
  102. public function get_css(){return $this->css;}
  103. /**
  104. * Retrieves the JS file URLs for the page.
  105. *
  106. * @return string[] JS files (by absolute URL.)
  107. */
  108. public function get_js(){return $this->js;}
  109. /**
  110. * Sets the page view.
  111. *
  112. * @param string $view View file. Can be the relative path, or just the filename.
  113. */
  114. protected function set_view($view){
  115. if (strripos($view, PATH::VIEW) === false) $file = PATH::VIEW . $view;
  116. else $file = $view;
  117. $this->view = $file;
  118. }
  119. /**
  120. * Retrieves the page view file.
  121. *
  122. * @return string Path to the view file.
  123. */
  124. public function get_view(){return $this->view;}
  125. /**
  126. * Sets the page title.
  127. *
  128. * The user name" will always be added at the end.
  129. *
  130. * @param string $title Page title.
  131. */
  132. protected function set_title($title){
  133. $this->title = htmlentities($title, ENT_QUOTES);
  134. if ($title != "") $this->title = $this->title . " - ";
  135. $this->title = $this->title . get_context()->get_user()->get_display_name();
  136. }
  137. /**
  138. * Retrieves the page title.
  139. *
  140. * @return string Page title.
  141. */
  142. public function get_title(){return $this->title;}
  143. /**
  144. * Retrieves the site name.
  145. *
  146. * @return string Site name.
  147. */
  148. public function get_name(){return $this->name;}
  149. /**
  150. * Sets the page description.
  151. *
  152. * The user tagline will always be added at the end.
  153. *
  154. * @param string $description Page description text.
  155. */
  156. protected function set_description($description){
  157. $this->description = htmlentities($description, ENT_QUOTES);
  158. if ($description != "") $this->description = $this->description . " - ";
  159. $this->description = $this->description . get_context()->get_user()->get_text("TAGLINE");
  160. }
  161. /**
  162. * Retrieves the page description
  163. *
  164. * @return string Page description.
  165. */
  166. public function get_description(){return $this->description;}
  167. /**
  168. * Retrieves the URL to the favicon.
  169. *
  170. * @return string Favicon URL.
  171. */
  172. public function get_favicon(){return $this->favicon;}
  173. /**
  174. * Retrieves the URL to the thumbnail image.
  175. *
  176. * @return string Thmbnail icon URL.
  177. */
  178. public function get_icon(){return $this->icon;}
  179. /**
  180. * Sets the page canonical URL.
  181. *
  182. * @param string $canonical Canonical URL, absolute or relative.
  183. */
  184. protected function set_canonical($canonical){
  185. if (strripos($canonical, URL::BASE) === false) $file = URL::BASE . $canonical;
  186. else $file = $canonical;
  187. $this->canonical = $file;
  188. }
  189. /**
  190. * Retrieves the canonical URL to the page.
  191. *
  192. * @return string Canonical URL.
  193. */
  194. public function get_canonical(){return $this->canonical;}
  195. /**
  196. * Retrieves the author URL (site main URL).
  197. *
  198. * @return string AUthor URL.
  199. */
  200. public function get_author(){return $this->author;}
  201. /**
  202. * Sets the HTTP status code for the page to return.
  203. *
  204. * @param int HTTP status code.
  205. */
  206. protected function set_code($code){
  207. if (is_int($code) && $code >= 200 && $code <= 500) $this->code = $code;
  208. }
  209. /**
  210. * Retrieves the page HTTP status code.
  211. *
  212. * @return int HTTP status code
  213. */
  214. public function get_code(){return $this->code;}
  215. /**
  216. * Sets the HTTP status message for the page to return.
  217. *
  218. * @param string HTTP status message.
  219. */
  220. protected function set_message($mesage){$this->message = strval($mesage);}
  221. /**
  222. * Retrieves the page HTTP status message.
  223. *
  224. * @return string HTTP status code
  225. */
  226. public function get_message(){return $this->message;}
  227. /**
  228. * Generates the content to be inserted in the HTML <head> section.
  229. *
  230. * It must go between the <head> and </head> tags, and anything can go behind it before closing
  231. * the section. It includes page title, description, css files and metadata.
  232. *
  233. * @return string HTML head content.
  234. */
  235. public function generate_head(){
  236. $head = "
  237. <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
  238. <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
  239. <title>" . $this->get_title() . "</title>
  240. <link rel='shortcut icon' href='" . $this->get_favicon() . "'/>
  241. <!-- CSS files -->";
  242. foreach ($this->get_css() as $css){
  243. $head .= "
  244. <link rel='stylesheet' type='text/css' href='$css'/>";
  245. }
  246. $head .= "
  247. <!-- JS files -->";
  248. foreach ($this->get_js() as $js){
  249. $head .= "
  250. <script type='text/javascript' src='$js'></script>";
  251. }
  252. $head .= "
  253. <!-- Meta tags -->
  254. <link rel='canonical' href='" . $this->get_canonical() . "'/>
  255. <link rel='author' href='" . $this->get_author() . "'/>
  256. <link rel='publisher' href='" . $this->get_author() . "'/>
  257. <meta name='description' content='" . $this->get_description() . "'/>";
  258. foreach (get_context()->get_available_langs() as $lang){
  259. if ($lang != get_context()->get_lang()){
  260. $url = substr($this->get_canonical(), strlen(URL::BASE));
  261. if (substr($url, 0, 3) == get_context()->get_lang() . "/")
  262. $url = URL::BASE . $lang . "/" . substr($url, 2);
  263. else $url = URL::BASE . $lang . "/" . $url;
  264. $head .= "
  265. <link rel='alternate' hreflang='$lang' href='$url' />";
  266. }
  267. }
  268. $head .= "
  269. <meta property='og:title' content='" . $this->get_title() . "'/>
  270. <meta property='og:url' content='" . $this->get_canonical() . "'/>
  271. <meta property='og:description' content='" . $this->get_description() . "'/>
  272. <meta property='og:image' content='" . $this->get_icon() . "'/>
  273. <meta property='og:site_name' content='" . $this->get_name() . "'/>
  274. <meta property='og:type' content='website'/>
  275. <meta property='og:locale' content='en'/>
  276. <meta name='twitter:card' content='summary'/>
  277. <meta name='twitter:title' content='" . $this->get_title() . "'/>
  278. <meta name='twitter:description' content='" . $this->get_description() . "'/>
  279. <meta name='twitter:image' content='" . $this->get_icon() . "'/>
  280. <meta name='twitter:url' content='" . $this->get_canonical() . "'/>
  281. <meta name='robots' content='index follow'/>\n";
  282. return $head;
  283. }
  284. }