Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
6 kaklik 1
<?php
2
// +-----------------------------------------------------------------------+
3
// | PhpWebGallery - a PHP based picture gallery                           |
4
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5
// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6
// +-----------------------------------------------------------------------+
7
// | branch        : BSF (Best So Far)
8
// | file          : $RCSfile: cat_list.php,v $
9
// | last update   : $Date: 2005/05/01 14:19:38 $
10
// | last modifier : $Author: plg $
11
// | revision      : $Revision: 1.32.2.1 $
12
// +-----------------------------------------------------------------------+
13
// | This program is free software; you can redistribute it and/or modify  |
14
// | it under the terms of the GNU General Public License as published by  |
15
// | the Free Software Foundation                                          |
16
// |                                                                       |
17
// | This program is distributed in the hope that it will be useful, but   |
18
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20
// | General Public License for more details.                              |
21
// |                                                                       |
22
// | You should have received a copy of the GNU General Public License     |
23
// | along with this program; if not, write to the Free Software           |
24
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25
// | USA.                                                                  |
26
// +-----------------------------------------------------------------------+
27
 
28
if (!defined('PHPWG_ROOT_PATH'))
29
{
30
  die('Hacking attempt!');
31
}
32
include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
33
// +-----------------------------------------------------------------------+
34
// |                            initialization                             |
35
// +-----------------------------------------------------------------------+
36
$errors = array();
37
$infos = array();
38
$categories = array();
39
$navigation = $lang['home'];
40
// +-----------------------------------------------------------------------+
41
// |                    virtual categories management                      |
42
// +-----------------------------------------------------------------------+
43
// request to delete a virtual category
44
if (isset($_GET['delete']) and is_numeric($_GET['delete']))
45
{
46
  delete_categories(array($_GET['delete']));
47
  array_push($infos, $lang['cat_virtual_deleted']);
48
  ordering();
49
  update_global_rank();
50
}
51
// request to add a virtual category
52
else if (isset($_POST['submit']))
53
{
54
  // is the given category name only containing blank spaces ?
55
  if (preg_match('/^\s*$/', $_POST['virtual_name']))
56
  {
57
    array_push($errors, $lang['cat_error_name']);
58
  }
59
 
60
  if (!count($errors))
61
  {
62
    $parent_id = !empty($_GET['parent_id'])?$_GET['parent_id']:'NULL';
63
 
64
    if ($parent_id != 'NULL')
65
    {
66
      $query = '
67
SELECT id,uppercats,global_rank,visible,status
68
  FROM '.CATEGORIES_TABLE.'
69
  WHERE id = '.$parent_id.'
70
;';
71
      $row = mysql_fetch_array(pwg_query($query));
72
      $parent = array('id' => $row['id'],
73
                      'uppercats' => $row['uppercats'],
74
                      'visible' => $row['visible'],
75
                      'status' => $row['status'],
76
                      'global_rank' => $row['global_rank']);
77
    }
78
 
79
    // what will be the inserted id ?
80
    $query = '
81
SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1)
82
  FROM '.CATEGORIES_TABLE.'
83
;';
84
    list($next_id) = mysql_fetch_array(pwg_query($query));
85
 
86
    $insert = array();
87
    $insert{'id'} = $next_id++;
88
    $insert{'name'} = $_POST['virtual_name'];
89
    $insert{'rank'} = $_POST['rank'];
90
    $insert{'commentable'} = $conf['newcat_default_commentable'];
91
 
92
    // a virtual category can't be uploadable
93
    $insert{'uploadable'} = 'false';
94
 
95
    if (isset($parent))
96
    {
97
      $insert{'id_uppercat'} = $parent{'id'};
98
      $insert{'uppercats'}   = $parent{'uppercats'}.','.$insert{'id'};
99
      $insert{'global_rank'} = $parent{'global_rank'}.'.'.$insert{'rank'};
100
      // at creation, must a category be visible or not ? Warning : if
101
      // the parent category is invisible, the category is automatically
102
      // create invisible. (invisible = locked)
103
      if ('false' == $parent['visible'])
104
      {
105
        $insert{'visible'} = 'false';
106
      }
107
      else
108
      {
109
        $insert{'visible'} = $conf['newcat_default_visible'];
110
      }
111
      // at creation, must a category be public or private ? Warning :
112
      // if the parent category is private, the category is
113
      // automatically create private.
114
      if ('private' == $parent['status'])
115
      {
116
        $insert{'status'} = 'private';
117
      }
118
      else
119
      {
120
        $insert{'status'} = $conf['newcat_default_status'];
121
      }
122
    }
123
    else
124
    {
125
      $insert{'visible'} = $conf['newcat_default_visible'];
126
      $insert{'status'} = $conf['newcat_default_status'];
127
      $insert{'uppercats'} = $insert{'id'};
128
      $insert{'global_rank'} = $insert{'rank'};
129
    }
130
 
131
    $inserts = array($insert);
132
 
133
    // we have then to add the virtual category
134
    $dbfields = array('id','site_id','name','id_uppercat','rank',
135
                      'commentable','uploadable','visible','status',
136
                      'uppercats','global_rank');
137
    mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
138
 
139
    array_push($infos, $lang['cat_virtual_added']);
140
  }
