source: extensions/event_cats/include/evntcats_admin_funcs.inc.php

Last change on this file was 26672, checked in by LucMorizur, 10 years ago

Compatibility with Piwigo version 2.6

File size: 24.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// Keeps file coded in UTF-8 without BOM : é
26
27// ***************************************************************************
28// ** evntcats_admin_funcs.php : Admin functions (include)                  **
29// ** for Piwigo plugin Event Cats                                          **
30// ***************************************************************************
31
32// +-----------------------------------------------------------------------+
33// | Header                                                                |
34// +-----------------------------------------------------------------------+
35
36if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
37
38global $conf, $page;
39
40// +-----------------------------------------------------------------------+
41// | Utilities functions                                                   |
42// +-----------------------------------------------------------------------+
43
44/*
45 * ec_end1()
46 * Process repetitive task when error in database modifying functions.
47 *
48 * @param
49 *   $pst : $_POST argument
50 *   $msg : message
51 * @return
52 *   false as this function is used when there is a problem
53 */
54function ec_end1($pst, $msg) {
55  global $page;
56  if (isset($_POST[$pst]))
57   $page['errors'][] =
58    l10n($msg).
59    '$_POST[\''.$pst.'\'] = '.
60    $_POST[$pst];
61  else
62    $page['errors'][] =
63    l10n($msg).
64    '$_POST[\''.$pst.'\'] unset';
65  return false;
66}
67
68/*
69 * ec_end2()
70 * Process repetitive task when error in database modifying functions.
71 *
72 * @param
73 *   $n : number to display
74 * @return
75 *   false as this function is used when there is a problem
76 */
77function ec_end2($n) {
78  global $page;
79  $page['errors'][] =
80   sprintf(l10n('ec_DB_problem'), $n).
81   'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
82  return false;
83}
84
85/*
86 * ec_create_user_OK()
87 * Creates new generic user and eventually new group as described in $_POST.
88 * Assumes that the validity of the different indexes of $_POST it uses, have
89 * already been checked.
90 *
91 * @param
92 *   no param needed
93 * @return
94 *   the created user_id or false whether all operations suceeded or not
95 */
96function ec_create_user_OK() {
97  global $page, $ec_lists;
98 
99  // This function assumes that the validity of the different indexes of
100  // $_POST it uses, have already been checked.
101 
102  // User creation, as generic
103  $ec_user_id = register_user(
104    $_POST['login'], $_POST['password'], '', false, $page['errors'], false
105  );
106  if (
107    count($page['errors']) != 0 or
108    !($ec_user_id = get_userid($_POST['login']))
109  ) {
110    array_unshift($page['errors'], l10n('ec_user_create_pb'));
111    return false;
112  }
113  else
114   $page['infos'][] = sprintf(l10n('ec_user_create_OK'), $_POST['login']);
115  if (
116    pwg_query("
117      UPDATE `".USER_INFOS_TABLE."`
118      SET `status` = 'generic'
119      WHERE `user_id` = ".$ec_user_id.";
120    ") !== false
121  )
122   $page['infos'][] = sprintf(l10n('ec_user_generic_OK'), $_POST['login']);
123  else
124   $page['errors'][] = sprintf(l10n('ec_user_generic_pb'), $_POST['login']);
125 
126  // New group creation if required, and association with user_id, and if
127  // needed category or add. p., at the same time
128  if (
129    isset($_POST['ec_in_up_newgroup']) and
130    isset($_POST['groupname']) and
131    $_POST['groupname'] != ''
132  ) {
133    // Checks if a group named $_POST['groupname'] already exists.
134    // If not, creates it : then, t4 is no more false. In any case, t3 gets
135    // the id of the group named $_POST['groupname'].
136    $t2 = 0; $t3 = false; $t4 = false;
137    while ( // The check is executed once at minimum
138      (is_null($t3 = pwg_db_fetch_row(pwg_query("
139        SELECT `id`
140        FROM `".GROUPS_TABLE."`
141        WHERE `name` = '".$_POST['groupname']."';
142      "))) or !$t3) and
143      $t2++ == 0 // The check is executed twice at maximum
144    )
145      $t4 = pwg_query("
146        INSERT INTO `".GROUPS_TABLE."` (`name`, `is_default`)
147        VALUES ('".$_POST['groupname']."', 'false');
148      "); // Cannot be executed twice
149    if ($t4)
150     $page['infos'][] =
151      sprintf(l10n('ec_group_create_OK'), $_POST['groupname']);
152    if (!$t3)
153     $page['errors'][] =
154      sprintf(l10n('ec_group_create_pb'), $_POST['groupname']).' (1) : '.
155      'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
156    if (
157      pwg_query("
158        INSERT INTO `".USER_GROUP_TABLE."` (`user_id`, `group_id`)
159        VALUES ('".$ec_user_id."', '".$t3[0]."');
160      ") === false
161    ) $page['errors'][] =
162      sprintf(l10n('ec_group_create_pb'), $_POST['groupname']).' (2) : '.
163      'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
164    else $page['infos'][] = sprintf(
165      l10n('ec_group_create_OK2'),
166      $_POST['login'], $_POST['groupname']
167    );
168   
169    // If a category id has been posted, the newly created group must be
170    // allowed to navigate in this category
171    // We are in the group creation block, thus the we know this group cannot
172    // be associated to any category
173    if (
174      isset($_POST['ec_in_up_cat']) and
175      array_key_exists($_POST['ec_in_up_cat'], $ec_lists['categories'])
176    ) {
177      if (pwg_db_num_rows(pwg_query("
178        SELECT `id`
179        FROM `".CATEGORIES_TABLE."`
180        WHERE `id` = '".$_POST['ec_in_up_cat']."';
181      ")) == 0) return ec_end1('ec_in_up_cat', 'Category doesn\'t exist : ');
182      else {
183        $private_uppercats = array_from_query("
184          SELECT `id`
185          FROM `".CATEGORIES_TABLE."`
186          WHERE `id` IN (".
187            implode(',', get_uppercat_ids(array($_POST['ec_in_up_cat']))).
188          ")
189          AND `status` = 'private';
190        ", 'id');
191        $inserts = array();
192        foreach ($private_uppercats as $cat_id)
193         $inserts[] = array(
194           'group_id' => $t3[0],
195           'cat_id'   => $cat_id
196         );
197        mass_inserts(GROUP_ACCESS_TABLE,array('group_id','cat_id'), $inserts);
198        if (pwg_db_errno() == 0)
199         $page['infos'][] = sprintf(
200           l10n('ec_group_create_OK2'),
201           $_POST['groupname'],$ec_lists['categories'][$_POST['ec_in_up_cat']]
202         );
203        else
204         $page['errors'][] = sprintf(
205          l10n('ec_assoc_pb'),
206          $_POST['groupname'],$ec_lists['categories'][$_POST['ec_in_up_cat']]
207         ).' : MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
208      }
209    }
210   
211    // If an add. p. id has been posted, the newly created group must be
212    // allowed to navigate in this additional page
213    if (
214      isset($_POST['ec_in_up_aps']) and
215      array_key_exists($_POST['ec_in_up_aps'], $ec_lists['add_pages'])
216    ) {
217      $granted_groups = array();
218      $title_arr = array_from_query("
219        SELECT `title`
220        FROM `".ADD_PAGES_TABLE."`
221        WHERE `id` = ".$_POST['ec_in_up_aps'].";
222      ", 'title');
223      $t_user = (is_in($title_arr[0], '/user_id=')) ?
224       explode('/user_id=', $title_arr[0]) : array($title_arr[0]);
225      if (is_in($t_user[0], '/group_id=')) {
226        $t_group = explode('/group_id=', $t_user[0]);
227        $granted_groups = explode(',', $t_group[1]);
228      }
229      else $t_group[0] = $t_user[0];
230      if (!in_array($t3[0], $granted_groups)) {
231        $granted_groups[] = $t3[0];
232        $t_group[1] = implode(',', $granted_groups);
233        $t_user[0] = implode('/group_id=', $t_group);
234        if (pwg_query("
235          UPDATE `".ADD_PAGES_TABLE."`
236            SET `title` = '".implode('/user_id=', $t_user)."'
237            WHERE `id` = ".$_POST['ec_in_up_aps'].";
238        ") === false)
239         $page['errors'][] = sprintf(
240          l10n('ec_assoc_pb'),
241          $_POST['groupname'],$ec_lists['add_pages'][$_POST['ec_in_up_aps']]
242         ).' : MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
243        else $page['infos'][] = sprintf(
244         l10n('ec_group_create_OK2'),
245         $_POST['groupname'],$ec_lists['add_pages'][$_POST['ec_in_up_aps']]
246        );
247      }
248    }
249  }
250  else {
251    // If a category id has been posted, the newly created user must be
252    // allowed to navigate in this category, if it is not the case yet
253    if (
254      isset($_POST['ec_in_up_cat']) and
255      array_key_exists($_POST['ec_in_up_cat'], $ec_lists['categories'])
256    ) {
257      $private_uppercats = array_from_query("
258        SELECT `id`
259        FROM `".CATEGORIES_TABLE."`
260        WHERE `id` IN (".
261          implode(',', get_uppercat_ids(array($_POST['ec_in_up_cat']))).
262        ")
263        AND `status` = 'private';
264      ", 'id');
265      // We must not reinsert already existing lines in user_access table
266      $granteds = array();
267      foreach ($private_uppercats as $cat_id)
268       $granteds[$cat_id] = array();
269      $result = pwg_query("
270        SELECT `user_id`, `cat_id`
271        FROM `".USER_ACCESS_TABLE."`
272        WHERE `cat_id` IN (".implode(',', $private_uppercats).")
273          AND `user_id` = '$ec_user_id';
274      ");
275      while ($row = pwg_db_fetch_assoc($result))
276       $granteds[$row['cat_id']][] = $row['user_id'];
277      $inserts = array();
278      foreach ($private_uppercats as $cat_id)
279       if (!in_array($ec_user_id, $granteds[$cat_id]))
280        $inserts[] = array(
281          'user_id' => $ec_user_id,
282          'cat_id'  => $cat_id
283        );
284      if (count($inserts) != 0) {
285        mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts);
286        if (pwg_db_errno() == 0)
287         $page['infos'][] = sprintf(
288           l10n('ec_group_create_OK2'),
289           $_POST['login'], $ec_lists['categories'][$_POST['ec_in_up_cat']]
290         );
291        else
292         $page['errors'][] = sprintf(
293          l10n('ec_assoc_pb'),
294          $_POST['login'], $ec_lists['categories'][$_POST['ec_in_up_cat']]
295         ).' : MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
296      }
297    }
298     
299    // If an add. p. id has been posted, the newly created user should be
300    // allowed to navigate in this additional page => give a warning message
301    if (
302      isset($_POST['ec_in_up_aps']) and
303      array_key_exists($_POST['ec_in_up_aps'], $ec_lists['add_pages'])
304    ) $page['errors'][] = sprintf(
305      l10n('ec_user_access_AP'),
306      $ec_lists['add_pages'][$_POST['ec_in_up_aps']],
307      $_POST['login']
308    );
309  }
310  return $ec_user_id;
311}
312
313// +-----------------------------------------------------------------------+
314// | Tables building functions                                             |
315// +-----------------------------------------------------------------------+
316
317/*
318 * build_ec_duplicable_codes()
319 *
320 *
321 * @param
322 *   no parameter passed, the main material on which works the function, is
323 *   the global array variable $ec_lists.
324 * @return
325 *   (no return value)
326 */
327function build_ec_duplicable_codes() {
328  global $ec_lists, $template;
329  $ec_lists['duplicable_codes'] = array();
330  $t                            = array();
331  foreach ($ec_lists['ec_table'] as $ec_entry) {
332    if (
333      is_in($ec_entry['action'], 'ec_ok') and
334      $ec_entry['forced'] == 'false'
335    ) {
336      $t[$ec_entry['id']] = $ec_entry['code'];
337      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['id'] =
338       $ec_entry['id'];
339      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['comment'] =
340       $ec_entry['comment'];
341      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['user_id'] =
342       $ec_entry['user_id'];
343    }
344  }
345  foreach ($t as $ec_id => $ec_code) {
346    $ec_lists['duplicable_codes']['ids'][$ec_id] =
347     $ec_lists['duplicable_codes']['codes'][$ec_code]['id'];
348    $ec_lists['duplicable_codes']['comment'][$ec_id] =
349     $ec_lists['duplicable_codes']['codes'][$ec_code]['comment'];
350  }
351 
352  // Builds a category list displayed a best way
353  build_ec_categories(false);
354}
355
356// +-----------------------------------------------------------------------+
357// | Database modifying functions                                          |
358// +-----------------------------------------------------------------------+
359
360/*
361 * ec_create_modify_entry_OK()
362 * returns true or false whether the creation of a new entry described by
363 * $_POST was OK or not.
364 *
365 * @param
366 *   no param
367 * @return
368 *   true if creation was OK ; false if not
369 */
370function ec_create_modify_entry_OK() {
371  global $page, $ec_lists;
372 
373  // $_POST validity checks : action prevented in case of bad arguments
374 
375  if (!isset($_POST['ec_act1']))
376   return ec_end1('ec_act1', 'Bad argument : ');
377 
378  if (
379    ($_POST['ec_act1']) != 'toggle_forced' and
380    !isset($_POST['ec_input_action'])
381  ) return ec_end1('ec_input_action', 'Bad argument : ');
382 
383  $is_creation = true;
384  $ec_user_id = 'NULL';
385  $action = 'ec_ok';
386  $del_other = false;
387  $comment = '';
388  switch ($_POST['ec_act1']) {
389   
390    // This "switch" statement is a little bit tricky... it has been a pain to
391    // debug, and I wish to nobody to have to modify it :-\ !
392    // Its principle is that it manages checks for four occurrences of
393    // $_POST['ec_act1'] : 'create', 'modify_entry_submit',
394    // 'duplicate_entry_submit', and 'toggle_forced'. Some checks are mutual
395    // between different occurences, but never all checks of each occurrence
396    // of $_POST['ec_act1']. So tests are done with "if" statements to
397    // produce "break" statements when needed.
398   
399    case 'create':
400     
401      // Stops if given code or user type are incorrect
402      if (
403        !isset($_POST['ec_in_up_code']) or
404        !preg_match('/^[a-zA-Z0-9_-]{4,32}$/', $_POST['ec_in_up_code'])
405      ) return ec_end1('ec_in_up_code', 'Improper code : ');
406      else $ec_code = $_POST['ec_in_up_code'];
407     
408      foreach ($ec_lists['ec_table'] as $ec_entry)
409       if ($ec_code == $ec_entry['code'])
410        return ec_end1('ec_in_up_code', 'Code already exists : ');
411     
412      if (
413        !isset($_POST['ec_sel_user']) or (
414          $_POST['ec_sel_user'] != 'new' and
415          $_POST['ec_sel_user'] != 'old'
416        )
417      ) return ec_end1('ec_sel_user', 'Bad argument : ');
418   
419    case 'modify_entry_submit':
420     
421      // First checks for user type and/or value
422      if (isset($_POST['ec_sel_user'])) {
423        if ($_POST['ec_sel_user'] == 'new') {
424          if (
425            !isset($_POST['login']) or
426            $_POST['login'] == ''
427          ) return ec_end1('login', 'Bad argument : ');
428          if (in_array($_POST['login'], $ec_lists['user_ids']))
429           return ec_end1('login', 'User already exists : ');
430        }
431        elseif ($_POST['ec_sel_user'] == 'old') {
432          if (!isset($_POST['ec_in_up_usr_list']))
433           return ec_end1('login', 'Bad argument : ');
434          $ec_user_id = $_POST['ec_in_up_usr_list'];
435          if (!array_key_exists($ec_user_id, $ec_lists['user_ids']))
436           return ec_end1('ec_in_up_usr_list', 'User doesn\'t exist : ');
437        }
438        else $action = 'ec_nok';
439      }
440      else $action = 'ec_nok';
441     
442    if ($_POST['ec_act1'] == 'create') break;
443     
444    case 'duplicate_entry_submit':
445     
446      // Checks of entry value validity
447      if (
448        !isset($_POST['ec_entry_sel']) or
449        !array_key_exists($_POST['ec_entry_sel'], $ec_lists['ec_table'])
450      ) return ec_end1(
451        'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : '
452      );
453     
454      // Other checks for user type and value
455      if ($_POST['ec_act1'] == 'modify_entry_submit') if (
456        !isset($_POST['ec_sel_user']) or (
457          $_POST['ec_sel_user'] == 'new' or
458          $_POST['ec_sel_user'] == 'none' or (
459            $_POST['ec_sel_user'] == 'old' and
460            $_POST['ec_in_up_usr_list'] !=
461             $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']
462          ) or
463          isset($_POST['ec_in_up_forced'])
464        )
465      ) $del_other = true;
466     
467    case 'toggle_forced':
468     
469      // Establish default values for unchanged values
470      $ec_code    = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code'];
471      if ($action == 'ec_ok' and $ec_user_id == 'NULL')
472       $ec_user_id = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id'];
473      $arg1       = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg1'];
474      $arg2       = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg2'];
475      $comment    = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['comment'];
476      if (empty($arg1)) $arg1 = 'NULL';
477      if (empty($arg2)) $arg2 = 'NULL';
478      if (empty($ec_user_id)) $ec_user_id = 'NULL';
479      if ($_POST['ec_act1'] == 'toggle_forced') {
480        $forced = (
481          $ec_lists['ec_table'][$_POST['ec_entry_sel']]['forced'] == 'true'
482        ) ? 'false' : 'true';
483        $del_other = ($forced == 'true');
484        $action     = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['action'];
485      }
486     
487    if (
488      $_POST['ec_act1'] == 'toggle_forced' or
489      $_POST['ec_act1'] == 'modify_entry_submit'
490    ) {
491      $is_creation = false;
492      break;
493    }
494     
495      // Final check for entry value
496      build_ec_duplicable_codes();
497      if (!array_key_exists($_POST['ec_entry_sel'],
498       $ec_lists['duplicable_codes']['ids'])
499      ) return ec_end1(
500        'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : '
501      );
502     
503    break;
504    default: ec_end1('ec_act1', 'Bad argument : ');
505  }
506  // Pfew !
507 
508  if ($_POST['ec_act1'] != 'toggle_forced') {
509    // Preparation of $arg1, $arg2
510    switch ($_POST['ec_input_action']) {
511      case 'add_p': // Additional Page
512        if (isset($_POST['ec_in_up_aps'])) $arg2 = $_POST['ec_in_up_aps'];
513        else ec_end1('ec_in_up_aps', 'Bad argument : ');
514        $arg1 = 'NULL';
515      break;
516      case 'cat':   // Category
517      case 'img':   // Image
518        if (isset($_POST['ec_in_up_cat'])) {
519          $arg1 = $_POST['ec_in_up_cat'];
520          if ($_POST['ec_input_action'] == 'img') {
521            if (isset($_POST['ec_in_up_img'])) $arg2 = $_POST['ec_in_up_img'];
522            else ec_end1('ec_in_up_img', 'Bad argument : ');
523          }
524          else $arg2 = 'NULL';
525        }
526        else ec_end1('ec_in_up_cat', 'Bad argument : ');
527      break;
528      case 'home':    // Home : nothing to do : "arg"s are ''
529      case 'refused': // $_POST['ec_sel_user'] unset, nothing to do
530        $arg1 = 'NULL'; $arg2 = 'NULL';
531      break;
532      default: ec_end1('ec_input_action', 'Bad argument : ');
533    }
534   
535    // Preparation of $forced
536    $forced = (isset($_POST['ec_in_up_forced'])) ? 'true' : 'false';
537    if ($_POST['ec_act1'] == 'duplicate_entry_submit' and $forced == 'true')
538     return ec_end1('ec_in_up_forced', 'Bad argument : ');
539   
540    // Preparation of $comment
541    $comment = (isset($_POST['ec_in_up_comment'])) ?
542     $_POST['ec_in_up_comment'] : $comment;
543   
544    // User and eventually group creation, if needed
545    if ($_POST['ec_act1'] != 'duplicate_entry_submit')
546     if (isset($_POST['ec_sel_user']) and $_POST['ec_sel_user'] == 'new')
547      if (!($ec_user_id = ec_create_user_OK())) return false;
548  }
549 
550  // Now we have all infos : check that future entry doesn't exist already
551  $arg1p = ($arg1 == 'NULL') ? 'IS NULL' : ' = '.$arg1;
552  $arg2p = ($arg2 == 'NULL') ? 'IS NULL' : ' = '.$arg2;
553  $ec_user_idp = ($ec_user_id == 'NULL') ? 'IS NULL' : ' = '.$ec_user_id;
554  $r = pwg_query("
555    SELECT `id`
556    FROM `".EVNTCATS_TABLE."`
557    WHERE `code`    = '".$ec_code."'
558      AND `user_id`    ".$ec_user_idp."
559      AND `action`  = '".$action."'
560      AND `arg1`       ".$arg1p."
561      AND `arg2`       ".$arg2p."
562      AND `forced`  = '".$forced."'
563      AND `comment` = '".$comment."'
564  ");
565  if (pwg_db_num_rows($r)) { // print("<pre>$arg1 $arg2<br>$q</pre>");
566    $t1 = pwg_db_fetch_row();
567    $page['errors'][] = sprintf(l10n('ec_entry_already_exists'), $t1[0]);
568    return false;
569  }
570 
571  // Delete other entries using the same code, if needed
572  if ($del_other) {
573    $r = pwg_query("
574      SELECT `code`
575      FROM `".EVNTCATS_TABLE."`
576      WHERE `id` = ".$_POST['ec_entry_sel']
577    );
578    if (!pwg_db_num_rows($r)) die('Entry not found in DB ?!');
579    $t1 = pwg_db_fetch_row($r);
580    $r = pwg_query("
581      SELECT `id`
582      FROM `".EVNTCATS_TABLE."`
583      WHERE `code` = '".$t1[0]."'
584       AND `id` <> ".$_POST['ec_entry_sel']
585    );
586    while ($t2 = pwg_db_fetch_row($r)) if (!ec_delete_entry_OK($t2[0]))
587     return false;
588  }
589 
590  // Action !
591  $ret = true;
592  if ($is_creation) {
593    if (
594      pwg_query("
595        INSERT INTO `".EVNTCATS_TABLE."` (
596          `code`,
597          `user_id`,
598          `action`,
599          `arg1`, `arg2`, `forced`, `comment`
600        )
601        VALUES (
602          '".$ec_code."',
603           ".$ec_user_id.",
604          '".$action."',
605          ".$arg1.", ".$arg2.", '".$forced."', '".$comment."'
606        );
607      ") === false
608    ) {
609      $page['errors'][] =
610       l10n('ec_entry_create_pb').' : '.
611       'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"'
612      ;
613      $ret = false;
614    }
615    else {
616      // There is an issue with pwg_db_insert_id() (actually with procedures
617      // used in pwg_db_insert_id()): if last entry has id 2,
618      // but entries 0 and 1 have been deleted, created entry gets id 3,
619      // but pwg_db_insert_id() returns 0. Thus is it necessary here to get
620      // the id another way.
621      $r = pwg_query("
622        SELECT `id`
623        FROM `".EVNTCATS_TABLE."`
624        WHERE `code` = '".$ec_code."'"
625      );
626      $t2 = pwg_db_fetch_row($r);
627     
628      build_ec_lists(); // Don't remember exactly why, but must be done here
629      $forced = ($forced == 'false') ? '' : l10n('ec_cnfrm_forced');
630      $page['infos'][] =
631       sprintf(l10n('ec_entry_create_OK'), $t2[0]).
632       $ec_code.' => '.
633       $ec_lists['user_ids'][$ec_user_id].$forced
634      ;
635      return true;
636    }
637  }
638  else {
639    if (
640      pwg_query("
641        UPDATE `".EVNTCATS_TABLE."`
642        SET
643          `user_id` =  ".$ec_user_id.",
644          `action`  = '".$action."',
645          `arg1`    =  ".$arg1.",
646          `arg2`    =  ".$arg2.",
647          `forced`  = '".$forced."',
648          `comment` = '".$comment."'
649        WHERE `id`  = '".$_POST['ec_entry_sel']."'
650      ") === false
651    ) {
652      $page['errors'][] =
653       l10n('ec_entry_create_pb').' : '.
654       'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"'
655      ;
656      $ret = false;
657    }
658    else $page['infos'][] = sprintf(
659      l10n('ec_entry_modify_OK'), $_POST['ec_entry_sel']
660    );
661  }
662  build_ec_lists();
663  return $ret;
664}
665
666/*
667 * ec_delete_entry_OK($ec_id)
668 * tries to delete an existing entry.
669 *
670 * @param
671 *   $ec_id : the entry to be deleted
672 * @return
673 *   true or false whether deleting succeeded.
674 */
675function ec_delete_entry_OK($ec_id) {
676  global $page;
677  $r = pwg_query("
678    SELECT `code`
679    FROM `".EVNTCATS_TABLE."`
680    WHERE `id` = $ec_id;
681  ");
682  if (!pwg_db_num_rows($r)) {
683    $page['errors'][] = sprintf(l10n('ec_entry_dont_exist'), $ec_id);
684    return false;
685  }
686  $t = pwg_db_fetch_row($r);
687  if (pwg_query("
688    DELETE FROM `".EVNTCATS_TABLE."`
689    WHERE `id` = ".$ec_id
690  ) === false) {
691    $page['errors'][] =
692     sprintf(l10n('ec_entry_del_nok'),
693     $ec_id).
694     'MySQL error '.pwg_db_errno().', "'.pwg_db_error().'"';
695    return false;
696  }
697  $page['infos'][] = sprintf(l10n('ec_entry_del_ok'), $ec_id, $t[0]);
698  return true;
699}
700
701?>
Note: See TracBrowser for help on using the repository browser.