/** validate and save modified data to database
  *
  * this saves data from both the edit and the edit theme dialog if data validates.
  * If the data does NOT validate, the edit screen is displayed again
  * otherwise the area overview is displayed again.
  *
  * @return void results are returned as output in $this->output
  * @uses $WAS_SCRIPT_NAME
  * @uses $CFG
  * @uses $USER
  */
 function area_save()
 {
     global $CFG, $WAS_SCRIPT_NAME, $USER;
     $area_id = get_parameter_int('area', 0);
     $areas = get_area_records();
     // 0 - basic sanity
     if ($areas === FALSE || !isset($areas[$area_id])) {
         // are they trying to trick us, specifying an invalid area?
         logger("areamanager: weird: user tried to save data to non-existing area '{$area_id}'");
         $this->output->add_message(t('invalid_area', 'admin', array('{AREA}' => strval($area_id))));
         $this->area_overview();
         return;
     }
     // 1 -- are we allowed to perform the edit and thus the save operation?
     if (!$USER->has_area_permissions(PERMISSION_AREA_EDIT_AREA, $area_id)) {
         logger("areamanager: user attempted to save data to area '{$area_id}' without permission");
         $msg = t('icon_area_edit_access_denied', 'admin');
         $this->output->add_message($msg);
         $this->output->add_popup_bottom($msg);
         $this->area_overview();
         return;
     }
     // 2 -- if the user cancelled the operation, there is no point in hanging 'round
     if (isset($_POST['button_cancel'])) {
         $this->output->add_message(t('cancelled', 'admin'));
         $this->area_overview();
         return;
     }
     // 3 -- we need to know which dialog we're dealing with
     if (!isset($_POST['dialog'])) {
         logger("areamanager: weird: 'dialog' not set in area_save() (area='{$area_id}')", WLOG_DEBUG);
         $this->area_overview();
         return;
     }
     $dialog = intval($_POST['dialog']);
     if ($dialog == AREAMANAGER_DIALOG_EDIT_THEME) {
         $theme_id = $areas[$area_id]['theme_id'];
         $themes = $this->get_theme_records();
         $theme_name = $themes[$theme_id]['name'];
         include_once $CFG->progdir . '/lib/configassistant.class.php';
         $table = 'themes_areas_properties';
         $keyfield = 'theme_area_property_id';
         $prefix = '';
         $language_domain = 't_' . $theme_name;
         $where = array('area_id' => $area_id, 'theme_id' => $theme_id);
         $hidden_fields = array(array('type' => F_INTEGER, 'name' => 'dialog', 'value' => AREAMANAGER_DIALOG_EDIT_THEME, 'hidden' => TRUE));
         $assistant = new ConfigAssistant($table, $keyfield, $prefix, $language_domain, $where, $hidden_fields);
         if (!$assistant->save_data($this->output)) {
             $href = href($WAS_SCRIPT_NAME, $this->a_param(AREAMANAGER_CHORE_SAVE, $area_id));
             $assistant->show_dialog($this->output, $href);
             // since they blew it, we will not show the edit menu at this point;
             // user should concentrate on getting input data right (or use cancel)
         } else {
             $this->area_overview();
         }
     } elseif ($dialog == AREAMANAGER_DIALOG_EDIT) {
         $dialogdef = $this->get_dialogdef_edit_area($area_id);
         if (!dialog_validate($dialogdef)) {
             // there were errors, show them to the user and do it again
             foreach ($dialogdef as $k => $item) {
                 if (isset($item['errors']) && $item['errors'] > 0) {
                     $this->output->add_message($item['error_messages']);
                 }
             }
             $this->output->add_content('<h2>' . t('areamanager_edit_area_header', 'admin') . '</h2>');
             $this->output->add_content(t('areamanager_edit_area_explanation', 'admin'));
             $href = href($WAS_SCRIPT_NAME, $this->a_param(AREAMANAGER_CHORE_SAVE, $area_id));
             $this->output->add_content(dialog_quickform($href, $dialogdef));
             // no edit menu, let user concentrate on task at hand ie errorfree data input
             return;
         }
         $now = strftime('%Y-%m-%d %T');
         $fields = array('mtime' => $now, 'muser_id' => $USER->user_id);
         $theme_id = 0;
         foreach ($dialogdef as $k => $item) {
             if (isset($item['name'])) {
                 switch ($item['name']) {
                     case 'area_title':
                         $fields['title'] = $item['value'];
                         break;
                         // This field should not be editable and thus should not be saved
                         //case 'area_is_private':
                         //    $fields['is_private'] = ($item['value'] == 1) ? TRUE : FALSE;
                         //    break;
                     // This field should not be editable and thus should not be saved
                     //case 'area_is_private':
                     //    $fields['is_private'] = ($item['value'] == 1) ? TRUE : FALSE;
                     //    break;
                     case 'area_is_active':
                         $fields['is_active'] = $item['value'] == 1 ? TRUE : FALSE;
                         break;
                     case 'area_theme_id':
                         $theme_id = intval($item['value']);
                         $fields['theme_id'] = $theme_id;
                         break;
                         // This field should not be editable and thus should not be saved
                         //case 'area_path':
                         //    $fields['path'] = $item['value'];
                         //    break;
                     // This field should not be editable and thus should not be saved
                     //case 'area_path':
                     //    $fields['path'] = $item['value'];
                     //    break;
                     case 'area_metadata':
                         $fields['metadata'] = $item['value'];
                         break;
                     case 'area_sort_order':
                         $fields['sort_order'] = intval($item['value']);
                         break;
                     default:
                         break;
                 }
             }
         }
         $where = array('area_id' => $area_id);
         $params = array('{AREA}' => $area_id, '{AREA_FULL_NAME}' => $fields['title']);
         if (db_update('areas', $fields, $where) === FALSE) {
             logger("areamanager: area data save failed for area '{$area_id}': " . db_errormessage());
             $this->output->add_message(t('areamanager_save_area_failure', 'admin', $params));
         } elseif (intval($areas[$area_id]['theme_id']) != $theme_id && $this->count_existing_theme_properties($area_id, $theme_id) <= 0) {
             // If the user changed the theme AND if there is no theme config yet, make sure there is one
             if ($this->reset_theme_defaults($area_id, $theme_id)) {
                 logger("areamanager: success saving area AND theme properties in area '{$area_id}', theme '{$theme_id}'", WLOG_DEBUG);
                 $this->output->add_message(t('areamanager_save_area_success', 'admin', $params));
             } else {
                 logger("areamanager: theme '{$theme_id}' data save failed for area '{$area_id}': " . db_errormessage());
                 $this->output->add_message(t('areamanager_save_area_failure', 'admin', $params));
             }
         } else {
             logger("areamanager: success saving changed properties in area '{$area_id}'", WLOG_DEBUG);
             $this->output->add_message(t('areamanager_save_area_success', 'admin', $params));
         }
         $areas = get_area_records(TRUE);
         // TRUE means force reread of area records
         $this->area_overview();
     } else {
         logger("areamanager: weird: invalid dialog '{$dialog}' in area_save (area={$area_id})", WLOG_DEBUG);
         $this->area_overview();
     }
 }