141
}
142
// +-----------------------------------------------------------------------+
143
// |                           Cache management                            |
144
// +-----------------------------------------------------------------------+
145
$query = '
146
SELECT *
147
  FROM '.CATEGORIES_TABLE;
148
if (!isset($_GET['parent_id']))
149
{
150
  $query.= '
151
  WHERE id_uppercat IS NULL';
152
}
153
else
154
{
155
  $query.= '
156
  WHERE id_uppercat = '.$_GET['parent_id'];
157
}
158
$query.= '
159
  ORDER BY rank ASC
160
;';
161
$result = pwg_query($query);
162
while ($row = mysql_fetch_assoc($result))
163
{
164
  $categories[$row['rank']] = $row;
165
  $categories[$row['rank']]['nb_subcats'] = 0;
166
}
167
// +-----------------------------------------------------------------------+
168
// |                            Navigation path                            |
169
// +-----------------------------------------------------------------------+
170
if (isset($_GET['parent_id']))
171
{
172
  $base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
173
 
174
  $navigation = '<a class="" href="'.add_session_id($base_url).'">';
175
  $navigation.= $lang['home'];
176
  $navigation.= '</a>';
177
  $navigation.= $conf['level_separator'];
178
 
179
  $current_category = get_cat_info($_GET['parent_id']);
180
  $navigation.= get_cat_display_name($current_category['name'],
181
                                     $base_url.'&amp;parent_id=',
182
                                     false);
183
}
184
// +-----------------------------------------------------------------------+
185
// |                               rank updates                            |
186
// +-----------------------------------------------------------------------+
187
$current_rank = 0;
188
if (isset($_GET['up']) and is_numeric($_GET['up']))
189
{
190
  // 1. searching the id of the category just above at the same level
191
  while (list ($id,$current) = each($categories))
192
  {
193
    if ($current['id'] == $_GET['up'])
194
    {
195
      $current_rank = $current['rank'];
196
      break;
197
    }
198
  }
199
  if ($current_rank > 1)
200
  {
201
    // 2. Exchanging ranks between the two categories
202
    $query = '
203
UPDATE '.CATEGORIES_TABLE.'
204
  SET rank = '.($current_rank-1).'
205
  WHERE id = '.$_GET['up'].'
206
;';
207
    pwg_query($query);
208
    $query = '
209
UPDATE '.CATEGORIES_TABLE.'
210
  SET rank = '.$current_rank.'
211
  WHERE id = '.$categories[($current_rank-1)]['id'].'
212
;';
213
    pwg_query($query);
214
    // 3. Updating the cache array
215
    $categories[$current_rank] = $categories[($current_rank-1)];
216
    $categories[($current_rank-1)] = $current;
217
  }
218
  else
219
  {
220
    // 2. Updating the rank of our category to be after the previous max rank
221
    $query = '
222
UPDATE '.CATEGORIES_TABLE.'
223
  SET rank = '.(count($categories) + 1).'
224
  WHERE id = '.$_GET['up'].'
225
;';
226
    pwg_query($query);
227
    $query = '
228
UPDATE '.CATEGORIES_TABLE.'
229
  SET rank = rank-1
230
  WHERE id_uppercat ';
231
    if (empty($_GET['parent_id']))
232
    {
233
      $query.= 'IS NULL';
234
    }
235
    else
236
    {
237
      $query.= '= '.$_GET['parent_id'];
238
    }
239
    $query.= '
240
;';
241
    pwg_query($query);
242
    // 3. Updating the cache array
243
    array_push($categories, $current);
244
    array_shift($categories);
245
  }
246
  update_global_rank(@$_GET['parent_id']);
247
}
248
else if (isset($_GET['down']) and is_numeric($_GET['down']))
249
{
250
  // 1. searching the id of the category just above at the same level
251
  while (list ($id,$current) = each($categories))
252
  {
253
    if ($current['id'] == $_GET['down'])
254
    {
255
      $current_rank = $current['rank'];
256
      break;
257
    }
258
  }
259
  if ($current_rank < count($categories))
260
  {
261
    // 2. Exchanging ranks between the two categories
262
    $query = '
263
UPDATE '.CATEGORIES_TABLE.'
264
  SET rank = '.($current_rank+1).'
265
  WHERE id = '.$_GET['down'].'
266
;';
267
    pwg_query($query);
268
    $query = '
269
UPDATE '.CATEGORIES_TABLE.'
270
  SET rank = '.$current_rank.'
271
  WHERE id = '.$categories[($current_rank+1)]['id'].'
272
;';
273
    pwg_query($query);
274
    // 3. Updating the cache array
275
    $categories[$current_rank]=$categories[($current_rank+1)];
276
    $categories[($current_rank+1)] = $current;
277
  }
278
  else 
279
  {
280
    // 2. updating the rank of our category to be the first one
281
    $query = '
282
UPDATE '.CATEGORIES_TABLE.'
283
  SET rank = 0
284
  WHERE id = '.$_GET['down'].'
285
;';
286
    pwg_query($query);
287
    $query = '
288
UPDATE '.CATEGORIES_TABLE.'
289
  SET rank = rank+1
290
  WHERE id_uppercat ';
291
    if (empty($_GET['parent_id']))
292
    {
293
      $query.= 'IS NULL';
294
    }
295
    else
296
    {
297
      $query.= '= '.$_GET['parent_id'];
298
    }
299
    $query.= '
300
;';
301
    pwg_query($query);
302
    // 3. Updating the cache array
303
    array_unshift($categories, $current);
304
    array_pop($categories);
305
  }
306
  update_global_rank(@$_GET['parent_id']);
307
}
308
reset($categories);
309
// +-----------------------------------------------------------------------+
310
// |                       template initialization                         |
311
// +-----------------------------------------------------------------------+
312
$template->set_filenames(array('categories'=>'admin/cat_list.tpl'));
313
 
