Your IP : 216.73.216.170


Current Path : /var/www/iplanru/data/www/i-plan.ru/components/com_sef/sef_ext/
Upload File :
Current File : /var/www/iplanru/data/www/i-plan.ru/components/com_sef/sef_ext/com_zoo.php

<?php
/**
 * Zoo SEF extension for ARTIO JoomSEF
 * 
 * @package   JoomSEF
 * @author    ARTIO s.r.o., http://www.artio.net
 * @copyright Copyright (C) 2013 ARTIO s.r.o. 
 * @license   GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access.');

jimport('joomla.registry.registry');

class SefExt_com_zoo extends SefExt
{
    var $params;
    var $origmetadesc='';
    var $origmetakey='';
    
    public function getNonSefVars(&$uri)
    {
        $this->_createNonSefVars($uri);

        return array($this->nonSefVars, $this->ignoreVars);
    }

    protected function _createNonSefVars(&$uri)
    {
        if (!isset($this->nonSefVars) && !isset($this->ignoreVars)) {
            $this->nonSefVars = array();
            $this->ignoreVars = array();
        }

        if (!is_null($uri->getVar('limit'))) {
            $this->nonSefVars['limit'] = $uri->getVar('limit');
        }
        if (!is_null($uri->getVar('limitstart'))) {
            $this->nonSefVars['limitstart'] = $uri->getVar('limitstart');
        }
        if (!is_null($uri->getVar('submission_hash'))) {
            $this->nonSefVars['submission_hash'] = $uri->getVar('submission_hash');
        }
        
        if (!is_null($uri->getVar('page')) && ($this->params->get('nonsef_page', '0') == '1')) {
            $this->nonSefVars['page'] = $uri->getVar('page');
        }
    }

    /**
     * Gets (nested) categories array.
     * 
     * @param    int     category id
     * @param    int     if get parent categories too (1=no, 2=yes)
     * @param    bool    if NOT append record id (if is set in config)
     */
    function getCategoryTitle($id,$cats=2,$nice=false) {
            
        //$this->params->get('categoryid_inc', '0') != '0')
        
        $categories = array();
        
        $field = 'name';
        if(SEFTools::UseAlias($this->params, 'category_part') AND !$nice)
            $field = 'alias';

        while ($id > 0) {
            $database =& JFactory::getDBO();
            $database->setQuery("SELECT id, $field AS title, parent FROM #__zoo_category WHERE id =".$id);
            $rows = $database->loadRow();
            $name = (($this->params->get('categoryid_inc', '0') != '0' AND !$nice)? $id.'-' : '').$rows[1];
            array_unshift($categories, $name);
            $id = $rows[2];
            if ($cats == '1')
                break; //  Only last cat
        }
        
        return $categories;
    }
    
    /**
     * Gets item title array (with (nested) categories)
     * 
     * @param    int        item id
     * @param    bool    if NOT append record id (if is set in config)
     */
    function getItemTitle($id, $nice=false) {

        $category = array();
        $field = 'i.name';
        if(SEFTools::UseAlias($this->params, 'item_part') AND !$nice) {
            $field = 'i.alias';
        }
        $database =& JFactory::getDBO();
        $database->setQuery ( "SELECT i.id, $field AS title, ci.category_id, i.params, i.elements, i.publish_up FROM #__zoo_item AS i, #__zoo_category_item AS ci WHERE ci.item_id = i.id AND i.id =".$id." ORDER BY ci.category_id DESC");
        $row = $database->loadObject();
        
        // Try to get category ID from params
        $catid = null;
        if (!empty($row->params)) {
            $itemParams = new JRegistry($row->params);
            
            // We need to use array conversion, as JRegistry can't correctly handle keys with dots
            $itemParams = $itemParams->toArray();
            if (isset($itemParams['config.primary_category'])) {
                $catid = $itemParams['config.primary_category'];
            }
        }
        if (is_null($catid) && !empty($row->category_id)) {
            $catid = $row->category_id;
        }
        if(!empty($catid) AND $this->params->get('category_inc', '2')!=0) //append category (if set to)
            $category = $this->getCategoryTitle($catid, $this->params->get('category_inc', '2'), $nice);

        if ($this->params->get('googlenewsnum', '0') == '0') {
            $name = ( ($this->params->get('itemid_inc', '0') != '0' AND !$nice) ? $id.'-' : '' ).$row->title;
            array_push($category, $name);
        }
        else {
            $category = array_merge($category, $this->GoogleNews($row));
        }

        return $category;
    }
    
    /**
     * Creates title for given item using Google News numbering parameters
     */
    function GoogleNews($item)
    {
        $num = '';
        $add = $this->params->get('googlenewsnum', '0');

        if ($add == '1' || $add == '3') {
            // Item ID
            $digits = trim($this->params->get('digits', '3'));
            if (!is_numeric($digits)) {
                $digits = '3';
            }

            $num1 = sprintf('%0'.$digits.'d', $item->id);
        }
        if ($add == '2' || $add == '3') {
            // Publish date
            $time = strtotime($item->publish_up);

            $date = $this->params->get('dateformat', 'ddmm');

            $search = array('dd', 'd', 'mm', 'm', 'yyyy', 'yy');
            $replace = array(
                date('d', $time),
                date('j', $time),
                date('m', $time),
                date('n', $time),
                date('Y', $time),
                date('y', $time));
            $num2 = str_replace($search, $replace, $date);
        }

        if ($add == '1') {
            $num = $num1;
        }
        else if ($add == '2') {
            $num = $num2;
        }
        else if ($add == '3') {
            $sep = $this->params->get('iddatesep', '');
            if ($this->params->get('iddateorder', '0') == '0') {
                $num = $num2.$sep.$num1;
            }
            else {
                $num = $num1.$sep.$num2;
            }
        }

        $title = $item->title;
        if (!empty($num)) {
            $onlyNum = ($this->params->get('googlenewstitle', '1') == '0');

            if ($onlyNum) {
                $title = $num;
            }
            else {
                $sep = $this->params->get('iddatesep', '');
                if (empty($sep)) {
                    $sefConfig =& SEFConfig::getConfig();
                    $sep = $sefConfig->replacement;
                }
    
                $where = $this->params->get('numberpos', '1');
    
                if( $where == '1' ) {
                    $title = $title.$sep.$num;
                } else {
                    $title = $num.$sep.$title;
                }
            }
        }
        
        // Support for slashes
        $title = explode('/', $title);

        return $title;
    }
    
    /**
     * Gets app title.
     * 
     * @param    int        app id
     * @param    bool    if NOT append record id (if is set in config)
     */
    function getApplicationTitle($id,$nice=false) {
        $database =& JFactory::getDBO();
        $database->setQuery ( "SELECT id, name FROM #__zoo_application WHERE id =".$id);
        $rows = $database->loadRow();
        
        $name = ( ($this->params->get('applicationid_inc', '0') != '0' AND !$nice) ? $id.'-' : '' ).$rows[1];
        return $name;
    }

    /**
     * Gets submission title.
     * 
     * @param    int        submission id
     */
    function getSubmissionTitle($id) {
        $database =& JFactory::getDBO();
        $database->setQuery("SELECT id, name, alias FROM #__zoo_submission WHERE id = ".intval($id));
        $row = $database->loadObject();
        if (is_null($row)) {
            return null;
        }
        
        $field = $this->params->get('submission_name_inc', 'name');
        $name = $row->$field;
        
        if ($this->params->get('submission_name_id_inc', '0') == '1') {
            $name = $id.'-'.$name;
        }
        
        return $name;
    }

    /**
     * Writes metadata to inner class variables.
     * 
     * @param    string    type of page
     * @param    id        pk of record
     * @param    object    uri object for frontpage
     */
    function getMetaData ($type, $id=0, & $uri=null) {
        
        $database =& JFactory::getDBO();

        switch ($type) {
             
             case 'item':
                 
                 //get new and old desc field
                 //get title and append cat ids �(if set)
                $database->setQuery ( "SELECT i.id, i.name AS title, ci.category_id,i.params,i.elements,i.application_id FROM #__zoo_item AS i, #__zoo_category_item AS ci WHERE ci.item_id = i.id AND i.id =".(int)$id." ORDER BY ci.category_id DESC");
                $rows = $database->loadAssoc();
                 
                //get meta title
                $this->metatitle = $rows['title'];
                if ($this->params->get('meta_titlecat', '1')>0 AND $rows['category_id']!=0) //add cat to title
                     $this->metatitle.= ' - '.implode(' - ',$this->getCategoryTitle($rows['category_id'],$this->params->get('meta_titlecat', '1'),true));
                if ($this->params->get('meta_titleapp', '0') == '1') //add app to title
                       $this->metatitle.=' - '.$this->getApplicationTitle($rows['application_id'],true);
                     
                //get metakey/desc
                $articleText='';

                if (preg_match_all('/<(?:textarea|text)(?: [^>]*)?>(.*)<\/(?:textarea|text)>/ixUs',$rows['elements'],$matches)>0)
                {
                    foreach ($matches[1] as $match)
                        $articleText.=strip_tags(preg_replace('/(<!\[cdata\[|\]\]>|\n|\r|\t)/isUx','',$match)).' ';
                }
                $this->metadesc = $this->metakeySource = $articleText;

                //get original metadata
                $params = json_decode($rows['params'],true);
                if (isset($params['metadata.description']) AND trim($params['metadata.description'])!='') $this->origmetadesc  = $params['metadata.description'];
                if (isset($params['metadata.keywords']) AND trim($params['metadata.keywords'])!='') $this->origmetakey  = $params['metadata.keywords'];
                
                 break;
             
             case 'category':
                                    
                 //get metakey/desc
                 $database->setQuery ( "SELECT id, name, description, application_id FROM #__zoo_category WHERE id =".(int)$id);
                $rowCat = $database->loadRow();
                $this->metadesc = $rowCat[1].' '.$rowCat[2];
                $this->metakeySource = $rowCat[1].' '.$rowCat[2];
                
                 //get meta title
                 $this->metatitle = implode(' - ',$this->getCategoryTitle($id,2,true));
                if ($this->params->get('meta_titleapp', '0') == '1') //add app to title
                       $this->metatitle.=' - '.$this->getApplicationTitle($rowCat[3],true);
                       
                 break;
                              
             case 'frontpage':
                 
                 //get application id from menu item params
                $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), $uri->getVar('task'), $uri->getVar('Itemid'));

                if ($menuItem && is_object($menuItem->params)){
                    $applicationId = $menuItem->params->get('application');                    
                    if(!empty($applicationId))
                        $this->metatitle = $this->getApplicationTitle($applicationId,true); 
                }

                 break;

        }
            
    }
    
    function _getMenuItemInfo($option, $task, $id = null, $string = null) {
        $db =& JFactory::getDBO();
        $sefConfig =& SEFConfig::getConfig();

        // JF translate extension.
        $jfTranslate = $sefConfig->translateNames ? ', `id`' : '';

		$item->title = JoomSEF::_getCustomMenuTitle($option);
        $item->params = null;

        // Which column to use?
        $column = 'title';
        if ($sefConfig->useAlias) $column = 'alias';

        // first test Itemid
        if (isset($id) && $id != 0) {
            $sql = "SELECT id, `$column` AS `name`, `params`$jfTranslate FROM `#__menu` WHERE `id` = $id AND `published` > 0";
        }        
        elseif (isset($string)) {
            $sql = "SELECT id, `$column`AS `name`, `params` $jfTranslate FROM `#__menu` WHERE `link` = '$string' AND `published` > 0";
        }
        else {
            // Search for direct link to component only
            $sql = "SELECT id, `$column` AS `name`, `params` $jfTranslate FROM `#__menu` WHERE `link` = 'index.php?option=$option' AND `published` > 0";
        }

        $db->setQuery($sql);
        $row = $db->loadObject();

        if (!empty($row)) {
            if (!empty($row->name) && !$item->title) $item->title = $row->name;
            $item->params = new JRegistry($row->params);
        }
        else {
            $item->title = str_replace('com_', '', $option);

            if (!isset($string) && !isset($id)) {
                // Try to extend the search for any link to component
                $sql = "SELECT id, `$column`, `params` AS `name`$jfTranslate FROM `#__menu` WHERE `link` LIKE 'index.php?option=$option%' AND `published` > 0";
                $db->setQuery($sql);
                $row = $db->loadObject();
                if (!empty($row)) {
                    if (!empty($row->name) && !$item->title) $item->title = $row->name;
                    $item->params = new JRegistry($row->params);
                }
            }
        }
        
        return $item;
    }   

    function beforeCreate(&$uri) {
        $this->params = SEFTools::getExtParams('com_zoo');

        // Version 2
        if(!is_null($uri->getVar('task')) && $uri->getVar('task') == 'category') {
            $uri->delVar('task');
            $uri->setVar('view', 'category');
        }
        
        if(!is_null($uri->getVar('task')) && $uri->getVar('task') == 'item') {
            $uri->delVar('task');
            $uri->setVar('view', 'item');
        }
        
        if($uri->getVar('view') == 'item' && !is_null($uri->getVar('category_id')) && !is_null($uri->getVar('item_id'))) {
            $uri->delVar('category_id');
        }
        
        // Load cat id if it is menu item
        if($uri->getVar('view') == 'category' && (is_null($uri->getVar('category_id')) || $uri->getVar('category_id') == 0)) {
            $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), null, $uri->getVar('Itemid'));
            if ($menuItem && is_object($menuItem->params)){
                $id = $menuItem->params->get('category');
                if(!empty($id)) {
                    $uri->setVar('category_id', $id);
                }
            }
        }
    
        // Load item id if it is menu item
        if($uri->getVar('view') == 'item' && (is_null($uri->getVar('item_id')) || $uri->getVar('item_id') == 0)){
            $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), null, $uri->getVar('Itemid'));
            if ($menuItem && is_object($menuItem->params)){
                $id = $menuItem->params->get('item_id');
                if(!empty($id)) {
                    $uri->setVar('item_id', $id);
                }
            }
        }
        
        if(!is_null($uri->getVar('category_id')) && is_null($uri->getVar('view'))) {
            $uri->setVar('view', 'category');
        }

        return;
    }

    function create(&$uri) {
        $vars = $uri->getQuery(true);
        extract($vars);
        $title = array();

        $this->params = SEFTools::getExtParams('com_zoo');
        
        //append menu title (?) 
        $title[] = JoomSEF::_getMenuTitle(@$option, @$task, @$Itemid);
        
        //append application name (if set)
        if($this->params->get('application_inc', '1') == '1') {

            $database =& JFactory::getDBO();
            
            if (!empty($item_id)) //get application id from item
            {
                $database->setQuery("SELECT application_id FROM #__zoo_item WHERE id =".(int)$item_id);
                $applicationTitle = $this->getApplicationTitle($database->loadResult());
            }
            elseif(!empty($category_id)) //get application id from category
            {
                $database->setQuery("SELECT application_id FROM #__zoo_category WHERE id =".(int)$category_id);
                $applicationTitle = $this->getApplicationTitle($database->loadResult());
            }
            elseif (isset($app_id))
                $applicationTitle = $this->getApplicationTitle($app_id);
            else //get application id from menu item params
            {
                $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), $uri->getVar('task'), $uri->getVar('Itemid'));
                if ($menuItem && isset($menuItem->params) && is_object($menuItem->params)){
                    $applicationId = $menuItem->params->get('application');
                    if(!empty($applicationId))
                        $applicationTitle = $this->getApplicationTitle($applicationId);
                        
            } }
            
            if (isset($applicationTitle))
                $title[] = $applicationTitle;
        }    
        
        /*
        if($this->params->get('version_inc', '1') == '1'){
        */
        // Version 2
            if(!empty($item_id)) { //it is item
                $title = array_merge($title, $this->getItemTitle($item_id));
                $this->getMetaData('item',$item_id);
            }
            elseif(!empty($category_id)) {
                $title = array_merge($title, $this->getCategoryTitle($category_id,2));
                $this->getMetaData('category',$category_id);
            }
            
            if(isset($view)) {
                switch($view) {
                    case 'category':
                        if($category_id == 0){
                            $title[] = JText::_('Categories');
                        }
                        unset($view);
                        break;
                    case 'item':
                        unset($view);
                        break;
                    case 'frontpage':

                        $this->getMetaData('frontpage',0,$uri);
                        
                        if($this->params->get('application_inc', '1') == '0') {
                            
                            //get application id from menu item params
                            $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), $uri->getVar('task'), $uri->getVar('Itemid'));
                            if ($menuItem && is_object($menuItem->params)){
                                $applicationId = $menuItem->params->get('application');
                                if(!empty($applicationId))
                                    array_splice($title, 1, 0, $this->getApplicationTitle($applicationId)); //insert application to the second position
                        }}

                        unset($view);
                        break;
                    case 'submission':
                        if ($this->params->get('submission_text_inc', '1') == '1') {
                            if (isset($layout) && ($layout == 'mysubmissions')) {
                                $title[] = JText::_('My submissions');
                            }
                            else {
                                $title[] = JText::_('Submission');
                            }
                        }

                        // Load submission id and type id
                        $submit = true;
                        if (!isset($submission_id) || !isset($type_id)) {
                            $submit = false;
                            $menuItem = $this->_getMenuItemInfo($uri->getVar('option'), null, $uri->getVar('Itemid'));
                            if ($menuItem && is_object($menuItem->params)) {
                                $submission_id = $menuItem->params->get('submission');
                                $type_id = $menuItem->params->get('type');
                            }
                        }
        
                        if (!empty($submission_id) && ($this->params->get('submission_name_inc', 'name') != 'no')) {
                            $title[] = $this->getSubmissionTitle($submission_id);
                        }
                        
                        if (!empty($type_id) && ($this->params->get('submission_type_inc', '1') == '1')) {
                            $title[] = $type_id;
                        }
                        
                        if ($submit) {
                            $title[] = JText::_('Submit');
                        }
                        
                        unset($view);
                        break;
                    default:
                        $title[] = $view;
                        unset($view);
                }
            }
        
            if(isset($task)) {
                switch($task) {
                    case 'category':
                    case 'tag':
                    case 'item':
                    case 'frontpage':
                        $this->getMetaData('frontpage',0,$uri);
                        unset($task);
                        break;
                    case 'alphaindex':
                        /*
                        if(isset($app_id)) {
                            $title[] = $this->getApplicationTitle($app_id);
                        }
                        */
                        if(isset($alpha_char)){
                            $title[] = JText::_('Index');
                            $title[] = $alpha_char;
                        }
                        unset($task);
                        break;
                    case 'save':
                        $title[] = JText::_('Save');
                        unset($task);
                        break;
                    case 'twitterconnect':
                        $title[] = JText::_('Twitter');
                        unset($task);
                        break;
                    case 'feed':
                        if (isset($type)) {
                            $title[] = $type;
                        }
                        unset($task);
                        break;
                    default:
                        $title[] = $task;
                        unset($task);
                }
            }
        
            if (isset($tag)) {
                if ($this->params->get('tag_text_inc', '0') == '1') {
                    $title[] = JText::_('Tag');
                }
                $title[] = $tag;
                unset($tag);
            }
            
            if(isset($controller)){
                $title[] = $controller;
                unset($controller);
            }
            
            if(isset($page) && ($this->params->get('nonsef_page', '0') == '0')) {
                $sefConfig = SEFConfig::getConfig();
                $cnfPageText = $sefConfig->getPageText();
                $title[] = str_replace('%s', $page, $cnfPageText);
                unset($page);
            }
        
        /*
        } else {
            
            // Version 1
            if(!empty($catalog_id)) {
                $title[] = $this->getCatalogTitleOld($catalog_id);
            }
            
            if(!empty($category_id)) {
                $title = array_merge( $title, $this->getCategoryTitleOld($category_id));
            }
            
            if(!empty($item_id)) {
                $title[] = $this->getItemTitleOld($item_id);
            }
            
            if(isset($view)) {
                switch($view) {
                    case 'category':
                        if(isset($alpha_char)){
                            $title[] = JText::_('INDEX');
                            $title[] = $alpha_char;
                        }
                        unset($view);
                        break;
                    case 'item':
                        unset($view);
                        break;
                    case 'element':
                        if(isset($method) && $method=='download')
                            $title[] = JText::_('DOWNLOAD');
                        unset($view);
                        unset($method);
                        unset($format);
                        unset($task);
                        unset($element);
                        break;
                    default:
                        $title[] = $view;
                        unset($view);
                }
            }
            
            if(isset($page)) {
                $segments[] = $page;
                unset($page);
            }
        }
        */

        // Generate meta tags
        $metatags = $this->getMetaTags();
        if (isset($this->metatitle) && ($this->params->get('meta_title', '1') == '1')) {
            $metatags['metatitle'] = $this->metatitle;
        }
        
        $newUri = $uri;
        if (count($title) > 0) {
            $this->_createNonSefVars($uri);
            $newUri = JoomSEF::_sefGetLocation($uri, $title, @$task, @$limit, @$limitstart, @$lang, $this->nonSefVars, null, $metatags);
        }
        
        return $newUri;
    }
}
?>