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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_add()
    {
        $template = $this->request->get('template');
        
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/add.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('help/add?template=' . $template);
                return;
            }
            
            $data = $form->get();
            $referer = $data['referer'];
            unset($data['referer']);
            help::add($data);
            $this->redirect($referer);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/add.php');
            }
            
            $form->template = $template;
            $form->referer = $this->request->HTTP_REFERER;
            $data['form'] = $form;
            $this->render('add.html', $data);
        }
    }
    
    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('help/edit/' . $id);
                return;
            }
            
            $data = $form->get();
            $referer = $data['referer'];
            unset($data['referer']);
            help::edit($data['id'], $data);
            $this->redirect($referer);
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit.php');
                $form->set(help::get($id));
            }
            
            $form->referer = $this->request->HTTP_REFERER;
            $data['form'] = $form;
            $this->render('edit.html', $data);
        }
    }
}