314
$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
315
if (isset($_GET['parent_id']))
316
{
317
  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
318
}
319
 
320
if (count($categories) > 0)
321
{
322
  $next_rank = max(array_keys($categories)) + 1;
323
}
324
else
325
{
326
  $next_rank = 1;
327
}
328
 
329
$template->assign_vars(array(
330
  'CATEGORIES_NAV'=>$navigation,
331
  'NEXT_RANK'=>$next_rank,
332
  'F_ACTION'=>add_session_id($form_action),
333
 
334
  'L_ADD_VIRTUAL'=>$lang['cat_add'],
335
  'L_SUBMIT'=>$lang['submit'],
336
  'L_STORAGE'=>$lang['storage'],
337
  'L_NB_IMG'=>$lang['pictures'],
338
  'L_MOVE_UP'=>$lang['up'],
339
  'L_MOVE_DOWN'=>$lang['down'],
340
  'L_EDIT'=>$lang['edit'],
341
  'L_INFO_IMG'=>$lang['cat_image_info'],
342
  'L_DELETE'=>$lang['delete'],
343
 ));
344
 
345
$tpl = array('cat_first','cat_last');
346
// +-----------------------------------------------------------------------+
347
// |                            errors & infos                             |
348
// +-----------------------------------------------------------------------+
349
if (count($errors) != 0)
350
{
351
  $template->assign_block_vars('errors',array());
352
  foreach ($errors as $error)
353
  {
354
    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
355
  }
356
}
357
if (count($infos) != 0)
358
{
359
  $template->assign_block_vars('infos',array());
360
  foreach ($infos as $info)
361
  {
362
    $template->assign_block_vars('infos.info',array('INFO'=>$info));
363
  }
364
}
365
// +-----------------------------------------------------------------------+
366
// |                          Categories display                           |
367
// +-----------------------------------------------------------------------+
368
$ranks = array();
369
 
