Ignore:
Timestamp:
May 29, 2014, 4:11:14 PM (10 years ago)
Author:
mistic100
Message:

feature 3038 : add $updateGlobal and $parser options to conf_update_param

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r28432 r28567  
    11631163 * @param string $param
    11641164 * @param string $value
    1165  */
    1166 function conf_update_param($param, $value)
    1167 {
     1165 * @param boolean $updateGlobal update global *$conf* variable
     1166 * @param callable $parser function to apply to the value before save in database
     1167      (eg: serialize, json_encode) will not be applied to *$conf* if *$parser* is *true*
     1168 */
     1169function conf_update_param($param, $value, $updateGlobal=false, $parser=null)
     1170{
     1171  if ($parser != null)
     1172  {
     1173    $dbValue = call_user_func($parser, $value);
     1174  }
     1175  else
     1176  {
     1177    $dbValue = $value;
     1178  }
     1179 
    11681180  $query = '
    1169 SELECT param
    1170   FROM '.CONFIG_TABLE.'
    1171   WHERE param = \''.$param.'\'
     1181INSERT INTO
     1182  '.CONFIG_TABLE.' (param, value)
     1183  VALUES(\''.$param.'\', \''.$dbValue.'\')
     1184  ON DUPLICATE KEY UPDATE value = \''.$dbValue.'\'
    11721185;';
    1173   $params = query2array($query, null, 'param');
    1174 
    1175   if (count($params) == 0)
    1176   {
    1177     $query = '
    1178 INSERT
    1179   INTO '.CONFIG_TABLE.'
    1180   (param, value)
    1181   VALUES(\''.$param.'\', \''.$value.'\')
    1182 ;';
    1183     pwg_query($query);
    1184   }
    1185   else
    1186   {
    1187     $query = '
    1188 UPDATE '.CONFIG_TABLE.'
    1189   SET value = \''.$value.'\'
    1190   WHERE param = \''.$param.'\'
    1191 ;';
    1192     pwg_query($query);
     1186 
     1187  pwg_query($query);
     1188 
     1189  if ($updateGlobal)
     1190  {
     1191    global $conf;
     1192    $conf[$param] = $value;
    11931193  }
    11941194}
     
    12231223    unset($conf[$param]);
    12241224  }
     1225}
     1226
     1227/**
     1228 * Apply *unserialize* on a value only if it is a string
     1229 * @since 2.7
     1230 *
     1231 * @param array|string $value
     1232 * @return array
     1233 */
     1234function safe_unserialize($value)
     1235{
     1236  if (is_string($value))
     1237  {
     1238    return unserialize($value);
     1239  }
     1240  return $value;
     1241}
     1242
     1243/**
     1244 * Apply *json_decode* on a value only if it is a string
     1245 * @since 2.7
     1246 *
     1247 * @param array|string $value
     1248 * @return array
     1249 */
     1250function safe_json_decode($value)
     1251{
     1252  if (is_string($value))
     1253  {
     1254    return json_decode($value, true);
     1255  }
     1256  return $value;
    12251257}
    12261258
Note: See TracChangeset for help on using the changeset viewer.