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

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

        if(isset($Route->params->self)) {

            $Listing = $this->db->from(null,'
                SELECT 
                posts.*,
                (SELECT 
                COUNT(comments.post_id) 
                FROM comments 
                WHERE comments.parent_id = 0 AND comments.type = "movie" AND post_id = posts.id) AS comments, 
                (SELECT 
                COUNT(posts_like.post_id) 
                FROM posts_like 
                WHERE posts_like.reaction = "up" AND post_id = posts.id) AS likes, 
                (SELECT 
                COUNT(posts_like.post_id) 
                FROM posts_like 
                WHERE posts_like.reaction = "down" AND post_id = posts.id) AS dislikes
                FROM `posts`  
                WHERE posts.self = "'. $Route->params->self .'" AND posts.status = 1')
                ->first(); 

            if(empty($Listing['id'])) {
                header('location:'.APP.'/404');
            }
            if(isset($AuthUser['id'])) {
                $Vote = $this->db->from('posts_like')->where('user_id',$AuthUser['id'])->where('post_id',$Listing['id'])->first();
            }

            $sessionGame = 'game:'.$Listing['id'];
            if(empty($_SESSION[$sessionGame])) {
                $_SESSION[$sessionGame] = $sessionGame;  
                $this->db->update('posts')->where('id',$Listing['id'])->set(array('view' => (int)($Listing['view']+1)));
                if(isset($AuthUser['id']) AND get($Settings,'data.history','general') == 1) {
                    $GameLog = $this->db->from('posts_log')->where('post_id',$Listing['id'],'=')->where('user_id',$AuthUser['id'])->first();
                    if(empty($GameLog['id'])) {
                        $this->db->insert('posts_log')->set(array(
                            'post_id' => $Listing['id'],
                            'user_id' => $AuthUser['id'],
                            'created' => date('Y-m-d H:i:s')
                        ));
                    }
                }
            }
            // Categories
            $Categories = $this->db->from(
                null,
                '
                SELECT 
                categories.id, 
                categories.name, 
                categories.self
                FROM `posts_category` 
                LEFT JOIN categories ON posts_category.category_id = categories.id     
                WHERE posts_category.post_id = ' . $Listing['id'] . '
                ORDER BY categories.name ASC'
            )->all(); 

            $Newests    = $this->db->from('posts')->where('status',1)->limit(0,12)->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();
            $Populars   = $this->db->from('posts')->where('status',1)->orderby('view','DESC')->limit(0,12)->all();
        }
        
        $new    = array($Listing['title']);
        $old    = array('[name]');

        $Config['title']        = isset($Listing['meta_title']) ? $Listing['meta_title'] : trim(str_replace($old, $new, trim(get($Settings,'data.play_title','seo'))));
        $Config['description']  = isset($Listing['meta_description']) ? $Listing['meta_description'] : trim(str_replace($old, $new, trim(get($Settings,'data.play_description','seo'))));
        $Config['url']          = play($Listing['id'],$Listing['self']);
        $Config['id']           = $Listing['id'];
        $Config['type']         = 'post';

        $this->setVariable("Config", $Config);  
        $this->setVariable("Listing", $Listing);  
        $this->setVariable('Vote',isset($Vote) ? $Vote : null); 
        $this->setVariable("Categories", $Categories);  
        $this->setVariable("Newests", $Newests);  
        $this->setVariable("Populars", $Populars);  
        $this->setVariable("Randoms", $Randoms);  
        
        if(isset($_POST['_ACTION']) AND Input::cleaner($_POST['_ACTION']) == 'report' AND isset($AuthUser['id'])) {
            $this->report();
        } 
        $this->view("play", "app");
    }
    /**
     * report
     */
    public function report() { 
        $AuthUser       = $this->getVariable("AuthUser");    
        $Listing        = $this->getVariable("Listing");     
        if (empty($Notify)) { 
            $dataarray          = array(
                "user_id"       => $AuthUser['id'],
                "type"          => 'movie',
                "report"        => Input::cleaner($_POST['report']),
                "report_id"     => $Listing['id'],
                "body"          => Input::cleaner($_POST['body']),
                "status"        => 2,
                "created"       => date('Y-m-d H:i:s')
            );   
            $this->db->insert('reports')->set($dataarray);  

            $Notify['type']     = 'success';
            $Notify['text']     = $this->translate('Action completed successfully'); 
            $this->notify($Notify);
            header('location:'.play($Listing['id'],$Listing['self']));
        } else {
            $this->notify($Notify);
        }
    }
}