Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Controller/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Controller/TfControllerQuery.php

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

namespace TechFry\Library\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Router\Route;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\Input\Input;
use TechFry\Library\TUser;
use TechFry\Library\TField;
use TechFry\Library\TArticle;
use TechFry\Library\TDb;
use TechFry\Library\TString;
use TechFry\Library\TDate;

class TfControllerQuery extends BaseController
{
    public $option;
    
    public $type;
    
    public $db_table;
    
    public $db_table_wo_prefix;

    public $table;
    
    public $info;
    
    public $column;
    
    public $column_save;
    
    public $function_name;
    
    public $conditions = [];
    
    public $text;
    
    public $value;
    
    public $rows;
    
    public $num_rows;
    
    public $users;
    public $articles;
    
    public function __construct($config = array(), ?MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null)
	{
	    parent::__construct($config, $factory, $app, $input);
	    
        $this->option = $this->input->get('option');
	}
	
// *****************************************************************************

    public function get_form_info()
    {
        $this->info = $this->input->get('jform', array(), 'array');
    }
    
    public function process()
    {
        $model = $this->getModel();
        
        if (!$model->pro())
        {
            $this->setMessage(Text::_('COM_TF_PRO_UPGRADE_MESSAGE'), 'error');
        
            $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=query', false));
        
            return;
        }
        
        $info = $this->input->get('jform', array(), 'array');
        $this->get_form_info();
        
        $this->type = $info['type'];
        if ($this->type == 'users')
        {
            $this->filter_users();
        }
        elseif ($this->type == 'content')
        {
            $a = new TArticle();
            $this->articles = $a->filter_articles($info['filter'], $info['filterarticles']);
        }

        $this->table = new TDb($info['type'], array('print_query' => true));
        
        $this->db_table = '#__' . $info['type'];
        $this->db_table_wo_prefix = $info['type'];
        $this->column = $info['column_field'];
        $this->column_save = ($info['column_save'] != '') ? $info['column_save'] : $info['column_field'];
        $this->text = $info['text'];
        $this->value = $info['value'];
        
        if ($info['tool'] == 'string')
        {
            $this->function_name = $info['str_function'];
        }
        elseif ($info['tool'] == 'date')
        {
            $this->function_name = $info['date_function'];
        }
        elseif ($info['tool'] == 'numeric')
        {
            $this->function_name = $info['num_function'];
        }
        
        // Conditions
        if (!empty($info['filter']))
        {
            foreach ($info['filter'] as $filter)
            {
                if ($filter['where_field'] != '' && $filter['where_value'] != '')
                {
                    $this->conditions[] = array($filter['where_field'], $filter['where_operator'], $filter['where_value']);
                }
                elseif ($filter['where_field'] != '' && $filter['where_value'] == '')
                {
                    $this->conditions[] = $filter['where_field'] . ' ' . $filter['where_operator'];
                }
            }
        }
        
        $function_name = $info['tool'] . '_tool';
        
        $message = $this->$function_name();
        if (is_array($message))
        {
            $message = '<pre>' . print_r($message, true) . '</pre>';
        }
        
        if ($this->num_rows != '')
        {
            $this->app->enqueueMessage('Affected Rows: ' . $this->num_rows . '<br>');
        }
        
        $this->setMessage($message);
        
        $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=query&type=' . $this->type, false));
    }
    
    // 01. Find text in table column
    public function find_tool()
    {
        $order = $this->info['order_col'] . ' ' . $this->info['order_dirn'];

        $this->table->set_conditions($this->info['filter']);
        $this->table->set_select($this->info['select']);
        $this->table->set_ordering($order);
        $this->table->set_limit($this->info['limit']);
        $this->table->set_joins($this->info['joins']);
        
        $result = $this->table->get_items();
        
        return $result;
    }
    
    // 02. Find duplicates by column
    public function find_duplicates_tool()
    {
        $this->table->set_select(array($this->column, 'COUNT(*) AS total'));
        $this->table->set_group($this->column);
        $this->table->set_having('total > 1');

        $result = $this->table->get_items();
        
        return $result;
    }
    
    // 03. Update text with value in table column
    public function update_tool()
    {
        if ($this->type == 'users')
        {
            foreach ($this->users as $user_id)
            {
                // Case 1: Update Users Custom Fields
                if (is_numeric($this->column))
                {
                    $cf = new TField();
                    $cf->save_custom_field($this->column, $user_id, $this->value);
                }
                // Case 2: Update Joomla Users Table
                else
                {
                    $action_field = $this->column;
                    $userdata = array('id' => $user_id, $action_field => $this->value);
                    $updates = array($action_field);
                    
                    $u = new TUser();
                    $u->update_user($userdata, $updates);
                }
            }
            
            $this->num_rows = count($this->users);
        }
        elseif ($this->type == 'content')
        {
            foreach ($this->articles as $article_id)
            {
                // Case 1: Update Content Custom Fields
                if (is_numeric($this->column))
                {
                    $cf = new TField();
                    $cf->save_custom_field($this->column, $article_id, $this->value);
                }
                // Case 2: Update Joomla Content Table
                else
                {
                    $action_field = $this->column;
                    $articledata = array('id' => $article_id, $action_field => $this->value);

                    $a = new TArticle();
                    $a->update_article($articledata);
                }
            }
            
            $this->num_rows = count($this->articles);
        }
        else
        {
            $sets = array($this->column => $this->value);

            $this->table->set_conditions($this->info['filter']);
            
            $this->num_rows = $this->table->update_records($sets);
        }
    }
    
