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

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

[Event Cats] Continue entry creation in DB

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