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

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

[Event Cats] Separate functions

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