Ignore:
Timestamp:
Nov 10, 2009, 4:04:07 PM (14 years ago)
Author:
LucMorizur
Message:

[Event Cats] Improve configuration parameters management

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/event_cats/include/ec_conf.inc.php

    r4233 r4239  
    2323// +-----------------------------------------------------------------------+
    2424
    25 /*****************************************************************************
    26 Parameters ($ec_conf) :
    27  _ plugin activated ($ec_conf[0] <=> read_ec_conf('activated')) yes/no ;
    28  _ duplication allowance ($ec_conf[1] <=> read_ec_conf('dup_allow')) :
    29    0 : no account ;
    30    1 : all accounts ;
    31    2 : no account but those specified ;    \
    32    3 : all accounts but those specified ;  | could be different finally
    33  _ the number of digits when creating a new code automatically
    34    ($ec_conf[2] <=> read_ec_conf('auto_code_dg_nb')) ;
    35  _ the way the "register" link is displayed, together with the original $lang
    36    value for this label ( $ec_conf[3] <=> read_ec_conf('') ) ;
    37  _ whether the "how to" banner has already been displayed or not
    38    ($ec_conf[4] <=> read_ec_conf('howto')) ;
    39  _ what to do in case an unknown code is used with "autolog" parameter
    40    ($ec_conf[5] <=> read_ec_conf('unknown_code')) :
    41    0 : nothing ("goto" home page, index.php (not logged in of course)) ;
    42    1 : redirected to "access denied" page ;
    43    2 : redirected to an Additional Page precised below.
    44  _ the id of the Additional Page which must be used in case of usage of an
    45    unknow code ($ec_conf[6] <=> read_ec_conf('unknown_code_ap_id')).
    46 *****************************************************************************/
     25global $ec_conf, $page, $ec_conf_index;
    4726
    48 global $ec_conf, $page;
     27// $ec_conf initalization
    4928
    50 // $ec_conf initalization / check
    51 if ($t = mysql_fetch_row(pwg_query("
    52  SELECT `value`
    53  FROM `".CONFIG_TABLE."`
    54  WHERE `param` = 'event_cats';
    55 "))) $ec_conf = explode(',' , $t[0]);
    56 else {
    57   $ec_conf = explode(',' , '0,1,10,0,0,0,0');
    58   if (!change_ec_conf('activated', 0))
    59    $page['errors'][] = 'Error updating $ec_conf';
     29$ec_conf_default = array( // PLUGIN CONFIGURATION ($ec_conf) :
     30                          // ---------------------------------
     31  'activated'             // plugin activated
     32    => '0',               //
     33                          //
     34  'dup_allow'             // duplication allowance :
     35    => '0',               //   0 : no account ;
     36                          //   1 : all accounts ;
     37                          //   2 : specified by group and user ids ;
     38                          //
     39  'auto_code_dg_nb'       // number of digits when creating a new code
     40    => '10',              // automatically
     41                          //
     42  'reg_display'           // the way the "register" link is displayed,
     43    => '0',               // together with the original $lang value for this
     44                          // label
     45                          //
     46  'howto'                 // whether the small aknowledgement message has
     47    => '0',               // already been displayed
     48                          //
     49  'unknown_code'          // what to do in case an unknown code is used with
     50    => '1',               // "autolog" parameter :
     51                          //   0 : nothing (display home page, index.php (not
     52                          //       logged in of course)) ;
     53                          //   1 : display "access denied" page ;
     54                          //   2 : display an Additional Page precised below.
     55                          //
     56  'unknown_code_ap_id'    // the id of the Additional Page which must be used
     57    => '0',               // in case of usage of an unknow code.
     58);
     59
     60$ec_conf_index = array_flip(array_keys($ec_conf_default));
     61
     62$t = array(); $u = '0'; $v = false;
     63if ($v = (
     64  ($t = mysql_fetch_row(pwg_query("
     65    SELECT `value`
     66    FROM `".CONFIG_TABLE."`
     67    WHERE `param` = 'event_cats';
     68  "))) !== false)
     69) {
     70  $v = (count($ec_conf = explode(',', $t[0])) == count($ec_conf_index));
     71  $u = $ec_conf[0];
    6072}
     73// $v is false
     74//   _ if the entry in the table doesn't exist ;
     75//   _ if the entry in the table exists, but its number of parameters is
     76//     different than the number of default parameters.
     77// $u is '0' if the plugin has not yet been activated, '1' if it has been.
     78if (!$v) {
     79  $ec_conf = array_values($ec_conf_default);
     80  change_ec_conf('activated', $u);
     81}
     82// now :
     83//   _ the number of parameters is the same in the DB as the number of
     84//     default parameters ;
     85//   _ the parameters have the default values if needed ;
     86//   _ the plugin shows it is active if it is the case.
     87unset($t, $u, $v);
    6188
    6289/*
     
    7299 */
    73100function change_ec_conf($c, $v) {
    74   global $ec_conf, $page;
    75   switch ($c) {
    76     case 'activated':
    77       $ec_conf[0] = $v;
    78     break;
    79     case 'dup_allow':
    80       $ec_conf[1] = $v;
    81     break;
    82     case 'auto_code_dg_nb':
    83       $ec_conf[2] = $v;
    84     break;
    85     case 'reg_display':
    86       $ec_conf[3] = $v;
    87     break;
    88     case 'howto':
    89       $ec_conf[4] = $v;
    90     break;
    91     case 'unknown_code':
    92       $ec_conf[5] = $v;
    93     break;
    94     case 'unknown_code_ap_id':
    95       $ec_conf[6] = $v;
    96     break;
    97     default:
    98       $page['errors'][] = 'Bad change_ec_conf argument';
     101  global $ec_conf, $page, $ec_conf_index;
     102  if (array_key_exists($c, $ec_conf_index)) {
     103    $ec_conf[$ec_conf_index[$c]] = $v;
     104    return (pwg_query("
     105      UPDATE `".CONFIG_TABLE."`
     106      SET `value` = \"".implode(',', $ec_conf)."\"
     107      WHERE `param` = 'event_cats';
     108    ") !== false);
     109  }
     110  else {
     111    $page['errors'][] = 'Bad change_ec_conf argument';
    99112    return false;
    100113  }
    101   return (pwg_query("
    102    UPDATE `".CONFIG_TABLE."`
    103    SET `value` = \"".implode(',', $ec_conf)."\"
    104    WHERE `param` = 'event_cats';
    105   ") !== false);
    106114}
    107115
     
    116124 */
    117125function read_ec_conf($c) {
    118   global $ec_conf, $page;
    119   switch ($c) {
    120     case 'activated'         : return $ec_conf[0];
    121     case 'dup_allow'         : return $ec_conf[1];
    122     case 'auto_code_dg_nb'   : return $ec_conf[2];
    123     case 'reg_display'       : return $ec_conf[3];
    124     case 'howto'             : return $ec_conf[4];
    125     case 'unknown_code'      : return $ec_conf[5];
    126     case 'unknown_code_ap_id': return $ec_conf[6];
    127     default:
    128       $page['errors'][] = 'Bad read_ec_conf argument';
     126  global $ec_conf, $page, $ec_conf_index;
     127  if (array_key_exists($c, $ec_conf_index))
     128   return $ec_conf[$ec_conf_index[$c]];
     129  else {
     130    $page['errors'][] = 'Bad read_ec_conf argument';
    129131    return false;
    130132  }
Note: See TracChangeset for help on using the changeset viewer.