<?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;
            }
            
            $data = $form->get();
            product::add($data);
            $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']);
            $this->render('add.html', $data);
        }
    }
    
    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);
            $data['product'] = $product;
            
            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']);
            $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' => '')));
    }
}
