<?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();
        $id = category::add($data);
        $image = 'admin/skin/icon/folder1.png';
        $this->response->set(json_encode(array('error' => '', 'id' => $id, 'image' => $image)));
    }
    
    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)
    {
        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/' . $form->get('id'));
        }
        else
        {
            $category = category::get($id);
            $data['category'] = $category;
            
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $form->set($category);
            }
            
            $data['form'] = $form;
            $data['current'] = $category;
            $data['path'] = category::get_path_array($category['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' => '')));
    }
}
