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

		$AuthUser		= $this->getVariable("AuthUser");
		$Route			= $this->getVariable("Route");
 
        // Config
        $Config['btn']      = 'game';
        $Config['nav']      = 'games';
        $Config['search']   = true;
        $Config['page']     = $this->translate('Games');


        // Filter
        $i              = 0;
        $FilterSlug     = null;
        if(isset($_POST['_ACTION']) AND $_POST['_ACTION'] == 'filter') {
            foreach ($_POST as $key => $value) {
                if ($i == 0 AND !empty($_POST[$key])) {
                    $FilterSlug .= '?'.$key.'='.$_POST[$key];
                } elseif(!empty($_POST[$key])) {
                    $FilterSlug .= '&'.$key.'='.$_POST[$key];
                }
                $i++;
            }
           header("location: ".APP.'/admin/'.$Config['nav'].$FilterSlug); 
        }

        $Filter = array (
            'type'      => 'filter',
            'q'         => 'search',
            'status'    => 'filter',
            'category'  => 'join',
            'sorting'   => 'order'
        );
        $Where          = null;
        $CategoryWhere  = null;
        $Orderby        = null;
        foreach($Filter as $key => $value) {
            if (isset($_GET[$key])) {
                if ($i == 0) {
                    $FilterSlug .= '?'.$key.'='.$_GET[$key];
                } else {
                    $FilterSlug .= '&'.$key.'='.$_GET[$key];
                }

                $i++;

                if($value == 'filter') {
                    $Where .= 'posts.'.$key.' = "'.$_GET[$key].'" AND '; 
                } elseif ($value == 'join') {
                    $Where          .= 'posts_category.category_id = '.$_GET['category'].' AND'; 
                    $CategoryWhere  = ' WHERE category_id = '.$_GET['category'].' ';
                } elseif ($value == 'search') {
                    $Where .= 'posts.title LIKE "%'.$_GET[$key].'%" AND ';
                } elseif ($value == 'order') {
                    $Orderby = 'Order by posts.id '.$_GET[$key];
                }
            } 
        }

        // 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
            LEFT JOIN categories ON categories.id = posts_category.category_id
            '.(isset($Where) ? 'WHERE '.rtrim($Where,' AND ') : ''))
            ->total(); 
        $LimitPage          = $this->db->pagination($TotalRecord, PAGE_LIMIT, PAGE_PARAM); 
   
        $Listings = $this->db->from(null,'
            SELECT 
            posts.*,
            categories.name
            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
            LEFT JOIN categories ON categories.id = posts_category.category_id
            '.(isset($Where) ? 'WHERE '.rtrim($Where,' AND ') : '').'
            '.(isset($Orderby) ? $Orderby : 'ORDER BY posts.id DESC').'
            LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
            ->all();
        $Pagination         = $this->db->showPagination(APP.'/admin/'.$Config['nav'].(isset($FilterSlug) ? $FilterSlug : '?').'page=[page]');

  
        $Categories = $this->db->from('categories')->orderby('name','ASC')->all();

        $this->setVariable('Listings', $Listings); 
        $this->setVariable('Pagination', $Pagination); 
        $this->setVariable('Categories', $Categories); 
        $this->setVariable('Config', $Config); 

        $this->view("games", "admin");
    }
}