<?php

/**
 * Description of SeoController
 *
 * @author Mohammed Osman  <osmancode@gmail.com>
 */
class SeoController extends CController {

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    private $_seo;
	private $modelTitle='';
	private $modelId=0;
     
	public function setData($_modelTitle,$_modelId)
	{
		$this->modelTitle=$_modelTitle;
		$this->modelId=$_modelId;
	}

    public function setSeoProperty($key, $value) {
        $this->_seo[$key] = $value;
    }

    public function getSeoProperty($key, $default = NULL) {
        return isset($this->_seo[$key]) ? $this->_seo[$key] : $default;
    }

    public function renderSEO() {
       
		//echo $this->modelTitle.' '.$this->modelId;
		
        $this->setSeoDefaults();
        /* @var $options OptionsManager */
        $options = Yii::app()->options;
        /* @var $cs CClientScript */
        $cs = Yii::app()->getClientScript();

        $cs->registerLinkTag('image_src', null, $this->_seo['image_src']);



        $cs->registerMetaTag($this->_seo['title'], 'application-name');
        $cs->registerMetaTag($this->_seo['title'], 'msapplication-tooltip');
        $cs->registerMetaTag($this->_seo['description'], 'description');
        $cs->registerMetaTag($this->_seo['keywords'], 'keywords');
        $cs->registerMetaTag($this->_seo['author'], 'author');

        if ($googleplus_page = $options->get('GOOGLEPLUS_PAGE', false)) {
            $cs->registerLinkTag('author', null, $googleplus_page);
            $cs->registerLinkTag('me', null, $googleplus_page);
            $cs->registerLinkTag('publisher', null, $googleplus_page);
        }


        /**
         * Open Graph Protocol
         * Please refer to http://ogp.me/ for more details
         */
        $cs->registerMetaTag($this->_seo['image_src'], null, null, array('property' => 'og:image'));
        $cs->registerMetaTag($this->_seo['description'], null, null, array('property' => 'og:description'));
        $cs->registerMetaTag($this->_seo['title'], null, null, array('property' => 'og:title'));
        $cs->registerMetaTag('website', null, null, array('property' => 'og:type'));
        $cs->registerMetaTag(Yii::app()->name, null, null, array('property' => 'og:site_name'));
        /**
         * Twitter Cards
         * Please refer to https://dev.twitter.com/docs/cards‎ for more details
         */
        $cs->registerMetaTag('summary', 'twitter:card');

        if ($twitter_page = $options->get('TWITTER_PAGE', false)) {
            $cs->registerMetaTag($twitter_page, 'twitter:site');
            $cs->registerMetaTag($twitter_page, 'twitter:creator');
        }
        $cs->registerMetaTag($this->_seo['title'], 'twitter:title');
        $cs->registerMetaTag($this->_seo['description'], 'description:title');
        $cs->registerMetaTag($this->_seo['image_src'], 'description:image');
        $cs->registerMetaTag($this->_seo['image_src'], 'description:image:src');
    }

    private function setSeoDefaults() {
  
        $baseUrl = Yii::app()->getBaseUrl(true);
        $options = Yii::app()->options;
        //
        if (!isset($this->_seo['image_src']))
            $this->_seo['image_src'] = $baseUrl . '/images/image_src.png';
        if (!isset($this->_seo['description']))
            $this->_seo['description'] = $options->get('SEO_DESCRIPTION');
        if (!isset($this->_seo['keywords']))
            $this->_seo['keywords'] = $options->get('SEO_KEYWORDS');
        if (!isset($this->_seo['author']))
            $this->_seo['author'] = $options->get('SEO_AUTHOR');
		if(!isset($this->_seo['title']))
			$this->_seo['title']=$this->pageTitle;
    }

    public function renderSEOBreadcrumb() {
        $hostInfo = Yii::app()->getRequest()->getHostInfo();

        $urlTemplate = "<meta itemprop=\"url\" content=\"{url}\" />\n";
        $titleTemplate = "<meta itemprop=\"title\" content=\"{title}\" />\n";
        $itemTemplate = "<div {child} itemscope itemtype=\"http://data-vocabulary.org/Breadcrumb\">\n";

        echo "<div id=\"seo-breadcrumb\" class=\"hide hidden display-none\" >\n";
        if (is_array($this->breadcrumbs) && count($this->breadcrumbs)) {
            $index = 1;
            foreach ($this->breadcrumbs as $label => $url) {
                if (is_string($label) && is_array($url)) {
                    echo str_repeat("\t", $index - 1) . str_replace('{child}', ($index > 1 ? 'itemprop="child"' : ''), $itemTemplate);
                    echo str_repeat("\t", $index) . str_replace('{url}', $hostInfo . CHtml::normalizeUrl($url), $urlTemplate);
                    echo str_repeat("\t", $index) . str_replace('{title}', CHtml::encode($label), $titleTemplate);
                }
                $index++;
            }
            echo str_repeat("</div>\n", $index - 1);
        }
        echo "</div>\n";
    }

}
