[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/administrator/components/com_menus/models/ -> menutype.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: menutype.php 18162 2010-07-16 07:00:47Z ian $
   4   * @package        Joomla
   5   * @subpackage    Menus
   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 to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport( 'joomla.application.component.model' );
  19  
  20  /**
  21   * @package        Joomla
  22   * @subpackage    Menus
  23   */
  24  class MenusModelMenutype extends JModel
  25  {
  26      var $_modelName = 'menutype';
  27  
  28      /** @var object JTable object */
  29      var $_table = null;
  30  
  31      /**
  32       * Returns the internal table object
  33       * @return JTable
  34       */
  35      function &getTable()
  36      {
  37          if ($this->_table == null) {
  38              $this->_table = & JTable::getInstance('menuTypes');
  39              if ($id = JRequest::getVar('id', false, '', 'int')) {
  40                  $this->_table->load($id);
  41              }
  42          }
  43          return $this->_table;
  44      }
  45  
  46      /**
  47       * Get a list of the menu records associated with the type
  48       *
  49       * @param string The menu type
  50       * @return array An array of records as objects
  51       */
  52  	function getMenus()
  53      {
  54          global $mainframe;
  55  
  56          $menus= array();
  57          $db = &$this->getDBO();
  58  
  59          // Preselect some aggregate data
  60  
  61          // Query to get published menu item counts
  62          $query = 'SELECT a.menutype, COUNT( a.menutype ) AS num' .
  63                  ' FROM #__menu AS a' .
  64                  ' WHERE a.published = 1' .
  65                  ' GROUP BY a.menutype';
  66          $db->setQuery( $query );
  67          $published = $db->loadObjectList( 'menutype' );
  68  
  69          // Query to get unpublished menu item counts
  70          $query = 'SELECT a.menutype, COUNT( a.menutype ) AS num' .
  71                  ' FROM #__menu AS a' .
  72                  ' WHERE a.published = 0' .
  73                  ' GROUP BY a.menutype';
  74          $db->setQuery( $query );
  75          $unpublished = $db->loadObjectList( 'menutype' );
  76  
  77          // Query to get trash menu item counts
  78          $query = 'SELECT a.menutype, COUNT( a.menutype ) AS num' .
  79                  ' FROM #__menu AS a' .
  80                  ' WHERE a.published = -2' .
  81                  ' GROUP BY a.menutype';
  82          $db->setQuery( $query );
  83          $trash = $db->loadObjectList( 'menutype' );
  84  
  85          $limit        = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
  86          $limitstart = $mainframe->getUserStateFromRequest( 'com_menus.limitstart', 'limitstart', 0, 'int' );
  87  
  88          $query = 'SELECT a.*, SUM(b.home) AS home' .
  89                  ' FROM #__menu_types AS a' .
  90                  ' LEFT JOIN #__menu AS b ON b.menutype = a.menutype' .
  91                  ' GROUP BY a.id';
  92          $db->setQuery( $query, $limitstart, $limit );
  93          $menuTypes    = $db->loadObjectList();
  94  
  95          $total        = count( $menuTypes );
  96          $i            = 0;
  97          for ($i = 0;  $i < $total; $i++) {
  98              $row = &$menuTypes[$i];
  99  
 100              // query to get number of modules for menutype
 101              $query = 'SELECT count( id )' .
 102                      ' FROM #__modules' .
 103                      ' WHERE module = "mod_mainmenu"' .
 104                      ' AND params LIKE '.$db->Quote('%menutype='.$row->menutype.'%');
 105              $db->setQuery( $query );
 106              $modules = $db->loadResult();
 107  
 108              if ( !$modules ) {
 109                  $modules = '-';
 110              }
 111              $row->modules        = $modules;
 112              $row->published        = @$published[$row->menutype]->num ? $published[$row->menutype]->num : '-' ;
 113              $row->unpublished    = @$unpublished[$row->menutype]->num ? $unpublished[$row->menutype]->num : '-';
 114              $row->trash            = @$trash[$row->menutype]->num ? $trash[$row->menutype]->num : '-';
 115              $menus[] = $row;
 116          }
 117          return $menus;
 118      }
 119  
 120      /**
 121       * Get a list of the menu records associated with the type
 122       *
 123       * @param string The menu type
 124       * @return array An array of records as objects
 125       */
 126  	function getPagination()
 127      {
 128          global $mainframe;
 129  
 130          $menutypes     = MenusHelper::getMenuTypeList();
 131          $total        = count( $menutypes );
 132          $limit        = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
 133          $limitstart = $mainframe->getUserStateFromRequest( 'com_menus.limitstart', 'limitstart', 0, 'int' );
 134  
 135          jimport('joomla.html.pagination');
 136          $pagination = new JPagination( $total, $limitstart, $limit );
 137          return $pagination;
 138      }
 139  
 140      /**
 141       * Get a list of the menu records associated with the type
 142       * @param string The menu type
 143       * @return array An array of records as objects
 144       */
 145  	function getMenuItems()
 146      {
 147          $table = & $this->getTable();
 148          if ($table->menutype == '') {
 149              $table->menutype = JRequest::getVar('menutype', '', '', 'menutype');
 150          }
 151  
 152          $db = &$this->getDBO();
 153          $query = 'SELECT a.name, a.id' .
 154                  ' FROM #__menu AS a' .
 155                  ' WHERE a.menutype = ' . $db->Quote( $table->menutype ) .
 156                  ' ORDER BY a.name';
 157          $db->setQuery( $query );
 158          $result = $db->loadObjectList();
 159          return $result;
 160      }
 161  
 162      /**
 163       * Get a list of the menu records associated with the type
 164       * @param string The menu type
 165       * @return array An array of records as objects
 166       */
 167  	function getModules( $type='' )
 168      {
 169          if ($type == '') {
 170              $type = $this->_table->menutype;
 171          }
 172  
 173          $db = &$this->getDBO();
 174          $query = 'SELECT id, title, params' .
 175                  ' FROM #__modules' .
 176                  ' WHERE module = "mod_mainmenu"' .
 177                  ' AND params LIKE ' . $db->Quote( '%menutype=' . $type . '%' );
 178          $db->setQuery( $query );
 179          $temp = $db->loadObjectList();
 180  
 181          $result = array();
 182          $n = count( $temp );
 183          for ($i = 0; $i < $n; $i++)
 184          {
 185              $params = new JParameter( $temp[$i]->params );
 186              if ($params->get( 'menutype' ) == $type) {
 187                   $result[] = $temp[$i];
 188              }
 189          }
 190          return $result;
 191      }
 192  
 193      /**
 194       * Checks if the menu can be deleted
 195       * @param string The menu type
 196       * @return boolean
 197       */
 198  	function canDelete( $type='' )
 199      {
 200          if ($type == '') {
 201              $type = $this->_table->menutype;
 202          }
 203          if ($type == 'mainmenu') {
 204              $this->setError( JText::_( 'WARNDELMAINMENU' ) );
 205              return false;
 206          }
 207          return true;
 208      }
 209  
 210      /**
 211       * Deletes menu type and associations
 212       * @param string The id of the menu type
 213       * @return boolean
 214       */
 215  	function delete( $id = 0 )
 216      {
 217          $table = &$this->getTable();
 218          if ($id != 0) {
 219              $table->load( $id );
 220          }
 221  
 222          $db = &$this->getDBO();
 223  
 224          // Delete Associations
 225          if (!$this->deleteByType( $table->menutype )) {
 226              $this->setError( $this->getError() );
 227              return false;
 228          }
 229  
 230          // TODO: Should invoke JModuleModel::delete to delete the actual module
 231          $moduleTable= &JTable::getInstance( 'module');
 232          $items        = &$this->getModules( $table->menutype );
 233          $modulesIds    = array();
 234          foreach ($items as $item)
 235          {
 236              if (!$moduleTable->delete( $item->id )) {
 237                  $this->setError( $moduleTable->getErrorMsg() );
 238                  return false;
 239              }
 240              $modulesIds[] = (int) $item->id;
 241          }
 242  
 243          if (count( $modulesIds )) {
 244              $query = 'DELETE FROM #__modules_menu' .
 245                      ' WHERE menuid = '.implode( ' OR moduleid = ', $modulesIds );
 246              $db->setQuery( $query );
 247              if (!$db->query()) {
 248                  $this->setError( $menuTable->getErrorMsg() );
 249                  return false;
 250              }
 251          }
 252  
 253          $result = $table->delete();
 254  
 255          return $result;
 256      }
 257  
 258      /**
 259       * Delete menu items by type
 260       */
 261  	function deleteByType( $type = '' )
 262      {
 263          if (!$type) {
 264              return false;
 265          }
 266          $db = &$this->getDBO();
 267          $query = 'DELETE FROM #__menu' .
 268                  ' WHERE menutype = '.$db->Quote( $type );
 269          $db->setQuery( $query );
 270          if (!$db->query()) {
 271              $this->setError( $menuTable->getErrorMsg() );
 272              return false;
 273          }
 274          
 275          // clean cache
 276          MenusHelper::cleanCache();
 277                  
 278          return true;
 279      }
 280  }


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