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: include_once _SYSTEM_DIRECTORY . 'helpers/common/InitUtil.php';
 30: 
 31: /**
 32:  * Utils to write pages and template.
 33:  * This class cannot be used before the building page time.
 34:  *
 35:  * @author Namide
 36:  */
 37: class BuildUtil extends InitUtil {
 38: 
 39:     /**
 40:      * Get the page with the pageName
 41:      * 
 42:      * @param string $pageName      Name of the page
 43:      * @param string $lang          Language of the page (if null it's the current language)
 44:      * @return Page                 Instance of the Page
 45:      */
 46:     public function getPage($pageName, $lang = null) {
 47:         if ($lang === null) {
 48:             $lang = General::getInstance()->getCurrentLang();
 49:         }
 50:         return PageList::getInstance()->getByName($pageName, $lang);
 51:     }
 52: 
 53:     /**
 54:      * Get the absolute URL for a page
 55:      * 
 56:      * @param Page $page            Page
 57:      * @param array $gets           Additionnal GET of the URL
 58:      * @return string               Absolute URL
 59:      */
 60:     public function getAbsUrlByPage(Page $page, array $gets = null) {
 61:         $relUrl = UrlUtil::getInstance()->getRelUrlByIdLang($page, $gets);
 62: 
 63:         return _ROOT_URL . $relUrl;
 64:     }
 65: 
 66:     /**
 67:      * Get the absolute URL of a page by his page URL
 68:      * 
 69:      * @param string $url               Page URL
 70:      * @param array $gets               Additionnal GET of the URL
 71:      * @param boolean $explicitGet      Are this GET datas explicit
 72:      * @return string                   Absolute URL of the page
 73:      */
 74:     public function getAbsUrlByPageUrl($url, array $gets = null, $explicitGet = true) {
 75:         $relUrl = UrlUtil::getInstance()->getRelUrlByPageUrl($url, $gets, $explicitGet);
 76: 
 77:         return _ROOT_URL . $relUrl;
 78:     }
 79: 
 80:     /**
 81:      * Fast method to get the page URL of a page by his page absolute URL.
 82:      * Not efficient if you have getters, setters...
 83:      * 
 84:      * @param string $absUrl        Absolute URL of the page
 85:      * @return string               Page URL
 86:      */
 87:     public function getPageUrlByAbsUrl($absUrl) {
 88:         return strtr($absUrl, array(_ROOT_URL => ''));
 89:     }
 90: 
 91:     /**
 92:      * Absolute URL for a page
 93:      * 
 94:      * @param string $pageName      Name of the page
 95:      * @param string $lang          Language of the page
 96:      * @param array $getUrl         Array of GET of the page (optional)
 97:      * @return string               Absolute URL
 98:      */
 99:     public function getAbsUrlByNameLang($pageName, $lang, array $getUrl = null) {
100:         $PageList = PageList::getInstance();
101:         $page = $PageList->getByName($pageName, $lang);
102:         $relUrl = UrlUtil::getInstance()->getRelUrlByIdLang($page, $getUrl);
103: 
104:         return _ROOT_URL . $relUrl;
105:     }
106: 
107:     /**
108:      * Reset the object (new evaluation of current page)
109:      */
110:     public function reset() {
111:         $pageList = PageList::getInstance();
112:         $general = General::getInstance();
113:         if (_DEBUG && !$general->isPagesInitialized()) {
114:             Debug::getInstance()->addError('All pages must be initialised after use BuildUtil class');
115:         }
116:         $pageUrl = $general->getCurrentPageUrl();
117:         $page = $pageList->getByUrl($pageUrl);
118:         $general->setCurrentPage($page);
119:     }
120: 
121:     /**
122:      * Absolute URL for a page
123:      * 
124:      * @param string $pageName  Name of the page
125:      * @param array $gets       Additionnal GET of the URL
126:      * @return string           Absolute URL of the page        
127:      */
128:     public function getAbsUrl($pageName, array $gets = null) {
129:         $lang = General::getInstance()->getCurrentLang();
130: 
131:         return $this->getAbsUrlByNameLang($pageName, $lang, $gets);
132:     }
133: 
134:     /**
135:      * Return the equivalent page in other language.
136:      * Return the default page if this one don't exist
137:      * 
138:      * @param string $lang
139:      * @param Page &$page
140:      * @return Page
141:      */
142:     public function getOtherLang($lang, Page &$page = null) {
143:         $pageList = PageList::getInstance();
144:         if ($page === null) {
145:             $page = General::getInstance()->getCurrentPage();
146:         }
147:         if ($pageList->has($page->getName(), $lang)) {
148:             return $pageList->getByName($page->getName(), $lang);
149:         }
150: 
151:         return $pageList->getDefaultPage($lang);
152:     }
153: 
154:     /**
155:      * Get one of the metass of a Page
156:      * 
157:      * @param type $metaKey             Key of the meta
158:      * @param Page $page                Page to get the meta
159:      * @param type $replaceFleaVars     Replace or not the FleaVars
160:      * @return string                   The corresponding formated meta
161:      */
162:     public function getContentOfPage($metaKey, Page &$page, $replaceFleaVars = false) {
163:         if (!$page->getMetas()->hasKey($metaKey)) {
164:             if (_DEBUG) {
165:                 Debug::getInstance()->addError('The meta "' . $metaKey . '" '
166:                         . ' don\'t exist for the page [' . $page->getId() . ']');
167:             }
168:             return '';
169:         }
170: 
171:         $meta = $page->getMetas()->getValue($metaKey);
172:         if ($replaceFleaVars) {
173:             return $this->replaceFleaVars($meta, $page);
174:         }
175: 
176:         return $meta;
177:     }
178: 
179:     /**
180:      * Format the text by converting the Flea variables to real datas.
181:      * List of Flea variables :
182:      * - {{rootPath}}           => URL of the root
183:      * - {{templatePath}}       => URL of the template directory
184:      * - {{contentPath}}        => URL of the content directory
185:      * - {{pageContentPath}}    => URL of the page in the content directory
186:      * - {{lang}}               => current language
187:      * - {{title}}              => title of the current page
188:      * - {{header}}             => HTML header of the current page
189:      * - {{body}}               => HTML body of the current page
190:      * - {{description}}        => HTML description of the current page
191:      * - {{meta:additionnal-label-meta}}        => $currentPage->getContent('additionnal-label-meta');
192:      * - {{pageNameToAbsUrl:page-name}}         => $buildUtil->getAbsUrlByNameLang( ‘page-name', $currentLanguage );  
193:      * 
194:      * @param string $text      Original text
195:      * @param Page $page        Current page
196:      * @return string           Formated text               
197:      */
198:     public function replaceFleaVars($text, Page &$page = null) {
199:         $general = General::getInstance();
200: 
201:         if ($page === null) {
202:             $page = $general->getCurrentPage();
203:         }
204: 
205:         //$replacePage = str_replace('{{title}}', $page->getHtmlTitle(), $text);
206:         //$replacePage = str_replace('{{header}}', $page->getHtmlHeader(), $replacePage);
207:         $replacePage = str_replace('{{body}}', $page->getHtmlBody(), $text);
208:         //$replacePage = str_replace('{{description}}', $page->getHtmlDescription(), $replacePage);
209:         //$replacePage = str_replace('{{cover}}', $page->getCover(), $replacePage);
210: 
211:         $replacePage = str_replace('{{rootPath}}', _ROOT_URL, $replacePage);
212:         $replacePage = str_replace('{{templatePath}}', _ROOT_URL . _TEMPLATE_DIRECTORY, $replacePage);
213:         $replacePage = str_replace('{{contentPath}}', _ROOT_URL . _CONTENT_DIRECTORY, $replacePage);
214: 
215: 
216:         $replacePage = str_replace('{{lang}}', $general->getCurrentLang(), $replacePage);
217:         $replacePage = str_replace('{{date}}', $page->getDate(), $replacePage);
218: 
219:         $replacePage = str_replace('{{pageContentPath}}', _ROOT_URL . _CONTENT_DIRECTORY . $page->getName() . '/', $replacePage);
220: 
221: 
222:         $replacePage = preg_replace_callback('/\{\{urlPageToAbsoluteUrl:(.*?)\}\}/', function ($matches) {
223:             return BuildUtil::getInstance()->getAbsUrlByPageUrl($matches[1]);
224:         }, $replacePage);
225: 
226:         if ($page !== null) {
227:             $replacePage = preg_replace_callback('/\{\{meta:(.*?)\}\}/', function ($matches) use($page) {
228:                 if (!$page->getMetas()->hasKey($matches[1])) {
229:                     if (_DEBUG) {
230:                         Debug::getInstance()->addError('The FleaVar {{meta:' . $matches[1] . '}}'
231:                                 . ' don\'t exist for the page [' . $page->getId() . ']');
232:                     }
233:                     return '';
234:                 }
235: 
236:                 return $page->getMetas()->getValue($matches[1]);
237:             }, $replacePage);
238: 
239:             $replacePage = preg_replace_callback('/\{\{pageNameToAbsUrl:(.*?)\}\}/', function ($matches) use($page) {
240:                 $lang = $page->getLang();
241: 
242:                 if (!PageList::getInstance()->has($matches[1], $lang)) {
243:                     if (_DEBUG) {
244:                         Debug::getInstance()->addError('The page {{pageNameToAbsUrl:' . $matches[1] . '}}'
245:                                 . ' don\'t exist for the language [' . $lang . ']');
246:                     }
247:                     return '';
248:                 }
249: 
250:                 return BuildUtil::getInstance()->getAbsUrlByNameLang($matches[1], $lang);
251:             }, $replacePage);
252:         }
253: 
254:         return $replacePage;
255:     }
256: 
257: }
258: 
Flea API documentation generated by ApiGen