<?php
/**
 * Tool Controller
 */
class Tool extends Controller
{
    /**
     * Process
     */
    public function process()
    {    
		$AuthUser		= $this->getVariable("AuthUser");
		$Route			= $this->getVariable("Route");
        $Settings       = $this->getVariable("Settings");


        // Config 
        $Config['nav']  = 'tools';

        $Filter     = null;
        if(isset($Route->params->self)) { 
            // Filter
            if(isset($_GET['filter'])) {
                $Filter     = json_decode($_GET['filter'], true);  
                $SearchPage = '?filter='.htmlspecialchars($_GET['filter']).'&'; 
            } 

            if(isset($_GET['_ACTION']) AND $_GET['_ACTION'] == 'update') {
                include PATH.'/view/tool/'.$Route->params->self.'/update.php';
            } elseif(file_exists(PATH.'/view/tool/'.$Route->params->self.'/data.json')) {
                $Data = file_get_contents(PATH.'/view/tool/'.$Route->params->self.'/data.json');
                $Data = json_decode($Data,true);
                if(isset($_GET['filter'])) {

                    include PATH.'/view/tool/'.$Route->params->self.'/listings.php';
                    $this->setVariable('Listings',(isset($Listings) ? $Listings : null));
                    $this->setVariable('Paginations',(isset($Paginations) ? $Paginations : null));
                    $this->setVariable('CategoryId',(isset($CategoryId) ? $CategoryId : null));
                }
                $Categories = $this->db->from('categories')->all();
            }
            $this->setVariable('Categories',(isset($Categories) ? $Categories : null));
            $this->setVariable('Data',(isset($Data) ? $Data : null));
            $this->setVariable('Filter',$Filter);
        }

        // Filter
        if(isset($_POST['_ACTION']) &&  $_POST['_ACTION'] == 'filter') {
            foreach ($_POST as $key => $value) {
                if(isset($value) AND Input::cleaner($value) != null) {
                    if($key != '_ACTION' AND $key != '_TOKEN') {
                        $Filter[$key] = Input::cleaner($value);
                    }
                }
            }
            if(count($Filter) > 0) {
                header("location: ".APP.'/admin/tool/'.$Route->params->self.'?filter='.json_encode($Filter));
            } else {
                header("location: ".APP.'/admin/tool/'.$Route->params->self);
            }
        }

        $this->setVariable("Config", $Config);  
        $this->view("tool", "admin");
    }
}