[ Index ] [ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

PHP Cross Reference of TXP stable 4.0.6

title

Body

[close]

/textpattern/include/ -> txp_category.php (source)

   1  <?php
   2  
   3  /*
   4      This is Textpattern
   5  
   6      Copyright 2005 by Dean Allen
   7      www.textpattern.com
   8      All rights reserved
   9  
  10      Use of this software indicates acceptance ofthe Textpattern license agreement 
  11  
  12  $HeadURL: http://svn.textpattern.com/releases/4.0.6/source/textpattern/include/txp_category.php $
  13  $LastChangedRevision: 2243 $
  14  */
  15  
  16  if (!defined('txpinterface')) die('txpinterface is undefined.');
  17  
  18  if ($event == 'category') {
  19      require_privs('category');    
  20  
  21      if(!$step or !in_array($step, array(
  22          'cat_category_list','cat_article_create','cat_image_create','cat_file_create','cat_link_create',
  23          'cat_category_multiedit','cat_article_save','cat_image_save','cat_file_save','cat_link_save',
  24          'cat_article_edit','cat_image_edit','cat_file_edit','cat_link_edit',        
  25      ))){
  26          cat_category_list();
  27      } else $step();
  28  }
  29  
  30  //-------------------------------------------------------------
  31  	function cat_category_list($message="")
  32      {
  33          pagetop(gTxt('categories'),$message);
  34          $out = array('<table cellspacing="20" align="center">',
  35          '<tr>',
  36              tdtl(cat_article_list(),' class="categories"'),
  37              tdtl(cat_link_list(),' class="categories"'),
  38              tdtl(cat_image_list(),' class="categories"'),
  39              tdtl(cat_file_list(),' class="categories"'),
  40          '</tr>',
  41          endTable());
  42          echo join(n,$out);
  43      }
  44  
  45   
  46  //-------------------------------------------------------------
  47  	function cat_article_list() 
  48      {
  49          return cat_event_category_list('article');
  50      }
  51  
  52  //-------------------------------------------------------------
  53  	function cat_article_create()
  54      {
  55          return cat_event_category_create('article');
  56      }
  57  
  58  //-------------------------------------------------------------
  59  	function cat_article_edit()
  60      {
  61          return cat_event_category_edit('article');
  62      }
  63  
  64  //-------------------------------------------------------------
  65  	function cat_article_save()
  66      {
  67          return cat_event_category_save('article', 'textpattern');
  68      }
  69  
  70  //--------------------------------------------------------------
  71  
  72  	function cat_parent_pop($name, $type, $id)
  73      {
  74          if ($id)
  75          {
  76              $id = assert_int($id);
  77              list($lft, $rgt) = array_values(safe_row('lft, rgt', 'txp_category', 'id = '.$id));
  78  
  79              $rs = getTree('root', $type, "lft not between $lft and $rgt");
  80          }
  81  
  82          else
  83          {
  84              $rs = getTree('root', $type);
  85          }
  86  
  87          if ($rs)
  88          {
  89              return treeSelectInput('parent', $rs, $name);
  90          }
  91  
  92          return gTxt('no_other_categories_exist');
  93      }
  94  
  95  // -------------------------------------------------------------
  96  	function cat_link_list() 
  97      {
  98          return cat_event_category_list('link');
  99      }
 100  
 101  //-------------------------------------------------------------
 102  	function cat_link_create()
 103      {
 104          return cat_event_category_create('link');
 105      }
 106  
 107  //-------------------------------------------------------------
 108  	function cat_link_edit()
 109      {
 110          return cat_event_category_edit('link');
 111      }
 112  
 113  //-------------------------------------------------------------
 114  	function cat_link_save()
 115      {
 116          return cat_event_category_save('link', 'txp_link');
 117      }
 118  
 119  // -------------------------------------------------------------
 120  	function cat_image_list() 
 121      {
 122          return cat_event_category_list('image');
 123      }
 124  
 125  //-------------------------------------------------------------
 126  	function cat_image_create()
 127      {
 128          return cat_event_category_create('image');
 129      }
 130  
 131  //-------------------------------------------------------------
 132  	function cat_image_edit()
 133      {
 134          return cat_event_category_edit('image');
 135      }
 136  
 137  //-------------------------------------------------------------
 138  	function cat_image_save()
 139      {
 140          return cat_event_category_save('image', 'txp_image');
 141      }
 142  
 143  
 144  // -------------------------------------------------------------
 145  	function cat_article_multiedit_form($area, $array)
 146      {
 147          $methods = array('delete'=>gTxt('delete'));
 148          if ($array) {
 149          return
 150          form(
 151              join('',$array).
 152              eInput('category').sInput('cat_category_multiedit').hInput('type',$area).
 153              small(gTxt('with_selected')).sp.selectInput('edit_method',$methods,'',1).sp.
 154              fInput('submit','',gTxt('go'),'smallerbox')
 155              ,'margin-top:1em',"verify('".gTxt('are_you_sure')."')"
 156          );
 157          } return;
 158      }
 159  
 160  // -------------------------------------------------------------
 161  	function cat_category_multiedit()
 162      {
 163          $type = ps('type');
 164          $method = ps('edit_method');
 165          $things = ps('selected');
 166          if ($things) {
 167              foreach($things as $catid) {
 168                  $catid = assert_int($catid);
 169                  if ($method == 'delete') {
 170                      $catname = safe_field('name', 'txp_category', "id=$catid");
 171                      if (safe_delete('txp_category',"id=$catid")) {
 172                          if ($catname)
 173                              safe_update('txp_category', "parent='root'", "type='".doSlash($type)."' and parent='".doSlash($catname)."'");
 174                          $categories[] = $catid;
 175                      }
 176                  }
 177              }
 178              rebuild_tree_full($type);
 179  
 180              $message = gTxt($type.'_categories_deleted', array('{list}' => join(', ',$categories)));
 181  
 182              cat_category_list($message);
 183          }
 184      }
 185  
 186  //Refactoring: Functions are more or less the same for all event types
 187  // so, merge them. Addition of new event categories is easiest now.
 188  
 189  //-------------------------------------------------------------
 190  
 191  	function cat_event_category_list($event) 
 192      {
 193          $out = n.n.hed(gTxt($event.'_head').sp.popHelp($event.'_category'), 3).
 194  
 195              form(
 196                  fInput('text', 'title', '', 'edit', '', '', 20).
 197                  fInput('submit', '', gTxt('Create'), 'smallerbox').
 198                  eInput('category').
 199                  sInput('cat_'.$event.'_create')
 200              );
 201  
 202          $rs = getTree('root', $event);
 203  
 204          if ($rs)
 205          {
 206              $total_count = array();
 207  
 208              if ($event == 'article')
 209              {
 210                  $rs2 = safe_rows_start('Category1, count(*) as num', 'textpattern', "1 = 1 group by Category1");
 211  
 212                  while ($a = nextRow($rs2))
 213                  {
 214                      $name = $a['Category1'];
 215                      $num = $a['num'];
 216  
 217                      $total_count[$name] = $num;
 218                  }
 219  
 220                  $rs2 = safe_rows_start('Category2, count(*) as num', 'textpattern', "1 = 1 group by Category2");
 221  
 222                  while ($a = nextRow($rs2))
 223                  {
 224                      $name = $a['Category2'];
 225                      $num = $a['num'];
 226  
 227                      if (isset($total_count[$name]))
 228                      {
 229                          $total_count[$name] += $num;
 230                      }
 231  
 232                      else
 233                      {
 234                          $total_count[$name] = $num;
 235                      }
 236                  }
 237              }
 238  
 239              else
 240              {
 241                  switch ($event)
 242                  {
 243                      case 'link':
 244                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_link', "1 group by category");
 245                      break;
 246  
 247                      case 'image':
 248                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_image', "1 group by category");
 249                      break;
 250  
 251                      case 'file':
 252                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_file', "1 group by category");
 253                      break;
 254                  }
 255  
 256                  while ($a = nextRow($rs2))
 257                  {
 258                      $name = $a['category'];
 259                      $num = $a['num'];
 260  
 261                      $total_count[$name] = $num;
 262                  }
 263              }
 264  
 265              $items = array();
 266  
 267              foreach ($rs as $a)
 268              {
 269                  extract($a);
 270  
 271                  // format count
 272                  switch ($event)
 273                  {
 274                      case 'article':
 275                          $url = 'index.php?event=list'.a.'search_method=categories'.a.'crit='.$name;
 276                      break;
 277      
 278                      case 'link':
 279                          $url = 'index.php?event=link'.a.'search_method=category'.a.'crit='.$name;
 280                      break;
 281      
 282                      case 'image':
 283                          $url = 'index.php?event=image'.a.'search_method=category'.a.'crit='.$name;
 284                      break;
 285      
 286                      case 'file':
 287                          $url = 'index.php?event=file'.a.'search_method=category'.a.'crit='.$name;
 288                      break;
 289                  }
 290  
 291                  $count = isset($total_count[$name]) ? '('.href($total_count[$name], $url).')' : '(0)';
 292  
 293                  if (empty($title)) {
 294                      $edit_link = '<em>'.eLink('category', 'cat_'.$event.'_edit', 'id', $id, gTxt('untitled')).'</em>';
 295                  } else {
 296                      $edit_link = eLink('category', 'cat_'.$event.'_edit', 'id', $id, $title);
 297                  }
 298  
 299                  $items[] = graf(
 300                      checkbox('selected[]', $id, 0).sp.str_repeat(sp.sp, $level * 2).$edit_link.sp.small($count)
 301                  );
 302              }
 303  
 304              if ($items)
 305              {
 306                  $out .= cat_article_multiedit_form($event, $items);
 307              }
 308          }
 309  
 310          else
 311          {
 312              $out .= graf(gTxt('no_categories_exist'));
 313          }
 314  
 315          return $out;
 316      }
 317  
 318  //-------------------------------------------------------------
 319  
 320  	function cat_event_category_create($event)
 321      {
 322          global $txpcfg;
 323  
 324          $title = ps('title');
 325  
 326          $name = sanitizeForUrl($title);
 327  
 328          if (!$name)
 329          {
 330              $message = gTxt($event.'_category_invalid', array('{name}' => $name));
 331  
 332              return cat_category_list($message);
 333          }
 334  
 335          $exists = safe_field('name', 'txp_category', "name = '".doSlash($name)."' and type = '".doSlash($event)."'");
 336  
 337          if ($exists)
 338          {
 339              $message = gTxt($event.'_category_already_exists', array('{name}' => $name));
 340  
 341              return cat_category_list($message);
 342          }
 343  
 344          $q = safe_insert('txp_category', "name = '".doSlash($name)."', title = '".doSlash($title)."', type = '".doSlash($event)."', parent = 'root'");
 345  
 346          if ($q)
 347          {
 348              rebuild_tree_full($event);
 349  
 350              $message = gTxt($event.'_category_created', array('{name}' => $name));
 351  
 352              cat_category_list($message);
 353          }
 354      }
 355  
 356  //-------------------------------------------------------------
 357  	function cat_event_category_edit($evname)
 358      {
 359          pagetop(gTxt('categories'));
 360  
 361          $id     = assert_int(gps('id'));
 362          $parent = doSlash(gps('parent'));
 363  
 364          $row = safe_row("*", "txp_category", "id=$id");
 365          if($row){
 366              extract($row);
 367              $out = stackRows(
 368                  fLabelCell($evname.'_category_name') . fInputCell('name', $name, 1, 20),
 369                  fLabelCell('parent') . td(cat_parent_pop($parent,$evname,$id)),
 370                  fLabelCell($evname.'_category_title') . fInputCell('title', $title, 1, 30),
 371                  hInput('id',$id),
 372                  tdcs(fInput('submit', '', gTxt('save_button'),'smallerbox'), 2)
 373              );
 374          }
 375          $out.= eInput( 'category' ) . sInput( 'cat_'.$evname.'_save' ) . hInput( 'old_name',$name );
 376          echo form( startTable( 'edit' ) . $out . endTable() );
 377      }
 378  
 379  //-------------------------------------------------------------
 380  
 381  	function cat_event_category_save($event, $table_name)
 382      {
 383          global $txpcfg;
 384  
 385          extract(doSlash(psa(array('id', 'name', 'old_name', 'parent', 'title'))));
 386          $id = assert_int($id);
 387  
 388          $name = sanitizeForUrl($name);
 389  
 390          // make sure the name is valid
 391          if (!$name)
 392          {
 393              $message = gTxt($event.'_category_invalid', array('{name}' => $name));
 394  
 395              return cat_category_list($message);
 396          }
 397  
 398          // don't allow rename to clobber an existing category
 399          $existing_id = safe_field('id', 'txp_category', "name = '$name' and type = '$event'");
 400  
 401          if ($existing_id and $existing_id != $id)
 402          {
 403              $message = gTxt($event.'_category_already_exists', array('{name}' => $name));
 404  
 405              return cat_category_list($message);
 406          }
 407  
 408          $parent = ($parent) ? $parent : 'root';
 409  
 410          if (safe_update('txp_category', "name = '$name', parent = '$parent', title = '$title'", "id = $id"))
 411          {
 412              safe_update('txp_category', "parent = '$name'", "parent = '$old_name'");
 413          }
 414  
 415          rebuild_tree_full($event);
 416  
 417          if ($event == 'article')
 418          {
 419              safe_update('textpattern', "Category1 = '$name'", "Category1 = '$old_name'");
 420              safe_update('textpattern', "Category2 = '$name'", "Category2 = '$old_name'");
 421          }
 422  
 423          else
 424          {
 425              safe_update($table_name, "category = '$name'", "category = '$old_name'");
 426          }
 427  
 428          $message = gTxt($event.'_category_updated', array('{name}' => doStrip($name)));
 429  
 430          cat_category_list($message);
 431      }
 432  
 433  // --------------------------------------------------------------
 434  // Non image file upload. Have I mentioned how much I love this file refactoring?
 435  // -------------------------------------------------------------
 436  	function cat_file_list()
 437      {
 438          return cat_event_category_list('file');
 439      }
 440  
 441  //-------------------------------------------------------------
 442  	function cat_file_create()
 443      {
 444          return cat_event_category_create('file');
 445      }
 446  
 447  //-------------------------------------------------------------
 448  	function cat_file_edit()
 449      {
 450          return cat_event_category_edit('file');
 451      }
 452  
 453  //-------------------------------------------------------------
 454  	function cat_file_save()
 455      {
 456          return cat_event_category_save('file','txp_file');
 457      }
 458      
 459      
 460  ?>


Generated: Mon Feb 18 03:42:45 2008 Cross-referenced by PHPXref 0.7