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

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

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

            $Listing = $this->db->from(null,'
                SELECT 
                users.*
                FROM `users`  
                WHERE users.username = "'. $Route->params->username .'" AND users.status = 1')
                ->first(); 

            if(empty($Listing['id'])) {
                header('location:'.APP.'/404');
            }
            if(empty($Route->params->tab)) {
                $Plays = $this->db->from(null,'
                    SELECT 
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `posts_log`  
                    LEFT JOIN posts ON posts.id = posts_log.post_id
                    WHERE posts_log.user_id = '.$Listing['id'].' AND posts.status = 1
                    ORDER BY posts_log.id DESC
                    LIMIT 0,6')
                ->all(); 

                $Likes = $this->db->from(null,'
                    SELECT 
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `posts_like`  
                    LEFT JOIN posts ON posts.id = posts_like.post_id
                    WHERE posts_like.user_id = '.$Listing['id'].' AND posts.status = 1 AND posts_like.reaction = "up"
                    ORDER BY posts_like.id DESC
                    LIMIT 0,6')
                ->all(); 

                // Query 
                $TotalRecord        = $this->db->from(null,'
                    SELECT 
                    COUNT(comments.id) as total 
                    FROM `comments` 
                    WHERE comments.user_id = '.$Listing['id'].' AND comments.status = 1')
                    ->total();  
                $LimitPage          = $this->db->pagination($TotalRecord, PAGE_LIMIT, PAGE_PARAM); 
           
                
                $Comments = $this->db->from(null,'
                    SELECT
                    comments.comment,
                    comments.created,
                    comments.id as comment_id,
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `comments`  
                    LEFT JOIN posts ON posts.id = comments.post_id
                    WHERE comments.user_id = '.$Listing['id'].' AND comments.status = 1
                    ORDER BY comments.id DESC
                    LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
                ->all(); 
                $Pagination         = $this->db->showPagination(user($Listing['id'],$Listing['username']).'?page=[page]');
                $this->setVariable("Comments", $Comments);   
                $this->setVariable("TotalRecord", $TotalRecord);   
                $this->setVariable("Pagination", $Pagination);  
            } elseif(isset($Route->params->tab) AND $Route->params->tab == 'playing') {

                // Query 
                $TotalRecord        = $this->db->from(null,'
                    SELECT 
                    COUNT(posts_log.id) as total 
                    FROM `posts_log` 
                    LEFT JOIN posts ON posts.id = posts_log.post_id
                    WHERE posts_log.user_id = '.$Listing['id'].' AND posts.status = 1')
                    ->total();  
                $LimitPage          = $this->db->pagination($TotalRecord, PAGE_LIMIT, PAGE_PARAM); 
           
                
                $Plays = $this->db->from(null,'
                    SELECT
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `posts_log`  
                    LEFT JOIN posts ON posts.id = posts_log.post_id
                    WHERE posts_log.user_id = '.$Listing['id'].' AND posts.status = 1
                    ORDER BY posts_log.id DESC
                    LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
                ->all(); 
                $Pagination         = $this->db->showPagination(user($Listing['id'],$Listing['username']).'/playing?page=[page]');
                $this->setVariable('Plays',isset($Plays) ? $Plays : null); 
                $this->setVariable('Pagination',isset($Pagination) ? $Pagination : null); 
            } elseif(isset($Route->params->tab) AND $Route->params->tab == 'like') {

                // Query 
                $TotalRecord        = $this->db->from(null,'
                    SELECT 
                    COUNT(posts_like.id) as total 
                    FROM `posts_like` 
                    LEFT JOIN posts ON posts.id = posts_like.post_id
                    WHERE posts_like.user_id = '.$Listing['id'].' AND posts.status = 1')
                    ->total();  
                $LimitPage          = $this->db->pagination($TotalRecord, PAGE_LIMIT, PAGE_PARAM); 
           
                
                $Likes = $this->db->from(null,'
                    SELECT
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `posts_like`  
                    LEFT JOIN posts ON posts.id = posts_like.post_id
                    WHERE posts_like.user_id = '.$Listing['id'].' AND posts.status = 1 AND posts_like.reaction = "up"
                    ORDER BY posts_like.id DESC
                    LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
                ->all(); 
                $Pagination         = $this->db->showPagination(user($Listing['id'],$Listing['username']).'/playing?page=[page]');
                $this->setVariable('Likes',isset($Likes) ? $Likes : null); 
                $this->setVariable('Pagination',isset($Pagination) ? $Pagination : null); 
            } elseif(isset($Route->params->tab) AND $Route->params->tab == 'comments') {

                // Query 
                $TotalRecord        = $this->db->from(null,'
                    SELECT 
                    COUNT(comments.id) as total 
                    FROM `comments` 
                    LEFT JOIN posts ON posts.id = comments.post_id
                    WHERE comments.user_id = '.$Listing['id'].' AND posts.status = 1')
                    ->total();  
                $LimitPage          = $this->db->pagination($TotalRecord, PAGE_LIMIT, PAGE_PARAM); 
           
                
                $Comments = $this->db->from(null,'
                    SELECT
                    comments.comment,
                    comments.created,
                    comments.id as comment_id,
                    posts.id, 
                    posts.title,  
                    posts.self, 
                    posts.image
                    FROM
                    `comments`  
                    LEFT JOIN posts ON posts.id = comments.post_id
                    WHERE comments.user_id = '.$Listing['id'].' AND posts.status = 1
                    ORDER BY comments.id DESC
                    LIMIT '.$LimitPage['start'].','.$LimitPage['limit'])
                ->all(); 
                $Pagination         = $this->db->showPagination(user($Listing['id'],$Listing['username']).'/comments?page=[page]');
                $this->setVariable('Comments',isset($Comments) ? $Comments : null); 
                $this->setVariable('Pagination',isset($Pagination) ? $Pagination : null); 
            }
        }
     
        $new    = array($Listing['username']);
        $old    = array('[username]');

        $Config['title']        = trim(str_replace($old, $new, trim(get($Settings,'data.profile_title','seo'))));
        $Config['description']  = trim(str_replace($old, $new, trim(get($Settings,'data.profile_description','seo'))));
        $Config['url']          = user($Listing['id'],$Listing['username']);
        $Config['id']           = $Listing['id']; 

        $this->setVariable("Config", $Config);  
        $this->setVariable("Listing", $Listing);    
        $this->setVariable('Plays',isset($Plays) ? $Plays : null); 
        $this->setVariable('Likes',isset($Likes) ? $Likes : null); 
        
        
        if(isset($_POST['_ACTION']) AND Input::cleaner($_POST['_ACTION']) == 'remove' AND isset($AuthUser['id'])) {
            $this->remove();
        } else {
            $this->view("profile", "app");
        }
    }
    /**
     * remove
     */
    public function remove() { 
        $AuthUser       = $this->getVariable("AuthUser");    
        $Listing    = $this->db->from('comments')->where('id',Input::cleaner($_POST['_ID']))->first();
        if (isset($Listing['id']) AND $Listing['id'] == Input::cleaner($_POST['_ID']) AND isset($AuthUser['id'])) { 
            $this->db->delete('comments')->where('id',$Listing['id'],'=')->done();
            echo 'OK'; 
        }
    }
}