<?php

/**
 * @package SP Page Builder
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2017 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */
//no direct accees
defined('_JEXEC') or die('restricted aceess');

class SppagebuilderAddonSocial_media extends SppagebuilderAddons {

    public function render() {
        // Addon Options
        $class = isset($this->addon->settings->class) ? $this->addon->settings->class : '';
        $social_items = isset($this->addon->settings->sp_social_media_items) ? $this->addon->settings->sp_social_media_items : [];
    
        $output = '';
        $output .= '<div class="sppb-addon sppb-addon-social-media ' . htmlspecialchars($class) . '">';
        $output .= '<div class="social-media-text-wrap">';
    
        foreach ($social_items as $social_item) {
            //updated code.
            // Initialize variables with default values
            $url = isset($social_item->url) ? htmlspecialchars($social_item->url) : '';
            $itemClass = isset($social_item->class) ? htmlspecialchars($social_item->class) : '';
            $faicon = isset($social_item->faicon) ? htmlspecialchars($social_item->faicon) : '';
    
            if ($url) {
                $output .= '<a href="' . $url . '" target="_blank" data-toggle="tooltip" data-placement="top" title="' . $itemClass . '" class="' . $itemClass . '">';
            }
    
            $output .= '<i class="fa-brands ' . $faicon . '"></i>'; /* class name updated. */
    
            if ($url) {
                $output .= '</a>';
            }
        }
    
        $output .= '</div>';
        $output .= '</div>';
    
        return $output;
    }

    public static function getTemplate() {
        $output = '
                <#
                    var contentClass = (data.class !== "undefined") ? data.class : "";
                    var social_items = (data.sp_social_media_items !== "undefined") ? data.sp_social_media_items : "";
                #>
                <div class="sppb-addon sppb-addon-social-media {{data.class}}">
                <div class="social-media-text-wrap">

                <# _.each (social_items, function (social_item) { #>
                   <# if (social_item.url) { #>
                        <a href=\'{{social_item.url}}\' target="_blank" data-toggle="tooltip" data-placement="top" title="{{social_item.class}}" class="{{social_item.class}}">
                    <# } #>
                    <i class="fa {{social_item.faicon}}"></i>
                    <# if (social_item.url) { #>
                      </a>
                    <# } #>
                <#})#>

                </div>
                </div>
                ';
        return $output;
    }

}
