[ Index ]

PHP Cross Reference of Textpattern 4.0.8

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: https://textpattern.googlecode.com/svn/releases/4.0.8/source/textpattern/include/txp_category.php $
  13  $LastChangedRevision: 3072 $
  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  
 167          if ($method == 'delete' and is_array($things) and $things and in_array($type, array('article','image','link','file')))
 168          {
 169              $things = array_map('assert_int', $things);
 170  
 171              if ($type === 'article')
 172              {
 173                  $cat1 = safe_column('DISTINCT category1', 'textpattern', '1=1');
 174                  $cat2 = safe_column('DISTINCT category2', 'textpattern', '1=1');
 175                  $used = array_unique($cat1 + $cat2);
 176              }
 177              else
 178              {
 179                  $used = safe_column('DISTINCT category', 'txp_'.$type, '1=1');
 180              }
 181  
 182              $rs = safe_rows('id, name', 'txp_category', "id IN (".join(',', $things).") AND type='".$type."' AND rgt - lft = 1 AND name NOT IN ('".join("','", doSlash($used))."')");
 183  
 184              if ($rs)
 185              {
 186                  foreach($rs as $cat)
 187                  {
 188                      $catid[] = $cat['id'];
 189                      $names[] = $cat['name'];
 190                  }
 191  
 192                  if (safe_delete('txp_category','id IN ('.join(',', $catid).') AND rgt - lft = 1'))
 193                  {
 194                      rebuild_tree_full($type);
 195  
 196                      $message = gTxt($type.'_categories_deleted', array('{list}' => join(', ',$catid)));
 197  
 198                      return cat_category_list($message);
 199                  }
 200              }
 201          }
 202  
 203          return cat_category_list();
 204      }
 205  
 206  //Refactoring: Functions are more or less the same for all event types
 207  // so, merge them. Addition of new event categories is easiest now.
 208  
 209  //-------------------------------------------------------------
 210  
 211  	function cat_event_category_list($event)
 212      {
 213          $out = n.n.hed(gTxt($event.'_head').sp.popHelp($event.'_category'), 3).
 214  
 215              form(
 216                  fInput('text', 'title', '', 'edit', '', '', 20).
 217                  fInput('submit', '', gTxt('Create'), 'smallerbox').
 218                  eInput('category').
 219                  sInput('cat_'.$event.'_create')
 220              );
 221  
 222          $rs = getTree('root', $event);
 223  
 224          if ($rs)
 225          {
 226              $total_count = array();
 227  
 228              if ($event == 'article')
 229              {
 230                  $rs2 = safe_rows_start('Category1, count(*) as num', 'textpattern', "1 = 1 group by Category1");
 231  
 232                  while ($a = nextRow($rs2))
 233                  {
 234                      $name = $a['Category1'];
 235                      $num = $a['num'];
 236  
 237                      $total_count[$name] = $num;
 238                  }
 239  
 240                  $rs2 = safe_rows_start('Category2, count(*) as num', 'textpattern', "1 = 1 group by Category2");
 241  
 242                  while ($a = nextRow($rs2))
 243                  {
 244                      $name = $a['Category2'];
 245                      $num = $a['num'];
 246  
 247                      if (isset($total_count[$name]))
 248                      {
 249                          $total_count[$name] += $num;
 250                      }
 251  
 252                      else
 253                      {
 254                          $total_count[$name] = $num;
 255                      }
 256                  }
 257              }
 258  
 259              else
 260              {
 261                  switch ($event)
 262                  {
 263                      case 'link':
 264                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_link', "1 group by category");
 265                      break;
 266  
 267                      case 'image':
 268                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_image', "1 group by category");
 269                      break;
 270  
 271                      case 'file':
 272                          $rs2 = safe_rows_start('category, count(*) as num', 'txp_file', "1 group by category");
 273                      break;
 274                  }
 275  
 276                  while ($a = nextRow($rs2))
 277                  {
 278                      $name = $a['category'];
 279                      $num = $a['num'];
 280  
 281                      $total_count[$name] = $num;
 282                  }
 283              }
 284  
 285              $items = array();
 286  
 287              foreach ($rs as $a)
 288              {
 289                  extract($a);
 290  
 291                  // format count
 292                  switch ($event)
 293                  {
 294                      case 'article':
 295                          $url = 'index.php?event=list'.a.'search_method=categories'.a.'crit='.$name;
 296                      break;
 297  
 298                      case 'link':
 299                          $url = 'index.php?event=link'.a.'search_method=category'.a.'crit='.$name;
 300                      break;
 301  
 302                      case 'image':
 303                          $url = 'index.php?event=image'.a.'search_method=category'.a.'crit='.$name;
 304                      break;
 305  
 306                      case 'file':
 307                          $url = 'index.php?event=file'.a.'search_method=category'.a.'crit='.$name;
 308                      break;
 309                  }
 310  
 311                  $count = isset($total_count[$name]) ? '('.href($total_count[$name], $url).')' : '(0)';
 312  
 313                  if (empty($title)) {
 314                      $edit_link = '<em>'.eLink('category', 'cat_'.$event.'_edit', 'id', $id, gTxt('untitled')).'</em>';
 315                  } else {
 316                      $edit_link = eLink('category', 'cat_'.$event.'_edit', 'id', $id, $title);
 317                  }
 318  
 319                  $items[] = graf(
 320                      checkbox('selected[]', $id, 0).sp.str_repeat(sp.sp, $level * 2).$edit_link.sp.small($count)
 321                  );
 322              }
 323  
 324              if ($items)
 325              {
 326                  $out .= cat_article_multiedit_form($event, $items);
 327              }
 328          }
 329  
 330          else
 331          {
 332              $out .= graf(gTxt('no_categories_exist'));
 333          }
 334  
 335          return $out;
 336      }
 337  
 338  //-------------------------------------------------------------
 339  
 340  	function cat_event_category_create($event)
 341      {
 342          global $txpcfg;
 343  
 344          $title = ps('title');
 345  
 346          $name = strtolower(sanitizeForUrl($title));
 347  
 348          if (!$name)
 349          {
 350              $message = gTxt($event.'_category_invalid', array('{name}' => $name));
 351  
 352              return cat_category_list($message);
 353          }
 354  
 355          $exists = safe_field('name', 'txp_category', "name = '".doSlash($name)."' and type = '".doSlash($event)."'");
 356  
 357          if ($exists)
 358          {
 359              $message = gTxt($event.'_category_already_exists', array('{name}' => $name));
 360  
 361              return cat_category_list($message);
 362          }
 363  
 364          $q = safe_insert('txp_category', "name = '".doSlash($name)."', title = '".doSlash($title)."', type = '".doSlash($event)."', parent = 'root'");
 365  
 366          if ($q)
 367          {
 368              rebuild_tree_full($event);
 369  
 370              $message = gTxt($event.'_category_created', array('{name}' => $name));
 371  
 372              cat_category_list($message);
 373          }
 374      }
 375  
 376  //-------------------------------------------------------------
 377  	function cat_event_category_edit($evname)
 378      {
 379          pagetop(gTxt('categories'));
 380  
 381          $id     = assert_int(gps('id'));
 382          $parent = doSlash(gps('parent'));
 383  
 384          $row = safe_row("*", "txp_category", "id=$id");
 385          if($row){
 386              extract($row);
 387              $out = stackRows(
 388                  fLabelCell($evname.'_category_name') . fInputCell('name', $name, 1, 20),
 389                  fLabelCell('parent') . td(cat_parent_pop($parent,$evname,$id)),
 390                  fLabelCell($evname.'_category_title') . fInputCell('title', $title, 1, 30),
 391                  hInput('id',$id),
 392                  tdcs(fInput('submit', '', gTxt('save_button'),'smallerbox'), 2)
 393              );
 394          }
 395          $out.= eInput( 'category' ) . sInput( 'cat_'.$evname.'_save' ) . hInput( 'old_name',$name );
 396          echo form( startTable( 'edit' ) . $out . endTable() );
 397      }
 398  
 399  //-------------------------------------------------------------
 400  
 401  	function cat_event_category_save($event, $table_name)
 402      {
 403          global $txpcfg;
 404  
 405          extract(doSlash(psa(array('id', 'name', 'old_name', 'parent', 'title'))));
 406          $id = assert_int($id);
 407  
 408          $name = sanitizeForUrl($name);
 409  
 410          // make sure the name is valid
 411          if (!$name)
 412          {
 413              $message = gTxt($event.'_category_invalid', array('{name}' => $name));
 414  
 415              return cat_category_list($message);
 416          }
 417  
 418          // don't allow rename to clobber an existing category
 419          $existing_id = safe_field('id', 'txp_category', "name = '$name' and type = '$event'");
 420  
 421          if ($existing_id and $existing_id != $id)
 422          {
 423              $message = gTxt($event.'_category_already_exists', array('{name}' => $name));
 424  
 425              return cat_category_list($message);
 426          }
 427  
 428          $parent = ($parent) ? $parent : 'root';
 429  
 430          if (safe_update('txp_category', "name = '$name', parent = '$parent', title = '$title'", "id = $id"))
 431          {
 432              safe_update('txp_category', "parent = '$name'", "parent = '$old_name'");
 433          }
 434  
 435          rebuild_tree_full($event);
 436  
 437          if ($event == 'article')
 438          {
 439              safe_update('textpattern', "Category1 = '$name'", "Category1 = '$old_name'");
 440              safe_update('textpattern', "Category2 = '$name'", "Category2 = '$old_name'");
 441          }
 442  
 443          else
 444          {
 445              safe_update($table_name, "category = '$name'", "category = '$old_name'");
 446          }
 447  
 448          $message = gTxt($event.'_category_updated', array('{name}' => doStrip($name)));
 449  
 450          cat_category_list($message);
 451      }
 452  
 453  // --------------------------------------------------------------
 454  // Non image file upload. Have I mentioned how much I love this file refactoring?
 455  // -------------------------------------------------------------
 456  	function cat_file_list()
 457      {
 458          return cat_event_category_list('file');
 459      }
 460  
 461  //-------------------------------------------------------------
 462  	function cat_file_create()
 463      {
 464          return cat_event_category_create('file');
 465      }
 466  
 467  //-------------------------------------------------------------
 468  	function cat_file_edit()
 469      {
 470          return cat_event_category_edit('file');
 471      }
 472  
 473  //-------------------------------------------------------------
 474  	function cat_file_save()
 475      {
 476          return cat_event_category_save('file','txp_file');
 477      }
 478  
 479  
 480  ?>


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7