source: trunk/admin/group_list.php @ 19286

Last change on this file since 19286 was 19286, checked in by flop25, 11 years ago

bug:2425
New interface like the Batch Mananger :
rename, delete, duplicate and toggle-default

TODO soon merge & finalisation of the interface

  • Property svn:eol-style set to LF
File size: 9.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if( !defined("PHPWG_ROOT_PATH") )
25{
26  die ("Hacking attempt!");
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36if (!empty($_POST) or isset($_GET['delete']) or isset($_GET['toggle_is_default']))
37{
38  check_pwg_token();
39}
40// +-----------------------------------------------------------------------+
41// |                              add a group                              |
42// +-----------------------------------------------------------------------+
43
44if (isset($_POST['submit_add']))
45{
46  if (empty($_POST['groupname']))
47  {
48    array_push($page['errors'], l10n('The name of a group must not contain " or \' or be empty.'));
49  }
50  if (count($page['errors']) == 0)
51  {
52    // is the group not already existing ?
53    $query = '
54SELECT COUNT(*)
55  FROM '.GROUPS_TABLE.'
56  WHERE name = \''.$_POST['groupname'].'\'
57;';
58    list($count) = pwg_db_fetch_row(pwg_query($query));
59    if ($count != 0)
60    {
61      array_push($page['errors'], l10n('This name is already used by another group.'));
62    }
63  }
64  if (count($page['errors']) == 0)
65  {
66    // creating the group
67    $query = '
68INSERT INTO '.GROUPS_TABLE.'
69  (name)
70  VALUES
71  (\''.pwg_db_real_escape_string($_POST['groupname']).'\')
72;';
73    pwg_query($query);
74
75    array_push(
76      $page['infos'],
77      sprintf(l10n('group "%s" added'), $_POST['groupname'])
78      );
79  }
80}
81
82// +-----------------------------------------------------------------------+
83// |                             action send                               |
84// +-----------------------------------------------------------------------+
85if (isset($_POST['submit']) and isset($_POST['selectAction']) and isset($_POST['group_selection']))
86{
87  // if the user tries to apply an action, it means that there is at least 1
88  // photo in the selection
89  $groups = $_POST['group_selection'];
90  if (count($groups) == 0)
91  {
92    array_push($page['errors'], l10n('Select at least one group'));
93  }
94
95  $action = $_POST['selectAction'];
96
97  // +
98  // |rename a group
99  // +
100
101  if ($action=="rename")
102  {
103    foreach($groups as $group)
104    {
105      if ( !empty($_POST['rename_'.$group.'']) )
106      {
107        $query = '
108        UPDATE '.GROUPS_TABLE.'
109        SET name = \''.$_POST['rename_'.$group.''].'\'
110        WHERE id = '.$group.'
111      ;';
112        pwg_query($query);
113      }
114    }
115  }
116
117  // +
118  // |delete a group
119  // +
120
121  if ($action=="delete" and $_POST['confirm_deletion'])
122  {
123    foreach($groups as $group)
124    {
125        // destruction of the access linked to the group
126      $query = '
127    DELETE
128      FROM '.GROUP_ACCESS_TABLE.'
129      WHERE group_id = '.$group.'
130    ;';
131      pwg_query($query);
132     
133      // destruction of the users links for this group
134      $query = '
135    DELETE
136      FROM '.USER_GROUP_TABLE.'
137      WHERE group_id = '.$group.'
138    ;';
139      pwg_query($query);
140   
141      $query = '
142    SELECT name
143      FROM '.GROUPS_TABLE.'
144      WHERE id = '.$group.'
145    ;';
146      list($groupname) = pwg_db_fetch_row(pwg_query($query));
147     
148      // destruction of the group
149      $query = '
150    DELETE
151      FROM '.GROUPS_TABLE.'
152      WHERE id = '.$group.'
153    ;';
154      pwg_query($query);
155   
156      array_push(
157        $page['infos'],
158        sprintf(l10n('group "%s" deleted'), $groupname)
159        );
160    }
161  }
162
163  // +
164  // |duplicate a group
165  // +
166
167  if ($action=="duplicate" )
168  {
169    foreach($groups as $group)
170    {
171      if ( empty($_POST['duplicate_'.$group.'']) )
172      {
173        break;
174      }
175      // is the group not already existing ?
176      $query = '
177  SELECT COUNT(*)
178    FROM '.GROUPS_TABLE.'
179    WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
180  ;';
181      list($count) = pwg_db_fetch_row(pwg_query($query));
182      if ($count != 0)
183      {
184        array_push($page['errors'], l10n('This name is already used by another group.'));
185        break;
186      }
187      // creating the group
188      $query = '
189  INSERT INTO '.GROUPS_TABLE.'
190    (name)
191    VALUES
192    (\''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\')
193  ;';
194      pwg_query($query);
195      $query = '
196      SELECT id
197        FROM '.GROUPS_TABLE.'
198        WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
199      ;';
200     
201      list($groupid) = pwg_db_fetch_row(pwg_query($query));
202      $query = '
203    SELECT *
204      FROM '.GROUP_ACCESS_TABLE.'
205      WHERE group_id = '.$group.'
206    ;';
207      $grp_access = array();
208      $res=pwg_query($query);
209      while ($row = pwg_db_fetch_assoc($res))
210      {
211          $grp_access[] = array(
212            'cat_id' => $row['cat_id'],
213            'group_id' => $groupid
214          );
215      }
216      mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $grp_access);
217
218      $query = '
219    SELECT *
220      FROM '.USER_GROUP_TABLE.'
221      WHERE group_id = '.$group.'
222    ;';
223      $usr_grp = array();
224      $res=pwg_query($query);
225      while ($row = pwg_db_fetch_assoc($res))
226      {
227          $usr_grp[] = array(
228            'user_id' => $row['user_id'],
229            'group_id' => $groupid
230          );
231      }
232      mass_inserts(USER_GROUP_TABLE, array('user_id','group_id'), $usr_grp);
233 
234      array_push(
235        $page['infos'],
236        sprintf(l10n('group "%s" added'), $_POST['duplicate_'.$group.''])
237        );
238    }
239  }
240
241
242  // +
243  // | toggle_default
244  // +
245 
246  if ($action=="toggle_default")
247  {
248    foreach($groups as $group)
249    {
250      $query = '
251    SELECT name, is_default
252      FROM '.GROUPS_TABLE.'
253      WHERE id = '.$group.'
254    ;';
255      list($groupname, $is_default) = pwg_db_fetch_row(pwg_query($query));
256     
257      // update of the group
258      $query = '
259    UPDATE '.GROUPS_TABLE.'
260      SET is_default = \''.boolean_to_string(!get_boolean($is_default)).'\'
261      WHERE id = '.$group.'
262    ;';
263      pwg_query($query);
264   
265      array_push(
266        $page['infos'],
267        sprintf(l10n('group "%s" updated'), $groupname)
268        );
269    }
270  }
271}
272// +-----------------------------------------------------------------------+
273// |                             template init                             |
274// +-----------------------------------------------------------------------+
275
276$template->set_filenames(array('group_list' => 'group_list.tpl'));
277
278$template->assign(
279  array(
280    'F_ADD_ACTION' => get_root_url().'admin.php?page=group_list',
281    'U_HELP' => get_root_url().'admin/popuphelp.php?page=group_list',
282    'PWG_TOKEN' => get_pwg_token(),
283    )
284  );
285
286// +-----------------------------------------------------------------------+
287// |                              group list                               |
288// +-----------------------------------------------------------------------+
289
290$query = '
291SELECT id, name, is_default
292  FROM '.GROUPS_TABLE.'
293  ORDER BY name ASC
294;';
295$result = pwg_query($query);
296
297$admin_url = get_root_url().'admin.php?page=';
298$perm_url    = $admin_url.'group_perm&amp;group_id=';
299$del_url     = $admin_url.'group_list&amp;delete=';
300$members_url = $admin_url.'user_list&amp;group=';
301$toggle_is_default_url     = $admin_url.'group_list&amp;toggle_is_default=';
302
303while ($row = pwg_db_fetch_assoc($result))
304{
305  $query = '
306SELECT COUNT(*)
307  FROM '.USER_GROUP_TABLE.'
308  WHERE group_id = '.$row['id'].'
309;';
310  list($counter) = pwg_db_fetch_row(pwg_query($query));
311 
312  $template->append(
313    'groups',
314    array(
315      'NAME' => $row['name'],
316      'ID' => $row['id'],
317      'IS_DEFAULT' => (get_boolean($row['is_default']) ? ' ['.l10n('default').']' : ''),
318      'NB_MEMBERS' => $counter,
319      'MEMBERS' => l10n_dec('%d member', '%d members', $counter),
320      'U_MEMBERS' => $members_url.$row['id'],
321      'U_DELETE' => $del_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
322      'U_PERM' => $perm_url.$row['id'],
323      'U_ISDEFAULT' => $toggle_is_default_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
324      )
325    );
326}
327
328// +-----------------------------------------------------------------------+
329// |                           sending html code                           |
330// +-----------------------------------------------------------------------+
331
332$template->assign_var_from_handle('ADMIN_CONTENT', 'group_list');
333
334?>
Note: See TracBrowser for help on using the repository browser.