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

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

[Event Cats] Modify entry in DB should be OK (not tested).

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