Your IP : 216.73.216.208


Current Path : /home/azimutno/www/plugins/jsnpagebuilder/defaultelements/articlelist/
Upload File :
Current File : /home/azimutno/www/plugins/jsnpagebuilder/defaultelements/articlelist/articlelist.php

<?php
/**
 * @version    $Id$
 * @package    JSN_PageBuilder
 * @author     JoomlaShine Team <support@joomlashine.com>
 * @copyright  Copyright (C) 2012 JoomlaShine.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.joomlashine.com
 * Technical Support:  Feedback - http://www.joomlashine.com/contact-us/get-support.html
 */

// No direct access to this file.
defined('_JEXEC') || die('Restricted access');

include_once JPATH_ROOT . '/components/com_content/helpers/route.php';
include_once 'helpers/helper.php';

/**
 * Market shortcode element
 *
 * @package  JSN_PageBuilder
 * @since    1.0.0
 */
class JSNPBShortcodeArticleList extends IG_Pb_Element
{
    /**
     * Constructor
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Include admin scripts
     *
     * @return mixed
     */
    public function backend_element_assets()
    {
        JSNPagebuilderHelpersFunctions::print_asset_tag(JSNPB_ELEMENT_URL . '/articlelist/assets/css/articlelist.css', 'css');
        JSNPagebuilderHelpersFunctions::print_asset_tag(JSNPB_ELEMENT_URL . '/articlelist/assets/js/articlelist.js', 'js');
    }

    /**
     * DEFINE configuration information of shortcode
     *
     * @return mixed
     */
    function element_config()
    {
        $this->config['shortcode'] = 'pb_articlelist';
        $this->config['name'] = JText::_('Article List');
        $this->config['cat'] = JText::_('Extra');
        $this->config['icon'] = "icon-article-list";
        $this->config['description'] = JText::_("Article List");

        $this->config['exception'] = array(
            'default_content'  => JText::_('Article List'),
            'data-modal-title' => JText::_('Article List'),
        );
    }

