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:  * List of pages in the website.
 31:  * 
 32:  * @author Namide
 33:  */
 34: class PageList {
 35: 
 36:     private static $_INSTANCE = null;
 37: 
 38:     //public static $LOAD_INIT = 0;
 39:     //public static $LOAD_LIST = 1;
 40: 
 41:     /**
 42:      * List of pages.
 43:      * By default this query return all the pages without the non-visible pages.
 44:      * To change this you must use a SqlQuery with an other where defined.
 45:      * Example:
 46:      * <pre>
 47:      * $query = SqlQuery::getTemp( SqlQuery::$TYPE_SELECT );
 48:      * $query->setWhere('_visible = 1 AND _visible = 0');
 49:      * $pages = PageList::getInstance()->getAll( $query );
 50:      * </pre>
 51:      * 
 52:      * @param SqlQuery $query       Query for the request
 53:      * @return array                All the Pages
 54:      */
 55:     public function getAll(SqlQuery $query = null) {
 56:         if ($query === null)
 57:             $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
 58:         else
 59:             $query->setType(SqlQuery::$TYPE_SELECT);
 60: 
 61:         if ($query->getWhere() == '')
 62:             $query->setWhere('_visible = 1');
 63: 
 64:         if ($query->getSelect() == '')
 65:             $query->setSelect('*');
 66: 
 67:         if ($query->getFrom() == '')
 68:             $query->setFrom('`' . DataBase::objectToTableName(Page::getEmptyPage()) . '`');
 69: 
 70:         if ($query->getOrderBy() == '')
 71:             $query->setOrderBy('_date DESC');
 72: 
 73:         $pages = array();
 74:         foreach (DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query) as $row) {
 75:             $page = new Page();
 76:             $page->setByObjectVars($row);
 77:             $pages[$page->getId()] = $page;
 78:         }
 79: 
 80:         return $pages;
 81:     }
 82: 
 83:     /**
 84:      * Get the page and use the list of the page.
 85:      * You can use the values used in the DataList of the Page (tags, metas...)
 86:      * <br>Example:
 87:      * <pre>
 88:      * $query = SqlQuery::getTemp( SqlQuery::$TYPE_SELECT );
 89:      * $query->setWhere('_lang = \'en\' AND page_prop = \'_tags\' AND value = \'post\' AND _visible = 1');
 90:      * $pages = PageList::getInstance()->getByList ( $query );
 91:      * </pre>
 92:      * 
 93:      * @param SqlQuery $query       Query for the request
 94:      * @return Page                 List the Pages corresponding of the request
 95:      */
 96:     public function getByList(SqlQuery $query) {
 97:         $table_page = DataBase::objectToTableName(Page::getEmptyPage());
 98:         $table_list = $table_page . '_array';
 99: 
100:         $pages = array();
101: 
102:         $query->setType(SqlQuery::$TYPE_SELECT);
103:         $query->setSelect('*');
104:         $query->setFrom('`' . $table_page . '` '
105:                 . 'LEFT JOIN `' . $table_list . '` '
106:                 . 'ON ' . $table_page . '._id = ' . $table_list . '.page_id');
107: 
108:         if ($query->getOrderBy() == '')
109:             $query->setOrderBy('_date DESC');
110: 
111:         foreach (DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query) as $row) {
112:             $page = new Page();
113:             $page->setByObjectVars($row);
114: 
115:             $pages[$page->getId()] = $page;
116:         }
117: 
118:         return $pages;
119:     }
120: 
121:     /**
122:      * Update the page.
123:      * Used to build the body with the "build file" (ex: en-build.php).
124:      * 
125:      * @param Page $page    Page to update
126:      * @return Page         Same page updated
127:      */
128:     public function buildPage(&$page) {
129:         if ($page->getBuildFile() === '') {
130:             return $page;
131:         }
132: 
133:         ob_start();
134:         include $page->getBuildFile();
135:         $page->setHtmlBody(ob_get_clean());
136: 
137:         return $page;
138:     }
139: 
140:     /**
141:      * List of pages in the language $lang
142:      * 
143:      * @param string $lang  Language
144:      * @return array        All the page for this language
145:      */
146:     public function getAllByLang($lang) {
147:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
148:         $query->setWhere('_lang = \'' . $lang . '\' AND _visible = 1');
149:         return $this->getAll($query);
150:     }
151: 
152:     /**
153:      * You must use the static method PageList::getInstance();
154:      */
155:     final private function __construct() {
156:         
157:     }
158: 
159:     /**
160:      * Return all the pages with the name $name (all langues)
161:      * 
162:      * @param string $name      Name of the pages
163:      * @return array            List of the pages with the name $name
164:      */
165:     public function getAllByName($name) {
166:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
167:         $query->setWhere('_name = \'' . $name . '\' AND _visible = 1');
168:         return $this->getAll($query);
169:     }
170: 
171:     /**
172:      * Return a list of pages with the tag
173:      * 
174:      * @param string $tag       Tag for the page
175:      * @param string $lang      Language of the page
176:      * @return array            List of the pages
177:      */
178:     public function getByTag($tag, $lang) {
179:         $query = SqlQuery::getTemp();
180:         $query->setWhere(' _lang = \'' . $lang . '\' AND page_prop = \'_tags\' AND value = \'' . $tag . '\' AND _visible = 1');
181:         return $this->getByList($query);
182:     }
183: 
184:     /**
185:      * Return a list of pages with each page has this meta
186:      * 
187:      * @param string $metaLabel     Label of the meta
188:      * @param string $metaValue     Value of the meta
189:      * @param string $lang          Language of the elements
190:      * @return array                List of elements
191:      */
192:     public function getWithThisContent($metaLabel, $metaValue, $lang) {
193:         $where = '_lang = \'' . $lang . '\' AND '
194:                 . 'page_prop = \'_metas\' AND '
195:                 . 'key = \'' . $metaLabel . '\' AND '
196:                 . 'value = \'' . $metaValue . '\' AND '
197:                 . '_visible = 1';
198: 
199:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
200:         $query->setWhere($where);
201: 
202:         return $this->getByList($query);
203:     }
204: 
205:     /**
206:      * Return a list of pages with each page has this meta
207:      * 
208:      * @param string $metaLabel     Label of the meta
209:      * @param array $metaValue      Value of the meta
210:      * @param string $lang          Language of the elements
211:      * @return array                List of elements
212:      */
213:     public function getWithOneOfThisContents($metaLabel, array $metaValues, $lang) {
214:         $where = '_lang = \'' . $lang . '\' AND '
215:                 . 'page_prop = \'_metas\' AND '
216:                 . 'key = \'' . $metaLabel . '\' AND '
217:                 . '_visible = 1 AND (';
218: 
219:         $first = true;
220:         foreach ($metaValues as $val) {
221:             if (!$first)
222:                 $where .= ' OR';
223: 
224:             $where .= ' value = \'' . $val . '\'';
225:             $first = false;
226:         }
227:         $where .= ')';
228: 
229:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
230:         $query->setWhere($where);
231: 
232:         return $this->getByList($query);
233:     }
234: 
235:     /**
236:      * Return a list of pages with each page has at least one of your tags
237:      * 
238:      * @param array $tags       List of tags (withouts keys)
239:      * @param string $lang      Language of the elements
240:      * @return array            List of elements
241:      */
242:     public function getWithOneOfTags(array $tags, $lang) {
243:         if (count($tags) < 1)
244:             return array();
245: 
246:         $where = '_lang = \'' . $lang . '\' AND '
247:                 . 'page_prop = \'_tags\' AND '
248:                 . '_visible = 1 AND (';
249: 
250:         $first = true;
251:         foreach ($tags as $tag) {
252:             if (!$first)
253:                 $where .= ' OR';
254: 
255:             $where .= ' value = \'' . $tag . '\'';
256:             $first = false;
257:         }
258:         $where .= ')';
259:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
260:         $query->setWhere($where);
261: 
262: 
263:         return $this->getByList($query);
264:     }
265: 
266:     /**
267:      * Return a list of pages with each pages has each of tags
268:      * 
269:      * @param array $tags       List of tags
270:      * @param string $lang      Language of the pages
271:      * @return array            List of pages
272:      */
273:     public function getWithAllTags(array $tags, $lang) {
274:         $allPages = $this->getWithOneOfTags($tags, $lang);
275:         $goodPages = array();
276: 
277:         foreach ($allPages as $page) {
278:             $ok = true;
279:             foreach ($tags as $tag) {
280:                 if (!$page->getTags()->hasValue($tag)) {
281:                     $ok = false;
282:                 }
283:             }
284:             if ($ok) {
285:                 $goodPages[] = $page;
286:             }
287:         }
288: 
289:         return $goodPages;
290:     }
291: 
292:     /**
293:      * Return a list of pages for a language
294:      * 
295:      * @param string $lang  Language of the pages
296:      * @return array        List of pages
297:      */
298:     public function getByLang($lang) {
299:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
300:         $query->setWhere('_lang = \'' . $lang . '\' AND _visible = 1');
301:         return $this->getAll($query);
302:     }
303: 
304:     /**
305:      * Test if the page with this ID and this language exist
306:      * 
307:      * @param string $name      Name of the page
308:      * @param string $lang      Language of the page
309:      * @return boolean          Exist
310:      */
311:     public function has($name, $lang = null) {
312:         $tableName = DataBase::objectToTableName(Page::getEmptyPage());
313: 
314:         $query = SqlQuery::getTemp();
315:         $query->initCount($tableName);
316:         //$where = '_name = \''.$name.'\' AND _visible = 1';
317:         $where = '_name = \'' . $name . '\'';
318:         if ($lang !== null) {
319:             $where .= ' AND _lang = \'' . $lang . '\'';
320:         }
321:         $query->setWhere($where);
322: 
323:         return ( DataBase::getInstance(_DB_DSN_PAGE)->count($query) > 0);
324:     }
325: 
326:     //private $_default;
327:     /**
328:      * The name of the default page
329:      * 
330:      * @return string   Default page
331:      */
332:     //public function getDefaultPageName() { return $this->_default; }
333: 
334:     private $_error404;
335: 
336:     /**
337:      * The name of the error 404 page
338:      * 
339:      * @return string   Error 404 page
340:      */
341:     public function getError404PageName() {
342:         return $this->_error404;
343:     }
344: 
345:     /**
346:      * Edit the page to convert in error page
347:      * 
348:      * @param Page &$page   Same page updated
349:      */
350:     private function makeError404Page(&$page) {
351:         $page->setVisible(false);
352:         $page->setCachable(false);
353:         $page->setPhpHeader('HTTP/1.0 404 Not Found');
354:         return $page;
355:     }
356: 
357:     /**
358:      * Get the page by relative URL
359:      * 
360:      * @param string $relURL    Relative URL
361:      * @return Page             Corresponding page
362:      */
363:     public function getByUrl($relURL) {
364:         if (_DEBUG && !General::getInstance()->isPagesInitialized()) {
365:             Debug::getInstance()->addError('All pages must be initialised after use getByUrl()');
366:         }
367: 
368:         // EXIST    
369:         $query = SqlQuery::getTemp();
370:         $query->setWhere('_url LIKE \'' . $relURL . '\' OR (page_prop = \'_url301\' AND value = \'' . $relURL . '\')');
371:         $pages1 = $this->getByList($query);
372:         if (count($pages1) > 0)
373:             return current($pages1);
374: 
375: 
376:         // EXIST WITH "/" AT THE END
377:         if (strlen($relURL) < 1 || (strlen($relURL) > 0 && $relURL[strlen($relURL) - 1] !== '/')) {
378:             $urlTemp = $relURL . '/';
379: 
380:             $query->clean(SqlQuery::$TYPE_SELECT);
381:             $query->setWhere('_url LIKE \'' . $urlTemp . '\' OR (page_prop = \'_url301\' AND value = \'' . $urlTemp . '\')');
382:             $pages = $this->getByList($query);
383:             if (count($pages) > 0)
384:                 return current($pages);
385:         }
386: 
387:         // EXIST WITHOUT "/" AT THE END
388:         if (strlen($relURL) > 0 && $relURL[strlen($relURL) - 1] === '/') {
389:             $urlTemp = substr($relURL, 0, strlen($relURL) - 1);
390: 
391:             $query->clean(SqlQuery::$TYPE_SELECT);
392:             $query->setWhere('_url LIKE \'' . $urlTemp . '\' OR (page_prop = \'_url301\' AND value = \'' . $urlTemp . '\')');
393:             $pages = $this->getByList($query);
394:             if (count($pages) > 0)
395:                 return current($pages);
396:         }
397: 
398: 
399: 
400:         $lang = LangList::getInstance()->getLangByNavigator();
401: 
402:         // IS DYNAMIC PAGE
403:         $query->clean(SqlQuery::$TYPE_SELECT);
404:         $query->setSelect('_id');
405:         $query->setFrom('`' . DataBase::objectToTableName(Page::getEmptyPage()) . '`');
406:         $query->setWhere('SUBSTR( \'' . $relURL . '\', 0, LENGTH(_url)+1 ) LIKE _url AND _getEnabled = 1');
407:         $query->setOrderBy('LENGTH(_url) DESC');
408: 
409:         $pages2 = DataBase::getInstance(_DB_DSN_PAGE)->fetchAll($query);
410:         if (count($pages2) > 0) {
411:             foreach ($pages2 as $pageTemp) {
412:                 $query->clean();
413:                 $query->setWhere('_id LIKE \'' . $pageTemp['_id'] . '\'');
414:                 $pagesTemp = $this->getByList($query);
415:                 if (count($pagesTemp) > 0) {
416:                     return current($pagesTemp);
417:                 }
418:             }
419:         }
420: 
421:         if (_DEBUG) {
422:             Debug::getInstance()->addError('The URL "' . $relURL . '" don\'t exist');
423:         }
424: 
425:         return $this->getError404Page($lang);
426:     }
427: 
428:     /**
429:      * Get the error 404 page if it's defined.
430:      * Otherwise return a standart error 404 page.
431:      * 
432:      * @param string $lang      Language of the page
433:      * @return Page             Error 404 page
434:      */
435:     public function getError404Page($lang) {
436:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
437:         $query->setLimit(1);
438:         $query->setWhere('_type = \'' . Page::$TYPE_ERROR404 . '\' AND _lang = \'' . $lang . '\'');
439:         $pages1 = $this->getAll($query);
440:         if (count($pages1) > 0) {
441:             return current($pages1);
442:         }
443: 
444:         $query->setWhere('_type = \'' . Page::$TYPE_ERROR404 . '\'');
445:         $pages2 = $this->getAll($query);
446:         if (count($pages2) > 0) {
447:             return current($pages2);
448:         }
449: 
450:         // CREATE PAGE ERROR 404
451:         $page = new Page();
452:         /* $page->setHtmlHeader( '<title>Error 404 - Not found</title>
453:           <meta charset="UTF-8" />
454:           <meta name="robots" content="noindex,nofollow" />
455:           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' ); */
456:         $page->setHtmlBody('<h1>Error 404 - Not found</h1>');
457:         $this->makeError404Page($page);
458:         return $page;
459:         // END OF CREATE PAGE ERROR 404
460:     }
461: 
462:     /**
463:      * Default page for the language
464:      * 
465:      * @param string $lang      Language of the default page
466:      * @return Page             Default page
467:      */
468:     public function getDefaultPage($lang) {
469:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
470:         $query->setLimit(1);
471: 
472:         $query->setWhere('_type = \'' . Page::$TYPE_DEFAULT . '\' AND _lang = \'' . $lang . '\'');
473:         $pages1 = $this->getAll($query);
474:         if (count($pages1) > 0) {
475:             return current($pages1);
476:         }
477: 
478:         /* IF THE LANGUAGE OF THE DEFAULT PAGE DON'T EXIST */
479:         $query->setWhere('_type = \'' . Page::$TYPE_DEFAULT . '\'');
480:         $pages2 = $this->getAll($query);
481:         if (count($pages2) > 0) {
482:             return current($pages2);
483:         }
484: 
485:         /* IF DEFAULT PAGE DON'T EXIST */
486:         return $this->getError404Page($lang);
487:     }
488: 
489:     /**
490:      * Test if the page exist.
491:      * 
492:      * @param string $name  Name of the page
493:      * @param string $lang  Language
494:      * @return bool         True if the page exist
495:      */
496:     public function exist($name, $lang) {
497:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
498:         $query->setWhere('_name = \'' . $name . '\' AND _lang = \'' . $lang . '\'');
499:         $query->setLimit(1);
500:         $pages = $this->getAll($query);
501: 
502:         return ( count($pages) > 0 );
503:     }
504: 
505:     /**
506:      * Return the page or, if it doesn't exist, the default page
507:      * 
508:      * @param string $name  Name of the page
509:      * @param string $lang  Language
510:      * @return Page         Page corresponding
511:      */
512:     public function getByName($name, $lang) {
513:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
514:         $query->setWhere('_name = \'' . $name . '\' AND _lang = \'' . $lang . '\'');
515:         $query->setLimit(1);
516:         $pages = $this->getAll($query);
517: 
518:         if (count($pages) > 0) {
519:             return current($pages);
520:         }
521: 
522:         if (_DEBUG) {
523:             Debug::getInstance()->addError('The page ' . $name . ',' . $lang . ' don\'t exist');
524:         }
525: 
526:         return $this->getDefaultPage($lang);
527:     }
528: 
529:     /**
530:      * Try if the URL exist.
531:      * 
532:      * @param string $url   Relative URL
533:      * @return boolean      URL exist
534:      */
535:     public function hasUrl($url) {
536:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
537:         $query->setWhere('_url LIKE \'' . $url . '\' OR (page_prop = \'_url301\' AND value = \'' . $url . '\')');
538:         $query->setLimit(1);
539:         $pages = $this->getByList($query);
540:         return ( count($pages) > 0 );
541:     }
542: 
543:     /**
544:      * Get the language from the URL
545:      * 
546:      * @param string $url   Relative URL
547:      * @return string       Language
548:      */
549:     private function getLangByUrl($url) {
550:         $query = SqlQuery::getTemp(SqlQuery::$TYPE_SELECT);
551:         $query->setWhere('_url LIKE \'' . $url . '\' OR (page_prop = \'_url301\' AND value = \'' . $url . '\')');
552:         $query->setLimit(1);
553:         $pages = $this->getByList($query);
554:         if (count($pages) > 0) {
555:             return current($pages)->getLang();
556:         }
557: 
558:         return LangList::getInstance()->getLangByNavigator();
559:     }
560: 
561:     final private function __clone() {
562:         if (_DEBUG) {
563:             Debug::getInstance()->addError('You can\'t clone a singleton');
564:         }
565:     }
566: 
567:     /**
568:      * Instance of the PageList
569:      * 
570:      * @return static   Instance of the object PageList
571:      */
572:     final public static function getInstance() {
573:         if (PageList::$_INSTANCE === null) {
574:             PageList::$_INSTANCE = new PageList();
575:         }
576:         return PageList::$_INSTANCE;
577:     }
578: 
579: }
580: 
Flea API documentation generated by ApiGen