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

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

[Event Cats] Continue entry creation in DB

File size: 12.2 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_categories($dsp)
165 * builds $ec_lists['categories'].
166 *
167 * @param
168 *   whether $ec_lists['categories'] must be of type "cat / sub-cat" or
169 *   or "cat <CR LF> - sub-cat".
170 * @return
171 *   (no return value)
172 */
173function build_ec_categories($dsp) {
174  global $template, $ec_lists;
175  $c = array();
176  display_select_cat_wrapper('
177     SELECT id,name,uppercats,global_rank
178     FROM '.CATEGORIES_TABLE,
179   $c, 'category_options', $dsp);
180  $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options'];
181}
182
183/*
184 * build_ec_table()
185 * builds a table showing the content of the <pwg>_event_cats database table,
186 * and a table showing the eventual errors.
187 *
188 * @param
189 *   no parameters passed ; the main material on which works the function, is
190 *   the global array variable $ec_lists.
191 * @return
192 *   (no return value)
193 */
194function build_ec_table() {
195  global $ec_lists, $conf, $ec_ap_ok;
196 
197  $ec_lists['ec_table'] = array();
198 
199  if (isset($conf['auto_log'])) {
200    // For tests purpose : table not in MySQL, but in $conf['xxx'] vars in
201    // config_local.inc.php
202   
203    foreach ($conf['auto_log'] as $ec_current_code => $ec_entry) {
204      $ec_current_entry = array_push($ec_lists['ec_table'], array(
205       'code'    => $ec_current_code,
206       'user_id' => get_userid($ec_entry[$ec_current_code]),
207       'action'  => 'ec_ok',
208       'arg1'    => NULL,
209       'arg2'    => NULL,
210       'forced'  => 'false',
211      )) - 1;
212      $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
213      if (isset($conf['prior_page'])) {
214        if (array_key_exists($ec_current_code, $conf['prior_page'])) {
215          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
216           $conf['prior_page'][$ec_current_code];
217          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
218        }
219      }
220      if (isset($conf['outdated_page'])) {
221        if (array_key_exists($ec_current_code, $conf['outdated_page'])) {
222          $ec_lists['ec_table'][$ec_current_entry]['action'] = 'ec_nok';
223          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
224           $conf['outdated_page'][$ec_current_code];
225          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
226        }
227      }
228    }
229   
230    // Inspection of $conf['outdated_page']
231    if (isset($conf['outdated_page'])) {
232      foreach ($conf['outdated_page'] as $ec_current_code => $ec_current_AP) {
233        if (!array_key_exists($ec_current_code, $conf['auto_log'])) {
234          $ec_current_entry = array_push($ec_lists['ec_table'], array(
235           'code'    => $ec_current_code,
236           'user_id' => '',
237           'action'  => 'ec_nok',
238           'arg1'    => NULL,
239           'arg2'    => $ec_current_AP,
240           'forced'  => 'true',
241          )) - 1;
242          $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
243        }
244      }
245    }
246   
247  }
248  else { // in "normal" use ($conf['auto_log'] NOT set), $ec_lists['ec_table']
249         // is built thanks to below code only. A little bit more simple...
250    $q = pwg_query('
251     SELECT * FROM `'.EVNTCATS_TABLE.'`
252     WHERE `code` IS NOT NULL
253     ORDER BY `id`
254    ');
255    while ($r = mysql_fetch_assoc($q)) {
256      $ec_lists['ec_table'][intval($r['id'])] = $r;
257    }
258  }
259 
260  // Construction of behaviour
261 
262  // Multiple action params for a single code check
263  ec_inspect('action', 'ec_nok_action_pb');
264 
265  // Multiple user_ids for a single code check
266  ec_inspect('user_id', 'ec_nok_userid_pb', false);
267 
268  // Multiple "forced" params for a single code check
269  ec_inspect('forced', '');
270 
271  // User id and associated page validities checks
272  foreach ($ec_lists['ec_table'] as &$ec_entry) {
273   
274    // Check if associated user_id exists
275    if (
276     is_in($ec_entry['action'], 'ec_ok') and
277     !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids'])
278    ) $ec_entry['action'] = 'ec_nok_userid_miss';
279   
280    // Check if associated displayed page exists
281    $a = 0;
282    if (!empty($ec_entry['arg1']) and
283     is_in($ec_entry['action'], 'ec_ok')) $a++; // Only arg2
284    // is significant if action is ec_nok[_xxx] .
285    if (!empty($ec_entry['arg2'])) $a+= 2;
286    switch ($a) {
287      // case 0 : home, nothing to check
288     
289      case 2: // Additional Page
290        if (
291          $ec_ap_ok and (
292            is_in($ec_entry['action'], 'ec_ok') or
293            $ec_entry['action'] == 'ec_nok'
294          ) and
295          !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages'])
296        ) {
297          $ec_entry['action'].= '_ap_pb';
298        }
299      break;
300     
301      case 1: // Category
302      case 3: // Image
303        if (is_in($ec_entry['action'], 'ec_ok')) {
304          if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) {
305            if ($a == 3) { // case 3: // Image
306              if (!ec_image_exists($ec_entry['arg1'], $ec_entry['arg2'])) {
307                $ec_entry['action'].= '_img_pb';
308              }
309            }
310          }
311          else {
312            $ec_entry['action'].= '_cat_pb';
313          }
314        }
315      break;
316    }
317  }
318}
319
320/*
321 * build_ec_lists()
322 * builds the main array variable contaning all informations for the plugin
323 *
324 * @param
325 *   no parameter passed, the main material on which works the function, is
326 *   the global array variable $ec_lists.
327 * @return
328 *   (no return value)
329 */
330function build_ec_lists() {
331 
332  global $ec_ap_ok, $template, $ec_lists;
333 
334  $ec_lists = array();
335
336  // Construction of $ec_lists['add_pages'] array var
337  $ec_lists['add_pages'] = array();
338  if ($ec_ap_ok) {
339    $res = pwg_query('SELECT id,title FROM '.ADD_PAGES_TABLE);
340    while ($r = mysql_fetch_assoc($res)) {
341      $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
342      $a = explode($c ,$r['title']);
343      $ec_lists['add_pages'][$r['id']] = $a[0];
344    }
345  }
346 
347  // Construction of $ec_lists['categories'] array var
348  build_ec_categories(true);
349 
350  // Construction of $ec_lists['user_ids'] array var
351  $ec_lists['user_ids'] = array();
352  $q = pwg_query('SELECT id,username FROM '.USERS_TABLE. ' WHERE id > 2');
353  while ($r = mysql_fetch_assoc($q)) {
354    $ec_lists['user_ids'][$r['id']] = $r['username'];
355  }
356 
357  // Construction of $ec_lists['ec_table'] array var
358  build_ec_table();
359}
360
361?>
Note: See TracBrowser for help on using the repository browser.