Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sky/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sky/Search.php

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

namespace TechFry\Library\View\Sky;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use TechFry\Library\TDb;

class Search extends Bootstrap
{
  	public function display()
    {
		// search_type: text, list, checkbox (boolean), datefrom, dateto, daterange
        
        $searches = $this->data;
        
        if (empty($searches))
        {
         	return;
        }
        
        if ($this->framework == 'amp')
		{
			return;
		}
		
		$input = Factory::getApplication()->input;
		
		$output = '<form class="mb-2" method="post" role="search">';
        $output .= '<div class="row row-cols-1 row-cols-sm-2 row-cols-md-4 g-2">';
		
		foreach ($searches as $s)
		{
			$options = [];
            $search = []; // name, type, options, attribs, value

			// $s is numeric for joomla custom fields
			if (\is_numeric($s))
			{
                $db = new TDb('fields');
                $cf = $db->get_item(array('id' => $s));
				$search['name'] = $cf->name;

				if ($cf->type == 'list' || $cf->type == 'radio')
				{
					$search['type'] = 'search_list';
                    $search['options'] = json_decode($cf->fieldparams, true)['options'];
				}
                else
                {
                    $search['type'] = 'search_text';
                    $search['attribs']['placeholder'] = $cf->label;
                }
			}
            elseif (\is_array($s))
            {
                $search['type'] = 'search_' . $s['search_type'];
                $search['name'] = $s['column_name'];

                if ($s['search_type'] == 'list')
                {
                    $search['options'] = $s['options'];
                }

                $search['attribs'] = isset($s['attribs']) && $s['attribs'] ? $s['attribs'] : [];
                if (isset($s['placeholder']) && $s['placeholder'])
                {
                    $search['attribs']['placeholder'] = $s['placeholder'];
                }
            }
            else
			{
				$search['type'] = 'search_text';
                $search['name'] = $s;
			}

			$search['value'] = $input->get($search['name'], '', 'RAW');
			
			$output .= '<div class="col">';
                $function = $search['type'];
				$output .= $this->$function($search);
			$output .= '</div>';
		}

		$output .= '<div class="col">';
            $output .= $this->get_submit_button();
        $output .= '</div>';
		
        $output .= '</div>';
		$output .= '</form>';
        
        return $output;
    }

	// 1. Text Field Type Search
  	public function search_text($search)
    {
        $attribs = isset($search['attribs']) && $search['attribs'] ? $search['attribs'] : [];
        $value = $search['value'];
        
        $output = '';

		$attribs['placeholder'] = $attribs['placeholder'] ?? Text::_('COM_TF_SEARCH_' . strtoupper($search['name']));
        $attribs['class'] = 'form-control form-control-sm mb-2';
        $attribs['name'] = $search['name'];
        $attribs['id'] = 'search_' . $search['name'];
      
      	$output .= '<div>';
        	$output .= '<input type="search"';
            $output .= $value ? ' value="' . $value . '"' : '';

            foreach ($attribs as $k => $v) 
            {
                $output .= ' ' . $k . '="' . htmlspecialchars($v) . '"';
            }

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

	// 2. Dropdown Type Search
  	public function search_list($search)
    {
        $options = $search['options'];
        $attribs = isset($search['attribs']) && $search['attribs'] ? $search['attribs'] : [];
        $value = $search['value'];

		// First option
		$placeholder = $attribs['placeholder'] ?? Text::_('JSELECT') . ' ' . ucwords(str_replace('-', ' ', $search['name']));
		
      	$output = '';
      
      	$output .= '<div>';
        	$output .= '<select class="form-select form-select-sm mb-2" name="' . $search['name'] . '" id="search-' . $search['name'] . '"';
            $output .= $value ? ' value="' . $value . '"' : '';
            $output .= '>';
      			$output .= '<option value="">' . $placeholder . '</option>';
      			foreach ($options as $option)
                {
                 	if ($option['value'] != '')
                    {
                    	$selected = ($option['value'] == $value) ? ' selected' : '';
                  		$output .= '<option value="' . $option['value'] . '"' . $selected . '>' . $option['name'] . '</option>';   
                    }
                }
      		$output .= '</select>';
        $output .= '</div>';
      
      	return $output;
    }

    public function search_datefrom($search)
    {
        $attribs = isset($search['attribs']) && $search['attribs'] ? $search['attribs'] : [];
        $value = $search['value'];
        
        $attribs['placeholder'] = $attribs['placeholder'] ?? Text::_('COM_TF_SEARCH_' . strtoupper($search['name']));
        $attribs['class'] = 'form-control form-control-sm mb-2';
        $attribs['name'] = $search['name'];
        $attribs['id'] = 'search_' . $search['name'];

        $output = '';

        $output .= '<div>';
            $output .= '<input type="text"';

            foreach ($attribs as $k => $v) 
            {
                $output .= ' ' . $k . '="' . htmlspecialchars($v) . '"';
            }

            $output .= $value ? ' value="' . $value . '"' : '';

            $output .= ' onfocus="(this.type=\'date\')"';
            $output .= ' onblur="(this.type=\'text\')"';

            $output .= '>';
        $output .= '</div>';

        return $output;
    }

	public function search_dateto($search)
    {
        return $this->search_datefrom($search);
    }

    public function search_daterange($search)
    {
        $names = array(
            'last_week' => 'Last week',
            'last_1month' => 'Last month',
            'last_3month' => 'Last 3 months',
            'last_6month' => 'Last 6 months',
            'last_year' => 'Last year',
            'post_last_year' => 'More than a year ago',
            'next_week' => 'Next week',
            'next_1month' => 'Next month',
            'next_3month' => 'Next 3 months',
            'next_6month' => 'Next 6 Months',
            'next_year' => 'Next year',
            'post_next_year' => 'More than a year ahead',
            'never' => 'Never',
        );
        
        $default_options = 'last_1month,last_6month,last_year,post_last_year,next_1month,next_6month,next_year,post_next_year';

        $search['attribs']['options'] = $search['attribs']['options'] ?? $default_options;

        $arr = explode(',', $search['attribs']['options']);

        foreach ($arr as $a)
        {
            $search['options'][] = array('name' => $names[$a], 'value' => $a);
        }

        $output = $this->search_list($search);

        return $output;
    }

	// Submit Button
	public function get_submit_button()
    {
        $output = '';
		
		$output .= '<button class="btn btn-sm btn-outline-primary">';
			$output .= '<span class="fa-solid fa-search"></span> ' . Text::_('COM_TF_BUTTON_SEARCH');
		$output .= '</button>';

		return $output;
    }
}