/library/Ecart/View/Helper/Meta.php
PHP | 247 lines | 184 code | 21 blank | 42 comment | 22 complexity | 1b8e89cc44d21e150e6600134a200852 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?php
- /**
- * Ecart
- *
- * This file is part of Ecart.
- *
- * Ecart is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Ecart is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
- * @copyright Copyright 2008-2009 E-Cart LLC
- * @license GNU Public License V3.0
- */
-
- /**
- *
- * @category Ecart
- * @package Ecart_View
- * @subpackage Ecart_View_Helper
- * @author Ecart Core Team <core@ecartcommerce.com>
- */
- class Ecart_View_Helper_Meta
- {
- private $_meta = array();
- private $_modes = array('cms_category', 'cms_page', 'category', 'product');
-
-
- public function __construct()
- {
- $this->_config = Ecart::config()->design->htmlHead;
- }
-
- public function setDefaults()
- {
- $this->setTitle();
- $this->setDescription();
- $this->setKeywords();
- $this->setRobots();
- }
-
- /**
- * Enter description here...
- *
- * @param array $meta
- * @param string $mode ['cms_category', 'cms_page', 'category', 'product']
- * @param int $parentId
- * @return array
- */
- public function set($meta = false, $mode = null, $parentId = null)
- {
- $this->setTitle($meta['title'], $mode, $parentId);
- unset($meta['title']);
-
- $meta = array_filter($meta);
- if (is_array($meta)) {
- $this->_meta = array_merge($this->_meta, $meta);
- }
- return $this->_meta;
- }
-
- public function meta()
- {
- return $this;
- }
-
- public function setTitle($title = null, $mode = null, $parentId = null)
- {
- // if (null !== $mode && in_array($mode, $this->_modes)) {
- // $title = $this->_getMeta($title, $mode, 'title');
- // }
- $titleArray = array();
- foreach ($this->_config->titlePattern as $titlePart) {
- switch (strtolower($titlePart)) {
- case 'page title':
- if (null !== $title) {
- $titleArray[] = $title;
- } else {
- $titleArray[] = $this->_config->defaultTitle;
- }
- break;
- case 'parent page titles':
- if (null === $mode || !in_array($mode, $this->_modes)) {
- break;
- }
-
- switch ($mode) {
- case 'category':
- $path = array_reverse(
- Ecart::single('catalog/category')
- ->find($parentId)->current()->getParentItems()
- );
- array_shift($path);
- break;
- case 'product':
- $path = array_reverse(
- Ecart::single('catalog/product')
- ->find($parentId)->current()->getParentItems()
- );
- break;
- case 'cms_category':
- $path = array_reverse(
- Ecart::single('cms/category')
- ->getParentCategory($parentId)
- );
- array_shift($path);
- break;
- case 'cms_page':
- $path= array_reverse(
- Ecart::single('cms/category')
- ->getParentCategory($parentId, true)
- );
- break;
- }
-
- foreach ($path as $item) {
- $item['name'] = isset($item['name']) ?
- $item['name'] : $item['title'];
- $titleArray[] = trim($item['meta_title']) == '' ?
- trim($item['name']) : trim($item['meta_title']);
- }
-
- break;
- case 'site name':
- $row = Ecart::single('core/site')
- ->find(Ecart::getSiteId())->current();
- if ($row) {
- $titleArray[] = $row->name;
- }
- break;
- }
- }
-
- $this->_meta['title'] = htmlspecialchars(
- $this->_config->titlePrefix .
- trim(implode($this->_config->titleDivider, $titleArray)).
- $this->_config->titleSuffix
- );
- return $this;
- }
-
- public function getTitle()
- {
- return $this->_meta['title'];
- }
-
- public function setDescription($description = null, $mode = null)
- {
- if (null === $description) {
- $description = $this->_config->defaultDescription;
- }
- if (null !== $mode && in_array($mode, $this->_modes)) {
- $description = $this->_getMeta($description, $mode, 'description');
- }
- if (!empty($description)) {
- $this->_meta['description'] = $description;
- }
- return $this;
- }
-
- public function getDescription()
- {
- return $this->_meta['description'];
- }
-
- public function setKeywords($keywords = null, $mode = null)
- {
- if (null === $keywords) {
- $keywords = $this->_config->defaultRobots;
- }
- if (null !== $mode && in_array($mode, $this->_modes)) {
- $keywords = $this->_getMeta($keywords, $mode, 'keywords');
- }
- if (!empty($keywords)) {
- $this->_meta['keywords'] = $keywords;
- }
- return $this;
- }
-
- public function getKeywords()
- {
- return $this->_meta['keywords'];
- }
-
- public function setRobots($robots = null)
- {
- if (null === $robots) {
- $robots = $this->_config->defaultRobots;
- }
- if (!empty($robots)) {
- $this->_meta['robots'] = str_replace(' ', ',', $robots);
- }
- return $this;
- }
-
- public function getRobots()
- {
- return $this->_meta['robots'];
- }
-
- private function _getMeta($id, $mode, $type = 'title')
- {
- switch ($mode) {
- case 'category':
- $entry = Ecart::single('catalog/category_description')
- ->find($id, Ecart_Locale::getLanguageId())->current();
- break;
- case 'product':
- $entry = Ecart::single('catalog/product_description')
- ->find($id, Ecart_Locale::getLanguageId())->current();
- break;
- case 'cms_category':
- $entry = Ecart::single('cms/category_content')
- ->find($id, Ecart_Locale::getLanguageId())->current();
- $entry->name = $entry->title;
- break;
- case 'cms_page':
- $entry = Ecart::single('cms/page_content')
- ->find($id, Ecart_Locale::getLanguageId())->current();
- $entry->name = $entry->title;
- break;
- }
-
- $result = null;
- switch ($type) {
- // case 'title':
- // $result = $entry->meta_title == '' ?
- // $entry->name : $entry->meta_title;
- // break;
- case 'description':
- $result = $entry->meta_description == '' ?
- $entry->description : $entry->meta_title;
- break;
- case 'keywords':
- $result = $entry->meta_keyword;
- break;
- }
- return $result;
- }
- }