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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_edit($type, $type_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('meta/edit/' . $type . '/' . $type_id);
                return;
            }
            
            $back = $form->get('back', '/');
            $form->clear('back');
            meta::edit($type, $type_id, $form->get());
            $this->redirect($back);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $meta = meta::get($type, $type_id);
                
                if ($meta)
                {
                    $form->set($meta);
                }
                
                $form->set('back', $this->request->HTTP_REFERER);
            }
            
            $data['form'] = $form;
            $this->render('index.html', $data);
        }
    }
}
