<?php
/**
 * Games Controller
 */
class Games extends Controller
{
    /**
     * Process
     */
    public function process()
    {    

		$AuthUser		= $this->getVariable("AuthUser");
		$Route			= $this->getVariable("Route");
        $Settings       = $this->getVariable("Settings");
        $isValid        = $this->getVariable("isValid");

        $Config['title']        = get($Settings,'data.games_title','seo');
        $Config['description']  = get($Settings,'data.games_description','seo');
        $Config['cache']        = 'true';
        

        if(isset($_POST['_ACTION']) AND Input::cleaner($_POST['_ACTION']) == 'filter' AND $isValid == 1) {
            header("location: ".category(null,$_POST['category'])); 
        }

        $Where          = "WHERE posts.status = 1 AND ";
        $CategoryWhere  = null;
        if(isset($Route->params->self)) {
            $Selected    = $this->db->from('categories')->where('self',$Route->params->self)->first();
            if(isset($Selected['id'])) {
                $Where          .= ' posts_category.category_id = '.$Selected['id'].' AND '; 
                $CategoryWhere  = 'WHERE posts_category.category_id = '.$Selected['id']; 
            }
        }

        if(isset($Selected['id'])) {
            $Config['heading']  = $Selected['name'].' '.$this->translate('Games');
            $Config['page']     = category($Selected['id'],$Selected['self']);

            $new    = array($Selected['name']);
            $old    = array('[name]');

            $Config['title']        = trim(str_replace($old, $new, trim(get($Settings,'data.category_title','seo'))));
            $Config['description']  = trim(str_replace($old, $new, trim(get($Settings,'data.category_description','seo'))));
        } elseif(isset($Route->params->order) AND $Route->params->order == $this->translate('most-popular')) {
            $Config['heading']  = $this->translate('Most Popular Games');
            $Config['page']     = games($this->translate('most-popular'));
        } else {
            $Config['heading']  = $this->translate('Newest Games');
            $Config['page']     = games();
        }
        
        $Where = rtrim($Where,' AND '); 

        // Query 
        $TotalRecord        = $this->db->from(null,'
            SELECT 
            count(posts.id) as total 
            FROM `posts`
            LEFT JOIN (
                  SELECT
                    post_id, 
                    category_id 
                  FROM `posts_category`   
                  '.$CategoryWhere.'
                  GROUP BY posts_category.post_id
                  ORDER BY posts_category.category_id DESC
                ) posts_category ON posts.id = posts_category.post_id
            '.$Where)
            ->total(); 
        $LimitPage          = $this->db->pagination($TotalRecord, get($Settings,'data.listing','customize') ? get($Settings,'data.listing','customize') : 76, PAGE_PARAM); 
   
        $Listings = $this->db->from(null,'
            SELECT 
            posts.*
            FROM `posts`  
            LEFT JOIN (
                  SELECT
                    post_id, 
                    category_id 
                  FROM `posts_category`   
                  '.$CategoryWhere.'
                  GROUP BY posts_category.post_id
                  ORDER BY posts_category.category_id DESC
                ) posts_category ON posts.id = posts_category.post_id
            '.$Where.'
            '.(isset($_GET['sort']) ? 'ORDER BY posts DESC' : 'ORDER BY posts.id DESC').'
            LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
            ->all();
        $Pagination         = $this->db->showPagination($Config['page'].'?page=[page]');


        $Categories = $this->db->from('categories')->orderby('name','ASC')->all();
     
        $this->setVariable("Config", $Config);  
        $this->setVariable("Listings", $Listings);  
        $this->setVariable("TotalRecord", $TotalRecord);  
        $this->setVariable("Pagination", $Pagination);  
        $this->setVariable("Categories", $Categories);  
        $this->setVariable('Selected',isset($Selected) ? $Selected : null); 
        
        $this->view("games", "app");
    }
}