$del = isset($_POST['del']) ? $_POST['del'] : 0;
$obj = new CPreferences();
$obj->pref_user = isset($_POST['pref_user']) ? $_POST['pref_user'] : 0;
foreach ($_POST['pref_name'] as $name => $value) {
    $obj->pref_name = $name;
    $obj->pref_value = $value;
    // prepare (and translate) the module name ready for the suffix
    $AppUI->setMsg('Preferences');
    if ($del) {
        if ($msg = $obj->delete()) {
            $AppUI->setMsg($msg, UI_MSG_ERROR);
        } else {
            $AppUI->setMsg("deleted", UI_MSG_ALERT, true);
        }
    } else {
        if ($msg = $obj->store()) {
            $AppUI->setMsg($msg, UI_MSG_ERROR);
        } else {
            if ($obj->pref_user) {
                // if user preferences, reload them now
                $AppUI->loadPrefs($AppUI->user_id);
                $AppUI->setUserLocale();
                include_once dPRealPath("./locales/{$AppUI->user_locale}/locales.php");
                include dPRealPath("./locales/core.php");
                $AppUI->setMsg('Preferences');
            }
            $AppUI->setMsg("updated", UI_MSG_OK, true);
        }
    }
}
$AppUI->redirect();
 /**
  * Launches module upgrade process
  *
  * @param string $oldRevision  Revision before upgrade
  * @param bool   $core_upgrade True if it's a core module upgrade
  *
  * @return string|null New revision, null on error
  */
 function upgrade($oldRevision, $core_upgrade = false)
 {
     /*if (array_key_exists($this->mod_version, $this->queries)) {
         CAppUI::setMsg("Latest revision '%s' should not have upgrade queries", UI_MSG_ERROR, $this->mod_version);
         return;
       }*/
     if (!array_key_exists($oldRevision, $this->queries) && !array_key_exists($oldRevision, $this->config_moves) && !array_key_exists($oldRevision, $this->functions)) {
         CAppUI::setMsg("No queries, functions or config moves for '%s' setup at revision '%s'", UI_MSG_WARNING, $this->mod_name, $oldRevision);
         return null;
     }
     // Point to the current revision
     reset($this->revisions);
     while ($oldRevision != ($currRevision = current($this->revisions))) {
         next($this->revisions);
     }
     $depFailed = false;
     do {
         // Check for dependencies
         foreach ($this->dependencies[$currRevision] as $dependency) {
             $module = @CModule::getInstalled($dependency->module);
             if (!$module || $module->mod_version < $dependency->revision) {
                 $depFailed = true;
                 CAppUI::setMsg("Failed module depency for '%s' at revision '%s'", UI_MSG_WARNING, $dependency->module, $dependency->revision);
             }
         }
         if ($depFailed) {
             return $currRevision;
         }
         // Set Time Limit
         if ($this->timeLimit[$currRevision]) {
             CApp::setTimeLimit($this->timeLimit[$currRevision]);
         }
         // Query upgrading
         foreach ($this->queries[$currRevision] as $_query) {
             list($query, $ignore_errors, $dsn) = $_query;
             $ds = $dsn ? CSQLDataSource::get($dsn) : $this->ds;
             if (!$ds->exec($query)) {
                 if ($ignore_errors) {
                     CAppUI::setMsg("Errors ignored for revision '%s'", UI_MSG_OK, $currRevision);
                     continue;
                 }
                 CAppUI::setMsg("Error in queries for revision '%s': see logs", UI_MSG_ERROR, $currRevision);
                 return $currRevision;
             }
         }
         // Callback upgrading
         foreach ($this->functions[$currRevision] as $function) {
             if (!call_user_func($function)) {
                 $function_name = get_class($function[0]) . "->" . $function[1];
                 CAppUI::setMsg("Error in function '%s' call back for revision '%s': see logs", UI_MSG_ERROR, $function_name, $currRevision);
                 return $currRevision;
             }
         }
         // Preferences
         foreach ($this->preferences[$currRevision] as $_pref) {
             list($_name, $_default, $_restricted) = $_pref;
             // Former pure SQL system
             // Cannot check against module version or fresh install will generate errors
             if (self::isOldPrefSystem($core_upgrade)) {
                 $query = "SELECT * FROM `user_preferences` WHERE `pref_user` = '0' AND `pref_name` = '{$_name}'";
                 $result = $this->ds->exec($query);
                 if (!$this->ds->numRows($result)) {
                     $query = "INSERT INTO `user_preferences` (`pref_user` , `pref_name` , `pref_value`)\n              VALUES ('0', '{$_name}', '{$_default}');";
                     $this->ds->exec($query);
                 }
             } else {
                 $pref = new CPreferences();
                 $where = array();
                 $where["user_id"] = " IS NULL";
                 $where["key"] = " = '{$_name}'";
                 if (!$pref->loadObject($where)) {
                     $pref->key = $_name;
                     $pref->value = $_default;
                     $pref->restricted = $_restricted ? "1" : "0";
                     $pref->store();
                 }
             }
         }
         // Config moves
         if (count($this->config_moves[$currRevision])) {
             foreach ($this->config_moves[$currRevision] as $config) {
                 CAppUI::setConf($config[1], CAppUI::conf($config[0]));
             }
         }
     } while ($currRevision = next($this->revisions));
     return $this->mod_version;
 }
 */
$prefs = CValue::post("pref", array());
$user_id = CValue::post("user_id");
$restricted = CValue::post("restricted");
$pref = new CPreferences();
$ds = $pref->getDS();
// @todo: voir à utiliser CDoObjectAddEdit
foreach ($prefs as $key => $value) {
    $pref = new CPreferences();
    $where = array("user_id" => $user_id ? $ds->prepare("= '{$user_id}'") : "IS NULL", "key" => $ds->prepare("= '{$key}'"));
    $pref->loadObject($where);
    $pref->user_id = $user_id;
    $pref->key = $key;
    $pref->value = stripslashes($value);
    $pref->restricted = $restricted ? "1" : "0";
    if ($msg = $pref->store()) {
        CAppUI::setMsg($msg, UI_MSG_ERROR);
    } else {
        CAppUI::setMsg("CPreferences-msg-modify", UI_MSG_OK);
    }
}
// Reload user preferences
if ($pref->user_id) {
    CAppUI::buildPrefs();
}
if ($redirect = CValue::post("postRedirect")) {
    echo $redirect;
    CAppUI::redirect($redirect);
} else {
    echo CAppUI::getMsg();
    CApp::rip();