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

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

[Event Cats] Bug corrections

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