370
if (count($categories) > 0)
371
{
372
  foreach ($categories as $category)
373
  {
374
    $ranks[$category['id']] = $category['rank'];
375
  }
376
 
377
  $query = '
378
SELECT id_uppercat, COUNT(*) AS nb_subcats
379
  FROM '. CATEGORIES_TABLE.'
380
  WHERE id_uppercat IN ('.implode(',', array_keys($ranks)).')
381
  GROUP BY id_uppercat
382
;';
383
  $result = pwg_query($query);
384
  while ($row = mysql_fetch_array($result))
385
  {
386
    $categories[$ranks[$row['id_uppercat']]]['nb_subcats']
387
      = $row['nb_subcats'];
388
  }
389
}
390
 
391
foreach ($categories as $category)
392
{
393
  $images_folder = PHPWG_ROOT_PATH.'template/';
394
  $images_folder.= $user['template'].'/admin/images';
395
 
396
  if ($category['visible'] == 'false')
397
  {
398
    $image_src = $images_folder.'/icon_folder_lock.gif';
399
    $image_alt = $lang['cat_private'];
400
    $image_title = $lang['cat_private'];
401
  }
402
  else if (empty($category['dir']))
403
  {
404
    $image_src = $images_folder.'/icon_folder_link.gif';
405
    $image_alt = $lang['cat_virtual'];
406
    $image_title = $lang['cat_virtual'];
407
  }
408
  else
409
  {
410
    if ($category['nb_subcats'] > 0)
411
    {
412
      $image_src = $images_folder.'/icon_subfolder.gif';
413
    }
414
    else
415
    {
416
      $image_src = $images_folder.'/icon_folder.gif';
417
    }
418
    $image_alt = '';
419
    $image_title = '';
420
  }
421
 
422
  $base_url = PHPWG_ROOT_PATH.'admin.php?page=';
423
  $cat_list_url = $base_url.'cat_list';
424
 
425
  $self_url = $cat_list_url;
426
  if (isset($_GET['parent_id']))
427
  {
428
    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
429
  }
430
 
431
  $template->assign_block_vars(
432
    'category',
433
    array(
434
      'CATEGORY_IMG_SRC'=>$image_src,
435
      'CATEGORY_IMG_ALT'=>$image_alt,
436
      'CATEGORY_IMG_TITLE'=>$image_title,
437
      'CATEGORY_NAME'=>$category['name'],
438
      'CATEGORY_DIR'=>@$category['dir'],
439
      'CATEGORY_NB_IMG'=>$category['nb_images'],
440
 
441
      'U_CATEGORY'=>
442
      add_session_id($cat_list_url.'&amp;parent_id='.$category['id']),
443
 
444
      'U_MOVE_UP'=>add_session_id($self_url.'&amp;up='.$category['id']),
445
 
446
      'U_MOVE_DOWN'=>add_session_id($self_url.'&amp;down='.$category['id']),
447
 
448
      'U_CAT_EDIT'=>
449
      add_session_id($base_url.'cat_modify&amp;cat_id='.$category['id']),
450
 
451
      'U_CAT_DELETE'=>add_session_id($self_url.'&amp;delete='.$category['id']),
452
 
453
      'U_INFO_IMG'
454
      => add_session_id($base_url.'infos_images&amp;cat_id='.$category['id'])
455
      ));
456
 
457
  if (!empty($category['dir']))
458
  {
459
    $template->assign_block_vars('category.storage' ,array());
460
  }
461
  else
462
  {
463
    $template->assign_block_vars('category.virtual' ,array());
464
  }
465
 
466
  if ($category['nb_images'] > 0)
467
  {
468
    $template->assign_block_vars('category.image_info' ,array());
469
  }
470
  else
471
  {
472
    $template->assign_block_vars('category.no_image_info' ,array()); 
473
  }
474
}
475
// +-----------------------------------------------------------------------+
476
// |                          sending html code                            |
477
// +-----------------------------------------------------------------------+
478
$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
479
?>