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

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

[Event Cats] Entry creation in DB OK.

File size: 13.7 KB
RevLine 
[4169]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 : Admin functions (include)                  **
27// ** for Piwigo plugin Event Cats                                          **
28// ***************************************************************************
29
30// +-----------------------------------------------------------------------+
31// | Header                                                                |
32// +-----------------------------------------------------------------------+
33
34global $conf, $page;
35
36// +-----------------------------------------------------------------------+
37// | Utilities functions                                                   |
38// +-----------------------------------------------------------------------+
39
40/*
41 * ec_end1()
42 * Process repetitive task when error in database modifying functions.
43 *
44 * @param
45 *   $pst : $_POST argument
46 *   $msg : message
47 * @return
48 *   false as this function is used when there is a problem
49 */
50function ec_end1($pst, $msg) {
51  global $page;
[4178]52  if (isset($_POST[$pst]))
53   $page['errors'][] =
54    l10n($msg).
55    '$_POST[\''.$pst.'\'] = '.
56    $_POST[$pst];
57  else
58    $page['errors'][] =
59    l10n($msg).
60    '$_POST[\''.$pst.'\'] unset';
[4169]61  return false;
62}
63
64/*
65 * ec_end2()
66 * Process repetitive task when error in database modifying functions.
67 *
68 * @param
69 *   $msg : message
70 *   $num : number precising the point where the error occurred
71 * @return
72 *   false as this function is used when there is a problem
73 */
74function ec_end2($msg, $num) {
75  global $page;
[4178]76 
[4169]77  return false;
78}
79
80// +-----------------------------------------------------------------------+
81// | Tables building functions                                             |
82// +-----------------------------------------------------------------------+
83
84/*
85 * build_ec_duplicable_codes()
86 *
87 *
88 * @param
89 *   no parameter passed, the main material on which works the function, is
90 *   the global array variable $ec_lists.
91 * @return
92 *   (no return value)
93 */
94function build_ec_duplicable_codes() {
[4173]95  global $ec_lists, $template;
[4169]96  $ec_lists['duplicable_codes'] = array();
97  $t                            = array();
98  foreach ($ec_lists['ec_table'] as $ec_entry) {
99    if (
100      is_in($ec_entry['action'], 'ec_ok') and
101      $ec_entry['forced'] == 'false'
102    ) {
103      $t[$ec_entry['id']] = $ec_entry['code'];
104      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['id'] =
105       $ec_entry['id'];
106      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['user_id'] =
107       $ec_entry['user_id'];
108    }
109  }
110  foreach ($t as $ec_id => $ec_code) {
111    $ec_lists['duplicable_codes']['ids'][$ec_id] =
112     $ec_lists['duplicable_codes']['codes'][$ec_code]['id'];
113  }
[4173]114 
115  // Builds a category list displayed a best way
116  build_ec_categories(false);
[4169]117}
118
119// +-----------------------------------------------------------------------+
120// | Database modifying functions                                          |
121// +-----------------------------------------------------------------------+
122
123/*
124 * ec_create_entry_OK()
125 * returns true or false whether the creation of a new entry described by
126 * $_POST was OK or not.
127 *
128 * @param
129 *   no param
130 * @return
131 *   true if creation was OK ; false if not
132 */
133function ec_create_entry_OK() {
134  global $page, $ec_lists;
135 
[4173]136  // $_POST validity checks : creation prevented in case of bad arguments
[4179]137  if (
[4173]138    !isset($_POST['ec_in_up_code']) or
139    !ereg('^[a-zA-Z0-9_-]{4,32}$', $_POST['ec_in_up_code'])
140  ) return ec_end1('ec_in_up_code', 'ec_bad_argument7');
141 
142  foreach ($ec_lists['ec_table'] as $ec_entry)
143   if ($_POST['ec_in_up_code'] == $ec_entry['code'])
144    return ec_end1('ec_in_up_code', 'ec_bad_argument2');
145 
146  if (
147    !isset($_POST['ec_sel_user']) or (
148      $_POST['ec_sel_user'] != 'new' and
149      $_POST['ec_sel_user'] != 'old'
150    )
151  ) return ec_end1('ec_sel_user', 'ec_bad_argument1');
152 
153  if ($_POST['ec_sel_user'] == 'new') {
154    if (
[4179]155      !isset($_POST['login']) or
156      $_POST['login'] == ''
157    ) return ec_end1('login', 'ec_bad_argument1');
158    if (in_array($_POST['login'], $ec_lists['user_ids']))
159     return ec_end1('login', 'ec_bad_argument3');
[4169]160  }
[4173]161  else {
162    if (!isset($_POST['ec_in_up_usr_list']))
[4179]163     return ec_end1('login', 'ec_bad_argument1');
[4173]164    $ec_user_id = $_POST['ec_in_up_usr_list'];
[4179]165    if (!array_key_exists($ec_user_id, $ec_lists['user_ids']))
166     return ec_end1('ec_in_up_usr_list', 'ec_bad_argument6');
[4173]167  }
168 
169  // code and user_id (if needed) are OK, creation can be done
[4169]170  if ($_POST['ec_sel_user'] == 'new') {
[4178]171   
172    // User creation, as generic
[4173]173    $page['errors'] = register_user(
[4179]174     $_POST['login'], $_POST['password'], '', false
[4173]175    );
176    if (
177      count($page['errors']) != 0 or
[4179]178      !($ec_user_id = get_userid($_POST['login']))
[4173]179    ) {
180      array_unshift($page['errors'], l10n('ec_user_create_pb'));
181      return false;
182    }
[4178]183    else
[4179]184     $page['infos'][] = l10n('ec_user_create_OK').$_POST['login'];
[4173]185    if (
[4178]186      pwg_query("
187        UPDATE `".USER_INFOS_TABLE."`
188        SET `status` = 'generic'
189        WHERE `user_id` = ".$ec_user_id.";
190      ") !== false
191    )
[4179]192     $page['infos'][] = l10n('ec_user_generic_OK').$_POST['login'];
[4178]193    else
[4179]194     $page['errors'][] =l10n('ec_user_generic_pb').$_POST['login'];
[4178]195   
196    // New group creation if required,
197    // and association with user_id at the same time
198    if (
[4173]199      isset($_POST['ec_in_up_newgroup']) and
200      isset($_POST['ec_in_up_grp_txt']) and
201      $_POST['ec_in_up_grp_txt'] != ''
[4178]202    ) {
203      $t2 = 0; $t4 = false;
204      while (
205        !($t3 = mysql_fetch_row(pwg_query("
[4173]206          SELECT `id`
207          FROM `".GROUPS_TABLE."`
208          WHERE `name` = '".$_POST['ec_in_up_grp_txt']."';
[4178]209        "))) and
210        $t2++ == 0
211      ) $t4 = pwg_query("
212          INSERT INTO `".GROUPS_TABLE."` (`name`, `is_default`)
213          VALUES ('".$_POST['ec_in_up_grp_txt']."', 'false');
214        ");
215      if ($t4)
216       $page['infos'][]=l10n('ec_group_create_OK').$_POST['ec_in_up_grp_txt'];
217      if (!$t3)
218       $page['errors'][] =
219        l10n('ec_group_create_pb').' (1) ; '.
220        'MySQL error '.mysql_errno().', "'.mysql_error().'"';
[4173]221      if (
[4178]222        pwg_query("
[4179]223          INSERT INTO `".USER_GROUP_TABLE."` (`user_id`, `group_id`)
224          VALUES ('".$ec_user_id."', '".$t3[0]."');
[4178]225        ") === false
[4179]226      ) $page['errors'][] =
[4178]227       l10n('ec_group_create_pb').' (2) ; '.
228       'MySQL error '.mysql_errno().', "'.mysql_error().'"';
229      else
230       $page['infos'][] =
[4179]231        $_POST['login'].
[4178]232        l10n('ec_group_create_OK2').
233        $_POST['ec_in_up_grp_txt']
234       ;
[4169]235    }
236  }
[4178]237 
[4179]238  // Preparation of $arg1, $arg2 and $forced
[4178]239  $arg1 = ''; $arg2 = '';
240  if (isset($_POST['ec_input_action'])) switch ($_POST['ec_input_action']) {
241    // case 'home':  // Home : nothing to do : "arg"s are ''
242    case 'add_p': // Additional Page
243      if (isset($_POST['ec_in_up_aps'])) $arg2 = $_POST['ec_in_up_aps'];
244      else ec_end1('ec_in_up_aps', 'ec_bad_argument1');
245    break;
246    case 'cat':   // Category
247    case 'img':   // Image
248      if (isset($_POST['ec_in_up_cat'])) {
[4179]249        $arg1 = $_POST['ec_in_up_cat'];
[4178]250        if ($_POST['ec_input_action'] == 'img') {
251          if (isset($_POST['ec_in_up_img'])) $arg2 = $_POST['ec_in_up_img'];
252          else ec_end1('ec_in_up_img', 'ec_bad_argument1');
253        }
[4169]254      }
[4178]255      else ec_end1('ec_in_up_cat', 'ec_bad_argument1');
256    break;
[4179]257    default: ec_end1('ec_input_action', 'ec_bad_argument1');
[4169]258  }
[4178]259  else ec_end1('ec_input_action', 'ec_bad_argument1');
[4179]260  $forced = (isset($_POST['ec_in_up_forced'])) ? 'true' : 'false';
[4178]261 
262  // Now we have the code, the user_id, arg1, arg2, and forced arguments
[4179]263  if (
264    pwg_query("
265      INSERT INTO `".EVNTCATS_TABLE."` (
266        `code`,
267        `user_id`,
268        `action`,
269        `arg1`, `arg2`, `forced`
270      )
271      VALUES (
272        '".$_POST['ec_in_up_code']."',
273        '".$ec_user_id."',
274        'ec_ok',
275        '".$arg1."', '".$arg2."', '".$forced."'
276      );
277    ") === false
278  ) {
279    $page['errors'][] =
280     l10n('ec_entry_create_pb').
281     'MySQL error '.mysql_errno().', "'.mysql_error().'"'
282    ;
283    return false;
284  }
285  else {
286    build_ec_lists();
287    $forced = ($forced == 'false') ? '' : l10n('ec_cnfrm_forced');
288    $page['infos'][] =
289     l10n('ec_entry_create_OK').mysql_insert_id().' : '.
290     $_POST['ec_in_up_code'].' => '.
291     $ec_lists['user_ids'][$ec_user_id].$forced
292    ;
293    return true;
294  }
295  /*
[4169]296 
297  $page['errors'][] = 'Y\'a p\'têt\' ben eu une erreur...';
[4179]298 
299   */
[4169]300}
301
302/*
303 * ec_duplicate_entry_OK()
304 * returns true or false whether the duplication of an existing entry which #
305 * is given by $_POST['ec_entry_sel'] was OK or not.
306 *
307 * @param
308 *   no param
309 * @return
310 *   true if creation was OK ; false if not
311 */
312function ec_duplicate_entry_OK() {
313  global $page, $ec_lists;
314 
315  build_ec_duplicable_codes();
316  if (array_key_exists($_POST['ec_entry_sel'],
317   $ec_lists['duplicable_codes']['ids'])) {
318    $arg1 = ''; $arg2 = '';
319    switch ($_POST['ec_input_action']) {
320      case 'add_p':
321        if (array_key_exists($_POST['ec_in_up_aps'],$ec_lists['add_pages'])) {
322         $arg2 = $_POST['ec_in_up_aps']; }
323        else return ec_end1('ec_in_up_aps', 'ec_bad_argument4');
324      case 'home':
325      break;
326      case 'img':
327      case 'cat':
328        if (array_key_exists($_POST['ec_in_up_cat'],
329         $ec_lists['categories'])) {
330          if ($_POST['ec_input_action'] == 'img') {
331            if (
332             ec_image_exists($_POST['ec_in_up_cat'], $_POST['ec_in_up_img'])
333            ) $arg2 = $_POST['ec_in_up_img'];
334            else return ec_end1('ec_in_up_img', 'ec_bad_argument4');
335          }
336          $arg1 = $_POST['ec_in_up_cat'];
337        }
338        else return ec_end1('ec_in_up_cat', 'ec_bad_argument4');
339      break;
340      default: return ec_end1('ec_input_action', 'ec_bad_argument1');
341    }
[4179]342    if (
343      pwg_query("
344        INSERT INTO `".EVNTCATS_TABLE."`
345         (`code`, `user_id`, `action`, `arg1`, `arg2`)
346        VALUES (
347          '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['code']."',
348          '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']."',
349          'ec_ok',
350          '".$arg1."',
351          '".$arg2."'
352        );
353      ") === false
354    ) {
355      $page['errors'][] =
356       l10n('ec_entry_dup_nok_pre1').
357       $_POST['ec_entry_sel'].
358       l10n('ec_entry_dup_nok_end1').
359       'MySQL error '.mysql_errno().', "'.mysql_error().'"'
360      ;
361      return false;
362    }
363    else {
[4169]364      $page['infos'][] =
365       l10n('ec_entry_dup_ok_pre').
366       $_POST['ec_entry_sel'].' ('.
367       $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code'].')'.
368       l10n('ec_entry_dup_ok_end')
369      ;
370      build_ec_table();
371      return true;
372    }
373  }
374  else return ec_end1('ec_entry_sel', 'ec_bad_argument5');
375}
376
377/*
378 * ec_modify_entry_OK()
379 * returns true or false whether the modification of an existing entry which #
380 * is given by $_POST['ec_entry_sel'] was OK or not.
381 *
382 * @param
383 *   no param
384 * @return
385 *   true if modification was OK ; false if not
386 */
387function ec_modify_entry_OK() {
388  global $page;
389  $page['errors'][] = 'Y\'a pas eu une erreur ?...';
390  return false;
391}
392
393/*
394 * ec_toggle_forced_entry()
395 * Toggles the 'forced' property of an entry, provided it complies to the
396 * rules.
397 *
398 * @param
399 *   no param
400 * @return
401 *   no return value
402 */
403function ec_toggle_forced_entry() {
404  global $page;
405  $page['errors'][] = 'ec_toggle_forced_entry';
406}
407
408/*
409 * ec_delete_entry()
410 * tries to delete an existing entry which # is given by
411 * $_POST['ec_entry_sel'].
412 *
413 * @param
414 *   no param
415 * @return
416 *   no return value : modifies $page['errors'] or $page['infos']
417 */
418function ec_delete_entry() {
419  global $page, $ec_lists;
420  if (array_key_exists($_POST['ec_entry_sel'], $ec_lists['ec_table'])) {
421    if (!pwg_query('
422      DELETE FROM `'.EVNTCATS_TABLE.'`
423      WHERE `id` = '.$_POST['ec_entry_sel']
424    )) {
425      $page['errors'][] =
426        l10n('ec_entry_del_nok_pre').
427        $_POST['ec_entry_sel'].
428        l10n('ec_entry_del_nok_end').
429        'MySQL error '.mysql_errno().', "'.mysql_error().'"'
430      ;
431    }
432    else {
433      $page['infos'][] =
434        l10n('ec_entry_del_ok_pre').
435        $_POST['ec_entry_sel'].
436        l10n('ec_entry_del_ok_end');
437      build_ec_table();
438    }
439  }
440  else ec_end1('ec_entry_sel', 'ec_bad_argument1');
441}
442
443?>
Note: See TracBrowser for help on using the repository browser.