<?php
defined('_JEXEC') or die;

Designer::load("Designer_Content_Item");

class DesignerContentSingleArticle extends DesignerContentItem
{
    public $print;

    public $toc;

    public $intro;

    public $text;

    public $tags = '';

    public function __construct($component, $componentParams, $article, $articleParams, $properties)
    {
        parent::__construct($component, $componentParams, $article, $articleParams);
        $this->print = isset($properties['print']) ? $properties['print'] : '';
        $this->pageHeading = $this->_componentParams->get('show_page_heading', 1)
                               ? $this->_componentParams->get('page_heading') : '';
        $this->titleLink = $this->_articleParams->get('link_titles') && !empty($this->_article->readmore_link)
                             ? $this->_article->readmore_link : '';
        $this->emailIconVisible = $this->emailIconVisible && !$this->print;
        $this->editIconVisible = $this->editIconVisible && !$this->print;
        $this->categoryLink = $this->_articleParams->get('link_category') && $this->_article->catslug
                                ? JRoute::_(ContentHelperRoute::getCategoryRoute($this->_article->catslug))
                                : '';
        $this->category = $this->_articleParams->get('show_category') ? $this->_article->category_title : '';
        $this->categoryLink = $this->_articleParams->get('link_category') && $this->_article->catslug
                                ? JRoute::_(ContentHelperRoute::getCategoryRoute($this->_article->catslug))
                                : '';
        $this->parentCategory = $this->_articleParams->get('show_parent_category') && $this->_article->parent_slug != '1:root'
                                  ? $this->_article->parent_title : '';
        $this->parentCategoryLink = $this->_articleParams->get('link_parent_category') && $this->_article->parent_slug
                                      ? JRoute::_(ContentHelperRoute::getCategoryRoute($this->_article->parent_slug))
                                      : '';
        $this->author = $this->_articleParams->get('show_author') && !empty($this->_article->author)
                          ? ($this->_article->created_by_alias ? $this->_article->created_by_alias : $this->_article->author)
                          : '';
        if (strlen($this->author) && $this->_articleParams->get('link_author') && !empty($this->_article->contactid)) {
            $needle = 'index.php?option=com_contact&view=contact&id=' . $this->_article->contactid;
            $menu = JFactory::getApplication()->getMenu();
            $item = $menu->getItems('link', $needle, true);
            $this->authorLink = !empty($item) ? $needle . '&Itemid=' . $item->id : $needle;
        } else
            $this->authorLink = '';

        if (version_compare(JVERSION, '3.1.0') >= 0) {
            if ($this->_articleParams->get('show_tags', 1) && !empty($this->_article->tags)) {
                $this->_article->tagLayout = new JLayoutFile('joomla.content.tags');
                $this->tags = $this->_article->tagLayout->render($this->_article->tags->itemTags);
            }
        }

        $this->toc = isset($this->_article->toc) ? $this->_article->toc : '';
        $this->text = $this->proccessingContent($this->_articleParams->get('access-view') ? $this->_article->text : '');
        $user = JFactory::getUser();
        $this->introVisible = !$this->_articleParams->get('access-view') && $params->get('show_noauth') && $user->get('guest');
        $this->intro = $this->_article->introtext;
        if (!$this->_articleParams->get('access-view') && $this->_articleParams->get('show_noauth') && $user->get('guest')
            && $this->_articleParams->get('show_readmore') && $this->_article->fulltext != null)
        {
            $attribs = json_decode($this->_article->attribs);
            if ($attribs->alternative_readmore == null)
                $this->readmore = JText::_('COM_CONTENT_REGISTER_TO_READ_MORE');
            elseif ($this->readmore = $this->_article->alternative_readmore) {
                if ($this->_articleParams->get('show_readmore_title', 0) != 0)
                    $this->readmore .= JHtml::_('string.truncate', ($this->_article->title), $this->_articleParams->get('readmore_limit'));
            } elseif ($this->_articleParams->get('show_readmore_title', 0) == 0)
                $this->readmore = JText::sprintf('COM_CONTENT_READ_MORE_TITLE');
            else
                $this->readmore = JText::_('COM_CONTENT_READ_MORE')
                                    . JHtml::_('string.truncate', $this->_article->title,
                                               $this->_articleParams->get('readmore_limit'));
            $link = new JURI(JRoute::_('index.php?option=com_users&view=login'));
            $this->readmoreLink = $link->__toString();
        } else {
            $this->readmore = '';
            $this->readmoreLink = '';
        }
        $this->paginationPosition = (isset($this->_article->pagination) && $this->_article->pagination && isset($this->_article->paginationposition))
            ? (($this->_article->paginationposition ? 'below' : 'above') . ' ' . ($this->_article->paginationrelative ? 'full article' : 'text'))
            : '';
        $this->showLinks = isset($this->_article->urls) && is_string($this->_article->urls) && !empty($this->_article->urls);
    }

    public function printIconInfo()
    {
        $info = array();
        $content = JHTML::_($this->print ? 'icon.print_screen' : 'icon.print_popup', $this->_article, $this->_articleParams);
        if ($this->showIcons) {
            $info['content'] = preg_replace('/<img[^>]+\/>/', '[[icon]]', $content);
            $info['showIcon'] = true;
        } else {
            $info['content'] = preg_replace('/(<a[^>]+>)[\s\S]+(<\/a>)/', '$1' . JText::_('JGLOBAL_PRINT') . '$2', $content);
            $info['showIcon'] = false;
        }
        return $info;
    }

    public function toc($toc)
    {
        return $toc;
    }

    public function intro($intro)
    {
        return $intro;
    }

    public function text($text)
    {
        return $text;
    }

    public function pagination() {
        return $this->_article->pagination;
    }

    private function buildTabs($matches) {
        preg_match_all('|<dt\b[^>]*>[\s\S]*?<span><h3><a\b[^>]*>(.*?)</a></h3></span>[\s\S]*?</dt>|', $matches[0], $tabsMatches);
        preg_match_all('|<dd class=\\"tabs\\">([\s\S]*?)</dd>|', $matches[0], $contentsMatches);

        $tabs = $tabsMatches[1];
        $contents = $contentsMatches[1];
        $tabsCount = count($tabs);
        $tabsItems = array();

        for ($i = 0; $i < $tabsCount; $i++) {
            $item = array('id'        => $this->buildSlug($tabs[$i]),
                          'caption'   => $tabs[$i],
                          'content'   => $contents[$i]);
            $tabsItems[$i] = $item;
        }

        return funcBuildTabs($tabsItems);
    }

    public function proccessingContent($content) {
        return preg_replace_callback('|<dl\b[^>]*>[\s\S]*</dl>|', array( &$this, 'buildTabs'), $content);
    }


    public function buildSlug($str) {
        $str = strtolower(trim($str));
        $str = preg_replace('/[^a-z0-9-]/', '-', $str);
        $str = preg_replace('/-+/', "-", $str);
        return $str;
    }
}