<?php
class controller_category extends controller
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index($id = null)
    {
        $current = category::get($id);
        $data['current'] = $current;
        $form = new form(include $this->directory . 'config/add.php');
        $form->parent_id = $current['id'];
        $data['form'] = $form;
        $data['category'] = category::get_child($current['id']);
        $data['path'] = category::get_path_array($current['id']);
        $data['product'] = product::get_list($current['id']);
        $this->render('index.html', $data);
    }
    
    protected function execute_add()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        $form = new form(include $this->directory . 'config/add.php');
        
        if (!$form->is_valid($this->request->post()))
        {
            $this->response->set(json_encode(array('error' => 'invalid data', 'message' => implode('-', array_keys($form->get_error())))));
            return;
        }
        
        $data = $form->get();
        
        if (isset($this->session->category_image))
        {
            $data['image'] = $image = $this->session->category_image;
        }
        
        $id = category::add($data);
        
        if (!isset($image))
        {
            $image = 'admin/skin/icon/folder1.png';
        }
        else
        {
            $image = category::DIRECTORY . $image . '-s.jpg';
        }
        
        unset($this->session->category_image);
        $this->response->set(json_encode(array('error' => '', 'id' => $id, 'image' => $image)));
    }
    
    protected function execute_upload($id = '')
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error:"Not post." }');
            return;
        }
        
        $file = $this->request->file('file');
        
        if (!$file)
        {
            $this->response->set('{ error:"No file uploaded." }');
            return;
        }
        
        if ($file['error'])
        {
            switch($file['error'])
            {
                case '1' : $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; break;
                case '2' : $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break;
                case '3' : $error = 'The uploaded file was only partially uploaded'; break;
                case '4' : $error = 'No file was uploaded.'; break;
                case '6' : $error = 'Missing a temporary folder'; break;
                case '7' : $error = 'Failed to write file to disk'; break;
                case '8' : $error = 'File upload stopped by extension'; break;
                default  : $error = 'No error code avaiable';
            }
            
            $this->response->set('{ error:"' . $error . '" }');
            return;
        }
        
        if(!$file['tmp_name'] || $file['tmp_name'] == 'none')
        {
            $this->response->set('{ error:"No file uploaded." }');
            return;
        }
        
        if (isset($this->session->category_image))
        {
            category::delete_image($this->session->category_image);
        }
        
        try
        {
            $image = category::save_image($id, $file);
        }
        catch (exception $e)
        {
            $this->response->set('{ error:"Image error." }');
            return;
        }
        
        if (!$id)
        {
            $this->session->category_image = pathinfo($file['name'], PATHINFO_FILENAME);
        }
        
        $this->response->set('{ error:"", image:"' . category::DIRECTORY . pathinfo($file['name'], PATHINFO_FILENAME) . '-s.jpg"}');
    }
    
    protected function execute_featured()
    {
        $data['category'] = category::get_featured();
        $data['product'] = product::get_featured();
        $this->render('featured.html', $data);
    }
    
    protected function execute_feature()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        category::feature($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_unfeature()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        category::unfeature($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_edit($id)
    {
        $current = category::get($id);
        
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/edit.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('category/edit/' . $form->id);
                return;
            }
            
            category::edit($form->get('id'), $form->get());
            $this->redirect('category/index/' . $current['id']);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $form->set($current);
            }
            
            $data['form'] = $form;
            $data['current'] = $current;
            $data['path'] = category::get_path_array($current['id']);
            $this->render('edit.html', $data);
        }
    }
    
    protected function execute_delete()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        category::delete($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
}
