1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: namespace Flea;
28:
29: 30: 31: 32: 33:
34: class Page {
35:
36: 37: 38: 39:
40: public static $TYPE_DEFAULT = 'default';
41:
42: 43: 44: 45:
46: public static $TYPE_ERROR404 = 'error404';
47:
48: 49: 50: 51:
52: public static $TYPE_REDIRECT301 = 'redirect301';
53:
54: 55: 56: 57:
58: public static $FORMAT_HTML = 'html';
59:
60: 61: 62: 63:
64: public static $FORMAT_XML = 'xml';
65:
66: 67: 68: 69:
70: public static $FORMAT_CSS = 'css';
71:
72: 73: 74: 75:
76: public static $FORMAT_JS = 'js';
77:
78: 79: 80: 81:
82: public static $FORMAT_PDF = 'pdf';
83:
84: 85: 86: 87:
88: public static $FORMAT_ZIP = 'zip';
89:
90: 91: 92: 93:
94: public static $FORMAT_JSON = 'json';
95: private static $_EMPTY = null;
96: private $_id;
97:
98: 99: 100: 101: 102: 103:
104: public function getId() {
105: return $this->_id;
106: }
107:
108: private function updateId() {
109: $this->_id = $this->_name . ',' . $this->_lang;
110: }
111:
112: private $_name;
113:
114: 115: 116: 117: 118: 119:
120: public function setName($name) {
121: $this->_name = $name;
122: $this->updateId();
123: }
124:
125: 126: 127: 128: 129:
130: public function getName() {
131: return $this->_name;
132: }
133:
134: private $_lang;
135:
136: 137: 138: 139: 140: 141:
142: public function setLang($lang) {
143: if (_DEBUG && !LangList::getInstance()->has($lang)) {
144: Debug::getInstance()->addError('The Language ' . $lang . ' don\'t exist');
145: }
146: $this->_lang = $lang;
147: $this->updateId();
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function getLang() {
157: return $this->_lang;
158: }
159:
160: private $_date;
161:
162: 163: 164: 165: 166:
167: public function setDate($date) {
168: $this->_date = $date;
169: }
170:
171: 172: 173: 174: 175:
176: public function getDate() {
177: return $this->_date;
178: }
179:
180: private $_type;
181:
182: 183: 184: 185: 186: 187: 188: 189: 190:
191: public function setType($type) {
192: if (_DEBUG &&
193: $type !== '' &&
194: $type !== Page::$TYPE_DEFAULT &&
195: $type !== Page::$TYPE_ERROR404 &&
196: $type !== Page::$TYPE_REDIRECT301) {
197: Debug::getInstance()->addError('The type: ' . $type . ' don\'t exist');
198: }
199: $this->_type = $type;
200: }
201:
202: 203: 204: 205: 206:
207: public function getType() {
208: return $this->_type;
209: }
210:
211: private $_format;
212:
213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
224: public function setFormat($format) {
225: if (_DEBUG &&
226: $format !== Page::$FORMAT_HTML &&
227: $format !== Page::$FORMAT_CSS &&
228: $format !== Page::$FORMAT_JS &&
229: $format !== Page::$FORMAT_JSON &&
230: $format !== Page::$FORMAT_XML) {
231: Debug::getInstance()->addError('The format: ' . $format . ' don\'t exist');
232: }
233: $this->_format = $format;
234: }
235:
236: 237: 238: 239: 240:
241: public function getFormat() {
242: return $this->_format;
243: }
244:
245: private $_tags;
246:
247: 248: 249: 250: 251: 252:
253: public function getTags() {
254: if ($this->_tags === null) {
255: $this->_tags = new DataList(false);
256:
257: $table_page = DataBase::objectToTableName($this);
258: if (DataBase::getInstance(_DB_DSN_PAGE)->exist($table_page)) {
259: $table_list = $table_page . '_array';
260:
261: $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
262: $where = array('page_id' => $this->getId(), 'page_prop' => '_tags');
263: $query->initSelect('value', '`' . $table_list . '`', $where);
264:
265: $rows = DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query);
266: foreach ($rows as $row) {
267: $this->_tags->add($row['value']);
268: }
269: }
270: }
271:
272: return $this->_tags;
273: }
274:
275: private $_metas;
276:
277: 278: 279: 280: 281: 282:
283: public function getMetas() {
284: if ($this->_metas === null) {
285: $this->_metas = new DataList(true);
286:
287: $table_page = DataBase::objectToTableName($this);
288: if (DataBase::getInstance(_DB_DSN_PAGE)->exist($table_page)) {
289: $table_list = $table_page . '_array';
290:
291: $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
292: $where = array('page_id' => $this->getId(), 'page_prop' => '_metas');
293: $query->initSelect('key, value', '`' . $table_list . '`', $where);
294: $rows = DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query);
295: foreach ($rows as $row) {
296: $meta = \Flea::getBuildUtil()->replaceFleaVars($row['value'], $this);
297: $this->_metas->add($meta, $row['key']);
298: }
299: }
300: }
301:
302: return $this->_metas;
303: }
304:
305: private $_phpHeader;
306:
307: 308: 309: 310: 311:
312: public function setPhpHeader($phpHeader) {
313: $this->_phpHeader = $phpHeader;
314: }
315:
316: 317: 318: 319: 320:
321: public function getPhpHeader() {
322: return $this->_phpHeader;
323: }
324:
325: private $_visible;
326:
327: 328: 329: 330: 331:
332: public function setVisible($visible) {
333: $this->_visible = $visible;
334: }
335:
336: 337: 338: 339: 340:
341: public function getVisible() {
342: return $this->_visible;
343: }
344:
345: private $_getEnabled;
346:
347: 348: 349: 350: 351:
352: public function setGetEnabled($enabled) {
353: $this->_getEnabled = $enabled;
354: }
355:
356: 357: 358: 359: 360:
361: public function getGetEnabled() {
362: return $this->_getEnabled;
363: }
364:
365: private $_getExplicit;
366:
367: 368: 369: 370: 371:
372: public function setGetExplicit($explicit) {
373: $this->_getExplicit = $explicit;
374: }
375:
376: 377: 378: 379: 380: 381: 382: 383: 384:
385: public function getGetExplicit() {
386: return $this->_getExplicit;
387: }
388:
389: private $_cachable;
390:
391: 392: 393: 394: 395:
396: public function setCachable($cachable) {
397: $this->_cachable = $cachable;
398: }
399:
400: 401: 402: 403: 404: 405:
406: public function getCachable() {
407: return $this->_cachable;
408: }
409:
410: private $_url;
411:
412: 413: 414: 415: 416:
417: public function setPageUrl($url) {
418: $this->_url = $url;
419: }
420:
421: 422: 423: 424: 425:
426: public function getPageUrl() {
427: return $this->_url;
428: }
429:
430: private $_url301;
431:
432: 433: 434: 435: 436:
437: public function getUrl301() {
438: if ($this->_url301 === null) {
439: $this->_url301 = new DataList(false);
440:
441: $table_page = DataBase::objectToTableName($this);
442: if (DataBase::getInstance(_DB_DSN_PAGE)->exist($table_page)) {
443: $table_list = $table_page . '_array';
444:
445: $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
446: $where = array('page_id' => $this->getId(), 'page_prop' => '_url301');
447: $query->initSelect('value', '`' . $table_list . '`', $where);
448: $rows = DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query);
449: foreach ($rows as $row) {
450: $this->_url301->add($row['value']);
451: }
452: }
453: }
454:
455: return $this->_url301;
456: }
457:
458: 459: 460: 461: 462: 463: 464: 465:
466: public function comparePageUrl($pageUrl) {
467: $thisLength = strlen($this->_url);
468: if ($this->_url == $pageUrl) {
469: return $thisLength + 1;
470: }
471: if (!$this->_getEnabled) {
472: return -1;
473: }
474:
475: $otherLength = strlen($pageUrl);
476: if ($thisLength > $otherLength) {
477: return -1;
478: }
479: if ($this->_url == substr($pageUrl, 0, $thisLength)) {
480: return $thisLength;
481: }
482:
483: return 0;
484: }
485:
486: private $_htmlBody;
487:
488: 489: 490: 491: 492:
493: public function setHtmlBody($body) {
494: $this->_htmlBody = $body;
495: }
496:
497: 498: 499: 500: 501:
502: public function getHtmlBody() {
503: return $this->_htmlBody;
504: }
505:
506: private $_template;
507:
508: 509: 510: 511: 512:
513: public function setTemplate($template) {
514: $this->_template = $template;
515: }
516:
517: 518: 519: 520: 521:
522: public function getTemplate() {
523: return $this->_template;
524: }
525:
526: private $_buildFile;
527:
528: 529: 530: 531: 532:
533: public function setBuildFile($buildFile) {
534: $this->_buildFile = $buildFile;
535: }
536:
537: 538: 539: 540: 541:
542: public function getBuildFile() {
543: return $this->_buildFile;
544: }
545:
546: 547: 548: 549: 550:
551: public function render($activeHeader = true) {
552: if (_DEBUG && $this->_template != '' &&
553: !file_exists(_TEMPLATE_DIRECTORY . $this->_template . '.php')) {
554: Debug::getInstance()->addError('The template "' . _TEMPLATE_DIRECTORY . $this->_template . '.php don\'t exist": page:' . $this->_id);
555: }
556:
557: if ($this->_template != '' && file_exists(_TEMPLATE_DIRECTORY . $this->_template . '.php')) {
558: \Flea::getHeader()->appliHeaderOfPage($this);
559:
560: ob_start();
561: include _TEMPLATE_DIRECTORY . $this->_template . '.php';
562: $content = ob_get_clean();
563: return \Flea::getBuildUtil()->replaceFleaVars($content, $this);
564: } else {
565: return $this->renderWithoutTemplate($activeHeader);
566: }
567: }
568:
569: 570: 571: 572: 573:
574: public function renderWithoutTemplate($activeHeader = true) {
575: 576: 577: 578: 579: 580: 581:
582:
583: \Flea::getHeader()->appliHeaderOfPage($this);
584:
585: return \Flea::getBuildUtil()->replaceFleaVars($this->_htmlBody, $this);
586: }
587:
588: 589: 590: 591: 592: 593:
594: public function __construct($name = '', $lang = null) {
595: if ($lang === null) {
596: $lang = LangList::getInstance()->getDefault();
597: }
598: $this->setLang($lang);
599: $this->setName($name);
600: $this->_metas = null;
601: $this->_tags = null;
602:
603: $this->_type = '';
604: $this->_date = '';
605: $this->_format = Page::$FORMAT_HTML;
606:
607: $this->_visible = true;
608: $this->_getEnabled = false;
609: $this->_getExplicit = true;
610: $this->_cachable = true;
611: $this->_url301 = null;
612:
613: $this->_url = '';
614:
615: $this->_htmlBody = '';
616:
617: $this->_template = '';
618:
619: $this->_phpHeader = '';
620: $this->_buildFile = '';
621: }
622:
623: 624: 625: 626: 627:
628: public static function getEmptyPage() {
629: if (Page::$_EMPTY === null)
630: Page::$_EMPTY = new Page();
631: return Page::$_EMPTY;
632: }
633:
634: 635: 636: 637: 638: 639: 640:
641: public function getObjectVars() {
642: $obj = get_object_vars($this);
643: foreach ($obj as $key => $value) {
644: if (gettype($value) == 'object' &&
645: get_class($value) == get_class(DataList::getEmptyDataList())) {
646: $obj[$key] = $value->getArray();
647: }
648: }
649:
650: return $obj;
651: }
652:
653: 654: 655: 656: 657:
658: public function setByObjectVars($obj) {
659: foreach ($obj as $key => $value) {
660: if (gettype($value) == 'array') {
661: $this->$key = new DataList();
662: $this->$key->setByArray($value);
663: } else {
664: $this->$key = $value;
665: }
666: }
667: }
668:
669: }
670: