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

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

        $new    = array(tagger($Route->params->self,true));
        $old    = array('[name]');

        $Config['title']        = trim(str_replace($old, $new, trim(get($Settings,'data.tag_title','seo'))));
        $Config['description']  = trim(str_replace($old, $new, trim(get($Settings,'data.tag_description','seo'))));
        if(empty($Route->params->self)) {
            header('location:'.APP.'/404');
        }
        $Where          = "WHERE posts.status = 1 AND ";
        if(isset($Route->params->self)) {  
            $Where          .= ' posts.keywords LIKE  "%'.Input::cleaner(tagger($Route->params->self,true)).'%" AND '; 
        }
        
        $Where = rtrim($Where,' AND '); 
        // Query 
        $TotalRecord        = $this->db->from(null,'
            SELECT 
            count(posts.id) as total 
            FROM `posts` '.$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`  
            '.$Where.'
            '.(isset($_GET['sort']) ? 'ORDER BY posts DESC' : 'ORDER BY posts.id DESC').'
            LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
            ->all();
        $Pagination         = $this->db->showPagination(tag(tagger($Route->params->self)).'?page=[page]');

     
        $this->setVariable("Config", $Config);   
        $this->setVariable("Listings", $Listings); 
        $this->setVariable("Pagination", $Pagination);   
        $this->setVariable("TotalRecord", $TotalRecord);     

        $this->view("tag", "app");
    }
}