aboutsummaryrefslogtreecommitdiffstats
path: root/admin/cat_list.php
blob: a54a70491b28079ce3d15789e5474a6fcfdb2b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | file          : $Id$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

if (!defined('PHPWG_ROOT_PATH'))
{
  die('Hacking attempt!');
}

include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');

// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok                      |
// +-----------------------------------------------------------------------+
check_status(ACCESS_ADMINISTRATOR);

// +-----------------------------------------------------------------------+
// |                               functions                               |
// +-----------------------------------------------------------------------+

/**
 * save the rank depending on given categories order
 *
 * The list of ordered categories id is supposed to be in the same parent
 * category
 *
 * @param array categories
 * @return void
 */
function save_categories_order($categories)
{
  $current_rank = 0;
  $datas = array();
  foreach ($categories as $id)
  {
    array_push($datas, array('id' => $id, 'rank' => ++$current_rank));
  }
  $fields = array('primary' => array('id'), 'update' => array('rank'));
  mass_updates(CATEGORIES_TABLE, $fields, $datas);

  update_global_rank();
}

// +-----------------------------------------------------------------------+
// |                            initialization                             |
// +-----------------------------------------------------------------------+

$categories = array();

$base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
$navigation = '<a class="" href="'.$base_url.'">';
$navigation.= l10n('home');
$navigation.= '</a>';

// +-----------------------------------------------------------------------+
// |                    virtual categories management                      |
// +-----------------------------------------------------------------------+
// request to delete a virtual category / not for an adviser
if (isset($_GET['delete']) and is_numeric($_GET['delete']) and !is_adviser())
{
  delete_categories(array($_GET['delete']));
  array_push($page['infos'], l10n('cat_virtual_deleted'));
  update_global_rank();
}
// request to add a virtual category
else if (isset($_POST['submitAdd']))
{
  $output_create = create_virtual_category(
    $_POST['virtual_name'],
    @$_GET['parent_id']
    );

  if (isset($output_create['error']))
  {
    array_push($page['errors'], $output_create['error']);
  }
  else
  {
    array_push($page['infos'], $output_create['info']);
  }
}
// save manual category ordering
else if (isset($_POST['submitOrder']))
{
  asort($_POST['catOrd'], SORT_NUMERIC);
  save_categories_order(array_keys($_POST['catOrd']));

  array_push(
    $page['infos'],
    l10n('Categories manual order was saved')
    );
}
// sort categories alpha-numerically
else if (isset($_POST['submitOrderAlphaNum']))
{
  $query = '
SELECT id, name
  FROM '.CATEGORIES_TABLE.'
  WHERE id_uppercat '.
    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
;';
  $result = pwg_query($query);
  while ($row = mysql_fetch_assoc($result))
  {
    $categories[ $row['id'] ] = strtolower($row['name']);
  }

  asort($categories, SORT_REGULAR);
  save_categories_order(array_keys($categories));

  array_push(
    $page['infos'],
    l10n('Categories ordered alphanumerically')
    );
}

// +-----------------------------------------------------------------------+
// |                            Navigation path                            |
// +-----------------------------------------------------------------------+

if (isset($_GET['parent_id']))
{
  $navigation.= $conf['level_separator'];

  $navigation.= get_cat_display_name_from_id(
    $_GET['parent_id'],
    $base_url.'&amp;parent_id=',
    false
    );
}
// +-----------------------------------------------------------------------+
// |                       template initialization                         |
// +-----------------------------------------------------------------------+
$template->set_filenames(array('categories'=>'admin/cat_list.tpl'));

$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
if (isset($_GET['parent_id']))
{
  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
}

$template->assign_vars(array(
  'CATEGORIES_NAV'=>$navigation,
  'F_ACTION'=>$form_action,

  'L_ADD_VIRTUAL'=>l10n('cat_add'),
  'L_SUBMIT'=>l10n('submit'),
  'L_STORAGE'=>l10n('storage'),
  'L_NB_IMG'=>l10n('pictures'),
  'L_MOVE_UP'=>l10n('up'),
  'L_EDIT'=>l10n('edit'),
  'L_DELETE'=>l10n('delete'),
 ));

$tpl = array('cat_first','cat_last');
// +-----------------------------------------------------------------------+
// |                          Categories display                           |
// +-----------------------------------------------------------------------+

$categories = array();

$query = '
SELECT id, name, permalink, dir, rank, nb_images, status
  FROM '.CATEGORIES_TABLE;
if (!isset($_GET['parent_id']))
{
  $query.= '
  WHERE id_uppercat IS NULL';
}
else
{
  $query.= '
  WHERE id_uppercat = '.$_GET['parent_id'];
}
$query.= '
  ORDER BY rank ASC
;';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
  $categories[$row['id']] = $row;
  // by default, let's consider there is no sub-categories. This will be
  // calculated after.
  $categories[$row['id']]['nb_subcats'] = 0;
}

if (count($categories) > 0)
{
  $query = '
SELECT id_uppercat, COUNT(*) AS nb_subcats
  FROM '. CATEGORIES_TABLE.'
  WHERE id_uppercat IN ('.implode(',', array_keys($categories)).')
  GROUP BY id_uppercat
;';
  $result = pwg_query($query);
  while ($row = mysql_fetch_array($result))
  {
    $categories[$row['id_uppercat']]['nb_subcats'] = $row['nb_subcats'];
  }

  $template->assign_block_vars('categories', array());
}

foreach ($categories as $category)
{
  // TODO : not used anymore ?
  //$images_folder = PHPWG_ROOT_PATH.'template/';
  //$images_folder.= $user['template'].'/admin/images';

  $base_url = PHPWG_ROOT_PATH.'admin.php?page=';
  $cat_list_url = $base_url.'cat_list';

  $self_url = $cat_list_url;
  if (isset($_GET['parent_id']))
  {
    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
  }

  $template->assign_block_vars(
    'categories.category',
    array(
      'NAME'       => 
        trigger_event(
          'render_category_name',
          $category['name'],
          'admin_cat_list'
          ),
      'ID'         => $category['id'],
      'RANK'       => $category['rank']*10,

      'U_JUMPTO'   => make_index_url(
        array(
          'category' => $category
          )
        ),

      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
      'U_EDIT'     => $base_url.'cat_modify&amp;cat_id='.$category['id'],
      )
    );

  if (empty($category['dir']))
  {
    $template->assign_block_vars(
      'categories.category.delete',
      array(
        'URL'=>$self_url.'&amp;delete='.$category['id']
        )
      );
    $template->assign_block_vars(
      'categories.category.virtual',
      array(
        'CLASS' => 'virtual_cat',
        )
      );
  }

  if ($category['nb_images'] > 0)
  {
    $template->assign_block_vars(
      'categories.category.elements',
      array(
        'URL'=>$base_url.'element_set&amp;cat='.$category['id']
        )
      );
  }

  if ('private' == $category['status'])
  {
    $template->assign_block_vars(
      'categories.category.permissions',
      array(
        'URL'=>$base_url.'cat_perm&amp;cat='.$category['id']
        )
      );
  }
}
// Add a link to Page bottom only if needed (10 or more categories)
if ( isset($category['rank']) and $category['rank'] > 9 )
{
  $template->assign_block_vars('eop_link', array('ICON'=>'Displayed'));
}
// +-----------------------------------------------------------------------+
// |                          sending html code                            |
// +-----------------------------------------------------------------------+
$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
?>