[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/administrator/components/com_categories/ -> admin.categories.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: admin.categories.php 19343 2010-11-03 18:12:02Z ian $
   4   * @package        Joomla
   5   * @subpackage    Categories
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  // no direct access
  16  defined( '_JEXEC' ) or die( 'Restricted access' );
  17  
  18  require_once( JApplicationHelper::getPath( 'admin_html' ) );
  19  
  20  // get parameters from the URL or submitted form
  21  $section     = JRequest::getCmd( 'section', 'com_content' );
  22  $cid         = JRequest::getVar( 'cid', array(0), '', 'array' );
  23  JArrayHelper::toInteger($cid, array(0));
  24  
  25  switch (JRequest::getCmd('task'))
  26  {
  27      case 'add' :
  28          editCategory(false);
  29          break;
  30  
  31      case 'edit':
  32          editCategory(true);
  33          break;
  34  
  35      case 'moveselect':
  36          moveCategorySelect( $option, $cid, $section );
  37          break;
  38  
  39      case 'movesave':
  40          moveCategorySave( $cid, $section );
  41          break;
  42  
  43      case 'copyselect':
  44          copyCategorySelect( $option, $cid, $section );
  45          break;
  46  
  47      case 'copysave':
  48          copyCategorySave( $cid, $section );
  49          break;
  50  
  51      case 'go2menu':
  52      case 'go2menuitem':
  53      case 'save':
  54      case 'apply':
  55          saveCategory( );
  56          break;
  57  
  58      case 'remove':
  59          removeCategories( $section, $cid );
  60          break;
  61  
  62      case 'publish':
  63          publishCategories( $section, $cid, 1 );
  64          break;
  65  
  66      case 'unpublish':
  67          publishCategories( $section, $cid, 0 );
  68          break;
  69  
  70      case 'cancel':
  71          cancelCategory();
  72          break;
  73  
  74      case 'orderup':
  75          orderCategory( $cid[0], -1 );
  76          break;
  77  
  78      case 'orderdown':
  79          orderCategory( $cid[0], 1 );
  80          break;
  81  
  82      case 'accesspublic':
  83          accessMenu( $cid[0], 0, $section );
  84          break;
  85  
  86      case 'accessregistered':
  87          accessMenu( $cid[0], 1, $section );
  88          break;
  89  
  90      case 'accessspecial':
  91          accessMenu( $cid[0], 2, $section );
  92          break;
  93  
  94      case 'saveorder':
  95          saveOrder( $cid, $section );
  96          break;
  97  
  98      default:
  99          showCategories( $section, $option );
 100          break;
 101  }
 102  
 103  /**
 104  * Compiles a list of categories for a section
 105  * @param string The name of the category section
 106  */
 107  function showCategories( $section, $option )
 108  {
 109      global $mainframe;
 110  
 111      $db                    =& JFactory::getDBO();
 112      $filter_order        = $mainframe->getUserStateFromRequest( $option.'.filter_order',                    'filter_order',        'c.ordering',    'cmd' );
 113      if ($filter_order == 'section_name' && ($section == 'com_newsfeeds' || $section == 'com_banner' || $section == 'com_weblinks' || $section == 'com_contact_details')){
 114                               $filter_order = 'c.ordering';
 115                           } 
 116      $filter_order_Dir    = $mainframe->getUserStateFromRequest( $option.'.filter_order_Dir',                'filter_order_Dir',    '',                'word' );
 117      $filter_state        = $mainframe->getUserStateFromRequest( $option.'.'.$section.'.filter_state',    'filter_state',        '',                'word' );
 118      $sectionid            = $mainframe->getUserStateFromRequest( $option.'.'.$section.'.sectionid',        'sectionid',        0,                'int' );
 119      $search                = $mainframe->getUserStateFromRequest( $option.'.search',                        'search',            '',                'string' );
 120      if (strpos($search, '"') !== false) {
 121          $search = str_replace(array('=', '<'), '', $search);
 122      }
 123      $search = JString::strtolower($search);
 124  
 125      $limit        = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
 126      $limitstart    = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
 127  
 128      $section_name     = '';
 129      $content_add     = '';
 130      $content_join     = '';
 131  
 132      // ensure we have a good value for $filter_order
 133      if (!in_array($filter_order, array('c.title', 'c.published', 'c.ordering', 'groupname', 'section_name', 'c.id'))) {
 134          $filter_order = 'c.ordering';
 135      }
 136  
 137      if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
 138          $filter_order_Dir = '';
 139      }
 140  
 141      if (intval($section) <= 0 && $section != 'com_content' && $filter_order == 'section_name') {
 142          $filter_order = 'c.ordering';
 143      }
 144  
 145      $order             = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', c.ordering';
 146      if (intval( $section ) > 0) {
 147          $table = 'content';
 148  
 149          $query = 'SELECT title'
 150          . ' FROM #__sections'
 151          . ' WHERE id = '.(int) $section;
 152          $db->setQuery( $query );
 153          $section_name = $db->loadResult();
 154          $section_name = JText::sprintf( 'Content:', JText::_( $section_name ) );
 155          $where     = ' WHERE c.section = '.$db->Quote($section);
 156          $type     = 'content';
 157      } else if (strpos( $section, 'com_' ) === 0) {
 158          $table = substr( $section, 4 );
 159  
 160          $query = 'SELECT name'
 161          . ' FROM #__components'
 162          . ' WHERE link = '.$db->Quote('option='.$section);
 163          ;
 164          $db->setQuery( $query );
 165          $section_name = $db->loadResult();
 166          $where     = ' WHERE c.section = '.$db->Quote($section);
 167          $type     = 'other';
 168          // special handling for contact component
 169          if ( $section == 'com_contact_details' ) {
 170              $section_name     = JText::_( 'Contact' );
 171          }
 172          $section_name = JText::sprintf( 'Component:', $section_name );
 173      } else {
 174          $table     = $section;
 175          $where     = ' WHERE c.section = '.$db->Quote($section);
 176          $type     = 'other';
 177      }
 178  
 179      // get the total number of records
 180      $query = 'SELECT COUNT(*)'
 181      . ' FROM #__categories'
 182      ;
 183      if ($section == 'com_content')
 184      {
 185          if($sectionid > 0)
 186          {
 187              $query .= ' WHERE section = '.(int) $sectionid;
 188          } else {
 189              $query .= ' WHERE section > 0';
 190          }
 191      } else {
 192          $query .= ' WHERE section = '.$db->quote($section);
 193      }
 194      if ( $filter_state ) {
 195          if ( $filter_state == 'P' ) {
 196              $query .= ' AND published = 1';
 197          } else if ($filter_state == 'U' ) {
 198              $query .= ' AND published = 0';
 199          }
 200      }
 201      $db->setQuery( $query );
 202      $total = $db->loadResult();
 203  
 204      // allows for viweing of all content categories
 205      if ( $section == 'com_content' ) {
 206          $table             = 'content';
 207          $content_add     = ' , z.title AS section_name';
 208          $content_join     = ' LEFT JOIN #__sections AS z ON z.id = c.section';
 209          $where             = ' WHERE c.section NOT LIKE "%com_%"';
 210  
 211          if ($filter_order == 'c.ordering'){
 212              $order             = ' ORDER BY  z.title, c.ordering '. $filter_order_Dir;
 213          } else {
 214              $order             = ' ORDER BY  '.$filter_order.' '. $filter_order_Dir.', z.title, c.ordering';
 215          }
 216  
 217          $section_name     = JText::_( 'All Content:' );
 218  
 219          $type             = 'content';
 220      }
 221  
 222      // used by filter
 223      if ( $sectionid > 0 ) {
 224          $filter = ' AND c.section = '.$db->Quote($sectionid);
 225      } else {
 226          $filter = '';
 227      }
 228      if ( $filter_state ) {
 229          if ( $filter_state == 'P' ) {
 230              $filter .= ' AND c.published = 1';
 231          } else if ($filter_state == 'U' ) {
 232              $filter .= ' AND c.published = 0';
 233          }
 234      }
 235      if ($search) {
 236          $filter .= ' AND LOWER(c.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
 237      }
 238  
 239      jimport('joomla.html.pagination');
 240      $pageNav = new JPagination( $total, $limitstart, $limit );
 241  
 242      $tablesAllowed = $db->getTableList();
 243      if (!in_array($db->getPrefix().$table, $tablesAllowed)) {
 244          $table = 'content';
 245      }
 246  
 247      $query = 'SELECT  c.*, c.checked_out as checked_out_contact_category, g.name AS groupname, u.name AS editor, COUNT( DISTINCT s2.checked_out ) AS checked_out_count'
 248      . $content_add
 249      . ' FROM #__categories AS c'
 250      . ' LEFT JOIN #__users AS u ON u.id = c.checked_out'
 251      . ' LEFT JOIN #__groups AS g ON g.id = c.access'
 252      . ' LEFT JOIN #__'.$table.' AS s2 ON s2.catid = c.id AND s2.checked_out > 0'
 253      . $content_join
 254      . $where
 255      . $filter
 256      . ' AND c.published != -2'
 257      . ' GROUP BY c.id'
 258      . $order
 259      ;
 260      $db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
 261      $rows = $db->loadObjectList();
 262      if ($db->getErrorNum()) {
 263          echo $db->stderr();
 264          return;
 265      }
 266  
 267      $count = count( $rows );
 268      // number of Active Items
 269      for ( $i = 0; $i < $count; $i++ ) {
 270          $query = 'SELECT COUNT( a.id )'
 271          . ' FROM #__content AS a'
 272          . ' WHERE a.catid = '. (int) $rows[$i]->id
 273          . ' AND a.state <> -2'
 274          ;
 275          $db->setQuery( $query );
 276          $active = $db->loadResult();
 277          $rows[$i]->active = $active;
 278      }
 279      // number of Trashed Items
 280      for ( $i = 0; $i < $count; $i++ ) {
 281          $query = 'SELECT COUNT( a.id )'
 282          . ' FROM #__content AS a'
 283          . ' WHERE a.catid = '. (int) $rows[$i]->id
 284          . ' AND a.state = -2'
 285          ;
 286          $db->setQuery( $query );
 287          $trash = $db->loadResult();
 288          $rows[$i]->trash = $trash;
 289      }
 290  
 291      // get list of sections for dropdown filter
 292      $javascript = 'onchange="document.adminForm.submit();"';
 293      $lists['sectionid']    = JHTML::_('list.section',  'sectionid', $sectionid, $javascript, 'ordering', false );
 294  
 295      // state filter
 296      $lists['state']    = JHTML::_('grid.state',  $filter_state );
 297  
 298      // table ordering
 299      $lists['order_Dir'] = $filter_order_Dir;
 300      $lists['order']        = $filter_order;
 301  
 302      // search filter
 303      $lists['search']= $search;
 304  
 305      categories_html::show( $rows, $section, $section_name, $pageNav, $lists, $type );
 306  }
 307  
 308  /**
 309  * Compiles information to add or edit a category
 310  * @param string The name of the category section
 311  * @param integer The unique id of the category to edit (0 if new)
 312  * @param string The name of the current user
 313  */
 314  function editCategory($edit )
 315  {
 316      global $mainframe;
 317  
 318      // Initialize variables
 319      $db            =& JFactory::getDBO();
 320      $user         =& JFactory::getUser();
 321      $uid        = $user->get('id');
 322  
 323      $type        = JRequest::getCmd( 'type' );
 324      $redirect    = JRequest::getCmd( 'section', 'com_content' );
 325      $section    = JRequest::getCmd( 'section', 'com_content' );
 326      $cid        = JRequest::getVar( 'cid', array(0), '', 'array' );
 327  
 328      JArrayHelper::toInteger($cid, array(0));
 329  
 330      // check for existance of any sections
 331      $query = 'SELECT COUNT( id )'
 332      . ' FROM #__sections'
 333      . ' WHERE scope = "content"'
 334      ;
 335      $db->setQuery( $query );
 336      $sections = $db->loadResult();
 337      if (!$sections && $type != 'other'
 338              && $section != 'com_weblinks'
 339              && $section != 'com_newsfeeds'
 340              && $section != 'com_contact_details'
 341              && $section != 'com_banner') {
 342          $mainframe->redirect( 'index.php?option=com_categories&section='. $section, JText::_( 'WARNSECTION', true ) );
 343      }
 344  
 345      $row =& JTable::getInstance('category');
 346      // load the row from the db table
 347      if ($edit)
 348          $row->load( $cid[0] );
 349  
 350      // fail if checked out not by 'me'
 351      if ( JTable::isCheckedOut($user->get ('id'), $row->checked_out )) {
 352          $msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The category' ), $row->title );
 353          $mainframe->redirect( 'index.php?option=com_categories&section='. $row->section, $msg );
 354      }
 355  
 356      if ( $edit ) {
 357          $row->checkout( $user->get('id'));
 358      } else {
 359          $row->published     = 1;
 360      }
 361  
 362  
 363      // make order list
 364      $order = array();
 365      $query = 'SELECT COUNT(*)'
 366      . ' FROM #__categories'
 367      . ' WHERE section = '.$db->Quote($row->section)
 368      ;
 369      $db->setQuery( $query );
 370      $max = intval( $db->loadResult() ) + 1;
 371  
 372      for ($i=1; $i < $max; $i++) {
 373          $order[] = JHTML::_('select.option',  $i );
 374      }
 375  
 376      // build the html select list for sections
 377      if ( $section == 'com_content' ) {
 378  
 379          if (!$row->section && JRequest::getInt('sectionid')) {
 380              $row->section = JRequest::getInt('sectionid');
 381          }
 382  
 383          $query = 'SELECT s.id AS value, s.title AS text'
 384          . ' FROM #__sections AS s'
 385          . ' ORDER BY s.ordering'
 386          ;
 387          $db->setQuery( $query );
 388          $sections = $db->loadObjectList();
 389          $lists['section'] = JHTML::_('select.genericlist',   $sections, 'section', 'class="inputbox" size="1"', 'value', 'text', $row->section );
 390      } else {
 391          if ( $type == 'other' ) {
 392              $section_name = JText::_( 'N/A' );
 393          } else {
 394              $temp =& JTable::getInstance('section');
 395              $temp->load( $row->section );
 396              $section_name = $temp->name;
 397          }
 398          if(!$section_name) $section_name = JText::_( 'N/A' );
 399          $row->section = $section;
 400          $lists['section'] = '<input type="hidden" name="section" value="'. $row->section .'" />'. $section_name;
 401      }
 402  
 403      // build the html select list for ordering
 404      $query = 'SELECT ordering AS value, title AS text'
 405      . ' FROM #__categories'
 406      . ' WHERE section = '.$db->Quote($row->section)
 407      . ' ORDER BY ordering'
 408      ;
 409      if ($edit) {
 410          $lists['ordering']             = JHTML::_('list.specificordering',  $row, $cid[0], $query );
 411      }
 412      else {
 413          $lists['ordering']             = JHTML::_('list.specificordering',  $row, '', $query );
 414      }
 415      // build the select list for the image positions
 416      $active =  ( $row->image_position ? $row->image_position : 'left' );
 417      $lists['image_position']     = JHTML::_('list.positions',  'image_position', $active, NULL, 0, 0 );
 418      // Imagelist
 419      $lists['image']             = JHTML::_('list.images',  'image', $row->image );
 420      // build the html select list for the group access
 421      $lists['access']             = JHTML::_('list.accesslevel',  $row );
 422      // build the html radio buttons for published
 423      $published = ($row->id) ? $row->published : 1;
 424      $lists['published']         = JHTML::_('select.booleanlist',  'published', 'class="inputbox"', $published );
 425  
 426       categories_html::edit( $row, $lists, $redirect );
 427  }
 428  
 429  /**
 430  * Saves the catefory after an edit form submit
 431  * @param string The name of the category section
 432  */
 433  function saveCategory()
 434  {
 435      global $mainframe;
 436  
 437      // Check for request forgeries
 438      JRequest::checkToken() or jexit( 'Invalid Token' );
 439  
 440      // Initialize variables
 441      $db         =& JFactory::getDBO();
 442      $menu         = JRequest::getVar( 'menu', 'mainmenu', 'post', 'menutype' );
 443      $menuid        = JRequest::getVar( 'menuid', 0, 'post', 'int' );
 444      $redirect     = JRequest::getCmd( 'redirect', '', 'post' );
 445      $oldtitle     = JRequest::getString( 'oldtitle', '', 'post' );
 446      $post        = JRequest::get( 'post' );
 447  
 448      // fix up special html fields
 449      $post['description'] = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWRAW );
 450  
 451      $row =& JTable::getInstance('category');
 452      if (!$row->bind( $post )) {
 453          JError::raiseError(500, $row->getError() );
 454      }
 455      if (!$row->check()) {
 456          JError::raiseError(500, $row->getError() );
 457      }
 458      // if new item order last in appropriate group
 459      if (!$row->id) {
 460          $where = "section = " . $db->Quote($row->section);
 461          $row->ordering = $row->getNextOrder( $where );
 462      }
 463  
 464      if (!$row->store()) {
 465          JError::raiseError(500, $row->getError() );
 466      }
 467      $row->checkin();
 468  
 469      if ($row->section > 0) {
 470          $query = 'UPDATE #__content'
 471                  .' SET sectionid = '.$row->section
 472                  .' WHERE catid = '.$row->id
 473          ;
 474          $db->setQuery( $query );
 475          $db->query();
 476      }
 477  
 478      if ( $oldtitle ) {
 479          if ($oldtitle != $row->title) {
 480              $query = 'UPDATE #__menu'
 481              . ' SET name = '.$db->Quote($row->title)
 482              . ' WHERE name = '.$db->Quote($oldtitle)
 483              . ' AND type = "content_category"'
 484              ;
 485              $db->setQuery( $query );
 486              $db->query();
 487          }
 488      }
 489  
 490      // Update Section Count
 491      if ($row->section != 'com_contact_details' &&
 492          $row->section != 'com_newsfeeds' &&
 493          $row->section != 'com_weblinks') {
 494          $query = 'UPDATE #__sections SET count=count+1'
 495          . ' WHERE id = '.$db->Quote($row->section)
 496          ;
 497          $db->setQuery( $query );
 498      }
 499  
 500      if (!$db->query()) {
 501          JError::raiseError(500, $db->getErrorMsg() );
 502      }
 503  
 504      switch ( JRequest::getCmd('task') )
 505      {
 506          case 'go2menu':
 507              $mainframe->redirect('index.php?option=com_menus&menutype=' . $menu);
 508              break;
 509  
 510          case 'go2menuitem':
 511              $mainframe->redirect(
 512                  'index.php?option=com_menus&menutype=' . $menu 
 513                  . '&task=edit&id='. $menuid 
 514              );
 515              break;
 516  
 517          case 'apply':
 518              $msg = JText::_( 'Changes to Category saved' );
 519              $mainframe->redirect( 'index.php?option=com_categories&section='. $redirect .'&task=edit&cid[]='. $row->id, $msg );
 520              break;
 521  
 522          case 'save':
 523          default:
 524              $msg = JText::_( 'Category saved' );
 525              $mainframe->redirect( 'index.php?option=com_categories&section='. $redirect, $msg );
 526              break;
 527      }
 528  }
 529  
 530  /**
 531  * Deletes one or more categories from the categories table
 532  * @param string The name of the category section
 533  * @param array An array of unique category id numbers
 534  */
 535  function removeCategories( $section, $cid )
 536  {
 537      global $mainframe;
 538  
 539      // Check for request forgeries
 540      JRequest::checkToken() or jexit( 'Invalid Token' );
 541  
 542      // Initialize variables
 543      $db =& JFactory::getDBO();
 544  
 545      JArrayHelper::toInteger($cid);
 546  
 547      if (count( $cid ) < 1) {
 548          JError::raiseError(500, JText::_( 'Select a category to delete', true ));
 549      }
 550  
 551      $cids = implode( ',', $cid );
 552  
 553      if (intval( $section ) > 0) {
 554          $table = 'content';
 555      } else if (strpos( $section, 'com_' ) === 0) {
 556          $table = substr( $section, 4 );
 557      } else {
 558          $table = $section;
 559      }
 560  
 561      $tablesAllowed = $db->getTableList();
 562      if (!in_array($db->getPrefix().$table, $tablesAllowed)) {
 563          $table = 'content';
 564      }
 565  
 566      $query = 'SELECT c.id, c.name, c.title, COUNT( s.catid ) AS numcat'
 567      . ' FROM #__categories AS c'
 568      . ' LEFT JOIN #__'.$table.' AS s ON s.catid = c.id'
 569      . ' WHERE c.id IN ( '.$cids.' )'
 570      . ' GROUP BY c.id'
 571      ;
 572      $db->setQuery( $query );
 573  
 574      if (!($rows = $db->loadObjectList())) {
 575          JError::raiseError( 500, $db->stderr() );
 576          return false;
 577      }
 578  
 579      $err = array();
 580      $cid = array();
 581      foreach ($rows as $row) {
 582          if ($row->numcat == 0) {
 583              $cid[] = (int) $row->id;
 584          } else {
 585              $err[] = $row->title;
 586          }
 587      }
 588  
 589      if (count( $cid )) {
 590          $cids = implode( ',', $cid );
 591          $query = 'DELETE FROM #__categories'
 592          . ' WHERE id IN ( '.$cids.' )'
 593          ;
 594          $db->setQuery( $query );
 595          if (!$db->query()) {
 596              JError::raiseError( 500, $db->stderr() );
 597              return false;
 598          }
 599      }
 600  
 601      if (count( $err )) {
 602          $cids = implode( ", ", $err );
 603          $msg = JText::sprintf( 'WARNNOTREMOVEDRECORDS', $cids );
 604          $mainframe->redirect( 'index.php?option=com_categories&section='. $section, $msg );
 605      }
 606  
 607      $mainframe->redirect( 'index.php?option=com_categories&section='. $section );
 608  }
 609  
 610  /**
 611  * Publishes or Unpublishes one or more categories
 612  * @param string The name of the category section
 613  * @param integer A unique category id (passed from an edit form)
 614  * @param array An array of unique category id numbers
 615  * @param integer 0 if unpublishing, 1 if publishing
 616  * @param string The name of the current user
 617  */
 618  function publishCategories( $section, $cid=null, $publish=1 )
 619  {
 620      global $mainframe;
 621  
 622      // Check for request forgeries
 623      JRequest::checkToken() or jexit( 'Invalid Token' );
 624  
 625      // Initialize variables
 626      $db        =& JFactory::getDBO();
 627      $user    =& JFactory::getUser();
 628      $uid    = $user->get('id');
 629  
 630      JArrayHelper::toInteger($cid);
 631  
 632      if (count( $cid ) < 1) {
 633          $action = $publish ? 'publish' : 'unpublish';
 634          JError::raiseError(500, JText::_( 'Select a category to '.$action, true ) );
 635      }
 636  
 637      $cids = implode( ',', $cid );
 638  
 639      $query = 'UPDATE #__categories'
 640      . ' SET published = ' . (int) $publish
 641      . ' WHERE id IN ( '.$cids.' )'
 642      . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
 643      ;
 644      $db->setQuery( $query );
 645      if (!$db->query()) {
 646          JError::raiseError(500, $db->getErrorMsg() );
 647      }
 648  
 649      if (count( $cid ) == 1) {
 650          $row =& JTable::getInstance('category');
 651          $row->checkin( $cid[0] );
 652      }
 653  
 654      $mainframe->redirect( 'index.php?option=com_categories&section='. $section );
 655  }
 656  
 657  /**
 658  * Cancels an edit operation
 659  * @param string The name of the category section
 660  * @param integer A unique category id
 661  */
 662  function cancelCategory()
 663  {
 664      global $mainframe;
 665  
 666      // Check for request forgeries
 667      JRequest::checkToken() or jexit( 'Invalid Token' );
 668  
 669      // Initialize variables
 670      $db =& JFactory::getDBO();
 671  
 672      $redirect = JRequest::getCmd( 'redirect', '', 'post' );
 673  
 674      $row =& JTable::getInstance('category');
 675      $row->bind( JRequest::get( 'post' ));
 676      $row->checkin();
 677  
 678      $mainframe->redirect( 'index.php?option=com_categories&section='. $redirect );
 679  }
 680  
 681  /**
 682  * Moves the order of a record
 683  * @param integer The increment to reorder by
 684  */
 685  function orderCategory( $uid, $inc )
 686  {
 687      global $mainframe;
 688  
 689      // Check for request forgeries
 690      JRequest::checkToken() or jexit( 'Invalid Token' );
 691  
 692      // Initialize variables
 693      $db        =& JFactory::getDBO();
 694      $row    =& JTable::getInstance('category' );
 695      $row->load( $uid );
 696      $row->move( $inc, 'section = '.$db->Quote($row->section) );
 697      $section = JRequest::getCmd('section');
 698      if($section) {
 699          $section = '&section='. $section;
 700      }
 701      $mainframe->redirect( 'index.php?option=com_categories'. $section );
 702  }
 703  
 704  /**
 705  * Form for moving item(s) to a specific menu
 706  */
 707  function moveCategorySelect( $option, $cid, $sectionOld )
 708  {
 709      global $mainframe;
 710  
 711      // Check for request forgeries
 712      JRequest::checkToken() or jexit( 'Invalid Token' );
 713  
 714      $db =& JFactory::getDBO();
 715      $redirect = JRequest::getCmd( 'section', 'com_content', 'post' );
 716  
 717      JArrayHelper::toInteger($cid);
 718  
 719      if (count( $cid ) < 1) {
 720          JError::raiseError(500, JText::_( 'Select an item to move', true ));
 721      }
 722  
 723      ## query to list selected categories
 724      $cids = implode( ',', $cid );
 725      $query = 'SELECT a.title, a.section'
 726      . ' FROM #__categories AS a'
 727      . ' WHERE a.id IN ( '.$cids.' )'
 728      ;
 729      $db->setQuery( $query );
 730      $items = $db->loadObjectList();
 731  
 732      ## query to list items from categories
 733      $query = 'SELECT a.title'
 734      . ' FROM #__content AS a'
 735      . ' WHERE a.catid IN ( '.$cids.' )'
 736      . ' ORDER BY a.catid, a.title'
 737      ;
 738      $db->setQuery( $query );
 739      $contents = $db->loadObjectList();
 740  
 741      ## query to choose section to move to
 742      $query = 'SELECT a.title AS text, a.id AS value'
 743      . ' FROM #__sections AS a'
 744      . ' WHERE a.published = 1'
 745      . ' ORDER BY a.title'
 746      ;
 747      $db->setQuery( $query );
 748      $sections = $db->loadObjectList();
 749  
 750      // build the html select list
 751      $SectionList = JHTML::_('select.genericlist',   $sections, 'sectionmove', 'class="inputbox" size="10"', 'value', 'text', null );
 752  
 753      categories_html::moveCategorySelect( $option, $cid, $SectionList, $items, $sectionOld, $contents, $redirect );
 754  }
 755  
 756  
 757  /**
 758  * Save the item(s) to the menu selected
 759  */
 760  function moveCategorySave( $cid, $sectionOld )
 761  {
 762      global $mainframe;
 763  
 764      // Check for request forgeries
 765      JRequest::checkToken() or jexit( 'Invalid Token' );
 766  
 767      $db =& JFactory::getDBO();
 768      $sectionMove = JRequest::getCmd( 'sectionmove' );
 769  
 770      //Check to see of a section was selected to copy the items too
 771      if (!$sectionMove)
 772      {
 773          $msg = JText::_('Please select a section from the list');
 774          moveCategorySelect( 'com_categories', $cid, $sectionOld );
 775          JError::raiseWarning(500, $msg);
 776          return;
 777      }
 778  
 779      JArrayHelper::toInteger($cid, array(0));
 780  
 781      $sectionNew =& JTable::getInstance('section');
 782      $sectionNew->load( $sectionMove );
 783  
 784      //Remove the categories was in destination section
 785      $cids = implode( ',', $cid );
 786  
 787      $query = 'SELECT id, title'
 788      . ' FROM #__categories'
 789      . ' WHERE id IN ( '.$cids.' )'
 790      . ' AND section = '.$db->Quote($sectionMove)
 791      ;
 792      $db->setQuery( $query );
 793  
 794      $scid   = $db->loadResultArray(0);
 795      $title  = $db->loadResultArray(1);
 796  
 797      $cid = array_diff($cid, $scid);
 798  
 799      //
 800      if ( !empty($cid) ) {
 801          $cids = implode( ',', $cid );
 802          $total = count( $cid );
 803  
 804          $query = 'UPDATE #__categories'
 805          . ' SET section = '.$db->Quote($sectionMove)
 806          . ' WHERE id IN ( '.$cids.' )'
 807          ;
 808          $db->setQuery( $query );
 809          if ( !$db->query() ) {
 810              JError::raiseError(500, $db->getErrorMsg() );
 811          }
 812          $query = 'UPDATE #__content'
 813          . ' SET sectionid = '.$db->Quote($sectionMove)
 814          . ' WHERE catid IN ( '.$cids.' )'
 815          ;
 816          $db->setQuery( $query );
 817          if ( !$db->query() ) {
 818              JError::raiseError(500, $db->getErrorMsg());
 819          }
 820  
 821          $msg = JText::sprintf( 'Categories moved to', $sectionNew->title );
 822          $mainframe->enqueueMessage($msg);
 823      }
 824      if ( !empty($title) && is_array($title) ) {
 825          if ( count($title) == 1 ) {
 826              $msg = JText::sprintf( 'Category already in', implode( ', ', $title ), $sectionNew->title );
 827          } else {
 828              $msg = JText::sprintf( 'Categories already in', implode( ', ', $title ), $sectionNew->title );
 829          }
 830          $mainframe->enqueueMessage($msg);
 831      }
 832  
 833      $mainframe->redirect( 'index.php?option=com_categories&section='. $sectionOld );
 834  }
 835  
 836  /**
 837  * Form for copying item(s) to a specific menu
 838  */
 839  function copyCategorySelect( $option, $cid, $sectionOld )
 840  {
 841      global $mainframe;
 842  
 843      // Check for request forgeries
 844      JRequest::checkToken() or jexit( 'Invalid Token' );
 845  
 846      $db =& JFactory::getDBO();
 847      $redirect = JRequest::getCmd( 'section', 'com_content', 'post' );
 848  
 849      JArrayHelper::toInteger($cid);
 850  
 851      if (count( $cid ) < 1) {
 852          JError::raiseError(500, JText::_( 'Select an item to move', true ));
 853      }
 854  
 855      ## query to list selected categories
 856      $cids = implode( ',', $cid );
 857      $query = 'SELECT a.title, a.section'
 858      . ' FROM #__categories AS a'
 859      . ' WHERE a.id IN ( '.$cids.' )'
 860      ;
 861      $db->setQuery( $query );
 862      $items = $db->loadObjectList();
 863  
 864      ## query to list items from categories
 865      $query = 'SELECT a.title, a.id'
 866      . ' FROM #__content AS a'
 867      . ' WHERE a.catid IN ( '.$cids.' )'
 868      . ' ORDER BY a.catid, a.title'
 869      ;
 870      $db->setQuery( $query );
 871      $contents = $db->loadObjectList();
 872  
 873      ## query to choose section to move to
 874      $query = 'SELECT a.title AS `text`, a.id AS `value`'
 875      . ' FROM #__sections AS a'
 876      . ' WHERE a.published = 1'
 877      . ' ORDER BY a.name'
 878      ;
 879      $db->setQuery( $query );
 880      $sections = $db->loadObjectList();
 881  
 882      // build the html select list
 883      $SectionList = JHTML::_('select.genericlist',   $sections, 'sectionmove', 'class="inputbox" size="10"', 'value', 'text', null );
 884  
 885      categories_html::copyCategorySelect( $option, $cid, $SectionList, $items, $sectionOld, $contents, $redirect );
 886  }
 887  
 888  
 889  /**
 890  * Save the item(s) to the menu selected
 891  */
 892  function copyCategorySave( $cid, $sectionOld )
 893  {
 894      global $mainframe;
 895  
 896      // Check for request forgeries
 897      JRequest::checkToken() or jexit( 'Invalid Token' );
 898  
 899      // Initialize variables
 900      $db =& JFactory::getDBO();
 901  
 902      $sectionMove     = JRequest::getInt( 'sectionmove' );
 903  
 904      //Check to see of a section was selected to copy the items too
 905      if (!$sectionMove)
 906      {
 907          $msg = JText::_('Please select a section from the list');
 908          copyCategorySelect( 'com_categories', $cid, $sectionOld );
 909          JError::raiseWarning(500, $msg);
 910          return;
 911      }
 912  
 913      $contentid         = JRequest::getVar( 'item', null, '', 'array' );
 914      JArrayHelper::toInteger($contentid);
 915  
 916      $category =& JTable::getInstance('category');
 917  
 918      foreach( $cid as $id )
 919      {
 920          $category->load( $id );
 921          $category->id         = NULL;
 922          $category->title     = JText::sprintf( 'Copy of', $category->title );
 923          $category->name     = JText::sprintf( 'Copy of', $category->name );
 924          $category->section     = $sectionMove;
 925          if (!$category->check()) {
 926              JError::raiseError(500, $category->getError());
 927          }
 928  
 929          if (!$category->store()) {
 930              JError::raiseError(500, $category->getError());
 931          }
 932          $category->checkin();
 933          // stores original catid
 934          $newcatids[]["old"] = $id;
 935          // pulls new catid
 936          $newcatids[]["new"] = $category->id;
 937      }
 938  
 939      $content =& JTable::getInstance('content');
 940      foreach( $contentid as $id) {
 941          $content->load( $id );
 942          $content->id         = NULL;
 943          $content->sectionid = $sectionMove;
 944          $content->hits         = 0;
 945          foreach( $newcatids as $newcatid ) {
 946              if ( $content->catid == $newcatid["old"] ) {
 947                  $content->catid = $newcatid["new"];
 948              }
 949          }
 950          if (!$content->check()) {
 951              JError::raiseError(500, $content->getError());
 952          }
 953  
 954          if (!$content->store()) {
 955              JError::raiseError(500, $content->getError());
 956          }
 957          $content->checkin();
 958      }
 959  
 960      $sectionNew =& JTable::getInstance('section');
 961      $sectionNew->load( $sectionMove );
 962  
 963      $msg = JText::sprintf( 'Categories copied to', count($cid), $sectionNew->title );
 964      $mainframe->redirect( 'index.php?option=com_categories&section='. $sectionOld, $msg );
 965  }
 966  
 967  /**
 968  * changes the access level of a record
 969  * @param integer The increment to reorder by
 970  */
 971  function accessMenu( $uid, $access, $section )
 972  {
 973      global $mainframe;
 974  
 975      // Check for request forgeries
 976      JRequest::checkToken() or jexit( 'Invalid Token' );
 977  
 978      // Initialize variables
 979      $db =& JFactory::getDBO();
 980  
 981      $row =& JTable::getInstance('category');
 982      $row->load( $uid );
 983      $row->access = $access;
 984  
 985      if ( !$row->check() ) {
 986          return $row->getError();
 987      }
 988      if ( !$row->store() ) {
 989          return $row->getError();
 990      }
 991  
 992      $mainframe->redirect( 'index.php?option=com_categories&section='. $section );
 993  }
 994  
 995  function saveOrder( &$cid, $section )
 996  {
 997      global $mainframe;
 998  
 999      // Check for request forgeries
1000      JRequest::checkToken() or jexit( 'Invalid Token' );
1001  
1002      // Initialize variables
1003      $db =& JFactory::getDBO();
1004  
1005      $total        = count( $cid );
1006      $order         = JRequest::getVar( 'order', array(0), 'post', 'array' );
1007      JArrayHelper::toInteger($order, array(0));
1008      $row        =& JTable::getInstance('category');
1009      $groupings = array();
1010  
1011      // update ordering values
1012      for( $i=0; $i < $total; $i++ ) {
1013          $row->load( (int) $cid[$i] );
1014          // track sections
1015          $groupings[] = $row->section;
1016          if ($row->ordering != $order[$i]) {
1017              $row->ordering = $order[$i];
1018              if (!$row->store()) {
1019                  JError::raiseError(500, $db->getErrorMsg());
1020              }
1021          }
1022      }
1023  
1024      // execute updateOrder for each parent group
1025      $groupings = array_unique( $groupings );
1026      foreach ($groupings as $group){
1027          $row->reorder('section = '.$db->Quote($group));
1028      }
1029  
1030      $msg     = JText::_( 'New ordering saved' );
1031      $mainframe->redirect( 'index.php?option=com_categories&section='. $section, $msg );
1032  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1