source: extensions/event_cats/include/ec_conf.inc.php @ 4329

Last change on this file since 4329 was 4329, checked in by LucMorizur, 15 years ago

[Event Cats] Finish duplication management, configuration management, code cleaning

File size: 6.8 KB
Line 
1<?php
2
3// +-----------------------------------------------------------------------+
4// | Piwigo - a PHP based picture gallery                                  |
5// +-----------------------------------------------------------------------+
6// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
7// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
8// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
9// +-----------------------------------------------------------------------+
10// | This program is free software; you can redistribute it and/or modify  |
11// | it under the terms of the GNU General Public License as published by  |
12// | the Free Software Foundation                                          |
13// |                                                                       |
14// | This program is distributed in the hope that it will be useful, but   |
15// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
16// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
17// | General Public License for more details.                              |
18// |                                                                       |
19// | You should have received a copy of the GNU General Public License     |
20// | along with this program; if not, write to the Free Software           |
21// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
22// | USA.                                                                  |
23// +-----------------------------------------------------------------------+
24
25global $ec_conf, $page, $ec_conf_index, $ec_conf_default,
26       $ec_conf_possible_values;
27
28// $ec_conf initalization
29
30// $ec_conf has numeric indexes (instead of string keys like 'activated',
31// 'howto'...) because of its first reading, done with the "explode" function.
32// It would be  very easy to build a string keys indexed array, thanks to
33// following instructions :
34/*
35$t = mysql_fetch_row(pwg_query("
36  SELECT `value`
37  FROM `".CONFIG_TABLE."`
38  WHERE `param` = 'event_cats';
39"));
40$ec_conf_temp = explode(',', $t[0]);
41$ec_conf = array(); $i = 0;
42foreach ($ec_conf_default as $k => $v) $ec_conf[$k] = ec_conf_temp[$i++];
43*/
44// Though, I prefer to keep $ec_conf with numeric indexes, so to be compelled
45// to go through function change_ec_conf() to modify ec_conf, and thus update
46// the DB at the same time.
47
48$ec_conf_default = array( // PLUGIN CONFIGURATION ($ec_conf) :
49                          // ---------------------------------
50  'activated'             // plugin activated, 0 or 1
51    => '0',               //
52                          //
53  'howto'                 // whether the small aknowledgement message has
54    => '0',               // already been displayed, 0 or 1
55                          //
56  'dup_allow'             // duplication allowance :
57    => '2',               //   0 : no account ;
58                          //   1 : all accounts ;
59                          //   2 : specified by group and user ids ;
60                          //
61  'duplic_display'        // duplication link display in the menubar :
62    => '2',               //   0 : "Register" for everybody ;
63                          //   1 : "Duplicate" for everybody ;
64                          //   2 : "Register" only for generic accounts,
65                          //       "Duplicate" for all other accounts ;
66                          //
67  'auto_code_dg_nb'       // number of digits when creating a new code
68    => '10',              // automatically, several values
69                          //
70  'unknown_code'          // what to do in case an unknown code is used with
71    => '1',               // "autolog" parameter :
72                          //   0 : nothing (display home page, index.php (not
73                          //       logged in of course)) ;
74                          //   1 : display "access denied" page ;
75                          //   2 : display an Additional Page precised below.
76                          //
77  'unknown_code_ap_id'    // the id of the Additional Page which must be used
78    => '0',               // in case of usage of an unknow code, several vals.
79);
80
81$ec_conf_possible_values = array(
82  'activated'          => array('0', '1'),
83  'howto'              => array('0', '1'),
84  'dup_allow'          => array('0', '1', '2'),
85  'duplic_display'     => array('0', '1', '2'),
86  'auto_code_dg_nb'    => array( '4',  '5',  '8', '10', '12', '15',
87                                '16', '20', '24', '25', '30', '32'),
88  'unknown_code'       => array('0', '1', '2'),
89  'unknown_code_ap_id' => array()
90);
91
92$ec_conf_index = array_flip(array_keys($ec_conf_default));
93
94$t = array(); $u = '0'; $v = false;
95if ($v = (
96  ($t = mysql_fetch_row(pwg_query("
97    SELECT `value`
98    FROM `".CONFIG_TABLE."`
99    WHERE `param` = 'event_cats';
100  "))) !== false)
101) {
102  $v = (count($ec_conf = explode(',', $t[0])) == count($ec_conf_index));
103  $u = $ec_conf[0];
104}
105// $v is false
106//   _ if the entry in the table doesn't exist ;
107//   _ if the entry in the table exists, but its number of parameters is
108//     different than the number of default parameters.
109// $u is '0' if the plugin has not yet been activated, '1' if it has been.
110if (!$v) {
111  $ec_conf = array_values($ec_conf_default);
112  change_ec_conf('activated', $u); // writes in the DB
113}
114// now :
115//   _ the number of parameters is the same in the DB as the number of
116//     default parameters ;
117//   _ the parameters have the default values if needed ;
118//   _ the plugin shows it is active if it is the case.
119unset($t, $u, $v);
120
121/*
122 * change_ec_conf($c, $v)
123 * updates Event Cats configuration values in the database as well as in
124 * $ec_conf.
125 *
126 * @param
127 *   $c : conf value to update ;
128 *   $v : value to give to $ec_conf[$c].
129 * @return
130 *   true or false whether the change was OK or not
131 */
132function change_ec_conf($c, $v) {
133  global $ec_conf, $page, $ec_conf_index, $ec_conf_possible_values;
134  if (array_key_exists($c, $ec_conf_index)) {
135    if (
136      count($ec_conf_possible_values[$c]) == 0 or
137      in_array($v, $ec_conf_possible_values[$c])
138    ) {
139      $ec_conf[$ec_conf_index[$c]] = $v;
140      return (pwg_query("
141        UPDATE `".CONFIG_TABLE."`
142        SET `value` = \"".implode(',', $ec_conf)."\"
143        WHERE `param` = 'event_cats';
144      ") !== false);
145    }
146    else {
147      $page['errors'][] = 'Bad change_ec_conf value';
148      return false;
149    }
150  }
151  else {
152    $page['errors'][] = 'Bad change_ec_conf index';
153    return false;
154  }
155}
156
157/*
158 * read_ec_conf($c)
159 * returns the value in $ec_conf using string keys.
160 *
161 * @param
162 *   $c : conf value to update ;
163 * @return
164 *   $ec_conf value
165 */
166function read_ec_conf($c) {
167  global $ec_conf, $page, $ec_conf_index;
168  if (array_key_exists($c, $ec_conf_index))
169   return $ec_conf[$ec_conf_index[$c]];
170  else {
171    $page['errors'][] = 'Bad read_ec_conf index';
172    return false;
173  }
174}
175
176?>
Note: See TracBrowser for help on using the repository browser.