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

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

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

        $Newests    = $this->db->from('posts')->where('status',1)->orderby('id','DESC')->limit(0,get($Settings,'data.newest','customize') ? get($Settings,'data.newest','customize') : 60)->all();
        $Populars   = $this->db->from('posts')->where('status',1)->orderby('view','DESC')->limit(0,get($Settings,'data.popular','customize') ? get($Settings,'data.popular','customize') : 16)->all();
        $Randoms    = $this->db->from('posts')->where('status',1)->orderby('RAND()')->limit(0,get($Settings,'data.random','customize') ? get($Settings,'data.random','customize') : 19)->all();

        $Collections = $this->db->from(null,'
                SELECT
                collections.*,   
                collections_post.post_id,
                posts.image,
                (SELECT 
                COUNT(collections_post.id) 
                FROM collections_post 
                WHERE collection_id = collections.id) AS total
                FROM
                `collections`   
                LEFT JOIN collections_post ON collections.id = collections_post.collection_id
                LEFT JOIN posts ON collections_post.post_id = posts.id
                WHERE collections.featured = 1
                GROUP BY collections_post.collection_id
                ORDER BY collections.id DESC
                LIMIT 0,10')
            ->all(); 
        $Categories    = $this->db->from('categories')->where('featured',1)->orderby('name','ASC')->limit(0,10)->all();
     
        $this->setVariable("Config", $Config);  
        $this->setVariable("Newests", $Newests);  
        $this->setVariable("Populars", $Populars);  
        $this->setVariable("Randoms", $Randoms);  
        $this->setVariable("Collections", $Collections);  
        $this->setVariable("Categories", $Categories);  
        
        $this->view("main", "app");
    }
}