source: extensions/event_cats/include/evntcats_main_funcs.inc.php @ 4239

Last change on this file since 4239 was 4239, checked in by LucMorizur, 14 years ago

[Event Cats] Improve configuration parameters management

File size: 10.0 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
25// ***************************************************************************
26// ** evntcats_admin_funcs.php : Main functions (include)                   **
27// ** for Piwigo plugin Event Cats                                          **
28// ***************************************************************************
29
30// +-----------------------------------------------------------------------+
31// | Header                                                                |
32// +-----------------------------------------------------------------------+
33
34include_once(EVNTCATS_PATH.'include/ec_conf.inc.php');
35
36// +-----------------------------------------------------------------------+
37// | Utilities functions                                                   |
38// +-----------------------------------------------------------------------+
39
40/*
41 * is_in($haystack, $needle)
42 * returns true or false whether $needle is a string found in string $haystack
43 *
44 * @param
45 *   $haystack : the string in which to search
46 *   $needle   : the string looked for
47 * @return
48 *   true if $needle is found in $haystack ; false if not
49 */
50function is_in($haystack, $needle) {
51  return (strpos($haystack, $needle) !== false);
52}
53
54/*
55 * ec_image_exists($cat, $img)
56 * returns true or false whether the image is associated with the category.
57 *
58 * @param
59 *   $cat : the category
60 *   $img : the image
61 * @return
62 *   treu or false whether the image is associated with the category.
63 */
64function ec_image_exists($cat, $img) {
65  return (mysql_fetch_row(pwg_query("
66    SELECT *
67    FROM `".IMAGE_CATEGORY_TABLE."`
68    WHERE `category_id` = ".$cat."
69     AND `image_id` = ".$img
70  )) !== false);
71}
72
73/*
74 * str_from_var($var)
75 * returns a string easing array var informations displaying in Piwigo :
76 *   _ the string return value starts with"<p align="left">" ;
77 *   _ all "TAB" characters (chr(10)) are replaced by "<br>" ;
78 *   _ all spaces are replaced by "&nbsp;".
79 *
80 * @param
81 *   $var : variable to display
82 * @return
83 *   string easy to display in Piwigo
84 */
85function str_from_var($var) {
86  return
87   '<p align="left">'.
88   str_replace(
89    chr(10),'<br>',
90    str_replace(' ','&nbsp;', print_r /* var_dump */ ($var,true))
91   ).
92   '</p>';
93}
94
95/*
96 * ec_inspect()
97 * goes through ec_lists['ec_table'] to check errors on (multiple) entries
98 * using the same code. Cannot really be used elsewhere than here in
99 * build_ec_table() .
100 *
101 * @param
102 *   
103 * @return
104 *   (no return value)
105 */
106function ec_inspect($checked_item, $new_action, $check_ec_nok = true) {
107  global $ec_lists;
108  $first = array();
109  $to_correct = array();
110 
111  /*
112  // $to_correct is needed cause following code would not work everywhere :
113  foreach ($table as $value) {
114    if ($value == $value_to_check and $value == $problem) {
115      foreach ($table as &$value2) { // key index of $table can be reset
116        if ($value2 == $value_to_check) {
117          $value2 = $better_value;
118        }
119      }
120    }
121  }
122  // It works with WinAmp Server, but not on Apache server of Free (french
123  // Internet provider).
124  */
125 
126  foreach ($ec_lists['ec_table'] as $ec_entry) {
127    $ec_current_code = $ec_entry['code'];
128    if (
129     $ec_entry['action'] == 'ec_ok' or
130     ($check_ec_nok and $ec_entry['action'] == 'ec_nok')
131    ) {
132      if (isset($first[$ec_current_code])) {
133        // $first[$ec_current_code] is set <=> code has already been met.
134        // Checked item MUST be equal to the first item (thus all items) for
135        // this code.
136        if (
137         $first[$ec_current_code] != $ec_entry[$checked_item] or
138         ($new_action == '' and $ec_entry[$checked_item] == 'true')
139        ) $to_correct[$ec_current_code] = true; // value not used in fact
140        // but using $ec_current_code as a key makes a smaller table
141      } // if the error comes back several times
142      else $first[$ec_current_code] = $ec_entry[$checked_item];
143    }
144  }
145  foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here
146    if (isset($to_correct[$ec_entry['code']])) {
147      if ($new_action == '') {
148        if (!pwg_query("
149          UPDATE `".EVNTCATS_TABLE."`
150          SET `forced` = 'false'
151          WHERE `id` = ".$ec_entry['id']
152        )) die('Could not fix a "_f_pb"');
153        $ec_entry['forced'] = 'false';
154      }
155      else $ec_entry['action'] = $new_action;
156    }
157  }
158}
159
160// +-----------------------------------------------------------------------+
161// | Tables building functions                                             |
162// +-----------------------------------------------------------------------+
163
164/*
165 * build_ec_categories($dsp)
166 * builds $ec_lists['categories'].
167 *
168 * @param
169 *   whether $ec_lists['categories'] must be of type "cat / sub-cat" or
170 *   or "cat <CR LF> - sub-cat".
171 * @return
172 *   (no return value)
173 */
174function build_ec_categories($dsp) {
175  global $template, $ec_lists;
176  $c = array();
177  display_select_cat_wrapper("
178     SELECT `id`, `name`, `uppercats`, `global_rank`
179     FROM `".CATEGORIES_TABLE."`
180   ", $c, 'category_options', $dsp);
181  $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options'];
182}
183
184/*
185 * build_ec_table()
186 * builds a table showing the content of the <pwg>_event_cats database table,
187 * and a table showing the eventual errors.
188 *
189 * @param
190 *   no parameters passed ; the main material on which works the function, is
191 *   the global array variable $ec_lists.
192 * @return
193 *   (no return value)
194 */
195function build_ec_table() {
196  global $ec_lists, $ec_ap_ok;
197 
198  $ec_lists['ec_table'] = array();
199 
200  $q = pwg_query("
201    SELECT * FROM `".EVNTCATS_TABLE."`
202    WHERE `code` IS NOT NULL
203    ORDER BY `id`
204  ");
205  while ($r = mysql_fetch_assoc($q))
206   $ec_lists['ec_table'][intval($r['id'])] = $r;
207 
208  // Construction of behaviour
209 
210  // Multiple action params for a single code check
211  ec_inspect('action', 'ec_nok_action_pb');
212 
213  // Multiple user_ids for a single code check
214  ec_inspect('user_id', 'ec_nok_userid_pb', false);
215 
216  // Multiple "forced" params for a single code check
217  ec_inspect('forced', '');
218 
219  // User id and associated page validities checks
220  foreach ($ec_lists['ec_table'] as &$ec_entry) {
221   
222    // Check if associated user_id exists
223    if (
224     is_in($ec_entry['action'], 'ec_ok') and
225     !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids'])
226    ) $ec_entry['action'] = 'ec_nok_userid_miss';
227   
228    // Check if associated displayed page exists
229    $a = 0;
230    if (
231      !empty($ec_entry['arg1']) and
232      // (Only arg2 is significant if action is ec_nok[_xxx] .)
233      is_in($ec_entry['action'], 'ec_ok')
234    ) $a++;
235    if (!empty($ec_entry['arg2'])) $a+= 2;
236    switch ($a) {
237      // case 0 : home, nothing to check
238     
239      case 2: // Additional Page
240        if (
241          $ec_ap_ok and (
242            is_in($ec_entry['action'], 'ec_ok') or
243            $ec_entry['action'] == 'ec_nok'
244          ) and
245          !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages'])
246        ) $ec_entry['action'].= '_ap_pb';
247      break;
248     
249      case 1: // Category
250      case 3: // Image
251        if (is_in($ec_entry['action'], 'ec_ok')) {
252          if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) {
253           if ($a == 3) // case 3: // Image
254            if (!ec_image_exists($ec_entry['arg1'], $ec_entry['arg2']))
255             $ec_entry['action'].= '_img_pb';
256          }
257          else $ec_entry['action'].= '_cat_pb';
258        }
259      break;
260    }
261  }
262}
263
264/*
265 * build_ec_lists()
266 * builds the main array variable contaning all informations for the plugin
267 *
268 * @param
269 *   no parameter passed, the main material on which works the function, is
270 *   the global array variable $ec_lists.
271 * @return
272 *   (no return value)
273 */
274function build_ec_lists() {
275 
276  global $ec_ap_ok, $template, $ec_lists;
277 
278  $ec_lists = array();
279
280  // Construction of $ec_lists['add_pages'] array var
281  $ec_lists['add_pages'] = array();
282  if ($ec_ap_ok) {
283    $res = pwg_query("SELECT `id`, `title` FROM `".ADD_PAGES_TABLE."`");
284    while ($r = mysql_fetch_assoc($res)) {
285      $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
286      $a = explode($c ,$r['title']);
287      $ec_lists['add_pages'][$r['id']] = $a[0];
288    }
289  }
290 
291  // Construction of $ec_lists['categories'] array var
292  build_ec_categories(true);
293 
294  // Construction of $ec_lists['user_ids'] array var
295  $ec_lists['user_ids'] = array();
296  $q = pwg_query("
297    SELECT `id`,`username`
298    FROM `".USERS_TABLE."`
299    WHERE `id` > 2
300  ");
301  while ($r = mysql_fetch_assoc($q))
302   $ec_lists['user_ids'][$r['id']] = $r['username'];
303 
304  // Construction of $ec_lists['ec_table'] array var
305  build_ec_table();
306}
307
308?>
Note: See TracBrowser for help on using the repository browser.