PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Ecart/View/Helper/Meta.php

https://code.google.com/p/ecartcommerce/
PHP | 247 lines | 184 code | 21 blank | 42 comment | 22 complexity | 1b8e89cc44d21e150e6600134a200852 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. * @copyright Copyright 2008-2009 E-Cart LLC
  20. * @license GNU Public License V3.0
  21. */
  22. /**
  23. *
  24. * @category Ecart
  25. * @package Ecart_View
  26. * @subpackage Ecart_View_Helper
  27. * @author Ecart Core Team <core@ecartcommerce.com>
  28. */
  29. class Ecart_View_Helper_Meta
  30. {
  31. private $_meta = array();
  32. private $_modes = array('cms_category', 'cms_page', 'category', 'product');
  33. public function __construct()
  34. {
  35. $this->_config = Ecart::config()->design->htmlHead;
  36. }
  37. public function setDefaults()
  38. {
  39. $this->setTitle();
  40. $this->setDescription();
  41. $this->setKeywords();
  42. $this->setRobots();
  43. }
  44. /**
  45. * Enter description here...
  46. *
  47. * @param array $meta
  48. * @param string $mode ['cms_category', 'cms_page', 'category', 'product']
  49. * @param int $parentId
  50. * @return array
  51. */
  52. public function set($meta = false, $mode = null, $parentId = null)
  53. {
  54. $this->setTitle($meta['title'], $mode, $parentId);
  55. unset($meta['title']);
  56. $meta = array_filter($meta);
  57. if (is_array($meta)) {
  58. $this->_meta = array_merge($this->_meta, $meta);
  59. }
  60. return $this->_meta;
  61. }
  62. public function meta()
  63. {
  64. return $this;
  65. }
  66. public function setTitle($title = null, $mode = null, $parentId = null)
  67. {
  68. // if (null !== $mode && in_array($mode, $this->_modes)) {
  69. // $title = $this->_getMeta($title, $mode, 'title');
  70. // }
  71. $titleArray = array();
  72. foreach ($this->_config->titlePattern as $titlePart) {
  73. switch (strtolower($titlePart)) {
  74. case 'page title':
  75. if (null !== $title) {
  76. $titleArray[] = $title;
  77. } else {
  78. $titleArray[] = $this->_config->defaultTitle;
  79. }
  80. break;
  81. case 'parent page titles':
  82. if (null === $mode || !in_array($mode, $this->_modes)) {
  83. break;
  84. }
  85. switch ($mode) {
  86. case 'category':
  87. $path = array_reverse(
  88. Ecart::single('catalog/category')
  89. ->find($parentId)->current()->getParentItems()
  90. );
  91. array_shift($path);
  92. break;
  93. case 'product':
  94. $path = array_reverse(
  95. Ecart::single('catalog/product')
  96. ->find($parentId)->current()->getParentItems()
  97. );
  98. break;
  99. case 'cms_category':
  100. $path = array_reverse(
  101. Ecart::single('cms/category')
  102. ->getParentCategory($parentId)
  103. );
  104. array_shift($path);
  105. break;
  106. case 'cms_page':
  107. $path= array_reverse(
  108. Ecart::single('cms/category')
  109. ->getParentCategory($parentId, true)
  110. );
  111. break;
  112. }
  113. foreach ($path as $item) {
  114. $item['name'] = isset($item['name']) ?
  115. $item['name'] : $item['title'];
  116. $titleArray[] = trim($item['meta_title']) == '' ?
  117. trim($item['name']) : trim($item['meta_title']);
  118. }
  119. break;
  120. case 'site name':
  121. $row = Ecart::single('core/site')
  122. ->find(Ecart::getSiteId())->current();
  123. if ($row) {
  124. $titleArray[] = $row->name;
  125. }
  126. break;
  127. }
  128. }
  129. $this->_meta['title'] = htmlspecialchars(
  130. $this->_config->titlePrefix .
  131. trim(implode($this->_config->titleDivider, $titleArray)).
  132. $this->_config->titleSuffix
  133. );
  134. return $this;
  135. }
  136. public function getTitle()
  137. {
  138. return $this->_meta['title'];
  139. }
  140. public function setDescription($description = null, $mode = null)
  141. {
  142. if (null === $description) {
  143. $description = $this->_config->defaultDescription;
  144. }
  145. if (null !== $mode && in_array($mode, $this->_modes)) {
  146. $description = $this->_getMeta($description, $mode, 'description');
  147. }
  148. if (!empty($description)) {
  149. $this->_meta['description'] = $description;
  150. }
  151. return $this;
  152. }
  153. public function getDescription()
  154. {
  155. return $this->_meta['description'];
  156. }
  157. public function setKeywords($keywords = null, $mode = null)
  158. {
  159. if (null === $keywords) {
  160. $keywords = $this->_config->defaultRobots;
  161. }
  162. if (null !== $mode && in_array($mode, $this->_modes)) {
  163. $keywords = $this->_getMeta($keywords, $mode, 'keywords');
  164. }
  165. if (!empty($keywords)) {
  166. $this->_meta['keywords'] = $keywords;
  167. }
  168. return $this;
  169. }
  170. public function getKeywords()
  171. {
  172. return $this->_meta['keywords'];
  173. }
  174. public function setRobots($robots = null)
  175. {
  176. if (null === $robots) {
  177. $robots = $this->_config->defaultRobots;
  178. }
  179. if (!empty($robots)) {
  180. $this->_meta['robots'] = str_replace(' ', ',', $robots);
  181. }
  182. return $this;
  183. }
  184. public function getRobots()
  185. {
  186. return $this->_meta['robots'];
  187. }
  188. private function _getMeta($id, $mode, $type = 'title')
  189. {
  190. switch ($mode) {
  191. case 'category':
  192. $entry = Ecart::single('catalog/category_description')
  193. ->find($id, Ecart_Locale::getLanguageId())->current();
  194. break;
  195. case 'product':
  196. $entry = Ecart::single('catalog/product_description')
  197. ->find($id, Ecart_Locale::getLanguageId())->current();
  198. break;
  199. case 'cms_category':
  200. $entry = Ecart::single('cms/category_content')
  201. ->find($id, Ecart_Locale::getLanguageId())->current();
  202. $entry->name = $entry->title;
  203. break;
  204. case 'cms_page':
  205. $entry = Ecart::single('cms/page_content')
  206. ->find($id, Ecart_Locale::getLanguageId())->current();
  207. $entry->name = $entry->title;
  208. break;
  209. }
  210. $result = null;
  211. switch ($type) {
  212. // case 'title':
  213. // $result = $entry->meta_title == '' ?
  214. // $entry->name : $entry->meta_title;
  215. // break;
  216. case 'description':
  217. $result = $entry->meta_description == '' ?
  218. $entry->description : $entry->meta_title;
  219. break;
  220. case 'keywords':
  221. $result = $entry->meta_keyword;
  222. break;
  223. }
  224. return $result;
  225. }
  226. }