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

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

[Event Cats] Continue entry creation in DB

File size: 13.9 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 : 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;
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';
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;
76 
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() {
95  global $ec_lists, $template;
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  }
114 
115  // Builds a category list displayed a best way
116  build_ec_categories(false);
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 
136/*
137
138(
139    [ec_act1] => create
140    [ec_entry_sel] => -1
141    [ec_sel_code] => new
142    [ec_in_up_auto_code_length] => 10
143    [ec_in_up_code] => 94q0V1067D
144    [ec_sel_user] => new
145    [ec_in_up_usr_txt] => san_gimi
146    [ec_in_up_psd_txt] => 7Mv8QxZV
147    [ec_in_up_newgroup] => on
148    [ec_in_up_grp_txt] => EC_san_gimi
149    [ec_input_action] => cat
150    [ec_in_up_cat] => 6
151    [ec_in_up_img] =>
152)
153
154(
155    [ec_act1] => create
156    [ec_entry_sel] => -1
157    [ec_sel_code] => new
158    [ec_in_up_auto_code_length] => 10
159    [ec_in_up_code] => NU1pah0qVJ
160    [ec_in_up_usr_txt] =>
161    [ec_in_up_psd_txt] =>
162    [ec_in_up_grp_txt] =>
163    [ec_sel_user] => old
164    [ec_in_up_usr_list] => 9
165    [ec_input_action] => cat
166    [ec_in_up_cat] => 6
167    [ec_in_up_img] =>
168)
169
170*/
171  // $_POST validity checks : creation prevented in case of bad arguments
172  /* if (
173    !isset($_POST['ec_in_up_code']) or
174    !ereg('^[a-zA-Z0-9_-]{4,32}$', $_POST['ec_in_up_code'])
175  ) return ec_end1('ec_in_up_code', 'ec_bad_argument7');
176 
177  foreach ($ec_lists['ec_table'] as $ec_entry)
178   if ($_POST['ec_in_up_code'] == $ec_entry['code'])
179    return ec_end1('ec_in_up_code', 'ec_bad_argument2');
180 
181  if (
182    !isset($_POST['ec_sel_user']) or (
183      $_POST['ec_sel_user'] != 'new' and
184      $_POST['ec_sel_user'] != 'old'
185    )
186  ) return ec_end1('ec_sel_user', 'ec_bad_argument1');
187 
188  if ($_POST['ec_sel_user'] == 'new') {
189    if (
190      !isset($_POST['ec_in_up_usr_txt']) or
191      $_POST['ec_in_up_usr_txt'] == ''
192    ) return ec_end1('ec_in_up_usr_txt', 'ec_bad_argument1');
193    $t1 = (in_array($_POST['ec_in_up_usr_txt'], $ec_lists['user_ids']));
194    if ($t1) return ec_end1('ec_in_up_usr_txt', 'ec_bad_argument3');
195  }
196  else {
197    if (!isset($_POST['ec_in_up_usr_list']))
198     return ec_end1('ec_in_up_usr_txt', 'ec_bad_argument1');
199    $ec_user_id = $_POST['ec_in_up_usr_list'];
200    $t1 = (array_key_exists($ec_user_id, $ec_lists['user_ids']));
201    if (!$t1) return ec_end1('ec_in_up_usr_list', 'ec_bad_argument6');
202  }
203 
204  // code and user_id (if needed) are OK, creation can be done
205  if ($_POST['ec_sel_user'] == 'new') {
206   
207    // User creation, as generic
208    $page['errors'] = register_user(
209     $_POST['ec_in_up_usr_txt'], $_POST['ec_in_up_psd_txt'], '', false
210    );
211    if (
212      count($page['errors']) != 0 or
213      !($ec_user_id = get_userid($_POST['ec_in_up_usr_txt']))
214    ) {
215      array_unshift($page['errors'], l10n('ec_user_create_pb'));
216      return false;
217    }
218    else
219     $page['infos'][] = l10n('ec_user_create_OK').$_POST['ec_in_up_usr_txt'];
220    if (
221      pwg_query("
222        UPDATE `".USER_INFOS_TABLE."`
223        SET `status` = 'generic'
224        WHERE `user_id` = ".$ec_user_id.";
225      ") !== false
226    )
227     $page['infos'][] = l10n('ec_user_generic_OK').$_POST['ec_in_up_usr_txt'];
228    else
229     $page['errors'][] =l10n('ec_user_generic_pb').$_POST['ec_in_up_usr_txt'];
230   
231    // New group creation if required,
232    // and association with user_id at the same time
233    if (
234      isset($_POST['ec_in_up_newgroup']) and
235      isset($_POST['ec_in_up_grp_txt']) and
236      $_POST['ec_in_up_grp_txt'] != ''
237    ) {
238      $t2 = 0; $t4 = false;
239      while (
240        !($t3 = mysql_fetch_row(pwg_query("
241          SELECT `id`
242          FROM `".GROUPS_TABLE."`
243          WHERE `name` = '".$_POST['ec_in_up_grp_txt']."';
244        "))) and
245        $t2++ == 0
246      ) $t4 = pwg_query("
247          INSERT INTO `".GROUPS_TABLE."` (`name`, `is_default`)
248          VALUES ('".$_POST['ec_in_up_grp_txt']."', 'false');
249        ");
250      if ($t4)
251       $page['infos'][]=l10n('ec_group_create_OK').$_POST['ec_in_up_grp_txt'];
252      if (!$t3)
253       $page['errors'][] =
254        l10n('ec_group_create_pb').' (1) ; '.
255        'MySQL error '.mysql_errno().', "'.mysql_error().'"';
256      if (
257        pwg_query("
258          INSERT INTO `".USER_GROUP_TABLE."` (`user_id `, `group_id`)
259          VALUES ('".$ec_user_id."', '".$t3['id']."');
260        ") === false
261      ) $page['errors'][] = ;
262       l10n('ec_group_create_pb').' (2) ; '.
263       'MySQL error '.mysql_errno().', "'.mysql_error().'"';
264      else
265       $page['infos'][] =
266        $_POST['ec_in_up_usr_txt'].
267        l10n('ec_group_create_OK2').
268        $_POST['ec_in_up_grp_txt']
269       ;
270    }
271  }
272 
273  // Creation of new line in DB event_cats table with all arguments
274  $arg1 = ''; $arg2 = '';
275  if (isset($_POST['ec_input_action'])) switch ($_POST['ec_input_action']) {
276    // case 'home':  // Home : nothing to do : "arg"s are ''
277    case 'add_p': // Additional Page
278      if (isset($_POST['ec_in_up_aps'])) $arg2 = $_POST['ec_in_up_aps'];
279      else ec_end1('ec_in_up_aps', 'ec_bad_argument1');
280    break;
281    case 'cat':   // Category
282    case 'img':   // Image
283      if (isset($_POST['ec_in_up_cat'])) {
284        $arg2 = $_POST['ec_in_up_cat'];
285        if ($_POST['ec_input_action'] == 'img') {
286          if (isset($_POST['ec_in_up_img'])) $arg2 = $_POST['ec_in_up_img'];
287          else ec_end1('ec_in_up_img', 'ec_bad_argument1');
288        }
289      }
290      else ec_end1('ec_in_up_cat', 'ec_bad_argument1');
291    break;
292    default : ec_end1('ec_input_action', 'ec_bad_argument1');
293  }
294  else ec_end1('ec_input_action', 'ec_bad_argument1');
295 
296  $forced = (isset($_POST['[ec_in_up_forced]'])) ? 'true' : 'false';
297 
298  // Now we have the code, the user_id, arg1, arg2, and forced arguments
299  */
300 
301  $page['errors'][] = 'Y\'a p\'têt\' ben eu une erreur...';
302}
303
304/*
305 * ec_duplicate_entry_OK()
306 * returns true or false whether the duplication of an existing entry which #
307 * is given by $_POST['ec_entry_sel'] was OK or not.
308 *
309 * @param
310 *   no param
311 * @return
312 *   true if creation was OK ; false if not
313 */
314function ec_duplicate_entry_OK() {
315  global $page, $ec_lists;
316 
317  build_ec_duplicable_codes();
318  if (array_key_exists($_POST['ec_entry_sel'],
319   $ec_lists['duplicable_codes']['ids'])) {
320    $arg1 = ''; $arg2 = '';
321    switch ($_POST['ec_input_action']) {
322      case 'add_p':
323        if (array_key_exists($_POST['ec_in_up_aps'],$ec_lists['add_pages'])) {
324         $arg2 = $_POST['ec_in_up_aps']; }
325        else return ec_end1('ec_in_up_aps', 'ec_bad_argument4');
326      case 'home':
327      break;
328      case 'img':
329      case 'cat':
330        if (array_key_exists($_POST['ec_in_up_cat'],
331         $ec_lists['categories'])) {
332          if ($_POST['ec_input_action'] == 'img') {
333            if (
334             ec_image_exists($_POST['ec_in_up_cat'], $_POST['ec_in_up_img'])
335            ) $arg2 = $_POST['ec_in_up_img'];
336            else return ec_end1('ec_in_up_img', 'ec_bad_argument4');
337          }
338          $arg1 = $_POST['ec_in_up_cat'];
339        }
340        else return ec_end1('ec_in_up_cat', 'ec_bad_argument4');
341      break;
342      default: return ec_end1('ec_input_action', 'ec_bad_argument1');
343    }
344    if (pwg_query("
345      INSERT INTO `".EVNTCATS_TABLE."`
346       (`code`, `user_id`, `action`, `arg1`, `arg2`)
347      VALUES (
348        '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['code']."',
349        '".$ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']."',
350        'ec_ok',
351        '".$arg1."',
352        '".$arg2."'
353      );
354    ")) {
355      $page['infos'][] =
356       l10n('ec_entry_dup_ok_pre').
357       $_POST['ec_entry_sel'].' ('.
358       $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code'].')'.
359       l10n('ec_entry_dup_ok_end')
360      ;
361      build_ec_table();
362      return true;
363    }
364    else {
365      $page['errors'][] =
366       l10n('ec_entry_dup_nok_pre1').
367       $_POST['ec_entry_sel'].
368       l10n('ec_entry_dup_nok_end1').
369       'MySQL error '.mysql_errno().', "'.mysql_error().'"'
370      ;
371      return false;
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.