function xanth_view_mode_admin_view_mode_edit($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage view_mode')) {
        return xSpecialPage::access_denied();
    }
    if (empty($arguments[0])) {
        new xPageContent('Edit View Mode', 'Wich view mode should edit?');
    }
    $form = new xForm('?p=admin/view_mode/edit//' . $arguments[0]);
    $view_mode = xViewMode::get($arguments[0]);
    $form->elements[] = new xFormElementHidden('view_mode_id', 'View Mode', $arguments[0], FALSE, new xInputValidatorInteger());
    $form->elements[] = new xFormElementTextField('view_mode_name', 'Name', '', $view_mode->name, FALSE, new xInputValidatorTextNoTags(32));
    //view elements option
    $velems = xVisualElement::find_all();
    $voptions = array();
    foreach ($velems as $velem) {
        $voptions[$velem->name] = $velem->name;
    }
    $form->elements[] = new xFormElementTextArea('view_mode_procedure', 'Procedure', '', $view_mode->display_procedure, FALSE, new xInputValidatorText(NULL));
    $form->elements[] = new xFormSubmit('submit', 'submit');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            $view_mode->name = $ret->valid_data['view_mode_name'];
            $view_mode->display_procedure = $ret->valid_data['view_mode_procedure'];
            $view_mode->update();
            return new xPageContent('Edit View Mode', 'View mode modified');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Edit View Mode', $form->render());
}
function xanth_theme_admin_theme_edit($hook_primary_id, $hook_secondary_id, $arguments)
{
    if (!xUser::check_current_user_access('manage theme')) {
        return xSpecialPage::access_denied();
    }
    if (empty($arguments[0])) {
        new xPageContent('Edit Theme', 'Wich theme should edit?');
    }
    $form = new xForm('?p=admin/theme/edit//' . $arguments[0]);
    $form->elements[] = new xFormElementHidden('theme_name', 'Theme Name', $arguments[0], FALSE, new xInputValidatorTextNameId(32));
    //iterate every registered visual element
    $edit_theme = xTheme::get($arguments[0]);
    $visual_elements = xVisualElement::find_all();
    foreach ($visual_elements as $visual_element) {
        //now iterate every registered view mode
        $view_modes = xViewMode::find_by_element($visual_element->name);
        $options = array();
        $default = '';
        foreach ($view_modes as $view_mode) {
            $options[$view_mode->name] = $view_mode->id;
            if (isset($edit_theme->themed_elements[$visual_element->name])) {
                $default = $edit_theme->themed_elements[$visual_element->name];
            } elseif ($view_mode->default_for_element) {
                $default = $view_mode->id;
            }
        }
        $form->elements[] = new xFormElementHidden('visual_elements', 'Visual elements', $visual_element->name, TRUE, new xInputValidatorTextNameId(32));
        $form->elements[] = new xFormElementOptions('view_mode_' . $visual_element->name, 'View Mode for v.e. ' . $visual_element->name, '', $default, $options, FALSE, FALSE, new xInputValidatorInteger());
    }
    $form->elements[] = new xFormSubmit('submit', 'submit');
    $ret = $form->validate_input();
    if (isset($ret->valid_data['submit'])) {
        if (empty($ret->errors)) {
            //process form
            $themed_elements = array();
            foreach ($ret->valid_data['visual_elements'] as $vename) {
                $themed_elements[$vename] = $ret->valid_data['view_mode_' . $vename];
            }
            $theme = new xTheme($ret->valid_data['theme_name'], $themed_elements);
            $theme->update();
            return new xPageContent('Edit Theme', 'Theme Edited');
        } else {
            foreach ($ret->errors as $error) {
                xanth_log(LOG_LEVEL_USER_MESSAGE, $error);
            }
        }
    }
    return new xPageContent('Edit Theme', $form->render());
}