<?php
/**
 * Delete Controller
 */
class Delete extends Controller
{
    /**
     * Process
     */
    public function process()
    {
        $AuthUser   = $this->getVariable("AuthUser");
        $Route      = $this->getVariable("Route");
        $this->{$Route->params->page}();
    }

    public function game() {   
        $Route      = $this->getVariable("Route");
        $Listing    = $this->db->from('posts')->where('id',$Route->params->id)->first();
        if(isset($Listing['id'])) {
            unlink(UPLOADPATH . '/'.GAME_FOLDER.'/'.$Listing['image']);
            unlink(UPLOADPATH . '/'.GAME_FOLDER.'/thumb-'.$Listing['image']);
            unlink(UPLOADPATH . '/'.GAME_FOLDER.'/'.webper($Listing['image']));
            unlink(UPLOADPATH . '/'.GAME_FOLDER.'/thumb-'.webper($Listing['image']));
            if(isset($Listing['source']) AND $Listing['type'] == 'upload') {
                removefolder(UPLOADPATH . '/source/'.$Listing['source']);
            }
            $this->db->delete('posts')->where('id',$Listing['id'],'=')->done(); 
            $this->db->delete('posts_category')->where('post_id',$Listing['id'],'=')->done(); 
            $this->db->delete('posts_log')->where('post_id',$Listing['id'],'=')->done(); 
            $this->db->delete('comments')->where('post_id',$Listing['id'],'=')->done(); 
            header("location: ".APP.'/admin/games');
        } else {
            header("location: ".APP.'/admin/games');
        }
    }


    public function category() {   
        $Route      = $this->getVariable("Route");
        $Listing    = $this->db->from('categories')->where('id',$Route->params->id)->first();
        if(isset($Listing['id'])) {
            unlink(UPLOADPATH . '/icon/'.$Listing['icon']);
            $this->db->delete('categories')->where('id',$Listing['id'],'=')->done(); 
            $this->db->delete('posts_category')->where('category_id',$Listing['id'],'=')->done(); 
            header("location: ".APP.'/admin/categories');
        } else {
            header("location: ".APP.'/admin/categories');
        }
    }
    
    public function report() {   
        $Route      = $this->getVariable("Route");
        $Listing    = $this->db->from('reports')->where('id',$Route->params->id)->first();
        if(isset($Listing['id'])) {
            $this->db->delete('reports')->where('id',$Listing['id'],'=')->done();  
            header("location: ".APP.'/admin/reports');
        } else {
            header("location: ".APP.'/admin/reports');
        }
    }

    public function collections() {   
        $Route      = $this->getVariable("Route");
        $Listing    = $this->db->from('collections')->where('id',$Route->params->id)->first();
        if(isset($Listing['id'])) {
            $this->db->delete('collections')->where('id',$Listing['id'],'=')->done(); 
            $this->db->delete('collections_post')->where('collection_id',$Listing['id'],'=')->done();
            header("location: ".APP.'/admin/collections');
        } else {
            header("location: ".APP.'/admin/collections');
        }
    }
    
    public function collection() {   
        $Route      = $this->getVariable("Route");
        $Listing    = $this->db->from('collections_post')->where('id',$Route->params->id)->first();
        if(isset($Listing['id'])) {
            $this->db->delete('collections_post')->where('id',$Listing['id'],'=')->done();
        }
    }

}