Ignore:
Timestamp:
Oct 4, 2010, 1:38:27 AM (14 years ago)
Author:
LucMorizur
Message:

At last a configuration management working as desired

File:
1 edited

Legend:

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

    r7068 r7084  
    3333// ---------------------------------------------------------------------------
    3434
     35// ----- Introduction
     36// Not sure I'm very proud of this quite complex configuration initialization
     37// part :-/ ...
     38// The reason of the complexity is the inital intention of building a
     39// configuration management allowing to use in the rest of the script only
     40// two functions: change_ec_conf() and read_ec_conf() .
     41// change_ec_conf() updates the DB systematically. The initialization part
     42// has the only function of determining what is the value of $ec_conf, which
     43// is the root configuration parameters provider.
     44// -----
     45
    3546// $ec_conf has numeric indexes (instead of string keys like 'activated',
    3647// 'howto'...) because of its first reading, done with the "explode" function.
     
    5263
    5364//----------------------------------------------------------------------------
    54 // The following lines allow to change the number of configuration items, the
    55 // default values they can have, and also their possible other values, very
    56 // quickly.
    5765
    5866// Give possible $ec_conf values. In case values cannot be predicted, just
     
    6068$ec_conf_possible_values = array(
    6169  'activated'           => array('0', '1'),                           //  0
     70    // plugin activated
    6271  'howto'               => array('0', '1'),                           //  1
     72    // whether the small aknowledgement message has already been displayed
    6373  'dup_allow'           => array('0', '1', '2'),                      //  2
     74    // duplication allowance :
     75    //   0 : no account;
     76    //   1 : all accounts;
     77    //   2 : specified by group, type, and user ids (default).
     78    // If Event Cats is in "simple mode", get the value of
     79    // $conf['ec_dup_allow'] if it is set; and an allowance to duplicate only
     80    // generic accounts will be defined if the value is 2.
    6481  'duplic_display'      => array('0', '1', '2'),                      //  3
     82    // duplication link display in the menubar :
     83    //   0 : "Register" for everybody;
     84    //   1 : "Duplicate" for everybody;
     85    //   2 : "Register" only for generic accounts,
     86    //       "Duplicate" for all other accounts (default);
     87    // If Event Cats is in "simple mode", takes the value of
     88    // $conf['ec_duplic_display'] if it's set.
    6589  'auto_code_dg_nb'     => array( '4',  '5',  '8', '10', '12', '15',
    6690                                 '16', '20', '24', '25', '30', '32'), //  4
     91    // number of digits when creating a new code automatically, several values
     92    // (default: 10)
     93    // If Event Cats is in "simple mode", get the value of
     94    // $conf['ec_auto_code_dg_nb'] if set.
    6795  'unknown_code'        => array('0', '1', '2'),                      //  5
     96    // what to do in case an unknown code is used with "autolog" parameter :
     97    //   0 : nothing (display home page, index.php (not logged in of course));
     98    //   1 : display "access denied" page (default);
     99    //   2 : display an A. P. precised below.
    68100  'unknown_code_ap_id'  => array(),                                   //  6
     101    // the id of the Additional Page which must be used in case of usage of an
     102    // unknow code; several vals (default: 0).
    69103  'comment_display'     => array('0', '1'),                           //  7
     104    // the way the comment is displayed in "entries" tab:
     105    // 0, as tooltip; 1 (default), on a line under code
     106    // If Event Cats is in "simple mode", get the value of
     107    // $conf['ec_comment_display'] if set.
    70108  'in_help_display'     => array('0', '1'),                           //  8
     109    // whether the help banner is displayed (default: 1).
    71110  'display_connection'  => array('0', '1'),                           //  9
     111    // whether the "Connection" link must be displayed for generic users
     112    // (default: 1)
     113    // If Event Cats is in "simple mode", takes the value of
     114    // $conf['ec_display_connection'] if set.
    72115  'simple_mode_on'      => array('0', '1'),                           // 10
     116    // whether the presentation of the plugin is in "simple mode" or not.
     117    // (default: 1)
     118    // The default value can be modified to 0 (normal view) if Event Cats was
     119    // already installed previously.
    73120  'display_full_button' => array('0', '1'),                           // 11
     121    // whether the "Show full features" button must be displayed (default: 1).
     122    // The default value can be modified to 0 (don't display) if
     123    // $conf['ec_display_full_button'] is set, and false.
    74124);
    75125
     
    81131$t = array(); $u = '0'; $v = false; $w = false; $ec_conf = array(); $s = '0';
    82132if ($w = ( // $w becomes true if the query succeeds <=> if EC was already in
    83   // $t gets event_cats config value, as an array with one unique entry
     133  // $t gets event_cats config value (array to be "exploded")
    84134  ($t = pwg_db_fetch_row(pwg_query("
    85135    SELECT `value`
     
    89139) {
    90140  $v = (count($ec_conf = explode(',', $t[0])) == count($ec_conf_index));
    91   $u = $ec_conf[0];
    92   $s = ($v) ? $ec_conf[1] : '0'; // the aknowledgement message is displayed
    93   // again if the configuration changes
     141  $u = $ec_conf[$ec_conf_index['activated']];
     142  $s = ($v) ? $ec_conf[$ec_conf_index['howto']] : '0'; // the aknowledgement
     143  // message is displayed again if the configuration changes
    94144}
    95145if ($v and !$w) die('$v true with $w false in ec_conf.inc.php');
     
    97147// $v is false if the entry in the table doesn't exist; or exists, but its
    98148// number of parameters is different than the number of default parameters.
    99 // $u is '0' if the plugin has not yet been activated, '1' if it has been.
     149// $u is '0' if the plugin is not activated, '1' if it is.
    100150// $s allows to display the aknowledgement message when config changes.
    101151// If $v or $w are false, all $ec_conf values are set to default values. But
    102152// default values must first take values specified in $conf, if they are set.
    103153
    104 // Determine whether simple mode is ON or OFF
     154// Determine whether simple mode is ON or OFF: simple mode must be on if Event
     155// Cats is just installed, or if it was ON previously, or if
     156// $conf['ec_display_full_button'] is set and is false.
    105157$simple_mode_on = (
    106158  (!$v and !$w) or (
    107     // $ec_conf[10] <=> read_ec_conf('simple_mode_on')
    108     isset($ec_conf[10]) and $ec_conf[10] == '1'
     159    isset($ec_conf[$ec_conf_index['simple_mode_on']]) and
     160    $ec_conf[$ec_conf_index['simple_mode_on']] == '1'
    109161  ) or (
    110162    isset($conf['ec_display_full_button']) and
     
    113165);
    114166
    115 if (!$v) {
    116   // Give configuration items names and default values. Only strings.
    117   $ec_conf_default = array( // PLUGIN CONFIGURATION ($ec_conf) :
    118                             // ---------------------------------
    119     'activated'       //  0 // plugin activated, 0 or 1
    120       => $u,                //
    121                             //
    122     'howto'           //  1 // whether the small aknowledgement message has
    123       => $s,                // already been displayed, 0 or 1
    124                             //
    125     'dup_allow' => ($simple_mode_on and isset($conf['ec_dup_allow']) and
    126                     in_array($conf['ec_dup_allow'],
    127                     $ec_conf_possible_values['ec_dup_allow'])) ?
    128       $conf['ec_dup_allow'] : (isset($ec_conf[2]) and in_array($ec_conf[2],
    129       $ec_conf_possible_values['dup_allow'])) ? $ec_conf[2] : '2',
    130                       //  2 // duplication allowance :
    131                             //   0 : no account;
    132                             //   1 : all accounts;
    133                             //   2 : specified by group, type, and user ids.
    134                             // If Event Cats is in "simple mode", takes the
    135                             // value of $conf['ec_dup_allow'] if it is set;
    136                             // and an allowance to duplicate only generic
    137                             // accounts will be defined.
    138                             //
    139     'duplic_display' => ($simple_mode_on and isset($conf['ec_duplic_display'])
    140                     and in_array($conf['ec_duplic_display'],
    141                     $ec_conf_possible_values['ec_duplic_display'])) ?
    142       $conf['ec_duplic_display'] : (isset($ec_conf[3]) and
    143       in_array($ec_conf[3], $ec_conf_possible_values['duplic_display'])) ?
    144       $ec_conf[3] : '2',
    145                       //  3 // duplication link display in the menubar :
    146                             //   0 : "Register" for everybody;
    147                             //   1 : "Duplicate" for everybody;
    148                             //   2 : "Register" only for generic accounts,
    149                             //       "Duplicate" for all other accounts;
    150                             // If Event Cats is in "simple mode", takes the
    151                             // value of $conf['ec_duplic_display'] if it's set.
    152                             //
    153   'auto_code_dg_nb' => ($simple_mode_on and isset($conf['ec_auto_code_dg_nb'])
    154                     and in_array($conf['ec_auto_code_dg_nb'],
    155                     $ec_conf_possible_values['ec_auto_code_dg_nb'])) ?
    156       $conf['ec_auto_code_dg_nb'] : (isset($ec_conf[4]) and
    157       in_array($ec_conf[4], $ec_conf_possible_values['auto_code_dg_nb'])) ?
    158       $ec_conf[4] : '10',
    159                       //  4 // number of digits when creating a new code
    160                             // automatically, several values
    161                             // If Event Cats is in "simple mode", takes the
    162                             // value of $conf['ec_auto_code_dg_nb'] if set.
    163                             //
    164     'unknown_code'    //  5 // what to do in case an unknown code is used with
    165       => (isset($ec_conf[5]) and in_array($ec_conf[5],
    166       $ec_conf_possible_values['unknown_code'])) ? $ec_conf[5] : '1',
    167                             // "autolog" parameter :
    168                             //   0 : nothing (display home page, index.php
    169                             //       (not logged in of course));
    170                             //   1 : display "access denied" page;
    171                             //   2 : display an A. P. precised below.
    172                             //
    173     'unknown_code_ap_id' // 6| the id of the Additional Page which must be
    174       => (isset($ec_conf[6])) ? $ec_conf[6] : '0',
    175                             // used in case of usage of an unknow code;
    176                             // several vals.
    177                             //
    178     'comment_display' => ($simple_mode_on and isset($conf['ec_comment_display'])
    179                     and in_array($conf['ec_comment_display'],
    180                     $ec_conf_possible_values['ec_comment_display'])) ?
    181       $conf['ec_comment_display'] : (isset($ec_conf[7]) and
    182       in_array($ec_conf[7], $ec_conf_possible_values['comment_display'])) ?
    183       $ec_conf[7] : '1',
    184                       //  7 // the way the comment is displayed in "entries"
    185                             // tab : 0, as tooltip; 1, on a line under code
    186                             // If Event Cats is in "simple mode", takes the
    187                             // value of $conf['ec_comment_display'] if set.
    188                             //
    189     'in_help_display' //  8 // whether the help banner is displayed.
    190       => (isset($ec_conf[8]) and in_array($ec_conf[8],
    191       $ec_conf_possible_values['in_help_display'])) ? $ec_conf[8] : '1',
    192                             // 0: not displayed; 1 : displayed (surprising !)
    193                             //
    194     'display_connection' => ($simple_mode_on and
    195                     isset($conf['ec_display_connection']) and
    196                     in_array($conf['ec_display_connection'],
    197                     $ec_conf_possible_values['ec_display_connection'])) ?
    198       $conf['ec_display_connection'] : (isset($ec_conf[9]) and
    199       in_array($ec_conf[9], $ec_conf_possible_values['display_connection'])) ?
    200       $ec_conf[9] : '1',
    201                       //  9 // whether the "Connection" link must be displayed
    202                             // for generic users, 0 or 1.
    203                             // If Event Cats is in "simple mode", takes the
    204                             // value of $conf['ec_display_connection'] if set.
    205                             //
    206     'simple_mode_on' => ($simple_mode_on) ?
    207       '1' : '0',      // 10 // whether the presentation of the plugin is in
    208                             // "simple mode" or not.
    209                             // 0: normal view; 1 : simple view.
    210                             // The default value can be modified to 0 (normal
    211                             // view) if Event Cats was already installed
    212                             // previously.
    213                             //
    214     'display_full_button' => ($simple_mode_on and
    215                     isset($conf['ec_display_full_button']) and
    216                     in_array($conf['ec_display_full_button'],
    217                     $ec_conf_possible_values['ec_display_full_button'])) ?
    218       $conf['ec_display_full_button'] : (isset($ec_conf[11]) and
    219     in_array($ec_conf[11], $ec_conf_possible_values['display_full_button'])) ?
    220        $ec_conf[11] : '1',
    221                       // 11 // whether the "Show full features" button must be
    222                             // displayed, 0 or 1. The default value can be
    223                             // modified to 0 (don't display) if
    224                             // $conf['ec_display_full_button'] has val. false.
    225   );
     167// Change $ec_conf values if needed
     168if (!$v or $simple_mode_on) {
    226169 
    227 // Once these lines have been updated, you don't need to change anything
    228 // else. Everything is managed by the code.
    229 //----------------------------------------------------------------------------
    230 
    231   // Set $ec_conf to proper default values if needed, and writes its values
    232   // in the DB
    233   $ec_conf = array_values($ec_conf_default);
    234 
    235   change_ec_conf('activated', $u); // writes in the DB
     170  // Perform repetitve work
     171  function test_conf($tbl, $k, $k2 = NULL) {
     172  global $ec_conf_possible_values;
     173    if (!isset($k2)) $k2 = $k;
     174    return (isset($tbl[$k]) and
     175     in_array($tbl[$k], $ec_conf_possible_values[$k2]));
     176  }
     177 
     178  // Avoid that $conf (very big) is passed as argument to function test_conf
     179  $conf_temp = array();
     180  foreach ($ec_conf_possible_values as $key => $val)
     181   if (isset($conf['ec_'.$key])) $conf_temp[$key] = $conf['ec_'.$key];
     182 
     183  if (!$v) {
     184    // Determine default values of $ec_conf
     185    $ec_conf = array(
     186      $ec_conf_index['activated'] => $u,
     187      $ec_conf_index['howto'] => $s,
     188      $ec_conf_index['dup_allow'] =>
     189        ($simple_mode_on and test_conf($conf_temp, 'dup_allow')) ?
     190          $conf_temp['dup_allow']
     191          : (test_conf($ec_conf, $ec_conf_index['dup_allow'], 'dup_allow')) ?
     192            $ec_conf[$ec_conf_index['dup_allow']]
     193            : '2',
     194      $ec_conf_index['duplic_display'] =>
     195        ($simple_mode_on and test_conf($conf_temp, 'duplic_display')) ?
     196          $conf_temp['duplic_display']
     197          : (test_conf($ec_conf, $ec_conf_index['duplic_display'],
     198             'duplic_display')) ?
     199            $ec_conf[$ec_conf_index['duplic_display']]
     200            : '2',
     201      $ec_conf_index['auto_code_dg_nb'] =>
     202        ($simple_mode_on and test_conf($conf_temp, 'auto_code_dg_nb')) ?
     203          $conf_temp['auto_code_dg_nb']
     204          : (test_conf($ec_conf, $ec_conf_index['auto_code_dg_nb'],
     205             'auto_code_dg_nb')) ?
     206            $ec_conf[$ec_conf_index['auto_code_dg_nb']]
     207            : '10',
     208      $ec_conf_index['unknown_code'] =>
     209        (test_conf($ec_conf, $ec_conf_index['unknown_code'],
     210         'unknown_code')) ?
     211          $ec_conf[$ec_conf_index['unknown_code']]
     212          : '1',
     213      $ec_conf_index['unknown_code_ap_id'] =>
     214        (isset($ec_conf[$ec_conf_index['unknown_code_ap_id']])) ?
     215          $ec_conf[$ec_conf_index['unknown_code_ap_id']]
     216          : '0',
     217      $ec_conf_index['comment_display'] =>
     218        ($simple_mode_on and test_conf($conf_temp, 'comment_display')) ?
     219          $conf_temp['comment_display']
     220          : (test_conf($ec_conf, $ec_conf_index['comment_display'],
     221             'comment_display')) ?
     222            $ec_conf[$ec_conf_index['comment_display']]
     223            : '1',
     224      $ec_conf_index['in_help_display'] =>
     225        (test_conf($ec_conf, $ec_conf_index['in_help_display'],
     226         'in_help_display')) ?
     227            $ec_conf[$ec_conf_index['in_help_display']]
     228            : '1',
     229      $ec_conf_index['display_connection'] =>
     230        ($simple_mode_on and test_conf($conf_temp, 'display_connection')) ?
     231          $conf_temp['display_connection']
     232          : (test_conf($ec_conf, $ec_conf_index['display_connection'],
     233             'display_connection')) ?
     234            $ec_conf[$ec_conf_index['display_connection']]
     235            : '1',
     236      $ec_conf_index['simple_mode_on'] =>
     237        ($simple_mode_on) ? '1' : '0',
     238      $ec_conf_index['display_full_button'] =>
     239        ($simple_mode_on and test_conf($conf_temp, 'display_full_button')) ?
     240          $conf_temp['display_full_button']
     241          : (test_conf($ec_conf, $ec_conf_index['display_full_button'],
     242             'display_full_button')) ?
     243            $ec_conf[$ec_conf_index['display_full_button']]
     244            : '1',
     245    );
     246    change_ec_conf('activated', $u); // write in the DB
     247  }
     248  else { // $simple_mode_on is true ; check $conf values for Event Cats
     249    foreach ($ec_conf_possible_values as $key => $val)
     250     if (test_conf($conf_temp, $key))
     251      change_ec_conf($key, $conf_temp[$key]);
     252  }
     253  unset($conf_temp);
    236254}
    237255// now :
    238256//   _ the number of parameters is the same in the DB as the number of
    239 //     default parameters;
     257//     possible parameters;
    240258//   _ the parameters have the default values if needed;
    241259//   _ the plugin shows it is active if it is the case;
Note: See TracChangeset for help on using the changeset viewer.