<?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);

                // Josh edit: show the title on the meta page for what the user
                // is editing.
                if($type == 'section') {
                    $data['section'] = section::get($type_id);
                }
                
                if($type == 'page') {
                    $data['page'] = page::get($type_id);
                }
            }
            
            $data['form'] = $form;
            $this->render('index.html', $data);
        }
    }
}
