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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        $data['module'] = module::get_list();
        $data['module_type'] = module_type::get_list();
        $this->render('index.html', $data);
    }
    
    protected function execute_add($module_type)
    {
        $module_type = module_type::get($module_type);
        
        if ($this->request->is_post())
        {
            $form = $this->getForm('add', $module_type);
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('module/add');
                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]);
                }
            }
            
            module::add($data);
            $this->redirect('module');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = $this->getForm('add', $module_type);
                $form->module_type_id = $module_type['id'];
            }
            
            $data['form'] = $form;
            $this->render('add.html', $data);
        }
    }
    
    protected function execute_edit($id)
    {
        $module = module::get($id);
        $module_type = module_type::get($module['module_type_id']);
        
        if ($this->request->is_post())
        {
            $form = $this->getForm('edit', $module_type);
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('module/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]);
                }
            }
            
            module::edit($form->get('id'), $data);
            $this->redirect('module');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = $this->getForm('edit', $module_type);
                
                foreach ($module['data'] as $k => $v) {
                    $module['data_' . $k] = $v;
                }
                
                unset($module['data']);
                $form->set($module);
            }
            
            $data['form'] = $form;
            $this->render('edit.html', $data);
        }
    }
    
    protected function execute_delete()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        module::delete($this->request->post('id'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function getForm($action, $module_type) {
        $config = include $this->directory . 'config/' . $action . '.php';
        
        foreach ($module_type['data'] as $k => $v) {
            $config['data_' . $k] = array('type' => $v);
            
            if ($v == 'textarea') {
                $config['data_' . $k]['rows'] = 15;
                $config['data_' . $k]['cols'] = 80;
            }
        }
        
        $form = new form($config);
        return $form;
    }
}
