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

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

[Event Cats] the two simplest actions added : delete entry and duplicate entry. Some JS bugs removed.

File size: 16.6 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 * ec_inspect()
35 * goes through ec_lists['ec_table'] to check errors on (multiple) entries
36 * using the same code. Cannot be used elsewhere than here in
37 * build_ec_table() .
38 *
39 * @param
40 *   
41 * @return
42 *   (no return value)
43 */
44function ec_inspect($checked_item, $new_action, $check_ec_nok = true) {
45  global $ec_lists;
46  $first = array();
47  $to_correct = array();
48 
49  // $to_correct is needed cause following code would not work everywhere :
50  /*
51  foreach ($table as $value) {
52    if ($value == $value_to_check and $value == $problem) {
53      foreach ($table as &$value2) { // key index of $table can be reset
54        if ($value2 == $value_to_check) {
55          $value2 = $better_value;
56        }
57      }
58    }
59  }
60  // It works with WinAmp Server, but not on Apache server of Free (french
61  // Internet provider).
62  */
63 
64  foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is not really needed
65    $ec_current_code = $ec_entry['code'];
66    if (
67     $ec_entry['action'] == 'ec_ok' or
68     ($check_ec_nok and $ec_entry['action'] == 'ec_nok')
69    ) {
70      if (isset($first[$ec_current_code])) {
71        // $first[$ec_current_code] is set <=> code has already been met.
72        // Checked item MUST be equal to the first item (thus all items) for
73        // this code.
74        if (
75         $first[$ec_current_code] != $ec_entry[$checked_item] or
76         ($new_action == '' and $ec_entry[$checked_item] == 'true')
77        ) {
78          $to_correct[$ec_current_code] = true; // value not used in fact
79        } // but using $ec_current_code as a key makes a smaller table
80      } // if the error comes back several times
81      else {
82        $first[$ec_current_code] = $ec_entry[$checked_item];
83      }
84    }
85  }
86  foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here
87    if (isset($to_correct[$ec_entry['code']])) {
88      if ($new_action == '') {
89        if (!pwg_query('
90          UPDATE `'.EVNTCATS_TABLE.'`
91          SET `forced` = "false"
92          WHERE `id` = '.$ec_entry['id']
93        )) die('Could not fix a "_f_pb"');
94        $ec_entry['forced'] = 'false';
95      }
96      else $ec_entry['action'] = $new_action;
97    }
98  }
99}
100
101/*
102 * build_ec_table()
103 * builds a table showing the content of the <pwg>_event_cats database table,
104 * and a table showing the eventual errors.
105 *
106 * @param
107 *   no parameters passed ; the main material on which works the function, is
108 *   the global array variable $ec_lists.
109 * @return
110 *   (no return value)
111 */
112function build_ec_table() {
113  global $ec_lists, $conf, $ec_ap_ok;
114 
115  $ec_lists['ec_table'] = array();
116 
117  if (isset($conf['auto_log'])) {
118    // For tests purpose : table not in MySQL, but in $conf['xxx'] vars in
119    // config_local.inc.php
120   
121    foreach ($conf['auto_log'] as $ec_current_code => $ec_entry) {
122      $ec_current_entry = array_push($ec_lists['ec_table'], array(
123       'code'    => $ec_current_code,
124       'user_id' => get_userid($ec_entry[$ec_current_code]),
125       'action'  => 'ec_ok',
126       'arg1'    => NULL,
127       'arg2'    => NULL,
128       'forced'  => 'false',
129      )) - 1;
130      $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
131      if (isset($conf['prior_page'])) {
132        if (array_key_exists($ec_current_code, $conf['prior_page'])) {
133          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
134           $conf['prior_page'][$ec_current_code];
135          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
136        }
137      }
138      if (isset($conf['outdated_page'])) {
139        if (array_key_exists($ec_current_code, $conf['outdated_page'])) {
140          $ec_lists['ec_table'][$ec_current_entry]['action'] = 'ec_nok';
141          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
142           $conf['outdated_page'][$ec_current_code];
143          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
144        }
145      }
146    }
147   
148    // Inspection of $conf['outdated_page']
149    if (isset($conf['outdated_page'])) {
150      foreach ($conf['outdated_page'] as $ec_current_code => $ec_current_AP) {
151        if (!array_key_exists($ec_current_code, $conf['auto_log'])) {
152          $ec_current_entry = array_push($ec_lists['ec_table'], array(
153           'code'    => $ec_current_code,
154           'user_id' => '',
155           'action'  => 'ec_nok',
156           'arg1'    => NULL,
157           'arg2'    => $ec_current_AP,
158           'forced'  => 'true',
159          )) - 1;
160          $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
161        }
162      }
163    }
164   
165  }
166  else { // in "normal" use ($conf['auto_log'] NOT set), $ec_lists['ec_table']
167         // is built thanks to below code only. A little bit more simple...
168    $q = pwg_query('
169     SELECT * FROM `'.EVNTCATS_TABLE.'`
170     WHERE `code` IS NOT NULL
171     ORDER BY `id`
172    ');
173    while ($r = mysql_fetch_assoc($q)) {
174      $ec_lists['ec_table'][intval($r['id'])] = $r;
175    }
176  }
177 
178  // Construction of behaviour
179 
180  // Multiple action params for a single code check
181  ec_inspect('action', 'ec_nok_action_pb');
182 
183  // Multiple user_ids for a single code check
184  ec_inspect('user_id', 'ec_nok_userid_pb', false);
185 
186  // Multiple "forced" params for a single code check
187  ec_inspect('forced', '');
188 
189  // User id and associated page validities checks
190  foreach ($ec_lists['ec_table'] as &$ec_entry) {
191   
192    // Check if associated user_id exists
193    if (
194     is_in($ec_entry['action'], 'ec_ok') and
195     !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids'])
196    ) {
197      $ec_entry['action'] = 'ec_nok_userid_miss';
198    }
199   
200    // Check if associated displayed page exists
201    $a = 0;
202    if (!empty($ec_entry['arg1']) and
203     is_in($ec_entry['action'], 'ec_ok')) $a++; // Only arg2
204    // is significant if action is ec_nok[_xxx] .
205    if (!empty($ec_entry['arg2'])) $a+= 2;
206    switch ($a) {
207      // case 0 : home, nothing to check
208     
209      case 2: // Additional Page
210        if (
211          $ec_ap_ok and (
212            is_in($ec_entry['action'], 'ec_ok') or
213            $ec_entry['action'] == 'ec_nok'
214          ) and
215          !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages'])
216        ) {
217          $ec_entry['action'].= '_ap_pb';
218        }
219      break;
220     
221      case 1: // Category
222      case 3: // Image
223        if (is_in($ec_entry['action'], 'ec_ok')) {
224          if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) {
225            if ($a == 3) { // case 3: // Image
226              if (!ec_image_exists($ec_entry['arg1'], $ec_entry['arg2'])) {
227                $ec_entry['action'].= '_img_pb';
228              }
229            }
230          }
231          else {
232            $ec_entry['action'].= '_cat_pb';
233          }
234        }
235      break;
236    }
237  }
238}
239
240/*
241 * build_ec_duplicable_codes()
242 *
243 *
244 * @param
245 *   no parameter passed, the main material on which works the function, is
246 *   the global array variable $ec_lists.
247 * @return
248 *   (no return value)
249 */
250function build_ec_duplicable_codes() {
251  global $ec_lists;
252  $ec_lists['duplicable_codes'] = array();
253  $t                            = array();
254  foreach ($ec_lists['ec_table'] as $ec_entry) {
255    if (
256      is_in($ec_entry['action'], 'ec_ok') and
257      $ec_entry['forced'] == 'false'
258    ) {
259      $t[$ec_entry['id']] = $ec_entry['code'];
260      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['id'] =
261       $ec_entry['id'];
262      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['user_id'] =
263       $ec_entry['user_id'];
264    }
265  }
266  foreach ($t as $ec_id => $ec_code) {
267    $ec_lists['duplicable_codes']['ids'][$ec_id] =
268     $ec_lists['duplicable_codes']['codes'][$ec_code]['id'];
269  }
270}
271
272/*
273 * build_ec_lists()
274 * builds the main array variable contaning all informations for the plugin
275 *
276 * @param
277 *   no parameter passed, the main material on which works the function, is
278 *   the global array variable $ec_lists.
279 * @return
280 *   (no return value)
281 */
282function build_ec_lists() {
283 
284  global $ec_ap_ok, $template, $ec_lists;
285 
286  $ec_lists = array();
287
288  // Construction of $ec_lists['add_pages'] array var
289  $ec_lists['add_pages'] = array();
290  if ($ec_ap_ok) {
291    $res = pwg_query('SELECT id,title FROM '.ADD_PAGES_TABLE);
292    while ($r = mysql_fetch_assoc($res)) {
293      $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
294      $a = explode($c ,$r['title']);
295      $ec_lists['add_pages'][$r['id']] = $a[0];
296    }
297  }
298 
299  // Construction of $ec_lists['categories'] array var
300  $c = array();
301  display_select_cat_wrapper('
302     SELECT id,name,uppercats,global_rank
303     FROM '.CATEGORIES_TABLE,
304   $c, 'category_options', true);
305  $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options'];
306 
307  // Construction of $ec_lists['user_ids'] array var
308  $ec_lists['user_ids'] = array();
309  $q = pwg_query('SELECT id,username FROM '.USERS_TABLE. ' WHERE id > 2');
310  while ($r = mysql_fetch_assoc($q)) {
311    $ec_lists['user_ids'][$r['id']] = $r['username'];
312  }
313 
314  // Construction of $ec_lists['ec_table'] array var
315  build_ec_table();
316}
317
318/*
319 * is_in($haystack, $needle)
320 * returns true or false whether $needle is a string found in string $haystack
321 *
322 * @param
323 *   $haystack : the string in which to search
324 *   $needle   : the string looked for
325 * @return
326 *   true if $needle is found in $haystack ; false if not
327 */
328function is_in($haystack, $needle) {
329  return (strpos($haystack, $needle) !== false);
330}
331
332/*
333 * ec_create_entry_OK()
334 * returns true or false whether the creation of a new entry described by
335 * $_POST was OK or not.
336 *
337 * @param
338 *   no param
339 * @return
340 *   true if creation was OK ; false if not
341 */
342function ec_create_entry_OK() {
343  global $page;
344  $page['errors'][] = 'Y\'a p\'têt\' ben eu une erreur...';
345  return false;
346}
347
348/*
349 * ec_duplicate_entry_OK()
350 * returns true or false whether the duplication of an existing entry which #
351 * is given by $_POST['ec_entry_sel'] was OK or not.
352 *
353 * @param
354 *   no param
355 * @return
356 *   true if creation was OK ; false if not
357 */
358function ec_duplicate_entry_OK() {
359  global $page, $ec_lists;
360 
361  build_ec_duplicable_codes();
362  if (array_key_exists($_POST['ec_entry_sel'],
363   $ec_lists['duplicable_codes']['ids'])) {
364    $arg1 = ''; $arg2 = '';
365    switch ($_POST['ec_input_action']) {
366      case 'add_p':
367        if (array_key_exists($_POST['ec_in_up_aps'], $ec_lists['add_pages'])) {
368         $arg2 = $_POST['ec_in_up_aps']; }
369        else {
370          $page['errors'][] =
371           l10n('ec_bad_argument1').
372           '$_POST[\'ec_in_up_aps\'] = '.
373           $_POST['ec_in_up_aps']
374          ;
375          return false;
376        }
377      case 'home':
378      break;
379      case 'img':
380      case 'cat':
381        if (array_key_exists($_POST['ec_in_up_cat'],
382         $ec_lists['categories'])) {
383          if ($_POST['ec_input_action'] == 'img') {
384            if (
385             ec_image_exists($_POST['ec_in_up_cat'], $_POST['ec_in_up_img'])
386            ) $arg2 = $_POST['ec_in_up_img'];
387            else {
388              $page['errors'][] =
389               l10n('ec_bad_argument1').
390               '$_POST[\'ec_in_up_img\'] = '.
391               $_POST['ec_in_up_img']
392              ;
393              return false;
394            }
395          }
396          $arg1 = $_POST['ec_in_up_cat'];
397        }
398        else {
399          $page['errors'][] =
400           l10n('ec_bad_argument1').
401           '$_POST[\'ec_in_up_cat\'] = '.
402           $_POST['ec_in_up_cat']
403          ;
404          return false;
405        }
406      break;
407      default:
408        $page['errors'][] =
409         l10n('ec_bad_argument1').
410         '$_POST[\'ec_input_action\'] = '.
411         $_POST['ec_input_action']
412        ;
413        return false;
414    }
415    if (pwg_query("
416      INSERT INTO `".EVNTCATS_TABLE."`
417       (`code`, `user_id`, `action`, `arg1`, `arg2`)
418      VALUES (
419        '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['code']."',
420        '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']."',
421        'ec_ok',
422        '".$arg1."',
423        '".$arg2."'
424      );
425    ")) {
426      $page['infos'][] =
427       l10n('ec_entry_dup_ok_pre').
428       $_POST['ec_entry_sel'].' ('.
429       $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code'].')'.
430       l10n('ec_entry_dup_ok_end')
431      ;
432      build_ec_table();
433      return true;
434    }
435    else {
436      $page['errors'][] =
437       l10n('ec_entry_dup_nok_pre1').
438       $_POST['ec_entry_sel'].
439       l10n('ec_entry_dup_nok_end1').
440       'MySQL error '.mysql_errno().', "'.mysql_error().'"'
441      ;
442      return false;
443    }
444  }
445  else {
446    $page['errors'][] =
447     l10n('ec_bad_argument1').
448     '$_POST[\'ec_entry_sel\'] = '.
449     $_POST['ec_entry_sel']
450    ;
451    return false;
452  }
453  $page['errors'][] = '?!';
454  return false;
455}
456
457/*
458 * ec_modify_entry_OK()
459 * returns true or false whether the modification of an existing entry which #
460 * is given by $_POST['ec_entry_sel'] was OK or not.
461 *
462 * @param
463 *   no param
464 * @return
465 *   true if modification was OK ; false if not
466 */
467function ec_modify_entry_OK() {
468  global $page;
469  $page['errors'][] = 'Y\'a pas eu une erreur ?...';
470  return false;
471}
472
473/*
474 * ec_delete_entry()
475 * tries to delete an existing entry which # is given by
476 * $_POST['ec_entry_sel'].
477 *
478 * @param
479 *   no param
480 * @return
481 *   no return value : modifies $page['errors'] or $page['infos']
482 */
483function ec_delete_entry() {
484  global $page, $ec_lists;
485  if (array_key_exists($_POST['ec_entry_sel'], $ec_lists['ec_table'])) {
486    if (!pwg_query('
487      DELETE FROM `'.EVNTCATS_TABLE.'`
488      WHERE `id` = '.$_POST['ec_entry_sel']
489    )) {
490      $page['errors'][] =
491        l10n('ec_entry_del_nok_pre').
492        $_POST['ec_entry_sel'].
493        l10n('ec_entry_del_nok_end').
494        'MySQL error '.mysql_errno().', "'.mysql_error().'"'
495      ;
496    }
497    else {
498      $page['infos'][] =
499        l10n('ec_entry_del_ok_pre').
500        $_POST['ec_entry_sel'].
501        l10n('ec_entry_del_ok_end');
502      build_ec_table();
503    }
504  }
505  else {
506    $page['errors'][] =
507     l10n('ec_bad_argument1').
508     '$_POST[\'ec_entry_sel\'] = '.
509     $_POST['ec_entry_sel']
510    ;
511  }
512}
513
514/*
515 * ec_image_exists($cat, $img)
516 * returns true or false whether the image is associated with the category.
517 *
518 * @param
519 *   $cat : the category
520 *   $img : the image
521 * @return
522 *   treu or false whether the image is associated with the category.
523 */
524function ec_image_exists($cat, $img) {
525  return (mysql_fetch_row(pwg_query('
526   SELECT *
527   FROM `'.IMAGE_CATEGORY_TABLE.'`
528   WHERE `category_id` = '.$cat.'
529    AND `image_id` = '.$img
530  )) !== false);
531}
532
533/*
534 * str_from_var($var)
535 * returns a string easing array var informations displaying in Piwigo :
536 *   _ the string return value starts with"<p align="left">" ;
537 *   _ all "TAB" characters (chr(10)) are replaced by "<br>" ;
538 *   _ all spaces are replaced by "&nbsp;".
539 *
540 * @param
541 *   $var : variable to display
542 * @return
543 *   string easy to display in Piwigo
544 */
545function str_from_var($var) {
546  return '<p align="left">'.str_replace(chr(10),'<br>',str_replace(' ','&nbsp;', print_r /* var_dump */ ($var,true))).'</p>';
547}
548
549?>
Note: See TracBrowser for help on using the repository browser.