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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        $data['header'] = header::get_list();
        $header = setting::get('header', 'theme');
        $data['current_header'] = $header['value'];
        $data['background'] = background::get_list();
        $background = setting::get('background', 'theme');
        $data['current_background'] = $background['value'];
        $data['favicon'] = favicon::get_list();
        $favicon = setting::get('favicon', 'theme');
        $data['current_favicon'] = $favicon['value'];
        $data['setting'] = setting::get_list('theme');
        $this->render('index.html', $data);
    }
    
    protected function execute_load()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        theme::load($this->request->post('name'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_save()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/save.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/save');
                return;
            }
            
            theme::save($form->name);
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/save.php');
            }
            
            $data['form'] = $form;
            $this->render('save.html', $data);
        }
    }
    
    protected function execute_list()
    {
        $data['theme'] = theme::get_list();
        $this->render('list.html', $data);
    }
    
    protected function execute_delete()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        theme::delete($this->request->post('name'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_add_header()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/add_header.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/add-header');
                return;
            }
            
            header::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/add_header.php');
            }
            
            $data['form'] = $form;
            $this->render('add_header.html', $data);
        }
    }
    
    protected function execute_edit_header($name)
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/edit_header.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/edit-header/' . $name);
                return;
            }
            
            header::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit_header.php');
                $form->name = $name;
            }
            
            $data['form'] = $form;
            $this->render('edit_header.html', $data);
        }
    }
    
    protected function execute_delete_header()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        header::delete($this->request->post('name'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_set_header()
    {
        setting::set('header', 'theme', $this->request->post('name'));
        $this->response->set('{ error:"" }');
    }
    
    protected function execute_add_background()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/add_background.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/add-background');
                return;
            }
            
            background::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/add_background.php');
            }
            
            $data['form'] = $form;
            $this->render('add_background.html', $data);
        }
    }
    
    protected function execute_edit_background($name)
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/edit_background.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/edit-background/' . $name);
                return;
            }
            
            background::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit_background.php');
                $form->name = $name;
            }
            
            $data['form'] = $form;
            $this->render('edit_background.html', $data);
        }
    }
    
    protected function execute_delete_background()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        background::delete($this->request->post('name'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_set_background()
    {
        setting::set('background', 'theme', $this->request->post('name'));
        $this->response->set('{ error:"" }');
    }
    
    protected function execute_add_favicon()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/add_favicon.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/add-favicon');
                return;
            }
            
            favicon::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/add_favicon.php');
            }
            
            $data['form'] = $form;
            $this->render('add_favicon.html', $data);
        }
    }
    
    protected function execute_edit_favicon($name)
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/edit_favicon.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('theme/edit-favicon/' . $name);
                return;
            }
            
            favicon::save($form->name, $this->request->file('file'));
            $this->redirect('theme');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/edit_favicon.php');
                $form->name = $name;
            }
            
            $data['form'] = $form;
            $this->render('edit_favicon.html', $data);
        }
    }
    
    protected function execute_delete_favicon()
    {
        if (!$this->request->is_post())
        {
            $this->response->set('{ error: "bad request" }');
            return;
        }
        
        favicon::delete($this->request->post('name'));
        $this->response->set(json_encode(array('error' => '')));
    }
    
    protected function execute_set_favicon()
    {
        setting::set('favicon', 'theme', $this->request->post('name'));
        $this->response->set('{ error:"" }');
    }
}
