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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_add($category_id)
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/add.php');
            $post = $this->request->post();
            
            if (!$form->is_valid($post))
            {
                $this->session->flash('form', $form);
                $this->redirect('product/add/' . $post['category_id']);
                return;
            }
            
            if (isset($this->session->product_image))
            {
                $data['image'] = $image = $this->session->product_image;
            }
            
            $data = $form->get();
            product::add($data);
            unset($this->session->product_image);
            $this->redirect('category/index/' . $data['category_id']);
        }
        else
        {
            $category = category::get($category_id);
            
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/add.php');
                $form->category_id = $category['id'];
            }
            
            $data['form'] = $form;
            $data['path'] = category::get_path_array($category['id']);
            
            if (isset($this->session->product_image))
            {
                $data['image'] = $this->session->product_image;
            }
            
            $this->render('add.html', $data);
        }
    }
    
    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->product_image))
        {
            product::delete_image($this->session->product_image);
        }
        
        try
        {
            product::save_image($id, $file);
        }
        catch (exception $e)
        {
            $this->response->set('{ error:"Image error." }');
            return;
        }
        
        if (!$id)
        {
            $this->session->product_image = pathinfo($file['name'], PATHINFO_FILENAME);
        }
        
        $this->response->set('{ error:"", image:"' . product::DIRECTORY . pathinfo($file['name'], PATHINFO_FILENAME) . '-s.jpg"}');
    }
    
    protected function execute_upload_extra($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;
        }
        
        try
        {
            $id = product::save_image_extra($id, $file);
        }
        catch (exception $e)
        {
            $this->response->set('{ error:"Image error." }');
            return;
        }
        
        $this->response->set('{ error:"", image:"' . product::DIRECTORY . pathinfo($file['name'], PATHINFO_FILENAME) . '-s.jpg", id:"' . $id . '"}');
    }
    
    protected function execute_edit($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;
            }
            
            $data = $form->get();
            product::edit($data['id'], $data);
            $this->redirect('category/index/' . $data['category_id']);
        }
        else
        {
            $product = product::get($id);
            
            if ($product['image'])
            {
                $data['image'] = $product['image'];
            }
            
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $form->set($product);
            }
            
            $data['form'] = $form;
            $data['category'] = category::get($product['category_id']);
            $data['path'] = category::get_path_array($product['category_id']);
            $data['image_extra'] = product::get_image_extra($product['id']);
            $this->render('edit.html', $data);
        }
    }
    
    protected function execute_feature()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        product::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;
        }
        
        product::unfeature($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_delete()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        product::delete($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_delete_image_extra()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        product::delete_image_extra($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
}