/** handle the editing/saving of the main configuration information
 *
 * this routine handles editing of the main configuration parameters.
 * It either displays the edit dialog or saves the modified data and
 * shows the configuration manager introduction screen.
 *
 * Note that we do NOT try to redirect the user via a header() after
 * a succesful save. It would be handy because this particular
 * save action may have had impact on the global configuration,
 * which is already read at this point. By redirecting we would
 * make a fresh start, with the new parameters.
 * However, we lose the easy ability to tell the user that the data
 * was saved (via $output->add_message()). So, either no feedback
 * or obsolete global config in core. Hmmmm. I settle for the feedback
 * and the 'wrong' settings.
 *
 * @param object &$output collects the html output
 * @return void results are returned as output in $output
 * @uses ConfigAssistant()
 */
function modulemanager_process(&$output, $task)
{
    global $CFG, $WAS_SCRIPT_NAME;
    // 0 -- sanity check
    $modules = modulemanager_get_modules();
    $module_id = get_parameter_int('module', 0);
    if (!isset($modules[$module_id])) {
        logger(sprintf('%s(): unknown module; id = %d', __FUNCTION__, $module_id));
        $output->add_message(t('error_invalid_parameters', 'admin'));
        modulemanager_show_intro($output);
        modulemanager_show_menu($output);
        return;
    }
    // 1 -- prepare
    include_once $CFG->progdir . '/lib/configassistant.class.php';
    $table = 'modules_properties';
    $keyfield = 'module_property_id';
    $prefix = 'config_';
    $domain = 'm_' . $modules[$module_id]['name'];
    $where = array('module_id' => $module_id);
    $assistant = new ConfigAssistant($table, $keyfield, $prefix, $domain, $where);
    $href = href($WAS_SCRIPT_NAME, array('job' => JOB_MODULEMANAGER, 'task' => TASK_MODULEMANAGER_SAVE, 'module' => $module_id));
    // 2 -- what do we need to do?
    if ($task == TASK_MODULEMANAGER_SAVE) {
        // save data (or cancel if they want to cancel)
        if (isset($_POST['button_save'])) {
            if ($assistant->save_data($output)) {
                modulemanager_show_intro($output);
                modulemanager_show_menu($output, $module_id);
            } else {
                $output->add_content('<h2>' . t($prefix . 'header', $domain) . '</h2>');
                $output->add_content(t($prefix . 'explanation', $domain));
                $assistant->show_dialog($output, $href);
            }
        } else {
            $output->add_message(t('cancelled', 'admin'));
            modulemanager_show_intro($output);
            modulemanager_show_menu($output, $module_id);
        }
    } else {
        // no save yet, simply show dialog
        $output->add_content('<h2>' . t($prefix . 'header', $domain) . '</h2>');
        $output->add_content(t($prefix . 'explanation', $domain));
        $assistant->show_dialog($output, $href);
        modulemanager_show_menu($output, $module_id);
    }
}
/** handle the editing/saving of the main configuration information
 *
 * this routine handles editing of the main configuration parameters.
 * It either displays the edit dialog or saves the modified data and
 * shows the configuration manager introduction screen.
 *
 * Note that we do NOT try to redirect the user via a header() after
 * a succesful save. It would be handy because this particular
 * save action may have had impact on the global configuration,
 * which is already read at this point. By redirecting we would
 * make a fresh start, with the new parameters.
 * However, we lose the easy ability to tell the user that the data
 * was saved (via $output->add_message()). So, either no feedback
 * or obsolete global config in core. Hmmmm. I settle for the feedback
 * and the 'wrong' settings.
 *
 * @param object &$output collects the html output
 * @return void results are returned as output in $output
 * @uses ConfigAssistant()
 */
