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

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

        if(isset($Route->params->self)) {
            $Collection = $this->db->from(null,'
                    SELECT
                    collections.*,
                    (SELECT 
                    COUNT(collections_post.id) 
                    FROM collections_post 
                    WHERE collection_id = collections.id) AS total
                    FROM `collections` WHERE collections.self = "'.$Route->params->self.'"')
                ->first(); 
            $Listings = $this->db->from(null,'
                SELECT
                posts.*
                FROM `collections_post` 
                LEFT JOIN posts ON posts.id = collections_post.post_id  
                WHERE posts.status = "1" AND collections_post.collection_id = '.$Collection['id'].'
                ORDER BY collections_post.id DESC
                LIMIT 0,100')
            ->all();
        }
        if(empty($Collection['id'])) {
            //header('location:'.APP.'/404');
        }

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

        $Config['title']        = trim(str_replace($old, $new, trim(get($Settings,'data.collection_title','seo'))));
        $Config['description']  = trim(str_replace($old, $new, trim(get($Settings,'data.collection_description','seo'))));


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

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