source: extensions/event_cats/include/evntcats_funcs.inc.php @ 3971

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

[Event Cats] Bug-free files... hopefully...

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