Commits

Christoffer Niska committed 689ed85

Added the SeoTitle widget and renamed the SeoMetaWidget to SeoMeta.

Comments (0)

Files changed (3)

widgets/SeoMeta.php

+<?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.
+	}
+}

widgets/SeoMetaWidget.php

-<?php
-/**
- * SeoMetaWidget 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 SeoMetaWidget 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.
-	}
-}

widgets/SeoTitle.php

+<?php
+/**
+ * SeoTitle 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
+ * @since 0.9.1
+ */
+
+class SeoTitle extends CWidget
+{
+	/**
+	 * @property string the page title separator.
+	 */
+	public $separator = ' | ';
+	/**
+	 * @property boolean whether to append the application name to the title.
+	 */
+	public $appendAppName = true;
+
+	/**
+	 * Runs the widget.
+	 */
+	public function run()
+	{
+		$parts = array();
+		$controller = $this->getController();
+		$pageTitle = $controller->pageTitle;
+
+		if (!$this->isDefaultPageTitle($pageTitle, $controller))
+		{
+			if (!is_array($pageTitle))
+				$pageTitle = array($pageTitle);
+
+			$parts = $pageTitle;
+		}
+		else if (($breadcrumbs = $controller->breadcrumbs) !== array())
+		{
+			foreach ($breadcrumbs as $key => $value)
+				$parts[] = !empty($value) ? $value : $key;
+
+			$parts = array_reverse($parts);
+		}
+		else
+		{
+			$name = ucfirst($controller->getId());
+			$action = $controller->getAction();
+			$module = $controller->getModule();
+
+			if ($action !== null && strcasecmp($action->getId(), $controller->defaultAction))
+				$parts[] = ucfirst($action->getId()).' '.$name;
+			else if ($module !== null && strcasecmp($name, $module->defaultController))
+				$parts[] = $name;
+
+			if ($module !== null)
+			{
+				$pieces = explode('/', $module->getId());
+				foreach (array_reverse($pieces) as $piece)
+					$parts[] = ucfirst($piece);
+			}
+		}
+
+		if ($this->appendAppName)
+			$parts[] = Yii::app()->name;
+
+		$title = implode($parts, $this->separator);
+		echo '<title>'.$title.'</title>';
+	}
+
+	/**
+	 * Returns whether or not the given title is the default page title.
+	 * @param mixed $pageTitle the page title
+	 * @param CController $controller the controller
+	 * @return bool
+	 */
+	protected function isDefaultPageTitle($pageTitle, $controller)
+	{
+		$name = ucfirst(basename($controller->getId()));
+		return is_string($pageTitle)
+				&& ($pageTitle === Yii::app()->name.' - '.ucfirst($controller->getAction()->getId()).' '.$name
+				|| $pageTitle === Yii::app()->name.' - '.$name);
+	}
+}