1: <?php
2:
3: /*
4: * The MIT License
5: *
6: * Copyright 2014 damien.
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: * Helper for tags.
31: * You can use this class for create tags ( a, img, breadcrump )
32: *
33: * @author damien
34: */
35: class TagUtil {
36:
37: private static $_INSTANCE;
38:
39: private function __construct() {
40:
41: }
42:
43: /**
44: * Simple method to create a link.
45: * Ex:
46: * <pre>
47: * $pageName = 'home';
48: * $tagBeforeText = '<em>';
49: * $tagAfterText = '</em>';
50: * $attInA = 'class="link-home blue"';
51: * getLink( $pageName, 'title', null, $attInA, $tagBeforeText, $tagAfterText );
52: * //output => <a href="http://flea.namide.com/en/home" class="link-home blue">Home page<em></em></a>
53: * </pre>
54: *
55: * @param string $pageName Name of the page to linked
56: * @param string $metaKey Key of the meta to the content of the tag '<a></a>'
57: * @param string $lang Language of the page (current language by default)
58: * @param string $attInA Additionnal attribute to the tag '<a></a>'
59: * @param string $tagBeforeText Tag before the title of the page (in the tag '<a></a>')
60: * @param string $tagAfterText Tag after the title of the page (in the tag '<a></a>')
61: * @return string HTML link generated
62: */
63: public function getLinkByName($pageName, $metaKey, $lang = null, $attInA = '', $tagBeforeText = '', $tagAfterText = '') {
64: if ($lang === null) {
65: $lang = General::getInstance()->getCurrentLang();
66: }
67: $pageList = PageList::getInstance();
68: $page = $pageList->getByName($pageName, $lang);
69: return $this->getLink($page, $metaKey, $attInA, $tagBeforeText, $tagAfterText);
70: }
71:
72: /**
73: * Simple method to create a link with a page object.
74: *
75: * @param \Flea\Page $page Page to linked
76: * @param string $metaKey Key of the meta to the content of the tag '<a></a>'
77: * @param string $attInA Additionnal attribute to the tag '<a></a>'
78: * @param string $tagBeforeText Tag before the title of the page (in the tag '<a></a>')
79: * @param string $tagAfterText Tag after the title of the page (in the tag '<a></a>')
80: * @return type
81: */
82: public function getLink(Page $page, $metaKey, $attInA = '', $tagBeforeText = '', $tagAfterText = '') {
83: $buildUtil = \Flea::getBuildUtil();
84: return '<a href="' . $buildUtil->getAbsUrlByPageUrl($page->getPageUrl())
85: . '" ' . $attInA . ' hreflang="' . $page->getLang() . '">'
86: . $tagBeforeText . $page->getMetas()->getValue($metaKey) . $tagAfterText . '</a>';
87: }
88:
89: /**
90: * Simple method to create an HTML list of pages.
91: *
92: * @param array $pageList Array of Page
93: * @param string $metaKey Key of the meta to the content of the tag '<a></a>'
94: * @param string $attInUl Additionnal attribute to the tag '<ul></ul>'
95: * @param string $attInLi Additionnal attribute to the tag '<li></li>'
96: * @param string $attInA Additionnal attribute to the tag '<a></a>'
97: * @return string HTML list generated
98: */
99: public function getLinkList(array $pageList, $metaKey, $attInUl = '', $attInLi = '', $attInA = '') {
100: $out = '<ul ' . $attInUl . '>';
101: foreach ($pageList as $page) {
102: $out .= '<li ' . $attInLi . '>' . $this->getLink($page, $metaKey, $attInA) . '</li>';
103: }
104: $out .= '</ul>';
105: return $out;
106: }
107:
108: /**
109: * Get an HTML list of all other languages with their language code ("en", "fr", "ko"...)
110: *
111: * @param \Flea\Page $page Actual page
112: * @return string An HTML list of others languages with links to the same page in other languages
113: */
114: public function getOtherLanguages(Page $page = null) {
115: if ($page === null) {
116: $page = General::getInstance()->getCurrentPage();
117: }
118: $langList = LangList::getInstance()->getList();
119: $currentLang = General::getInstance()->getCurrentLang();
120:
121: $output = '<ul>';
122: foreach ($langList as $langTemp) {
123: if ($langTemp != 'all' && $langTemp != $currentLang) {
124: if (PageList::getInstance()->exist($page->getName(), $langTemp)) {
125: $output .= '<li><a href="'
126: . \Flea::getBuildUtil()->getAbsUrlByNameLang($page->getName(), $langTemp)
127: . '" hreflang="' . $langTemp . '">'
128: . $langTemp . '</a></li>';
129: } else {
130: $output .= '<li><a href="'
131: . \Flea::getBuildUtil()->getAbsUrlByNameLang(PageList::getInstance()->getDefaultPage($langTemp)->getName(), $langTemp)
132: . '" hreflang="' . $langTemp . '">'
133: . $langTemp . '</a></li>';
134: }
135: }
136: }
137: $output .= '</ul>';
138:
139: return $output;
140: }
141:
142: /**
143: * Simple method to create an img
144: *
145: * @param type $fileName Name of the image to include
146: * @param type $alt Alternative content of the tag <img/>
147: * @param type $attInImg Additionnal attribute to the tag <img/>
148: * @return type Tag img with : alt, width, height and $attInImg
149: */
150: public function getImg($fileName, $alt = '', $attInImg = '') {
151: if (!is_file($fileName)) {
152: return '<img src="' . $fileName . '" alt="' . $alt . '" ' . $attInImg . '/>';
153: }
154: list( $width, $height, $type, $attr ) = getimagesize($filename);
155:
156: $img = '<img src="' . $fileName . '" width="' . $width . '" height="' . $height . '"';
157: if ($alt != '')
158: $img.= ' alt="' . $alt . '"';
159: if ($attInImg != '')
160: $img.= ' ' . $attInImg;
161: $img .= '/>';
162: return $img;
163: }
164:
165: /**
166: * Simple method to get breadcrump of the current page.
167: * It have microdatas.
168: *
169: * @param type $metaTitle Key of the meta to have the title of the page
170: * @param Page $currentPage Current page (optional if the pages are initialized)
171: * @param string $delimiter String between the links
172: * @return string Tag of the breadcrump
173: */
174: public function getBreadcrump($metaTitle, Page $currentPage = null, $delimiter = '') {
175: if ($currentPage === null) {
176: if (_DEBUG && !General::getInstance()->isPagesInitialized()) {
177: Debug::getInstance()->addError('All pages must be initialised after use TagUtil::getBreadcrump( $argument ) method without argument');
178: }
179: $currentPage = General::getInstance()->getCurrentPage();
180: }
181:
182: $path = explode('/', $currentPage->getPageUrl());
183: $lang = $currentPage->getLang();
184: $numParentsPages = count($path);
185: $output = '';
186:
187: if ($numParentsPages > 1) {
188: $output = '<nav class="breadcrumb"><ul>';
189: foreach ($path as $l => $url) {
190: $url = $path[0];
191: for ($i = 1; $i <= $l; $i++) {
192: $url .= '/' . $path[$i];
193: }
194: $temp = '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb">';
195: $temp .= '<a href="' . \Flea::getBuildUtil()->getAbsUrlByPageUrl($url) . '" '
196: . 'hreflang="' . $lang . '" '
197: . 'itemprop="url">';
198: $temp .= '<span itemprop="title">' . PageList::getInstance()->getByUrl($url)->getMetas()->getValue($metaTitle) . '</span>';
199: $temp .= '</a></li>';
200: $output .= ( $l > 0 ) ? $delimiter : '';
201: $output .= $temp;
202: }
203: $output .= '</ul></nav>';
204: }
205: return $output;
206: }
207:
208: private function __clone() {
209:
210: }
211:
212: /**
213: * Get the instance of TagUtil
214: *
215: * @return TagUtil TagUtil instancied
216: */
217: public static function getInstance() {
218: if (!isset(self::$_INSTANCE)) {
219: self::$_INSTANCE = new self();
220: }
221:
222: return self::$_INSTANCE;
223: }
224:
225: }
226: