<?php

class FormSettingsController extends MainController implements ControllerInterface {

    public function IndexAction($param) {
        $errors = array();

        if (Request::GetPost('doFormSettingsEdit')) {
            $post = Request::GetAllPost();
            $errors = RegistrationFormSettings::Validate($post);

            if (empty($errors)) {
                RegistrationFormSettings::SaveSettings($post);
                $this->smarty->assign('info', 'Zapisano ustawienia formularza.');
                $this->smarty->assign('type', 'ok');
            } else {
                $this->smarty->assign('info', 'Popraw pola formularza.');
                $this->smarty->assign('type', 'error');
            }
        }

        $settings = RegistrationFormSettings::GetSettings();
        $this->smarty->assign('settings', $settings);
        $this->smarty->assign('errors', $errors);
    }

    public function preDispatch($param) {
        $this->RunShared('Auth', $param);
        $this->Run($param);
        $admin = AuthDAL::GetAdmin();
        $this->user = $admin;

        $this->smarty->assign('titleAdmin', 'Administracja');
        $struct = array(
            'Słowniki' => array('Dictionary' => 'Index'),
            'Zmienne serwisu' => array('Setup' => 'Index'),
            'Ustawienia formularza' => array('FormSettings' => 'Index'),
        );

        $this->smarty->assign('structure', $this->renderStruct($struct));
    }

    private function renderStruct($struct) {
        $return = '';

        foreach ($struct as $k => $row) {
            $return .= '<li><a href="' . Router::GenerateUrl('dictpig', $row) . '">' . $k . '</a></li>';
        }

        $html = '<ul>';
        $html .= $return;
        $html .= '</ul>';

        return $html;
    }

    public function postDispatch($param) {
    }
}

?>
