/administrator/components/com_menus/models/menutype.php

https://github.com/MaBelleEcole/Main · PHP · 280 lines · 180 code · 35 blank · 65 comment · 24 complexity · 54ae825fdd8b2ea2e0bf7dbdda04ea99 MD5 · raw file

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