function process_task_site(&$output)
{
    global $CFG, $WAS_SCRIPT_NAME;
    // 1 -- prepare
    include_once $CFG->progdir . '/lib/configassistant.class.php';
    $table = 'config';
    $keyfield = 'name';
    $prefix = 'site_config_';
    $domain = 'admin';
    $where = '';
    $assistant = new ConfigAssistant($table, $keyfield, $prefix, $domain, $where);
    $href = href($WAS_SCRIPT_NAME, array('job' => JOB_CONFIGURATIONMANAGER, 'task' => TASK_SITE, 'chore' => CHORE_SAVE));
    // 2 -- what do we need to do?
    $chore = get_parameter_string('chore');
    if ($chore == CHORE_SAVE) {
        // save data (or cancel if they want to cancel)
        if (isset($_POST['button_save'])) {
            if ($assistant->save_data($output)) {
                //if (!headers_sent()) {
                //    header('Location: '.href($WAS_SCRIPT_NAME,array('job' => JOB_CONFIGURATIONMANAGER)));
                //    exit;
                //} else {
                show_configuration_intro($output);
                show_configuration_menu($output);
                //}
            } else {
                $output->add_content('<h2>' . t($prefix . 'header', 'admin') . '</h2>');
                $output->add_content(t($prefix . 'explanation', 'admin'));
                $assistant->show_dialog($output, $href);
            }
        } else {
            $output->add_message(t('cancelled', 'admin'));
            show_configuration_intro($output);
            show_configuration_menu($output);
        }
    } else {
        // no save yet, simply show dialog
        $output->add_content('<h2>' . t($prefix . 'header', 'admin') . '</h2>');
        $output->add_content(t($prefix . 'explanation', 'admin'));
        $assistant->show_dialog($output, $href);
        show_configuration_menu($output, TASK_SITE);
    }
}