Source

yii-seo / widgets / SeoMeta.php

Full commit
<?php
/**
 * SeoMeta class file.
 * @author Christoffer Niska <ChristofferNiska@gmail.com>
 * @copyright Copyright &copy; Christoffer Niska 2011-
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */

class SeoMeta extends CWidget
{
	/**
	 * @property array the page http-equivs.
	 */
	public $httpEquivs = array();
	/**
	 * @property string the page meta description.
	 */
	public $defaultDescription;
	/**
	 * @property string the page meta keywords.
	 */
	public $defaultKeywords;
	/**
	 * @property array the page meta properties.
	 */
	public $defaultProperties = array();

	private $_description;
	private $_keywords;
	private $_properties = array();

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		$behavior = $this->controller->asa('seo');

		if ($behavior !== null && $behavior->metaDescription !== null)
			$this->_description = $behavior->metaDescription;
		else if ($this->defaultDescription !== null)
			$this->_description = $this->defaultDescription;

		if ($behavior !== null && $behavior->metaKeywords !== null)
			$this->_keywords = $behavior->metaKeywords;
		else if ($this->defaultKeywords !== null)
			$this->_keywords = $this->defaultKeywords;

		if ($behavior !== null)
			$this->_properties = CMap::mergeArray($behavior->metaProperties, $this->defaultProperties);
		else
			$this->_properties = $this->defaultProperties;
	}

	/**
	 * Runs the widget.
	 */
	public function run()
	{
		foreach ($this->httpEquivs as $name => $content)
			echo '<meta http-equiv="'.$name.'" content="'.$content.'" />';

		if ($this->_description !== null)
			echo CHtml::metaTag($this->_description, 'description');

		if ($this->defaultKeywords !== null)
			echo CHtml::metaTag($this->_keywords, 'keywords');

		foreach ($this->_properties as $name => $content)
			echo '<meta property="'.$name.'" content="'.$content.'" />'; // we can't use Yii's method for this.
	}
}