Commits

Christoffer Niska committed f6d9a16

Added the project files.

Comments (0)

Files changed (5)

+Copyright (c) 2010, Christoffer Niska
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+    * Neither the name of the Christoffer Niska nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

components/SeoControllerBehavior.php

+<?php
+/**
+ * SeoControllerBehavior 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 SeoControllerBehavior extends CBehavior
+{
+	/**
+	 * @property string the page meta description.
+	 */
+	public $metaDescription;
+	/**
+	 * @property string the page meta keywords.
+	 */
+	public $metaKeywords;
+	/**
+	 * @property array the page meta properties.
+	 */
+	public $metaProperties = array();
+
+	/**
+	 * Adds a meta property to the current page.
+	 * @param string $name the property name
+	 * @param string $content the property content
+	 */
+	public function addMetaProperty($name, $content)
+	{
+		$this->metaProperties[$name] = $content;
+	}
+}

components/SeoFilter.php

+<?php
+/**
+ * SeoFilter 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 SeoFilter extends CFilter
+{
+	/**
+	 * Performs the pre-action filtering.
+	 * @param CFilterChain $filterChain the filter chain that the filter is on.
+	 * @return boolean whether the filtering process should continue and the action
+	 * should be executed.
+	 */
+	protected function preFilter($filterChain)
+	{
+		$controller = $filterChain->controller;
+
+		if (method_exists($controller, 'loadModel'))
+		{
+			$model = $controller->loadModel($_GET['id']);
+			$url = $model->getUrl();
+
+			if (strpos(Yii::app()->request->getRequestUri(), $url) === false)
+				$controller->redirect($url, true, 301);
+		}
+
+		return parent::preFilter($filterChain);
+	}
+}

components/SeoRecordBehavior.php

+<?php
+/**
+ * SeoRecordBehavior 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 SeoRecordBehavior extends CActiveRecordBehavior
+{
+	/**
+	 * @property string the route used for SEO.
+	 */
+	public $route;
+	/**
+	 * @property array GET parameters used for SEO.
+	 */
+	public $params = array();
+
+	/**
+	 * Returns the URL for this model.
+	 * @param array $params additional GET parameters (name=>value)
+	 * @return string the URL
+	 */
+	public function getUrl($params=array())
+	{
+		return Yii::app()->createUrl($this->route, CMap::mergeArray($params, $this->params));
+	}
+
+	/**
+	 * Returns the absolute URL for this model.
+	 * @param array $params additional GET parameters (name=>value)
+	 * @return string the URL
+	 */
+	public function getAbsoluteUrl($params=array())
+	{
+		return Yii::app()->createAbsoluteUrl($this->route, CMap::mergeArray($params, $this->params));
+	}
+}

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