    // 04. Replace text with value 
    public function replace_tool()
    {
        $this->table->set_conditions($this->info['filter']);

        $this->num_rows = $this->table->replace_column($this->column, $this->text, $this->value);
    }
    
    // 05. Delete rows
    public function delete_tool()
    {
        $this->table->set_conditions($this->info['filter']);
        
        $this->num_rows = $this->table->delete_items();
    }
    
    // 06. Truncate table
    public function truncate_tool()
    {
        $this->table->truncate_table();
        
        return 'Database table ' . $this->info['type'] . ' truncated.';
    }
    
    // 07. Get columns in a table
    public function table_columns_tool()
    {
        $cols = $this->table->get_table_columns();
        
        return $cols;
    }
    
    // 08. Get create statement for a table
    public function table_create_tool()
    {
        $cols = $this->table->get_table_create();
        
        return $cols;
    }
    
    // 09. Get list of all database tables
    public function table_list_tool()
    {
        $tables = $this->table->get_all_tables();
        
        return $tables;
    }
    
    // 10. String function
    public function string_tool()
    {
        $this->get_rows();
        
        $column_name = $this->info['column_field'];
        $column_save = $this->column_save;
        
        if ($this->function_name == 'preg_replace')
        {
            $this->info['p1'] = '/' . $this->info['p1'] . '/i';
        }
        
        $params = array($this->info['p1'], $this->info['p2']);
        
        foreach ($this->rows as $row)
        {
            $s = new TString($row->$column_name);
            $row->$column_save = $s->process($this->function_name, $params);
            
            $this->table->insert_update($row);
        }
    }
    
    // 11. Date function
    public function date_tool()
    {
        $this->get_rows();
        
        $column_name = $this->info['column_field'];
        $column_save = $this->column_save;
        
        foreach ($this->rows as $row)
        {
            $d = new TDate($row->$column_name);
            $row->$column_save = $d->process($this->function_name, $this->info['p1']);
            
            $this->table->insert_update($row);
        }
    }
    
    // 12. Numeric function
    public function numeric_tool()
    {
        // $this->function_tool();
        
        return 'Coming Soon!';
    }
    
    // Function tool
    public function function_tool()
    {
        $model = $this->getModel();
        
        $this->get_rows();
        
        $function_name = $this->function_name;
        $column_name = $this->info['column_field'];
        $column_save = $this->column_save;
        
        foreach ($this->rows as $row)
        {
            if ($this->info['p1'] == '' && $this->info['p2'] == '' && $this->info['p3'] == '')
            {
                $row->$column_save = $function_name($row->$column_name);
            }
            elseif ($this->info['p1'] != '' && $this->info['p2'] == '' && $this->info['p3'] == '')
            {
                $row->$column_save = $function_name($row->$column_name, $this->info['p1']);
            }
            elseif ($this->info['p1'] != '' && $this->info['p2'] != '' && $this->info['p3'] == '')
            {
                $row->$column_save = $function_name($row->$column_name, $this->info['p1'], $this->info['p2']);
            }
            elseif ($this->info['p1'] != '' && $this->info['p2'] != '' && $this->info['p3'] != '')
            {
                $row->$column_save = $function_name($row->$column_name, $this->info['p1'], $this->info['p2'], $this->info['p3']);
            }
            
            $model->direct_save($row, $this->db_table_wo_prefix);
        }
    }

    // Custom query
    public function custom_tool()
    {
        $this->table->custom_query($this->info['custom_query']);

        return $this->info['custom_query'];
    }
    
    public function get_rows()
    {
        $order = $this->info['order_col'] . ' ' . $this->info['order_dirn'];

        $this->table->set_conditions($this->info['filter']);
        $this->table->set_select(array('id', $this->column));
        $this->table->set_ordering($order);
        $this->table->set_limit($this->info['limit']);
        
        $result = $this->table->get_items();
        
        $this->rows = $result;
    }
    
    public function filter_users()
    {
        $u = new TUser();
        $this->users = $u->filter_users($this->info['filter'], $this->info['filterusers']);
    }
    
    public function cancel($key = null)
    {
        $this->checkToken();
        
        $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=dashboard', false));
        
        return true;
    }
}