source: trunk/admin/cat_list.php @ 345

Last change on this file since 345 was 345, checked in by gweltas, 20 years ago

Merge of the 1.3.1 release
Creation of an unique include file (common.php)
Creation of an unique define file (include/constants.php)
Modification of the installation procedure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
1<?php
2/***************************************************************************
3 *                                  cat_list.php                           *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   website              : http://www.phpwebgallery.net                   *
7 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
8 *                                                                         *
9 *   $Id: cat_list.php 345 2004-02-02 00:55:18Z gweltas $
10 *                                                                         *
11 ***************************************************************************/
12
13/***************************************************************************
14 *                                                                         *
15 *   This program is free software; you can redistribute it and/or modify  *
16 *   it under the terms of the GNU General Public License as published by  *
17 *   the Free Software Foundation;                                         *
18 *                                                                         *
19 ***************************************************************************/
20include_once( './admin/include/isadmin.inc.php' );
21
22//----------------------------------------------------- template initialization
23$sub = $vtp->Open( './template/'.$user['template'].'/admin/cat_list.vtp' );
24$tpl = array( 'cat_edit','cat_up','cat_down','cat_image_info',
25              'cat_permission','cat_update','cat_add','cat_parent','submit',
26              'cat_virtual','delete','cat_first','cat_last','errors_title' );
27templatize_array( $tpl, 'lang', $sub );
28$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
29//--------------------------------------------------- adding a virtual category
30$errors = array();
31if ( isset( $_POST['submit'] ) )
32{
33  // is the given category name only containing blank spaces ?
34  if ( preg_match( '/^\s*$/', $_POST['virtual_name'] ) )
35    array_push( $errors, $lang['cat_error_name'] );
36  // does the uppercat id exists in the database ?
37  if ( $_POST['associate'] == '' )
38  {
39    $_POST['associate'] = -1;
40  }
41  else if ( !is_numeric( $_POST['associate'] ) )
42  {
43    array_push( $errors, $lang['cat_unknown_id'] );
44  }
45  else
46  {
47    $query = 'SELECT id';
48    $query.= ' FROM '.PREFIX_TABLE.'categories';
49    $query.= ' WHERE id = '.$_POST['associate'];
50    $query.= ';';
51    if ( mysql_num_rows( mysql_query( $query ) ) == 0 )
52      array_push( $errors, $lang['cat_unknown_id'] );
53  }
54 
55  if ( count( $errors ) == 0 )
56  {
57    // we have then to add the virtual category
58    $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
59    $query.= ' (name,id_uppercat) VALUES ';
60    if ( $_POST['associate'] == -1 )
61    {
62      $_POST['associate'] = 'NULL';
63    }
64    $query.= " ('".$_POST['virtual_name']."',".$_POST['associate'].")";
65    $query.= ';';
66    mysql_query( $query );
67    synchronize_all_users();
68  }
69}
70//---------------------------------------------------------------  rank updates
71if ( isset( $_GET['up'] ) and is_numeric( $_GET['up'] ) )
72{
73  // 1. searching level (id_uppercat)
74  //    and rank of the category to move
75  $query = 'SELECT id_uppercat,rank';
76  $query.= ' FROM '.PREFIX_TABLE.'categories';
77  $query.= ' WHERE id = '.$_GET['up'];
78  $query.= ';';
79  $row = mysql_fetch_array( mysql_query( $query ) );
80  $level = $row['id_uppercat'];
81  $rank  = $row['rank'];
82  // 2. searching the id and the rank of the category
83  //    just above at the same level
84  $query = 'SELECT id,rank';
85  $query.= ' FROM '.PREFIX_TABLE.'categories';
86  $query.= ' WHERE rank < '.$rank;
87  if ( $level == '' )
88  {
89    $query.= ' AND id_uppercat IS NULL';
90  }
91  else
92  {
93    $query.= ' AND id_uppercat = '.$level;
94  }
95  $query.= ' ORDER BY rank DESC';
96  $query.= ' LIMIT 0,1';
97  $query.= ';';
98  $row = mysql_fetch_array( mysql_query( $query ) );
99  $new_rank     = $row['rank'];
100  $replaced_cat = $row['id'];
101  // 3. exchanging ranks between the two categories
102  $query = 'UPDATE '.PREFIX_TABLE.'categories';
103  $query.= ' SET rank = '.$new_rank;
104  $query.= ' WHERE id = '.$_GET['up'];
105  $query.= ';';
106  mysql_query( $query );
107  $query = 'UPDATE '.PREFIX_TABLE.'categories';
108  $query.= ' SET rank = '.$rank;
109  $query.= ' WHERE id = '.$replaced_cat;
110  $query.= ';';
111  mysql_query( $query );
112}
113if ( isset( $_GET['down'] ) and is_numeric( $_GET['down'] ) )
114{
115  // 1. searching level (id_uppercat)
116  //    and rank of the category to move
117  $query = 'SELECT id_uppercat,rank';
118  $query.= ' FROM '.PREFIX_TABLE.'categories';
119  $query.= ' WHERE id = '.$_GET['down'];
120  $query.= ';';
121  $row = mysql_fetch_array( mysql_query( $query ) );
122  $level = $row['id_uppercat'];
123  $rank  = $row['rank'];
124  // 2. searching the id and the rank of the category
125  //    just below at the same level
126  $query = 'SELECT id,rank';
127  $query.= ' FROM '.PREFIX_TABLE.'categories';
128  $query.= ' WHERE rank > '.$rank;
129  if ( $level == '' )
130  {
131    $query.= ' AND id_uppercat IS NULL';
132  }
133  else
134  {
135    $query.= ' AND id_uppercat = '.$level;
136  }
137  $query.= ' ORDER BY rank ASC';
138  $query.= ' LIMIT 0,1';
139  $query.= ';';
140  $row = mysql_fetch_array( mysql_query( $query ) );
141  $new_rank     = $row['rank'];
142  $replaced_cat = $row['id'];
143  // 3. exchanging ranks between the two categories
144  $query = 'UPDATE '.PREFIX_TABLE.'categories';
145  $query.= ' SET rank = '.$new_rank;
146  $query.= ' WHERE id = '.$_GET['down'];
147  $query.= ';';
148  mysql_query( $query );
149  $query = 'UPDATE '.PREFIX_TABLE.'categories';
150  $query.= ' SET rank = '.$rank;
151  $query.= ' WHERE id = '.$replaced_cat;
152  $query.= ';';
153  mysql_query( $query );
154}
155if ( isset( $_GET['last'] ) and is_numeric( $_GET['last'] ) )
156{
157  // 1. searching level (id_uppercat) of the category to move
158  $query = 'SELECT id_uppercat,rank';
159  $query.= ' FROM '.PREFIX_TABLE.'categories';
160  $query.= ' WHERE id = '.$_GET['last'];
161  $query.= ';';
162  $row = mysql_fetch_array( mysql_query( $query ) );
163  $level = $row['id_uppercat'];
164  // 2. searching the highest rank of the categories of the same parent
165  $query = 'SELECT MAX(rank) AS max_rank';
166  $query.= ' FROM '.PREFIX_TABLE.'categories';
167  $query.= ' WHERE id_uppercat';
168  if ( $level == '' ) $query.= ' IS NULL';
169  else                $query.= ' = '.$level;
170  $query.= ';';
171  $row = mysql_fetch_array( mysql_query( $query ) );
172  $max_rank = $row['max_rank'];
173  // 3. updating the rank of our category to be after the previous max rank
174  $query = 'UPDATE '.PREFIX_TABLE.'categories';
175  $query.= ' SET rank = '.($max_rank + 1);
176  $query.= ' WHERE id = '.$_GET['last'];
177  $query.= ';';
178  mysql_query( $query );
179}
180if ( isset( $_GET['first'] ) and is_numeric( $_GET['first'] ) )
181{
182  // to place our category as first, we simply say that is rank is 0, then
183  // reordering will move category ranks correctly (first rank should be 1
184  // and not 0)
185  $query = 'UPDATE '.PREFIX_TABLE.'categories';
186  $query.= ' SET rank = 0';
187  $query.= ' WHERE id = '.$_GET['first'];
188  $query.= ';';
189  mysql_query( $query );
190}
191if ( isset( $_GET['delete'] ) and is_numeric( $_GET['delete'] ) )
192{
193  delete_category( $_GET['delete'] );
194  synchronize_all_users();
195}
196//------------------------------------------------------------------ reordering
197function ordering( $id_uppercat )
198{
199  $rank = 1;
200               
201  $query = 'SELECT id';
202  $query.= ' FROM '.PREFIX_TABLE.'categories';
203  if ( !is_numeric( $id_uppercat ) )
204  {
205    $query.= ' WHERE id_uppercat IS NULL';
206  }
207  else
208  {
209    $query.= ' WHERE id_uppercat = '.$id_uppercat;
210  }
211  $query.= ' ORDER BY rank ASC, dir ASC';
212  $query.= ';';
213  $result = mysql_query( $query );
214  while ( $row = mysql_fetch_array( $result ) )
215  {
216    $query = 'UPDATE '.PREFIX_TABLE.'categories';
217    $query.= ' SET rank = '.$rank;
218    $query.= ' WHERE id = '.$row['id'];
219    $query.= ';';
220    mysql_query( $query );
221    $rank++;
222    ordering( $row['id'] );
223  }
224}
225ordering( 'NULL' );
226//-------------------------------------------------------------- errors display
227if ( count( $errors ) != 0 )
228{
229  $vtp->addSession( $sub, 'errors' );
230  foreach ( $errors as $error ) {
231    $vtp->addSession( $sub, 'li' );
232    $vtp->setVar( $sub, 'li.content', $error );
233    $vtp->closeSession( $sub, 'li' );
234  }
235  $vtp->closeSession( $sub, 'errors' );
236}
237//---------------------------------------------------------------- page display
238function display_cat_manager( $id_uppercat, $indent,
239                              $uppercat_visible, $level )
240{
241  global $lang,$conf,$sub,$vtp,$page;
242               
243  // searching the min_rank and the max_rank of the category
244  $query = 'SELECT MIN(rank) AS min, MAX(rank) AS max';
245  $query.= ' FROM '.PREFIX_TABLE.'categories';
246  if ( !is_numeric( $id_uppercat ) )
247  {
248    $query.= ' WHERE id_uppercat IS NULL';
249  }
250  else
251  {
252    $query.= ' WHERE id_uppercat = '.$id_uppercat;
253  }
254  $query.= ';';
255  $result = mysql_query( $query );
256  $row    = mysql_fetch_array( $result );
257  if ( !isset( $row['min'] ) ) $row['min'] = 0;
258  if ( !isset( $row['max'] ) ) $row['max'] = 0;
259  $min_rank = $row['min'];
260  $max_rank = $row['max'];
261               
262  // will we use <th> or <td> lines ?
263  $td    = 'td';
264  $class = '';
265  if ( $level > 0 ) $class = 'row'.$level;
266  else              $td = 'th';
267               
268  $query = 'SELECT id,name,dir,nb_images,status,rank,site_id,visible';
269  $query.= ' FROM '.PREFIX_TABLE.'categories';
270  if ( !is_numeric( $id_uppercat ) )
271  {
272    $query.= ' WHERE id_uppercat IS NULL';
273  }
274  else
275  {
276    $query.= ' WHERE id_uppercat = '.$id_uppercat;
277  }
278  $query.= ' ORDER BY rank ASC';
279  $query.= ';';
280  $result = mysql_query( $query );
281  while ( $row = mysql_fetch_array( $result ) )
282  {
283    $subcat_visible = true;
284    if ( !isset( $row['dir'] ) ) $row['dir'] = '';
285
286    $vtp->addSession( $sub, 'cat' );
287    // is the category expanded or not ?
288    if ( isset($page['expand']) && $page['expand'] == 'all' )
289    {
290      $vtp->addSession( $sub, 'bullet_wo_link' );
291      $vtp->closeSession( $sub, 'bullet_wo_link' );
292    }
293    else if ( isset($page['tab_expand']) && in_array( $row['id'], $page['tab_expand'] ) )
294    {
295      $vtp->addSession( $sub, 'bullet_expanded' );
296      $tab_expand = array_diff( $page['tab_expand'], array( $row['id'] ) );
297      $expand = implode( ',', $tab_expand );
298      $url = './admin.php?page=cat_list&amp;expand='.$expand;
299      $vtp->setVar( $sub, 'bullet_expanded.link', add_session_id( $url ) );
300      $vtp->closeSession( $sub, 'bullet_expanded' );
301    }
302    else
303    {
304      $vtp->addSession( $sub, 'bullet_collapsed' );
305      $tab_expand = array_merge( $page['tab_expand'], array( $row['id'] ) );
306      $expand = implode( ',', $tab_expand );
307      $url = './admin.php?page=cat_list&amp;expand='.$expand;
308      $vtp->setVar( $sub, 'bullet_collapsed.link', add_session_id( $url ) );
309      $vtp->closeSession( $sub, 'bullet_collapsed' );
310    }
311   
312    $vtp->setVar( $sub, 'cat.td', $td );
313    $vtp->setVar( $sub, 'cat.class', $class );
314    $vtp->setVar( $sub, 'cat.indent', $indent );
315    $vtp->setVar( $sub, 'cat.name', $row['name'] );
316
317    if ( $row['dir'] != '' )
318    {
319      $vtp->addSession( $sub, 'storage' );
320      $vtp->setVar( $sub, 'storage.dir', $row['dir'] );
321      $vtp->closeSession( $sub, 'storage' );
322      // category can't be deleted
323      $vtp->addSession( $sub, 'no_delete' );
324      $vtp->closeSession( $sub, 'no_delete' );
325    }
326    else
327    {
328      $vtp->addSession( $sub, 'virtual' );
329      $vtp->closeSession( $sub, 'virtual' );
330      // category can be deleted
331      $vtp->addSession( $sub, 'delete' );
332      $url = './admin.php?page=cat_list&amp;expand='.$page['expand'];
333      $url.= '&amp;delete='.$row['id'];
334      $vtp->setVar( $sub, 'delete.delete_url', add_session_id( $url ) );
335      $vtp->closeSession( $sub, 'delete' );
336    }
337    if ( $row['visible'] == 'false' or !$uppercat_visible )
338    {
339      $subcat_visible = false;
340      $vtp->setVar( $sub, 'cat.invisible', $lang['cat_invisible'] );
341    }
342    if ( $row['status'] == 'private' )
343    {
344      $vtp->setVar( $sub, 'cat.private', $lang['private'] );
345    }
346    $vtp->setVar( $sub, 'cat.nb_picture', $row['nb_images'] );
347    $url = add_session_id( './admin.php?page=cat_modify&amp;cat='.$row['id'] );
348    $vtp->setVar( $sub, 'cat.edit_url', $url );
349    if ( $row['rank'] != $min_rank )
350    {
351      $vtp->addSession( $sub, 'up' );
352      $url = './admin.php?page=cat_list&amp;expand='.$page['expand'];
353      $url.= '&amp;up='.$row['id'];
354      $vtp->setVar( $sub, 'up.up_url', add_session_id( $url ) );
355      $vtp->closeSession( $sub, 'up' );
356    }
357    else if ( $min_rank != $max_rank )
358    {
359      $vtp->addSession( $sub, 'no_up' );
360      $url = './admin.php?page=cat_list&amp;expand='.$page['expand'];
361      $url.= '&amp;last='.$row['id'];
362      $vtp->setVar( $sub, 'no_up.last_url', add_session_id( $url ) );
363      $vtp->closeSession( $sub, 'no_up' );
364    }
365    if ( $row['rank'] != $max_rank )
366    {
367      $vtp->addSession( $sub, 'down' );
368      $url = './admin.php?page=cat_list&amp;expand='.$page['expand'];
369      $url.= '&amp;down='.$row['id'];
370      $vtp->setVar( $sub, 'down.down_url', add_session_id( $url ) );
371      $vtp->closeSession( $sub, 'down' );
372    }
373    else if ( $min_rank != $max_rank )
374    {
375      $vtp->addSession( $sub, 'no_down' );
376      $url = './admin.php?page=cat_list&amp;expand='.$page['expand'];
377      $url.= '&amp;first='.$row['id'];
378      $vtp->setVar( $sub, 'no_down.first_url', add_session_id( $url ) );
379      $vtp->closeSession( $sub, 'no_down' );
380    }
381    if ( $row['nb_images'] > 0 )
382    {
383      $vtp->addSession( $sub, 'image_info' );
384      $url = './admin.php?page=infos_images&amp;cat_id='.$row['id'];
385      $vtp->setVar( $sub, 'image_info.image_info_url', add_session_id($url) );
386      $vtp->closeSession( $sub, 'image_info' );
387    }
388    else
389    {
390      $vtp->addSession( $sub, 'no_image_info' );
391      $vtp->closeSession( $sub, 'no_image_info' );
392    }
393    if ( $row['status'] == 'private' )
394    {
395      $vtp->addSession( $sub, 'permission' );
396      $url=add_session_id('./admin.php?page=cat_perm&amp;cat_id='.$row['id']);
397      $vtp->setVar( $sub, 'permission.url', $url );
398      $vtp->closeSession( $sub, 'permission' );
399    }
400    else
401    {
402      $vtp->addSession( $sub, 'no_permission' );
403      $vtp->closeSession( $sub, 'no_permission' );
404    }
405    // you can individually update a category only if it is on the main site
406    // and if it's not a virtual category (a category is virtual if there is
407    // no directory associated)
408    if ( $row['site_id'] == 1 and $row['dir'] != '' )
409    {
410      $vtp->addSession( $sub, 'update' );
411      $url = add_session_id('./admin.php?page=update&amp;update='.$row['id']);
412      $vtp->setVar( $sub, 'update.update_url', $url );
413      $vtp->closeSession( $sub, 'update' );
414    }
415    else
416    {
417      $vtp->addSession( $sub, 'no_update' );
418      $vtp->closeSession( $sub, 'no_update' );
419    }
420
421    $vtp->closeSession( $sub, 'cat' );
422
423    if ( in_array( $row['id'], $page['tab_expand'] )
424         or $page['expand'] == 'all')
425      display_cat_manager( $row['id'], $indent.str_repeat( '&nbsp', 4 ),
426                           $subcat_visible, $level + 1 );
427  }
428}
429display_cat_manager( 'NULL', str_repeat( '&nbsp', 4 ), true, 0 );
430// add a virtual category ?
431// We only show a List Of Values if the number of categories is less than
432// $conf['max_LOV_categories']
433$query = 'SELECT COUNT(id) AS nb_total_categories';
434$query.= ' FROM '.PREFIX_TABLE.'categories';
435$query.= ';';
436$row = mysql_fetch_array( mysql_query( $query ) );
437if ( $row['nb_total_categories'] < $conf['max_LOV_categories'] )
438{
439  $vtp->addSession( $sub, 'associate_LOV' );
440  $vtp->addSession( $sub, 'associate_cat' );
441  $vtp->setVar( $sub, 'associate_cat.value', '-1' );
442  $vtp->setVar( $sub, 'associate_cat.content', '' );
443  $vtp->closeSession( $sub, 'associate_cat' );
444  $page['plain_structure'] = get_plain_structure( true );
445  $structure = create_structure( '', array() );
446  display_categories( $structure, '&nbsp;' );
447  $vtp->closeSession( $sub, 'associate_LOV' );
448}
449// else, we only display a small text field, we suppose the administrator
450// knows the id of its category
451else
452{
453  $vtp->addSession( $sub, 'associate_text' );
454  $vtp->closeSession( $sub, 'associate_text' );
455}
456//----------------------------------------------------------- sending html code
457$vtp->Parse( $handle , 'sub', $sub );
458?>
Note: See TracBrowser for help on using the repository browser.