    /**
     * DEFINE setting options of shortcode
     *
     * @return mixed
     */
    function element_items()
    {
        $this->items = array(
            "content" => array(
                array(
                    "name"    => JText::_("Element Title"),
                    "id"      => "el_title",
                    "type"    => "text_field",
                    "class"   => "jsn-input-large-fluid",
                    "std"     => JText::_('Sample Article List PB_INDEX_TRICK'),
                    "role"    => "title",
                    "tooltip" => JText::_("Set title for current element")
                ),
                array(
                    "name"    => JText::_("Title"),
                    "id"      => "articlelist_title",
                    "type"    => "text_field",
                    "class"   => "jsn-input-xxlarge-fluid",
                    "std"     => JText::_('Article List'),
                ),
            	array(
            		"name"    => JText::_("Show Title"),
            		"id"      => "articlelist_show_title",
            		"type"    => "radio",
            		"std"     => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
            		"options" => JSNPagebuilderHelpersType::getYesNoQuestion(),
            		),            		
                array(
                    "name"    => JText::_("Content source"),
                    "id"      => "articlelist_source",
                    "type"    => "select",
                    "std"     => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListSource()),
                    "options" => JSNPbArticleListHelper::getArticleListSource(),
                ),
                array(
                    "name"    => JText::_("Filter by category"),
                    "id"      => "articlelist_filter",
                    "type"    => "select",
                    "class"   => "jsn-input-large-fluid",
                    "std"     => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListCategories()),
                    "options" => JSNPbArticleListHelper::getArticleListCategories(),
                ),
                array(
                    "name"    => JText::_("Sort by"),
                    "id"      => "articlelist_sort_by",
                    "type"    => "select",
                    "std"     => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListSortBy()),
                    "options" => JSNPbArticleListHelper::getArticleListSortBy(),
                ),
                array(
                    "name"    => JText::_("Sort order"),
                    "id"      => "articlelist_sort_order",
                    "type"    => "select",
                    "std"     => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListSortOrder()),
                    "options" => JSNPbArticleListHelper::getArticleListSortOrder(),
                ),
                array(
                    "name" => JText::_("Number of displayed items"),
                    "id"   => "articlelist_amount",
                    "type" => "text_number",
                    "std"  => 5,
                ),
                array(
                    "name"    => JText::_("Show 'See More'"),
                    "id"      => "articlelist_see_more",
                    "type"    => "radio",
                    "std"     => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    "options" => JSNPagebuilderHelpersType::getYesNoQuestion(),
                ),
            ),
            'styling' => array(
                array(
                    'type' => 'preview',
                ),
                array(
                    "name"       => JText::_('Choose Layout'),
                    "id"         => 'articlelist_layout',
                    "type"       => 'radio',
                    "label_type" => 'image',
                    "dimension"  => array(40, 40),
                    "std"        => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListLayout()),
                    "options"    => JSNPbArticleListHelper::getArticleListLayout(),
                    "has_depend" => '1'
                ),
                // First item
                array(
                    "name"            => JText::_('First item'),
                    "container_class" => "articlelist-style-label",
                    "type"            => "field_set_label",
                    "dependency"      => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                array(
                    'name'       => JText::_('Show description'),
                    'id'         => 'articlelist_show_first_description',
                    'type'       => 'radio',
                    'std'        => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    'options'    => JSNPagebuilderHelpersType::getYesNoQuestion(),
                    "dependency" => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                array(
                    'name'       => JText::_('Description character limits'),
                    'id'         => 'articlelist_first_description_limit',
                    'type'       => 'text_number',
                    'std'        => 300,
                    "dependency" => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                array(
                    'name'       => JText::_('Show thumbnail'),
                    'id'         => 'articlelist_show_first_thumbnail',
                    'type'       => 'radio',
                    'std'        => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    'options'    => JSNPagebuilderHelpersType::getYesNoQuestion(),
                    'dependency' => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                array(
                    'name'            => JText::_('Thumbnail dimension'),
                    'id'              => 'articlelist_first_thumbnail_dimension',
                    'container_class' => 'combo-group',
                    'type'            => 'dimension',
                    'dependency'      => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                    'extended_ids'    => array('articlelist_first_thumbnail_dimension_width', 'articlelist_first_thumbnail_dimension_height'),
                    'articlelist_first_thumbnail_dimension_width'  => array('std' => '320'),
                    'articlelist_first_thumbnail_dimension_height' => array('std' => '180'),
                ),
                array(
                    "name"       => JText::_('Show Meta Data'),
                    "id"         => 'articlelist_show_first_meta_data',
                    "type"       => "checkbox",
                    "class"      => "checkbox inline",
                    'std'        => JSNPbArticleListHelper::getArticleDefaultMetaDataType(),
                    'options'    => JSNPbArticleListHelper::getArticleMetaDataType(),
                    'dependency' => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                array(
                    'name'       => JText::_("Show 'Read More'"),
                    'id'         => 'articlelist_show_read_more_first',
                    'type'       => 'radio',
                    'std'        => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    'options'    => JSNPagebuilderHelpersType::getYesNoQuestion(),
                    'dependency' => array('articlelist_layout', '!=', JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT),
                ),
                // List item
                array(
                    "name"            => JText::_('Item list'),
                    "container_class" => "articlelist-style-label",
                    "type"            => "field_set_label",
                ),
                array(
                    "name"       => JText::_('List style'),
                    "id"         => "articlelist_list_style",
                    "type"       => "select",
                    "std"        => JSNPagebuilderHelpersType::getFirstOption(JSNPbArticleListHelper::getArticleListStyle()),
                    "options"    => JSNPbArticleListHelper::getArticleListStyle(),
                    "has_depend" => '1',
                ),
                array(
                    'name'            => JText::_('Thumbnail dimension'),
                    'id'              => 'articlelist_thumbnail_dimension',
                    'container_class' => 'combo-group',
                    'type'            => 'dimension',
                    'dependency'      => array('articlelist_list_style', '=', JSNPbArticleListHelper::PB_ARTICLE_LIST_STYLE_THUMBNAIL),
                    'extended_ids'    => array('articlelist_thumbnail_dimension_width', 'articlelist_thumbnail_dimension_height'),
                    'articlelist_thumbnail_dimension_width'  => array('std' => '120'),
                    'articlelist_thumbnail_dimension_height' => array('std' => '90'),
                ),
                array(
                    "name"       => JText::_('Update image'),
                    "id"         => "articlelist_list_image",
                    "type"       => "select_media",
                    'std'        => '',
                    'class'      => 'jsn-input-large-fluid',
                    "dependency" => array('articlelist_list_style', '=', JSNPbArticleListHelper::PB_ARTICLE_LIST_STYLE_IMAGE),
                ),
                array(
                    'name'       => JText::_('Show description'),
                    'id'         => 'articlelist_show_description',
                    'type'       => 'radio',
                    'std'        => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    'options'    => JSNPagebuilderHelpersType::getYesNoQuestion(),
                    "has_depend" => '1'
                ),
                array(
                    'name'       => JText::_('Description character limits'),
                    'id'         => 'articlelist_description_limit',
                    'type'       => 'text_number',
                    'std'        => 100,
                    "dependency" => array('articlelist_show_description', '=', JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES),
                ),
                array(
                    "name"    => JText::_('Show Meta Data'),
                    "id"      => 'articlelist_show_meta_data',
                    "type"    => "checkbox",
                    'std'     => JSNPbArticleListHelper::getArticleDefaultMetaDataType(),
                    'options' => JSNPbArticleListHelper::getArticleMetaDataType(),
                    "class"   => "checkbox inline",
                ),
                array(
                    'name'    => JText::_("Show 'Read More'"),
                    'id'      => 'articlelist_show_read_more',
                    'type'    => 'radio',
                    'std'     => JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES,
                    'options' => JSNPagebuilderHelpersType::getYesNoQuestion(),
                ),
            ),
        );
    }

    /**
     * DEFINE shortcode content
     *
     * @param array $attributes
     * @param mixed $content
     *
     * @return string
     */
    function element_shortcode($attributes = null, $content = null)
    {
        $document = JFactory::getDocument();
        $document->addStyleSheet(JSNPB_ELEMENT_URL . '/articlelist/assets/css/articlelist.css', 'text/css');

        $arr_params = JSNPagebuilderHelpersShortcode::shortcodeAtts($this->config['params'], $attributes);
        extract($arr_params);
        $html_element = '';
        $articles = JSNPbArticleListHelper::getArticles($arr_params);
        if (count($articles) == 0) {
            $html_element = "<div class='alert alert-warning' style='width: 100%;'>" . JText::_('This category has no article yet') . "</div>";
            return $this->element_wrapper($html_element, $arr_params);
        }
        $app = JFactory::getApplication();
        $isAdmin = $app->isAdmin() ? true : false;
        $adminPreviewClass =  $isAdmin ? "in-admin" : "";
        $url_pattern = '/^(http|https)/';

        $html_element .= "<div class='pb-articlelist-wrapper " . str_replace('_', '-', $arr_params['articlelist_layout']) . " " . $adminPreviewClass . "'>";
        $urlTitle = $isAdmin ? "#" : JRoute::_(ContentHelperRoute::getCategoryRoute($attributes['articlelist_filter']));
        
        if ($arr_params['articlelist_show_title'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES)
        {	
        	$html_element .= "<div class='pb-articlelist-title'><h3 class='pb-articlelist-head'><a href='{$urlTitle}'>{$arr_params['articlelist_title']}</a></h3></div>";
        }
        

        $html_element .= "<div class='row'>";
        if ($arr_params['articlelist_layout'] != JSNPbArticleListHelper::PB_ARTICLE_LIST_LAYOUT) {
            if ($arr_params['articlelist_layout'] == JSNPbArticleListHelper::PB_ARTICLE_HORIZONTAL_LAYOUT) {
                $html_element .= "<div class='col-md-6 col-sm-6 col-xs-6 pb-articlelist-first'>";
            } else if ($arr_params['articlelist_layout'] == JSNPbArticleListHelper::PB_ARTICLE_VERTICAL_LAYOUT) {
                $html_element .= "<div class='col-md-12 col-sm-12 col-xs-12 pb-articlelist-first'>";
            } else {
                $html_element .= "<div class='pb-articlelist-first'>";
            }
            // Display with first item
            $articleFirst = array_shift($articles);
            // Default thumbnail dimension
            $thumbnailFirstWidth = ((int) $arr_params['articlelist_first_thumbnail_dimension_width'] > 0) ? (int) $arr_params['articlelist_first_thumbnail_dimension_width'] : 320;
            $thumbnailFirstHeight = ((int) $arr_params['articlelist_first_thumbnail_dimension_height'] > 0) ? (int) $arr_params['articlelist_first_thumbnail_dimension_height'] : 180;
            $thumbnailFirstWidth .= "px";
            $thumbnailFirstHeight .= "px";
            $firstMetaData = explode("__#__", $arr_params['articlelist_show_first_meta_data']);
            $url = $isAdmin ? "#" : JRoute::_(ContentHelperRoute::getArticleRoute($articleFirst['id'], $articleFirst['catid']));

            if ($arr_params['articlelist_show_first_thumbnail'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES) {
               
                $articleFirstImages = json_decode($articleFirst['images'], true);

                $imageFirstUrl = '';
                $imageFirstAlt = "No image";
                if (isset($articleFirstImages['image_intro']) && $articleFirstImages['image_intro'] != "") {
                    $imageFirstUrl = $articleFirstImages['image_intro'];
                    $imageFirstAlt = $articleFirstImages['image_intro_alt'];
                } else if (isset($articleFirstImages['image_fulltext']) && $articleFirstImages['image_fulltext'] != "") {
                    $imageFirstUrl = $articleFirstImages['image_fulltext'];
                    $imageFirstAlt = $articleFirstImages['image_fulltext_alt'];
                }
				if ($imageFirstUrl != '')
				{
                    preg_match($url_pattern, $imageFirstUrl, $_f);
                    if (!count($_f)) {
                        $imageFirstUrl = JUri::root() . $imageFirstUrl;
                    }
					$html_element .= "<div class='first-thumbnail'>";
					$html_element .= "<a href='{$url}'><img src='{$imageFirstUrl}' alt='{$imageFirstAlt}' style='height: {$thumbnailFirstHeight}; width:{$thumbnailFirstWidth};' /></a>";
					$html_element .= "</div>";
				}
                
            }
            $html_element .= "<div class='title'><a class='title' href='{$url}'>{$articleFirst['title']}</a></div>";
            if (count($firstMetaData) > 1) {
                $html_element .= "<div class='meta-data-wrapper muted'>";
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_AUTHOR, $firstMetaData)) {
                    $html_element .= "<div class='created-by'>" . JText::_('Written by') . ": <span>{$articleFirst['author']}</span></div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_DATE_PUBLISHED, $firstMetaData)) {
                    $_publishData = new DateTime($articleFirst['publish_up']);
                    $html_element .= "<div class='published'>" . JText::_('Published') . ": " . $_publishData->format('d M Y') . "</div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_CATEGORY, $firstMetaData)) {
                    $urlCat = $isAdmin ? "#" : JRoute::_(ContentHelperRoute::getCategoryRoute($articleFirst['catid']));
                    $html_element .= "<div class='category'>" . JText::_('Category') . ": <a href='{$urlCat}'>{$articleFirst['category_title']}</a></div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_HITS, $firstMetaData)) {
                    $html_element .= "<div class='hits'>" . JText::_('Hits') . ": {$articleFirst['hits']}</div>";
                }
                $html_element .= "</div>";
            }

            if ($attributes['articlelist_show_first_description'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES) {
                preg_match_all(JSNPbArticleListHelper::PB_SHORTCODE_SYNTAX, $articleFirst['introtext'], $out);
                if (!count($out[0])) {
                    $html_element .= "<div class='articlelist-first-thumbnail'><p>" . JSNPbArticleListHelper::wordLimiter($articleFirst['introtext'], (int) $attributes['articlelist_first_description_limit']) . "</p></div>";
                }
            }
            $html_element .= "</div>";
        }
        if ($arr_params['articlelist_layout'] == JSNPbArticleListHelper::PB_ARTICLE_HORIZONTAL_LAYOUT) {
            $html_element .= "<div class='col-md-6 col-sm-6 col-xs-6 pb-articlelist-list'>";
        } else {
            $html_element .= "<div class='col-md-12 col-sm-12 col-xs-12 pb-articlelist-list'>";
        } 
        // Display list

        if ($isOrderList = in_array($arr_params['articlelist_list_style'], JSNPbArticleListHelper::getArticleOlListStyle())) {
            $html_element .= "<ol type='{$arr_params['articlelist_list_style']}'>";
        } else {
            switch ($arr_params['articlelist_list_style']) {
                case JSNPbArticleListHelper::PB_ARTICLE_LIST_STYLE_IMAGE :
                    if (getimagesize(JUri::root() . $attributes['articlelist_list_image']) !== false) {
                        $styleUl = "list-style-image: url(\"" . JUri::root() . $attributes['articlelist_list_image'] . "\")";
                    } else {
                        $styleUl = "list-style-image: url(\"" . JUri::root() . "plugins/jsnpagebuilder/defaultelements/articlelist/assets/img/10x10.png" . "\")";
                    }
                    break;
                case JSNPbArticleListHelper::PB_ARTICLE_LIST_STYLE_THUMBNAIL :
                    $styleUl = "list-style-type: none";
                    break;
                default :
                    $styleUl = "list-style-type:{$arr_params['articlelist_list_style']}";
                    break;
            }
            $html_element .= "<ul class='list-style-type-{$arr_params['articlelist_list_style']}' style='{$styleUl}'>";
        }

        // Default thumbnail dimension
        $thumbnailWidth = ((int) $arr_params['articlelist_thumbnail_dimension_width'] > 0) ? (int) $arr_params['articlelist_thumbnail_dimension_width'] : 40;
        $thumbnailHeight = ((int) $arr_params['articlelist_thumbnail_dimension_height'] > 0) ? (int) $arr_params['articlelist_thumbnail_dimension_height'] : 40;
        $thumbnailWidth .= "px";
        $thumbnailHeight .= "px";

        $metaData = explode("__#__", $arr_params['articlelist_show_meta_data']);

        foreach ($articles as $_article) {
            $url = $isAdmin ? "#" : JRoute::_(ContentHelperRoute::getArticleRoute($_article['id'], $_article['catid']));
            $html_element .= "<li class='articlelist-item'>";
            if ($arr_params['articlelist_list_style'] == JSNPbArticleListHelper::PB_ARTICLE_LIST_STYLE_THUMBNAIL) {
                $articleImages = json_decode($_article['images'], true);

                //$bgImageItemUrl = JUri::root() . "plugins/jsnpagebuilder/defaultelements/articlelist/assets/img/120x90.png";
                $bgImageItemUrl = "";
                if (isset($articleImages['image_intro']) && $articleImages['image_intro'] != "") {
                    $bgImageItemUrl = $articleImages['image_intro'];
                } else if (isset($articleImages['image_fulltext']) && $articleImages['image_fulltext'] != "") {
                    $bgImageItemUrl = $articleImages['image_fulltext'];
                }
                if ($bgImageItemUrl != '')
                {
                    preg_match($url_pattern, $bgImageItemUrl, $_n);
                    if (!count($_n)) {
                        $bgImageItemUrl = JUri::root() . $bgImageItemUrl;
                    }
                	$html_element .= "<div class='articlelist-item-thumbnail'><a href='{$url}'><img src='{$bgImageItemUrl}' style='height:{$thumbnailHeight}; width:$thumbnailWidth;'></a></div>";
                }
            }

            $html_element .= "<div class='articlelist-item-content'>";
            $html_element .= "<div class='title'><a class='title' href='{$url}'>{$_article['title']}</a></div>";

            if (count($metaData) > 1) {
                $html_element .= "<div class='meta-data-wrapper muted'>";
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_AUTHOR, $metaData)) {
                    $html_element .= "<div class='created-by'>" . JText::_('Written by') . ": <span>{$_article['author']}</span></div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_DATE_PUBLISHED, $metaData)) {
                    $_publishData = new DateTime($_article['publish_up']);
                    $html_element .= "<div class='published'>" . JText::_('Published') . ": " . $_publishData->format('d M Y') . "</div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_CATEGORY, $metaData)) {
                    $urlCat = $isAdmin ? "#" : JRoute::_(ContentHelperRoute::getCategoryRoute($_article['catid']));
                    $html_element .= "<div class='category'>" . JText::_('Category') . ": <a href='{$urlCat}'>{$_article['category_title']}</a></div>";
                }
                if (in_array(JSNPbArticleListHelper::PB_ARTICLE_META_DATA_HITS, $metaData)) {
                    $html_element .= "<div class='hits'>" . JText::_('Hits') . ": {$_article['hits']}</div>";
                }
                $html_element .= "</div>";
            }

            if ($arr_params['articlelist_show_read_more'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES) {
                $html_element .= "<div class=''><a href='{$url}'>" . JText::_('Read more') . "</a></div>";
            }

            if ($attributes['articlelist_show_description'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES) {
                preg_match_all(JSNPbArticleListHelper::PB_SHORTCODE_SYNTAX, $_article['introtext'], $out);
                if (!count($out[0])) {
                    $html_element .= "<div class='articlelist-item-description'><p>" . JSNPbArticleListHelper::wordLimiter($_article['introtext'], (int) $attributes['articlelist_description_limit']) . "</p></div>";
                }
            }

            $html_element .= "</div></li>";
        }
        $html_element .= $isOrderList ? "</ol>" : "</ul>";
        $html_element .= "</div></div>";
        if ($arr_params['articlelist_see_more'] == JSNPagebuilderHelpersType::PB_HELPER_ANSWER_YES) {
            $html_element .= "<div class='see-more'><a href='{$urlTitle}'>" . JText::_('See more') . "</a></div>";
        }
        $html_element .= "</div>";

        return $this->element_wrapper($html_element, $arr_params);
    }
}