Page.php 7.9 KB

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