Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Back/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Back/TfViewDisplayTrait.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Back;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;

trait TfViewDisplayTrait
{
    // get_binary, get_status, get_link, get_date, get_number, get_small, get_json
    // get_button, get_label, get_alert, get_progress, get_table, get_modal, get_accordion
    // get_joomla_version, get_no_results_msg
    // Tabs: start_tabset, end_tabset, add_tab
    // Lists: get_dl
    
    // 01. Display data in the form of 0 or 1
    public function get_binary($value)
    {
        $color = $value ? 'Green' : 'Red';
        
        $output = '';
        
        $output .= '<span style="color: ' . $color . ';" class="fas fa-';
        $output .= $value ? 'check' : 'times';
        $output .= '" aria-hidden="true"></span>';
        
        return $output;
    }
    
    // 02. Display status icon with color
    public function get_status($value, $row_index, $publish_up = null, $publish_down = null)
    {
        /*
        $tip = '';
        
        switch ($value)
        {
            case 1 :
                $null_date = Factory::getDbo()->getNullDate();
                $now_date  = Factory::getDate();

                $publish_up = ($publish_up !== null && $publish_up !== $null_date) ? Factory::getDate($publish_up) : false;
                $publish_down = ($publish_down !== null && $publish_down !== $null_date) ? Factory::getDate($publish_down) : false;

                if ($publish_up && $now_date < $publish_up)
                {
                    $color = 'Purple';
                    $icon = 'calendar-alt';
                    $tip = '<strong>' . Text::_('JLIB_HTML_PUBLISHED_PENDING_ITEM') . '</strong><br>';
                    $tip .= Text::sprintf('JLIB_HTML_PUBLISHED_START', HTMLHelper::_('date', $publish_up, Text::_('DATE_FORMAT_LC5')));
                }
                elseif ($publish_down && $now_date > $publish_down)
                {
                    $color = 'Orange';
                    $icon = 'clock';
                    $tip = '<strong>' . Text::_('JLIB_HTML_PUBLISHED_EXPIRED_ITEM') . '</strong><br>';
                    $tip .= Text::sprintf('JLIB_HTML_PUBLISHED_FINISHED', HTMLHelper::_('date', $publish_down, Text::_('DATE_FORMAT_LC5')));
                }
                else
                {
                    $color = 'MediumSeaGreen';
                    $icon = 'check';
                }
                break;
                
            case 0 :
                $color = 'Tomato';
                $icon = 'times';
                break;
                
            case 2 :
                $color = 'DodgerBlue';
                $icon = 'archive';
                break;
                
            case -2 :
                $color = 'Gray';
                $icon = 'trash';
        }
        
        $output = '';
        
            $output .= '<span style="color: ' . $color . ';" class="fas fa-' . $icon . '"';
            $output .= ' aria-labelledby="state-' . $row_id . '"';
            $output .= ' aria-hidden="true"></span>';
            if ($tip)
            {
                $output .= '<div id="state-' . $row_id . '" role="tooltip">' . $tip . '</div>';
            }
        */

        $prefix = $this->view_name . '.';
        $enabled = 1;

        $output = HTMLHelper::_('jgrid.published', $value, $row_index, $prefix, $enabled, 'cb', $publish_up, $publish_down);
        
        return $output;
    }
    
    // 03. Display title with link, alias to edit form
    public function get_link($link, $text, $alias = '')
    {
        $title = Text::_('JACTION_EDIT') . ' ' . $this->escape($text);
        
        $output = '';
        
        if (!empty($link))
        {
            $output .= '<strong><a href="' . $link . '" title="' . $title . '">' . $this->escape($text) . '</a></strong>';
        }

        // Alias
        if ($alias)
        {
            $output .= '<div class="small break-word">';
                $output .= Text::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($alias));
            $output .= '</div>';
        }

