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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        $form = new form(include $this->directory . 'config/add.php');
        $data['form'] = $form;
        $data['menus'] = menu::get_list();
        $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();
        $module = $data['module'];
        $position = $data['position'];
        unset($data['module'], $data['position']);
        $id = menu::add($data);
        
        if ($module) {
            module::add(array(
                'module_type_id' => '4',
                'position_id' => $position,
                'name' => $data['name'],
                'data' => array(
                    'menu_id' => $id
                )
            ));
        }
        
        $this->response->set(json_encode(array('error' => '', '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('menu/edit/' . $id);
                return;
            }
            
            menu::edit($form->get('id'), $form->get());
            $this->redirect('menu');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $form->set(menu::get($id));
            }
            
            $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::delete($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
}
