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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index($menu)
    {
        $data['menu'] = menu::get($menu);
        $data['item'] = menu_item::get_list($menu);
        $data['menu_item_type'] = menu_item_type::get_list();
        $this->render('index.html', $data);
    }
    
    protected function execute_add($menu, $menu_item_type)
    {
        $menu = menu::get($menu);
        $menu_item_type = menu_item_type::get($menu_item_type);
        
        if ($this->request->is_post())
        {
            $form = $this->getForm('add', $menu_item_type);
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('menu_item/add/' . $menu);
                return;
            }
            
            $data = $form->get();
            $data['data'] = array();
            
            foreach ($data as $k => $v) {
                if (preg_match('/^data_/', $k)) {
                    $data['data'][substr($k, 5)] = $v;
                    unset($data[$k]);
                }
            }
            
            menu_item::add($data);
            $this->redirect('menu_item/index/' . $menu['id']);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = $this->getForm('add', $menu_item_type);
            }
            
            $form->menu_id = $menu['id'];
            $form->menu_item_type_id = $menu_item_type['id'];
            $data['form'] = $form;
            $this->render('add.html', $data);
        }
    }
    
    protected function execute_edit($id)
    {
        $menu_item = menu_item::get($id);
        $menu_item_type = menu_item_type::get($menu_item['menu_item_type_id']);
        
        if ($this->request->is_post())
        {
            $form = $this->getForm('edit', $menu_item_type);
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('menu_item/edit/' . $id);
                return;
            }
            
            $data = $form->get();
            $data['data'] = array();
            
            foreach ($data as $k => $v) {
                if (preg_match('/^data_/', $k)) {
                    $data['data'][substr($k, 5)] = $v;
                    unset($data[$k]);
                }
            }
            
            menu_item::edit($form->get('id'), $data);
            $this->redirect('menu_item/index/' . $form->menu_id);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = $this->getForm('edit', $menu_item_type);
                
                foreach ($menu_item['data'] as $k => $v) {
                    $menu_item['data_' . $k] = $v;
                }
                
                unset($menu_item['data']);
                $form->set($menu_item);
            }
            
            $data['form'] = $form;
            $this->render('edit.html', $data);
        }
    }
    
    protected function execute_delete()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        menu_item::delete($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function getForm($action, $menu_item_type) {
        $config = include $this->directory . 'config/' . $action . '.php';
        
        foreach ($menu_item_type['data'] as $k => $v) {
            $config['data_' . $k] = array('type' => $v);
        }
        
        $form = new form($config);
        return $form;
    }
}