        return $output;
    }
    
    // 04A. Display date
    public function get_date($date)
    {
        $output = '';
        
        if (!empty($date) && $date != 0)
        {
            $mydate = Factory::getDate($date);
            $output = '<small>' . $mydate->format(Text::_('DATE_FORMAT_LC3')) . '</small>';
        }
        
        return $output;
    }
    
    // 04A. Display date
    public function get_date_relative($date)
    {
        $output = '';
        
        if (!empty($date) && $date != 0)
        {
            $output = HTMLHelper::_('date.relative', $date);
        }
        
        return $output;
    }

    // 05. Display number or count
    public function get_number($number, $style = 'success')
    {
        $output = '';
        
        $output .= '<span class="badge';

        $output .= (!empty((int) $number)) ? ' bg-' . $style : ' bg-secondary';

        $output .= '">' . $number . '</span>';
        
        return $output;
    }
    
    // 06. Display in small like alias
    public function get_small($sm)
    {
        $output = '';
        
        if (!empty($sm))
        {
            $output .= '<br><small>';
                $output .= $sm;
            $output .= '</small>';
        }
        
        return $output;
    }

    // 07. Display JSON Value
    public function get_json($json, $key)
    {
        $array = json_decode($json, true);

        return $array[$key];
    }
    
    // 08. Display action button
    public function get_button($link, $text, $type = 'success', $icon = '')
    {
        $output = '';
        
        $output .= '<a class="btn btn-sm btn-' . $type . '" href="' . Route::_($link) . '">';
        if ($icon)
        {
            $output .= '<span class="' . $icon . '"></span> ';
        }
        $output .= Text::_($text);
        $output .= '</a>';
        
        return $output;
    }
    
    // 09. Display badges or labels
    public function get_label($labels, $class = 'light')
    {
        $output = '';

        if (!$labels)
        {
            return;
        }

        if (\is_array($labels))
        {
            foreach ($labels as $label)
	        {
                $output .= '<span class="badge text-bg-' . $class . '">' . $label . '</span> ';
	        }
        }
        else
        {
            $output .= '<span class="badge text-bg-' . $class . '">' . $labels . '</span> ';
        }
        
        return $output;
    }
    
    // 10. Display alert message
    public function get_alert($message, $type = 'info', $icon = '', $display = true)
    {
        $output = '';
        
        $output .= '<div class="alert alert-' . $type . '">';
            if ($icon)
            {
                $output .= '<span class="' . $icon . '" aria-hidden="true"></span> ';
            }
            $output .= $message;
        $output .= '</div>';

        if ($display)
        {
            echo $output;
        }

        return $output;
    }

    // 11. progress bar
    public function get_progress($progress = 0, $text = '', $type = 'info')
    {
        if (!$text)
        {
            $text = $progress . '%';
        }
        
        echo '<div class="progress mb-3" role="progressbar" aria-label="Progress" aria-valuenow="' . $progress . '" aria-valuemin="0" aria-valuemax="100">';
            
            echo '<div class="progress-bar bg-' . $type . ' progress-bar-striped" style="width: ' . $progress . '%">' . $text . '</div>';

        echo '</div>';
    }

    // 12. Display table
    public function get_table($heads, $rows, $display = true)
    {
        $output = '';
        
        if (!$rows)
        {
            $output .= $this->get_alert(Text::_('JGLOBAL_NO_MATCHING_RESULTS'), 'info', 'icon-info-circle', false);
            
            if ($display)
            {
                echo $output;
            }
            
            return $output;
        }
        
        $output .= '<table class="table table-striped table-hover table-bordered">';
            if ($heads)
            {
                $output .= '<thead><tr>';
                foreach ($heads as $head)
                {
                    $output .= '<th>' . Text::_($head) . '</th>';
                }
                $output .= '</tr></thead>';
            }
            $output .= '<tbody>';
            
                foreach ($rows as $row)
                {
                    $output .= '<tr>';
                    foreach ($row as $td)
                    {
                        $output .= '<td>' . $td . '</td>';
                    }
                    $output .= '</tr>';
                }
    
            $output .= '</tbody>';    
        $output .= '</table>';

        if ($display)
        {
            echo $output;
        }

        return $output;
    }

    public function get_modal_trigger($data = array(), $display = true)
    {
        // $data: text, class, icon, modal_id, custom

        $output = '';

        $button_class = $data['class'] ?? 'btn-sm btn-primary'; 

        $output .= '<button type="button" class="btn ' . $button_class . '" data-bs-toggle="modal" data-bs-target="#modal-' . $data['modal_id'] . '"';
        if (isset($data['custom']) && $data['custom'])
        {
            $output .= ' data-bs-custom="' . $data['custom'] . '"';
        }
        
        $output .= '>';    
            if (isset($data['icon']))
            {
                $output .= '<i class="' . $data['icon'] . '"></i> ';
            }
            $output .= Text::_($data['text']);
        $output .= '</button>';

        if ($display)
        {
            echo $output;
        }

        return $output;
    }

    // 12. Get modal popup
    public function get_modal($data = array(), $display = true)
    {
        // $data: title, url or body, modal_id

        if (!isset($data['modal_id']))
        {
            return;
        }

        $output = '';
   
        $modal_params = array(
            'title' => $data['title'] ?? '',
            //'height' => '100%',
            //'width' => '100%',
            //'bodyHeight' => '70',
            //'modalWidth' => '80',
            'url' => isset($data['url']) && $data['url'] ? $data['url'] : '',
            'backdrop' => 'static',
            //'footer' => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>',
        );

        $body = isset($data['body']) && $data['body'] ? '<div class="p-3">' . $data['body'] . '</div>' : '';

        $output .= HTMLHelper::_('bootstrap.renderModal', 'modal-' . $data['modal_id'], $modal_params, $body);

        if ($display)
        {
            echo $output;
        }

        return $output;
    }

    public function get_accordion($items = array())
    {
        $accordion_id = 'a' . rand(1000, 9999);
        
        echo HTMLHelper::_('bootstrap.startAccordion', $accordion_id);

        foreach ($items as $k => $item)
        {
            $item = array_values($item);

            $slide_id = $accordion_id . 's' . $k;
            
            echo HTMLHelper::_('bootstrap.addSlide', $accordion_id, $item[0], $slide_id);
                if (isset($item[1]))
                {
                    echo $item[1];
                }
            echo HTMLHelper::_('bootstrap.endSlide');
        }

        echo HTMLHelper::_('bootstrap.endAccordion');
    }
    
    // 14. Joomla version - Depreciated: Use TVersion Class
    public function get_joomla_version($type = 'major')
	{
		$version = JVERSION;
		list ($major, $minor, $patch) = explode('.', $version);

		if (strpos($patch, '-') !== false)
		{
			$patch = explode('-', $patch)[0];
		}

		switch ($type)
		{
			case 'minor' :
				return (int) $minor;
				break;
				
			case 'patch' :
				return (int) $patch;
				break;
				
			case 'major' :
			default :
				return (int) $major;
		}
	}
    
    // 15. No results message
    public function get_no_results_msg()
    {
        $output = '<div class="alert alert-info">';
            $output .= '<span class="icon-info-circle" aria-hidden="true"></span> ';
            $output .= '<span class="visually-hidden">' . Text::_('INFO') . '</span>';
            $output .= Text::_('JGLOBAL_NO_MATCHING_RESULTS');
        $output .= '</div>';
        
        return $output;
    }

    // 16A. Start Tabset
    public function start_tabset($config = [])
    {
        // $config - active, orientation (horizontal/vertical), recall
        
        $params = []; 
        $params['recall'] = $config['recall'] ?? true;
        $params['orientation'] = $config['orientation'] ?? 'horizontal';
        $params['active'] = $config['active'] ?? '';
        
        echo '<div class="main-card">';
        echo HTMLHelper::_('uitab.startTabSet', 'myTab', $params);
    }

    // 16B. End Tabset
    public function end_tabset()
    {
        echo HTMLHelper::_('uitab.endTabSet');
        echo '</div>';
    }

    // 16C. Add tab
    public function add_tab($name, $text)
    {
        echo HTMLHelper::_('uitab.addTab', 'myTab', $name, Text::_($text));
            
            echo $this->loadTemplate($name);
            
        echo HTMLHelper::_('uitab.endTab');
    }

    // 17. Definition List
    public function get_dl($definitions = [])
    {
        echo '<dl>';

        foreach ($definitions as $term => $description) 
        {
            $words = explode('_', $term);
            $capitalized = array_map('ucfirst', $words);
            $term = implode(' ', $capitalized);
            
            echo '<dt>' . $term . '</dt>';
            echo '<dd>' . $description . '</dd>';
        }

        echo '</dl>';
    }
}