source: tags/release-1_3_1beta/admin/cat_list.php @ 27558

Last change on this file since 27558 was 330, checked in by z0rglub, 20 years ago

Php warnings correction

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