Overview

Namespaces

  • Flea
  • None

Classes

  • Flea
  • Flea\BuildUtil
  • Flea\Cache
  • Flea\DataBase
  • Flea\DataBaseCRUD
  • Flea\DataList
  • Flea\DataUtil
  • Flea\Debug
  • Flea\FileUtil
  • Flea\General
  • Flea\Header
  • Flea\InitUtil
  • Flea\LangList
  • Flea\Login
  • Flea\LoginFormHelper
  • Flea\LoginTableName
  • Flea\LoginUser
  • Flea\Page
  • Flea\PageList
  • Flea\PageListCreate
  • Flea\SqlQuery
  • Flea\TagUtil
  • Flea\UrlUtil
  • Flea\ValueObject
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * The MIT License
  5:  *
  6:  * Copyright 2014 Damien Doussaud (namide.com).
  7:  *
  8:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9:  * of this software and associated documentation files (the "Software"), to deal
 10:  * in the Software without restriction, including without limitation the rights
 11:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12:  * copies of the Software, and to permit persons to whom the Software is
 13:  * furnished to do so, subject to the following conditions:
 14:  *
 15:  * The above copyright notice and this permission notice shall be included in
 16:  * all copies or substantial portions of the Software.
 17:  *
 18:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24:  * THE SOFTWARE.
 25:  */
 26: 
 27: namespace Flea;
 28: 
 29: /**
 30:  * All the datas of a page
 31:  *
 32:  * @author namide.com
 33:  */
 34: class Page {
 35: 
 36:     /**
 37:      * Default page (homepage)
 38:      * @var string
 39:      */
 40:     public static $TYPE_DEFAULT = 'default';
 41: 
 42:     /**
 43:      * Page not found
 44:      * @var string 
 45:      */
 46:     public static $TYPE_ERROR404 = 'error404';
 47: 
 48:     /**
 49:      * Redirection of this page
 50:      * @var string 
 51:      */
 52:     public static $TYPE_REDIRECT301 = 'redirect301';
 53: 
 54:     /**
 55:      * HTML
 56:      * @var string 
 57:      */
 58:     public static $FORMAT_HTML = 'html';
 59: 
 60:     /**
 61:      * XML
 62:      * @var string 
 63:      */
 64:     public static $FORMAT_XML = 'xml';
 65: 
 66:     /**
 67:      * CSS
 68:      * @var string 
 69:      */
 70:     public static $FORMAT_CSS = 'css';
 71: 
 72:     /**
 73:      * JavaScript
 74:      * @var string 
 75:      */
 76:     public static $FORMAT_JS = 'js';
 77: 
 78:     /**
 79:      * PDF
 80:      * @var string 
 81:      */
 82:     public static $FORMAT_PDF = 'pdf';
 83: 
 84:     /**
 85:      * ZIP
 86:      * @var string 
 87:      */
 88:     public static $FORMAT_ZIP = 'zip';
 89: 
 90:     /**
 91:      * Json
 92:      * @var string 
 93:      */
 94:     public static $FORMAT_JSON = 'json';
 95:     private static $_EMPTY = null;
 96:     private $_id;
 97: 
 98:     /**
 99:      * ID of the page.
100:      * The ID is unique, it's composed by the name and the language of the page.
101:      * 
102:      * @return string       ID of the page
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:      * Name of the Page.
116:      * Like an ID, but an page has the same ID for differents languages
117:      * 
118:      * @param string $name  Name of the Page
119:      */
120:     public function setName($name) {
121:         $this->_name = $name;
122:         $this->updateId();
123:     }
124: 
125:     /**
126:      * Name of the Page
127:      * 
128:      * @return string   Name of the Page
129:      */
130:     public function getName() {
131:         return $this->_name;
132:     }
133: 
134:     private $_lang;
135: 
136:     /**
137:      * Language of the Page (fr, en, ko...)
138:      * The list of languages is in the LangList.php
139:      * 
140:      * @param string $lang  Language
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:      * Language of the Page (fr, en, ko...)
152:      * The list of languages is in the LangList.php
153:      * 
154:      * @return string   Language
155:      */
156:     public function getLang() {
157:         return $this->_lang;
158:     }
159: 
160:     private $_date;
161: 
162:     /**
163:      * Date of the Page, no set format
164:      * 
165:      * @param string $date  Date
166:      */
167:     public function setDate($date) {
168:         $this->_date = $date;
169:     }
170: 
171:     /**
172:      * Date of the Page
173:      * 
174:      * @return string   Date
175:      */
176:     public function getDate() {
177:         return $this->_date;
178:     }
179: 
180:     private $_type;
181: 
182:     /**
183:      * Type of the page.
184:      * The page can be :
185:      * - Page::$TYPE_DEFAULT, 
186:      * - Page::$TYPE_ERROR404, 
187:      * - Page::$TYPE_REDIRECT301
188:      * 
189:      * @param string $type  Type of the page
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:      * Type of the page
204:      * 
205:      * @return string       Type of the page
206:      */
207:     public function getType() {
208:         return $this->_type;
209:     }
210: 
211:     private $_format;
212: 
213:     /**
214:      * Format of the page.
215:      * The format can be :
216:      * - Page::$FORMAT_HTML, 
217:      * - Page::$FORMAT_XML, 
218:      * - Page::$FORMAT_CSS, 
219:      * - Page::$FORMAT_JS,
220:      * - Page::$FORMAT_JSON
221:      * 
222:      * @param string $format    format of the page
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:      * Format of the page
238:      * 
239:      * @return string       Foramt of the page
240:      */
241:     public function getFormat() {
242:         return $this->_format;
243:     }
244: 
245:     private $_tags;
246: 
247:     /**
248:      * Tags of the page.
249:      * You can use tags for search a list of pages.
250:      * 
251:      * @return DataList     List of tags
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:      * A meta is a pair with key and value.
279:      * You can't add 2 metas with same label.
280:      * 
281:      * @return DataList 
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:      * Set phpHeader (for header() function)
309:      * 
310:      * @param string $phpHeader     PHP header
311:      */
312:     public function setPhpHeader($phpHeader) {
313:         $this->_phpHeader = $phpHeader;
314:     }
315: 
316:     /**
317:      * phpHeader if the page has a special format (XML...)
318:      * 
319:      * @return string   PHP header
320:      */
321:     public function getPhpHeader() {
322:         return $this->_phpHeader;
323:     }
324: 
325:     private $_visible;
326: 
327:     /**
328:      * Set the visibility of the page
329:      * 
330:      * @param boolean $visible  Visibility of the page
331:      */
332:     public function setVisible($visible) {
333:         $this->_visible = $visible;
334:     }
335: 
336:     /**
337:      * If the page is visible (sitemap.xml...)
338:      * 
339:      * @return boolean  Visibility of the page
340:      */
341:     public function getVisible() {
342:         return $this->_visible;
343:     }
344: 
345:     private $_getEnabled;
346: 
347:     /**
348:      * Active or unactive the GET method
349:      * 
350:      * @param boolean $enabled      Global variables GET enabled
351:      */
352:     public function setGetEnabled($enabled) {
353:         $this->_getEnabled = $enabled;
354:     }
355: 
356:     /**
357:      * If the GET is activated the page can have several URL with the same base
358:      * 
359:      * @return boolean      Global variables GET enabled
360:      */
361:     public function getGetEnabled() {
362:         return $this->_getEnabled;
363:     }
364: 
365:     private $_getExplicit;
366: 
367:     /**
368:      * Active or unactive the explicit GET
369:      * 
370:      * @param boolean $explicit     Global variables GET explicit or not
371:      */
372:     public function setGetExplicit($explicit) {
373:         $this->_getExplicit = $explicit;
374:     }
375: 
376:     /**
377:      * If the GET is explicit the URL contains the labels of values.
378:      * - URL: www.flea.namide.com/games
379:      * - GET: array( 'page'=>2, 'tag'=>'RTS' );
380:      * - explicit: www.flea.namide.com/games/page/2/tag/RTS
381:      * - !explicit: www.flea.namide.com/games/2/RTS
382:      * 
383:      * @return boolean      Global variables GET explicit or not
384:      */
385:     public function getGetExplicit() {
386:         return $this->_getExplicit;
387:     }
388: 
389:     private $_cachable;
390: 
391:     /**
392:      * set the cachability of the page
393:      * 
394:      * @param boolean $cachable     Is the page cachable or not
395:      */
396:     public function setCachable($cachable) {
397:         $this->_cachable = $cachable;
398:     }
399: 
400:     /**
401:      * Cachability of the page.
402:      * It's recomended to don't cache a page with POST datas.
403:      * 
404:      * @return boolean      Is the page cachable or not
405:      */
406:     public function getCachable() {
407:         return $this->_cachable;
408:     }
409: 
410:     private $_url;
411: 
412:     /**
413:      * Set the URL of the page (without Root and GET)
414:      * 
415:      * @param string $url       URL of the page
416:      */
417:     public function setPageUrl($url) {
418:         $this->_url = $url;
419:     }
420: 
421:     /**
422:      * URL of the page (without Root and GET)
423:      * 
424:      * @return string   URL of the page
425:      */
426:     public function getPageUrl() {
427:         return $this->_url;
428:     }
429: 
430:     private $_url301;
431: 
432:     /**
433:      * Additional 301 URL for this page (without Root and GET)
434:      * 
435:      * @return DataList
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:      * Compare the URL, if this page accept GET it can accept others URL.
460:      * = -1 if the URL is different
461:      * = +x to x = the sames caracters
462:      * 
463:      * @param string $pageUrl   URL of the page (without root, with GET)
464:      * @return int              If they are differents: -1 or 0 , otherwise positive
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:      * Set the body of the page
490:      * 
491:      * @param string $body      Body of the page
492:      */
493:     public function setHtmlBody($body) {
494:         $this->_htmlBody = $body;
495:     }
496: 
497:     /**
498:      * Body content in the HTML page
499:      * 
500:      * @return type         Body of the page
501:      */
502:     public function getHtmlBody() {
503:         return $this->_htmlBody;
504:     }
505: 
506:     private $_template;
507: 
508:     /**
509:      * Change the template of the page
510:      * 
511:      * @param string $template      Template of the page
512:      */
513:     public function setTemplate($template) {
514:         $this->_template = $template;
515:     }
516: 
517:     /**
518:      * Template used for the page
519:      * 
520:      * @return string       Template of the page
521:      */
522:     public function getTemplate() {
523:         return $this->_template;
524:     }
525: 
526:     private $_buildFile;
527: 
528:     /**
529:      * Change the build file of the page
530:      * 
531:      * @param string $buildFile     Build file
532:      */
533:     public function setBuildFile($buildFile) {
534:         $this->_buildFile = $buildFile;
535:     }
536: 
537:     /**
538:      * Used in second time to update the content(s) of the page
539:      * 
540:      * @return string       Build file
541:      */
542:     public function getBuildFile() {
543:         return $this->_buildFile;
544:     }
545: 
546:     /**
547:      * Echo the page (with template and Flea variables {{...}} transformed)
548:      * 
549:      * @param bool $activeHeader        Active the header datas (redirect301, error404...)
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:      * Echo the page with Flea variables {{...}} transformed but without template
571:      * 
572:      * @param bool $activeHeader        Active the header datas (redirect301, error404...)
573:      */
574:     public function renderWithoutTemplate($activeHeader = true) {
575:         /* if ( $this->_type === Page::$TYPE_REDIRECT301 && $activeHeader )
576:           {
577:           $absNewURL = \Flea::getBuildUtil()->replaceFleaVars( $this->_htmlBody, $this );
578:           header( 'Status: 301 Moved Permanently' );
579:           header( 'Location: '.$absNewURL );
580:           exit;
581:           } */
582: 
583:         \Flea::getHeader()->appliHeaderOfPage($this);
584: 
585:         return \Flea::getBuildUtil()->replaceFleaVars($this->_htmlBody, $this);
586:     }
587: 
588:     /**
589:      * A page object contain all the datas of an HTML page
590:      * 
591:      * @param type $name        Name of the page
592:      * @param type $lang        Language of the page
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:      * Get an empty page to use temporarily.
625:      * 
626:      * @return Page     Empty page
627:      */
628:     public static function getEmptyPage() {
629:         if (Page::$_EMPTY === null)
630:             Page::$_EMPTY = new Page();
631:         return Page::$_EMPTY;
632:     }
633: 
634:     /**
635:      * Get an associative array with all the unstatic properties of the page
636:      * (public, private and protected).
637:      * The DataList properties are converted in an array.
638:      * 
639:      * @return array        Associative array with all the properties       
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:      * Set all this Page with an object_vars
655:      * 
656:      * @param array $obj    Object_vars (associative array)
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: 
Flea API documentation generated by ApiGen