Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Front/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Front/TfSearchTrait.php

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

namespace TechFry\Library\Model\Front;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use TechFry\Library\TDb;
use TechFry\Library\View\Sky\Search;

// Display search form and handle results based on search
trait TfSearchTrait
{
    public $searches = [];

    // 01. Get Search and Filter Form for Front-end List Views
    public function getSearchForm()
    {
        // Add $this->searches in ListModel - 2D Array: search_type, column_name, placeholder
        // search_type: text, list, checkbox (boolean), datefrom, dateto, daterange
        
        if (empty($this->searches))
        {
            $this->searches = array(
                array('search_type' => 'text', 'column_name' => 'title', 'placeholder' => Text::_('JGLOBAL_TITLE')),
            );
        }

        foreach ($this->searches as $k => $search)
        {
            // $search is numeric for joomla custom field and array for direct database field
            if (\is_numeric($search))
            {
                // Do Nothing
                
            }
            elseif (\is_array($search))
            {
                if ($search['search_type'] == 'list')
                {
                    $db = new TDb($this->table_name);
                    $column = $db->get_column(array('select' => 'DISTINCT ' . $search['column_name']));
                    foreach ($column as $value)
                    {
                        $this->searches[$k]['options'][] = array('value' => $value, 'name' => $value);
                    }
                }

                if (isset($search['attribs']) && $search['attribs'])
                {
                    $arr = explode(';', $search['attribs']);
                    unset($this->searches[$k]['attribs']);

                    foreach ($arr as $a)
                    {
                        $parts = explode(':', $a);
                        $key = $parts[0];

                        $this->searches[$k]['attribs'][$key] = $parts[1];
                    }
                }
            }
        }

        $s = new Search($this->searches);
        $output = $s->display();

        return $output;
    }

    // Return conditions
    public function handle_search()
    {
        if (empty($this->searches))
        {
            $this->searches = array(
                array('search_type' => 'text', 'column_name' => 'title', 'placeholder' => Text::_('JGLOBAL_TITLE')),
            );
        }
        
        $conditions = [];
        
        $input = Factory::getApplication()->input;
        $this->searches = \is_array($this->searches) ? $this->searches : [];

        $search_char = 'a';

        foreach ($this->searches as $search)
        {
            // $search is numeric for joomla custom field and array/string for direct db field
            if (\is_numeric($search))
            {
                $db = new TDb('fields');
                $cf = $db->get_item(array('id' => $search));
                $search_field = $cf->name;

                $search_value = $input->get($search_field, '', 'RAW');
                if ($search_value)
                {
                    $join_as = 'fv' . $search_char++;
                    $this->left_joins[] = array('table' => 'fields_values', 'as' => $join_as, 'on1' => 'a.id', 'on2' => $join_as . '.item_id');
                    
                    $conditions[] = array($join_as . '.field_id', '=', $search);
                    $conditions[] = array($join_as . '.value', 'LIKE', '%' . $search_value . '%');
                }
            }
            elseif (\is_array($search))
            {
                if (strpos($search['column_name'], '.') == true)
                {
                    $arr = explode('.', $search['column_name']);
                    $search_field = $arr[1];
                }
                else
                {
                    $search_field = $search['column_name'];
                }

                $search_value = $input->get($search_field, '', 'RAW');
                if ($search_value)
                {
                    if ($search['search_type'] == 'datefrom')
                    {
                        $conditions[] = array($search['column_name'], '>=', $search_value);
                    }
                    elseif ($search['search_type'] == 'dateto')
                    {
                        $conditions[] = array($search['column_name'], '<=', $search_value);
                    }
                    elseif ($search['search_type'] == 'daterange')
                    {
                        $range = $this->get_date_range($search_value);

                        if ($range['start'])
                        {
                            $conditions[] = array($search['column_name'], '>=', $range['start']);
                        }
                        if ($range['end'])
                        {
                            $conditions[] = array($search['column_name'], '<=', $range['end']);
                        }
                    }
                    else
                    {
                        $conditions[] = array($search['column_name'], 'LIKE', '%' . $search_value . '%');
                    }
                }
            }
            elseif (\is_string($search))
            {
                $search_field = $search;
                $search_value = $input->get($search_field, '', 'RAW');
                if ($search_value)
                {
                    $conditions[] = array($search_field, 'LIKE', '%' . $search_value . '%');
                }
            }
        }

        return $conditions;
    }

    // Function to get date ranges based on filter
    public function get_date_range($filter)
    {
        $start_date = new \DateTime();
        $end_date = new \DateTime();

        switch($filter) 
        {
            case 'last_week':
                $start_date->modify('-7 days');
                break;
            
            case 'last_1month':
                $start_date->modify('-1 month');
                break;
            
            case 'last_3month':
                $start_date>modify('-3 months');
                break;
            
            case 'last_6month':
                $start_date->modify('-6 months');
                break;
            
            case 'last_year':
                $start_date->modify('-1 year');
                break;
            
            case 'post_last_year':
                $start_date = false;
                $end_date->modify('-1 year');
                break;

            case 'next_week':
                $end_date->modify('+7 days');
                break;

            case 'next_1month':
                $end_date->modify('+1 month');
                break;

            case 'next_3month':
                $end_date->modify('+3 months');
                break;
            
            case 'next_6month':
                $end_date->modify('+6 months');
                break;

            case 'next_year':
                $end_date->modify('+1 year');
                break;

            case 'post_next_year':
                $start_date->modify('+1 year');
                $end_date = false;
                break;

            case 'never':
                $start_date = false;
                $end_date = false;
                break;

            default:
                return null;
        }

        return [
            'start' => $start_date ? $start_date->format('Y-m-d H:i:s') : false,
            'end' => $end_date ? $end_date->format('Y-m-d H:i:s') : false,
        ];
    }
}