PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_menus/admin.menus.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 934 lines | 651 code | 165 blank | 118 comment | 98 complexity | e89c7b904fba4f43f02cd75773628273 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: admin.menus.php 5045 2006-09-14 13:49:01Z friesengeist $
  4. * @package Joomla
  5. * @subpackage Menus
  6. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html 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. // no direct access
  15. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  16. require_once( $mainframe->getPath( 'admin_html' ) );
  17. $path = $mosConfig_absolute_path .'/administrator/components/com_menus/';
  18. $menutype = stripslashes( strval( mosGetParam( $_REQUEST, 'menutype', 'mainmenu' ) ) );
  19. $type = stripslashes( strval( mosGetParam( $_REQUEST, 'type', false ) ) );
  20. $menu = stripslashes( strval( mosGetParam( $_POST, 'menu', '' ) ) );
  21. $cid = josGetArrayInts( 'cid' );
  22. switch ($task) {
  23. case 'new':
  24. addMenuItem( $cid, $menutype, $option, $task );
  25. break;
  26. case 'edit':
  27. $cid[0] = ( $id ? $id : intval( $cid[0] ) );
  28. $menu = new mosMenu( $database );
  29. if ( $cid[0] ) {
  30. $menu->load( $cid[0] );
  31. }
  32. $menu->type = $type ? $type : $menu->type;
  33. if ( $menu->type ) {
  34. $type = $menu->type;
  35. require_once( $path . $menu->type .'/'. $menu->type .'.menu.php' );
  36. }
  37. break;
  38. case 'save':
  39. case 'apply':
  40. // clean any existing cache files
  41. mosCache::cleanCache( 'com_content' );
  42. require_once( $path . $type .'/'. $type .'.menu.php' );
  43. break;
  44. case 'publish':
  45. case 'unpublish':
  46. publishMenuSection( $cid, ($task == 'publish'), $menutype );
  47. break;
  48. case 'default':
  49. defaultMenu( $cid, $menutype );
  50. break;
  51. case 'remove':
  52. TrashMenusection( $cid, $menutype );
  53. break;
  54. case 'cancel':
  55. cancelMenu( $option );
  56. break;
  57. case 'orderup':
  58. orderMenu( intval( $cid[0] ), -1, $option );
  59. break;
  60. case 'orderdown':
  61. orderMenu( intval( $cid[0] ), 1, $option );
  62. break;
  63. case 'accesspublic':
  64. accessMenu( intval( $cid[0] ), 0, $option, $menutype );
  65. break;
  66. case 'accessregistered':
  67. accessMenu( intval( $cid[0] ), 1, $option, $menutype );
  68. break;
  69. case 'accessspecial':
  70. accessMenu( intval( $cid[0] ), 2, $option, $menutype );
  71. break;
  72. case 'movemenu':
  73. moveMenu( $option, $cid, $menutype );
  74. break;
  75. case 'movemenusave':
  76. moveMenuSave( $option, $cid, $menu, $menutype );
  77. break;
  78. case 'copymenu':
  79. copyMenu( $option, $cid, $menutype );
  80. break;
  81. case 'copymenusave':
  82. copyMenuSave( $option, $cid, $menu, $menutype );
  83. break;
  84. case 'cancelcopymenu':
  85. case 'cancelmovemenu':
  86. viewMenuItems( $menutype, $option );
  87. break;
  88. case 'saveorder':
  89. saveOrder( $cid, $menutype );
  90. break;
  91. default:
  92. $type = stripslashes( strval( mosGetParam( $_REQUEST, 'type' ) ) );
  93. if ($type) {
  94. // adding a new item - type selection form
  95. require_once( $path . $type .'/'. $type .'.menu.php' );
  96. } else {
  97. viewMenuItems( $menutype, $option );
  98. }
  99. break;
  100. }
  101. /**
  102. * Shows a list of items for a menu
  103. */
  104. function viewMenuItems( $menutype, $option ) {
  105. global $database, $mainframe, $mosConfig_list_limit;
  106. $limit = intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
  107. $limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart$menutype", 'limitstart', 0 ) );
  108. $levellimit = intval( $mainframe->getUserStateFromRequest( "view{$option}limit$menutype", 'levellimit', 10 ) );
  109. $search = $mainframe->getUserStateFromRequest( "search{$option}$menutype", 'search', '' );
  110. if (get_magic_quotes_gpc()) {
  111. $search = stripslashes( $search );
  112. }
  113. // select the records
  114. // note, since this is a tree we have to do the limits code-side
  115. if ($search) {
  116. $query = "SELECT m.id"
  117. . "\n FROM #__menu AS m"
  118. //. "\n LEFT JOIN #__content AS c ON c.id = m.componentid AND type='content_typed'"
  119. . "\n WHERE menutype = " . $database->Quote( $menutype )
  120. . "\n AND LOWER( m.name ) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'"
  121. ;
  122. $database->setQuery( $query );
  123. $search_rows = $database->loadResultArray();
  124. }
  125. $query = "SELECT m.*, u.name AS editor, g.name AS groupname, c.publish_up, c.publish_down, com.name AS com_name"
  126. . "\n FROM #__menu AS m"
  127. . "\n LEFT JOIN #__users AS u ON u.id = m.checked_out"
  128. . "\n LEFT JOIN #__groups AS g ON g.id = m.access"
  129. . "\n LEFT JOIN #__content AS c ON c.id = m.componentid AND m.type = 'content_typed'"
  130. . "\n LEFT JOIN #__components AS com ON com.id = m.componentid AND m.type = 'components'"
  131. . "\n WHERE m.menutype = " . $database->Quote( $menutype )
  132. . "\n AND m.published != -2"
  133. . "\n ORDER BY parent, ordering"
  134. ;
  135. $database->setQuery( $query );
  136. $rows = $database->loadObjectList();
  137. // establish the hierarchy of the menu
  138. $children = array();
  139. // first pass - collect children
  140. foreach ($rows as $v ) {
  141. $pt = $v->parent;
  142. $list = @$children[$pt] ? $children[$pt] : array();
  143. array_push( $list, $v );
  144. $children[$pt] = $list;
  145. }
  146. // second pass - get an indent list of the items
  147. $list = mosTreeRecurse( 0, '', array(), $children, max( 0, $levellimit-1 ) );
  148. // eventually only pick out the searched items.
  149. if ($search) {
  150. $list1 = array();
  151. foreach ($search_rows as $sid ) {
  152. foreach ($list as $item) {
  153. if ($item->id == $sid) {
  154. $list1[] = $item;
  155. }
  156. }
  157. }
  158. // replace full list with found items
  159. $list = $list1;
  160. }
  161. $total = count( $list );
  162. require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
  163. $pageNav = new mosPageNav( $total, $limitstart, $limit );
  164. $levellist = mosHTML::integerSelectList( 1, 20, 1, 'levellimit', 'size="1" onchange="document.adminForm.submit();"', $levellimit );
  165. // slice out elements based on limits
  166. $list = array_slice( $list, $pageNav->limitstart, $pageNav->limit );
  167. $i = 0;
  168. foreach ( $list as $mitem ) {
  169. $edit = '';
  170. switch ( $mitem->type ) {
  171. case 'separator':
  172. case 'component_item_link':
  173. break;
  174. case 'url':
  175. if ( eregi( 'index.php\?', $mitem->link ) ) {
  176. if ( !eregi( 'Itemid=', $mitem->link ) ) {
  177. $mitem->link .= '&Itemid='. $mitem->id;
  178. }
  179. }
  180. break;
  181. case 'newsfeed_link':
  182. $edit = 'index2.php?option=com_newsfeeds&task=edit&hidemainmenu=1A&id=' . $mitem->componentid;
  183. $list[$i]->descrip = 'Edit this Newsfeed';
  184. $mitem->link .= '&Itemid='. $mitem->id;
  185. break;
  186. case 'contact_item_link':
  187. $edit = 'index2.php?option=com_contact&task=editA&hidemainmenu=1&id=' . $mitem->componentid;
  188. $list[$i]->descrip = 'Edit this Contact';
  189. $mitem->link .= '&Itemid='. $mitem->id;
  190. break;
  191. case 'content_item_link':
  192. $edit = 'index2.php?option=com_content&task=edit&hidemainmenu=1&id=' . $mitem->componentid;
  193. $list[$i]->descrip = 'Edit this Content';
  194. break;
  195. case 'content_typed':
  196. $edit = 'index2.php?option=com_typedcontent&task=edit&hidemainmenu=1&id='. $mitem->componentid;
  197. $list[$i]->descrip = 'Edit this Static Content';
  198. break;
  199. default:
  200. $mitem->link .= '&Itemid='. $mitem->id;
  201. break;
  202. }
  203. $list[$i]->link = $mitem->link;
  204. $list[$i]->edit = $edit;
  205. $i++;
  206. }
  207. $i = 0;
  208. foreach ( $list as $row ) {
  209. // pulls name and description from menu type xml
  210. $row = ReadMenuXML( $row->type, $row->com_name );
  211. $list[$i]->type = $row[0];
  212. if (!isset($list[$i]->descrip)) $list[$i]->descrip = $row[1];
  213. $i++;
  214. }
  215. HTML_menusections::showMenusections( $list, $pageNav, $search, $levellist, $menutype, $option );
  216. }
  217. /**
  218. * Displays a selection list for menu item types
  219. */
  220. function addMenuItem( &$cid, $menutype, $option, $task ) {
  221. global $mosConfig_absolute_path, $database;
  222. josSpoofCheck(null, null, 'request');
  223. $types = array();
  224. // list of directories
  225. $dirs = mosReadDirectory( $mosConfig_absolute_path .'/administrator/components/com_menus' );
  226. // load files for menu types
  227. foreach ( $dirs as $dir ) {
  228. // needed within menu type .php files
  229. $type = $dir;
  230. $dir = $mosConfig_absolute_path .'/administrator/components/com_menus/'. $dir;
  231. if ( is_dir( $dir ) && $type!= 'components' ) {
  232. $files = mosReadDirectory( $dir, ".\.menu\.php$" );
  233. foreach ($files as $file) {
  234. require_once( "$dir/$file" );
  235. // type of menu type
  236. $types[]->type = $type;
  237. }
  238. }
  239. }
  240. $i = 0;
  241. foreach ( $types as $type ) {
  242. // pulls name and description from menu type xml
  243. $row = ReadMenuXML( $type->type );
  244. $types[$i]->name = $row[0];
  245. $types[$i]->descrip = $row[1];
  246. $types[$i]->group = $row[2];
  247. $i++;
  248. }
  249. $database->setQuery("select name, 'components' as type, 'Link to a component' as descript, 'Internal' as `group`, id from #__components
  250. WHERE parent = 0 AND link != '' AND `option` NOT IN ('com_contact','com_newsfeeds','com_weblinks') order by `name`");
  251. $components = $database->loadObjectList('name');
  252. $types = array_merge($types, $components);
  253. // sort array of objects alphabetically by name of menu type
  254. SortArrayObjects( $types, 'name', 1 );
  255. $menu = array();
  256. $i = 0;
  257. foreach ( $types as $type ) {
  258. $groups = explode('|', $type->group);
  259. switch(count($groups)){
  260. case 1:
  261. if(!key_exists($groups[0], $menu)) $menu[$groups[0]] = array();
  262. $menu[$groups[0]][] = $types[$i];
  263. break;
  264. case 2:
  265. if(!key_exists($groups[0], $menu)) $menu[$groups[0]] = array();
  266. if(!key_exists($groups[1], $menu[$groups[0]])) $menu[$groups[0]][$groups[1]] = array();
  267. $menu[$groups[0]][$groups[1]][] = $types[$i];
  268. break;
  269. case 3:
  270. if(!key_exists($groups[0], $menu)) $menu[$groups[0]] = array();
  271. if(!key_exists($groups[1], $menu[$groups[0]])) $menu[$groups[0]][$groups[1]] = array();
  272. if(!key_exists($groups[2], $menu[$groups[0]][$groups[1]])) $menu[$groups[0]][$groups[1]][$groups[2]] = array();
  273. $menu[$groups[0]][$groups[1]][$groups[2]][] = $types[$i];
  274. break;
  275. }
  276. $i++;
  277. }
  278. // split into Content
  279. $i = 0;
  280. foreach ( $types as $type ) {
  281. if ( strstr( $type->group, 'Content' ) ) {
  282. $types_content[] = $types[$i];
  283. }
  284. $i++;
  285. }
  286. // split into Links
  287. $i = 0;
  288. foreach ( $types as $type ) {
  289. if ( strstr( $type->group, 'Link' ) ) {
  290. $types_link[] = $types[$i];
  291. }
  292. $i++;
  293. }
  294. // split into Component
  295. $i = 0;
  296. foreach ( $types as $type ) {
  297. if ( strstr( $type->group, 'Component' ) ) {
  298. $types_component[] = $types[$i];
  299. }
  300. $i++;
  301. }
  302. // split into Other
  303. $i = 0;
  304. foreach ( $types as $type ) {
  305. if ( strstr( $type->group, 'Other' ) || !$type->group ) {
  306. $types_other[] = $types[$i];
  307. }
  308. $i++;
  309. }
  310. // split into Submit
  311. $i = 0;
  312. foreach ( $types as $type ) {
  313. if ( strstr( $type->group, 'Submit' ) || !$type->group ) {
  314. $types_submit[] = $types[$i];
  315. }
  316. $i++;
  317. }
  318. HTML_menusections::addMenuItem( $cid, $menutype, $option, $types_content, $types_component, $types_link, $types_other, $types_submit, $menu );
  319. }
  320. /**
  321. * Generic function to save the menu
  322. */
  323. function saveMenu( $option, $task='save' ) {
  324. global $database;
  325. josSpoofCheck();
  326. $params = mosGetParam( $_POST, 'params', '' );
  327. if (is_array( $params )) {
  328. $txt = array();
  329. foreach ($params as $k=>$v) {
  330. $txt[] = "$k=$v";
  331. }
  332. $_POST['params'] = mosParameters::textareaHandling( $txt );
  333. }
  334. $row = new mosMenu( $database );
  335. if (!$row->bind( $_POST )) {
  336. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  337. exit();
  338. }
  339. $row->name = ampReplace( $row->name );
  340. if (!$row->check()) {
  341. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  342. exit();
  343. }
  344. if (!$row->store()) {
  345. echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
  346. exit();
  347. }
  348. $row->checkin();
  349. $row->updateOrder( 'menutype = ' . $database->Quote( $row->menutype ) . ' AND parent = ' . (int) $row->parent );
  350. $msg = 'Menu item Saved';
  351. switch ( $task ) {
  352. case 'apply':
  353. mosRedirect( 'index2.php?option='. $option .'&menutype='. $row->menutype .'&task=edit&id='. $row->id . '&hidemainmenu=1' , $msg );
  354. break;
  355. case 'save':
  356. default:
  357. mosRedirect( 'index2.php?option='. $option .'&menutype='. $row->menutype, $msg );
  358. break;
  359. }
  360. }
  361. /**
  362. * Publishes or Unpublishes one or more menu sections
  363. * @param database A database connector object
  364. * @param string The name of the category section
  365. * @param array An array of id numbers
  366. * @param integer 0 if unpublishing, 1 if publishing
  367. */
  368. function publishMenuSection( $cid=null, $publish=1, $menutype ) {
  369. global $database, $mosConfig_absolute_path;
  370. if (!is_array( $cid ) || count( $cid ) < 1) {
  371. return 'Select an item to ' . ($publish ? 'publish' : 'unpublish');
  372. }
  373. $menu = new mosMenu( $database );
  374. foreach ($cid as $id) {
  375. $menu->load( $id );
  376. $menu->published = $publish;
  377. if (!$menu->check()) {
  378. return $menu->getError();
  379. }
  380. if (!$menu->store()) {
  381. return $menu->getError();
  382. }
  383. if ($menu->type) {
  384. $database = &$database;
  385. $task = $publish ? 'publish' : 'unpublish';
  386. // $type value is used in *.menu.php
  387. $type = $menu->type;
  388. require_once( $mosConfig_absolute_path . '/administrator/components/com_menus/' . $type . '/' . $type . '.menu.php' );
  389. }
  390. }
  391. // clean any existing cache files
  392. mosCache::cleanCache( 'com_content' );
  393. mosRedirect( 'index2.php?option=com_menus&menutype='. $menutype );
  394. }
  395. /**
  396. * Sets a menu link to default
  397. * @param database A database connector object
  398. * @param array An array of id numbers
  399. */
  400. function defaultMenu( $cid=null, $menutype = null) {
  401. global $database, $mosConfig_absolute_path;
  402. if (!is_array( $cid ) || count( $cid ) < 1) {
  403. return 'Select an item to set as default';
  404. }
  405. //Set all menu items to not be default
  406. $database->setQuery("UPDATE #__menu set `default` = 0");
  407. $database->query();
  408. $menu = new mosMenu( $database );
  409. $menu->load( $cid[0] );
  410. $menu->default = 1;
  411. if (!$menu->check()) {
  412. return $menu->getError();
  413. }
  414. if (!$menu->store()) {
  415. return $menu->getError();
  416. }
  417. mosRedirect( 'index2.php?option=com_menus&menutype='. $menutype );
  418. }
  419. /**
  420. * Trashes a menu record
  421. */
  422. function TrashMenuSection( $cid=NULL, $menutype='mainmenu' ) {
  423. global $database;
  424. $nullDate = $database->getNullDate();
  425. $state = -2;
  426. $query = "SELECT *"
  427. . "\n FROM #__menu"
  428. . "\n WHERE menutype = " . $database->Quote( $menutype )
  429. . "\n AND published != " . (int) $state
  430. . "\n ORDER BY menutype, parent, ordering"
  431. ;
  432. $database->setQuery( $query );
  433. $mitems = $database->loadObjectList();
  434. // determine if selected item has an child items
  435. $children = array();
  436. foreach ( $cid as $id ) {
  437. foreach ( $mitems as $item ) {
  438. if ( $item->parent == $id ) {
  439. $children[] = $item->id;
  440. }
  441. }
  442. }
  443. $list = josMenuChildrenRecurse( $mitems, $children, $children );
  444. $list = array_merge( $cid, $list );
  445. mosArrayToInts( $list );
  446. $ids = 'id=' . implode( ' OR id=', $list );
  447. $query = "UPDATE #__menu"
  448. . "\n SET published = " . (int) $state . ", ordering = 0, checked_out = 0, checked_out_time = " . $database->Quote( $nullDate )
  449. . "\n WHERE ( $ids )"
  450. ;
  451. $database->setQuery( $query );
  452. if ( !$database->query() ) {
  453. echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
  454. exit();
  455. }
  456. $total = count( $cid );
  457. // clean any existing cache files
  458. mosCache::cleanCache( 'com_content' );
  459. $msg = $total .' Item(s) sent to the Trash';
  460. mosRedirect( 'index2.php?option=com_menus&menutype='. $menutype, $msg );
  461. }
  462. /**
  463. * Cancels an edit operation
  464. */
  465. function cancelMenu( $option ) {
  466. global $database;
  467. josSpoofCheck();
  468. $menu = new mosMenu( $database );
  469. $menu->bind( $_POST );
  470. $menuid = intval( mosGetParam( $_POST, 'menuid', 0 ) );
  471. if ( $menuid ) {
  472. $menu->id = $menuid;
  473. }
  474. $menu->checkin();
  475. mosRedirect( 'index2.php?option='. $option .'&menutype='. $menu->menutype );
  476. }
  477. /**
  478. * Moves the order of a record
  479. * @param integer The increment to reorder by
  480. */
  481. function orderMenu( $uid, $inc, $option ) {
  482. global $database;
  483. $row = new mosMenu( $database );
  484. $row->load( $uid );
  485. $row->move( $inc, "menutype = " . $database->Quote( $row->menutype ) . " AND parent = " . (int) $row->parent );
  486. // clean any existing cache files
  487. mosCache::cleanCache( 'com_content' );
  488. mosRedirect( 'index2.php?option='. $option .'&menutype='. $row->menutype );
  489. }
  490. /**
  491. * changes the access level of a record
  492. * @param integer The increment to reorder by
  493. */
  494. function accessMenu( $uid, $access, $option, $menutype ) {
  495. global $database;
  496. $menu = new mosMenu( $database );
  497. $menu->load( $uid );
  498. $menu->access = $access;
  499. if (!$menu->check()) {
  500. return $menu->getError();
  501. }
  502. if (!$menu->store()) {
  503. return $menu->getError();
  504. }
  505. // clean any existing cache files
  506. mosCache::cleanCache( 'com_content' );
  507. mosRedirect( 'index2.php?option='. $option .'&menutype='. $menutype );
  508. }
  509. /**
  510. * Form for moving item(s) to a specific menu
  511. */
  512. function moveMenu( $option, $cid, $menutype ) {
  513. global $database;
  514. if (!is_array( $cid ) || count( $cid ) < 1) {
  515. echo "<script> alert('Select an item to move'); window.history.go(-1);</script>\n";
  516. exit;
  517. }
  518. ## query to list selected menu items
  519. mosArrayToInts( $cid );
  520. $cids = 'a.id=' . implode( ' OR a.id=', $cid );
  521. $query = "SELECT a.name"
  522. . "\n FROM #__menu AS a"
  523. . "\n WHERE ( $cids )"
  524. ;
  525. $database->setQuery( $query );
  526. $items = $database->loadObjectList();
  527. ## query to choose menu
  528. $query = "SELECT a.params"
  529. . "\n FROM #__modules AS a"
  530. . "\n WHERE a.module = 'mod_mainmenu'"
  531. . "\n ORDER BY a.title"
  532. ;
  533. $database->setQuery( $query );
  534. $modules = $database->loadObjectList();
  535. foreach ( $modules as $module) {
  536. $params = mosParseParams( $module->params );
  537. // adds menutype to array
  538. $type = trim( @$params->menutype );
  539. $menu[] = mosHTML::makeOption( $type, $type );
  540. }
  541. // build the html select list
  542. $MenuList = mosHTML::selectList( $menu, 'menu', 'class="inputbox" size="10"', 'value', 'text', null );
  543. HTML_menusections::moveMenu( $option, $cid, $MenuList, $items, $menutype );
  544. }
  545. /**
  546. * Add all descendants to list of meni id's
  547. */
  548. function addDescendants($id, &$cid) {
  549. global $database;
  550. $query = "SELECT id"
  551. . "\n FROM #__menu"
  552. . "\n WHERE parent = " . (int) $id
  553. ;
  554. $database->setQuery( $query );
  555. $rows = $database->loadObjectList();
  556. if ($database->getErrorNum()) {
  557. echo "<script> alert('". $database->getErrorMsg() ."'); window.history.go(-1); </script>\n";
  558. exit();
  559. } // if
  560. foreach ($rows as $row) {
  561. $found = false;
  562. foreach ($cid as $idx)
  563. if ($idx == $row->id) {
  564. $found = true;
  565. break;
  566. } // if
  567. if (!$found) $cid[] = $row->id;
  568. addDescendants($row->id, $cid);
  569. } // foreach
  570. } // addDescendants
  571. /**
  572. * Save the item(s) to the menu selected
  573. */
  574. function moveMenuSave( $option, $cid, $menu, $menutype ) {
  575. global $database;
  576. // add all decendants to the list
  577. foreach ($cid as $id) addDescendants($id, $cid);
  578. $row = new mosMenu( $database );
  579. $ordering = 1000000;
  580. $firstroot = 0;
  581. foreach ($cid as $id) {
  582. $row->load( $id );
  583. // is it moved together with his parent?
  584. $found = false;
  585. if ($row->parent != 0)
  586. foreach ($cid as $idx)
  587. if ($idx == $row->parent) {
  588. $found = true;
  589. break;
  590. } // if
  591. if (!$found) {
  592. $row->parent = 0;
  593. $row->ordering = $ordering++;
  594. if (!$firstroot) $firstroot = $row->id;
  595. } // if
  596. $row->menutype = $menu;
  597. if ( !$row->store() ) {
  598. echo "<script> alert('". $database->getErrorMsg() ."'); window.history.go(-1); </script>\n";
  599. exit();
  600. } // if
  601. } // foreach
  602. if ($firstroot) {
  603. $row->load( $firstroot );
  604. $row->updateOrder( 'menutype = ' . $database->Quote( $row->menutype ) . ' AND parent = ' . (int) $row->parent );
  605. } // if
  606. // clean any existing cache files
  607. mosCache::cleanCache( 'com_content' );
  608. $msg = count($cid) .' Menu Items moved to '. $menu;
  609. mosRedirect( 'index2.php?option='. $option .'&menutype='. $menutype .'&mosmsg='. $msg );
  610. } // moveMenuSave
  611. /**
  612. * Form for copying item(s) to a specific menu
  613. */
  614. function copyMenu( $option, $cid, $menutype ) {
  615. global $database;
  616. if (!is_array( $cid ) || count( $cid ) < 1) {
  617. echo "<script> alert('Select an item to move'); window.history.go(-1);</script>\n";
  618. exit;
  619. }
  620. ## query to list selected menu items
  621. mosArrayToInts( $cid );
  622. $cids = 'a.id=' . implode( ' OR a.id=', $cid );
  623. $query = "SELECT a.name"
  624. . "\n FROM #__menu AS a"
  625. . "\n WHERE ( $cids )"
  626. ;
  627. $database->setQuery( $query );
  628. $items = $database->loadObjectList();
  629. $menuTypes = mosAdminMenus::menutypes();
  630. foreach ( $menuTypes as $menuType ) {
  631. $menu[] = mosHTML::makeOption( $menuType, $menuType );
  632. }
  633. // build the html select list
  634. $MenuList = mosHTML::selectList( $menu, 'menu', 'class="inputbox" size="10"', 'value', 'text', null );
  635. HTML_menusections::copyMenu( $option, $cid, $MenuList, $items, $menutype );
  636. }
  637. /**
  638. * Save the item(s) to the menu selected
  639. */
  640. function copyMenuSave( $option, $cid, $menu, $menutype ) {
  641. global $database;
  642. $curr = new mosMenu( $database );
  643. $cidref = array();
  644. foreach( $cid as $id ) {
  645. $curr->load( $id );
  646. $curr->id = NULL;
  647. if ( !$curr->store() ) {
  648. mosErrorAlert( $curr->getError() );
  649. exit();
  650. }
  651. $cidref[] = array($id, $curr->id);
  652. }
  653. foreach ( $cidref as $ref ) {
  654. $curr->load( $ref[1] );
  655. if ($curr->parent!=0) {
  656. $found = false;
  657. foreach ( $cidref as $ref2 )
  658. if ($curr->parent == $ref2[0]) {
  659. $curr->parent = $ref2[1];
  660. $found = true;
  661. break;
  662. } // if
  663. if (!$found && $curr->menutype!=$menu)
  664. $curr->parent = 0;
  665. } // if
  666. $curr->menutype = $menu;
  667. $curr->ordering = '9999';
  668. if ( !$curr->store() ) {
  669. mosErrorAlert( $curr->getError() );
  670. exit();
  671. }
  672. $curr->updateOrder( 'menutype = ' . $database->Quote( $curr->menutype ) . ' AND parent = ' . (int) $curr->parent );
  673. } // foreach
  674. // clean any existing cache files
  675. mosCache::cleanCache( 'com_content' );
  676. $msg = count( $cid ) .' Menu Items Copied to '. $menu;
  677. mosRedirect( 'index2.php?option='. $option .'&menutype='. $menutype .'&mosmsg='. $msg );
  678. }
  679. function ReadMenuXML( $type, $component=-1 ) {
  680. global $mosConfig_absolute_path;
  681. // XML library
  682. require_once( $mosConfig_absolute_path . '/includes/domit/xml_domit_lite_include.php' );
  683. // xml file for module
  684. $xmlfile = $mosConfig_absolute_path .'/administrator/components/com_menus/'. $type .'/'. $type .'.xml';
  685. $xmlDoc = new DOMIT_Lite_Document();
  686. $xmlDoc->resolveErrors( true );
  687. if ($xmlDoc->loadXML( $xmlfile, false, true )) {
  688. $root = &$xmlDoc->documentElement;
  689. if ( $root->getTagName() == 'mosinstall' && ( $root->getAttribute( 'type' ) == 'component' || $root->getAttribute( 'type' ) == 'menu' ) ) {
  690. // Menu Type Name
  691. $element = &$root->getElementsByPath( 'name', 1 );
  692. $name = $element ? trim( $element->getText() ) : '';
  693. // Menu Type Description
  694. $element = &$root->getElementsByPath( 'description', 1 );
  695. $descrip = $element ? trim( $element->getText() ) : '';
  696. // Menu Type Group
  697. $element = &$root->getElementsByPath( 'group', 1 );
  698. $group = $element ? trim( $element->getText() ) : '';
  699. }
  700. }
  701. if ( ( $component != -1 ) && ( $name == 'Component') ) {
  702. $name .= ' - '. $component;
  703. }
  704. $row[0] = $name;
  705. $row[1] = $descrip;
  706. $row[2] = $group;
  707. return $row;
  708. }
  709. function saveOrder( &$cid, $menutype ) {
  710. global $database;
  711. josSpoofCheck();
  712. $total = count( $cid );
  713. $order = josGetArrayInts( 'order' );
  714. $row = new mosMenu( $database );
  715. $conditions = array();
  716. // update ordering values
  717. for( $i=0; $i < $total; $i++ ) {
  718. $row->load( (int) $cid[$i] );
  719. if ($row->ordering != $order[$i]) {
  720. $row->ordering = $order[$i];
  721. if (!$row->store()) {
  722. echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
  723. exit();
  724. }
  725. // remember to updateOrder this group
  726. $condition = "menutype = " . $database->Quote( $menutype ) . " AND parent = " . (int) $row->parent . " AND published >= 0";
  727. $found = false;
  728. foreach ( $conditions as $cond )
  729. if ($cond[1]==$condition) {
  730. $found = true;
  731. break;
  732. }
  733. if (!$found) $conditions[] = array($row->id, $condition);
  734. }
  735. }
  736. // execute updateOrder for each group
  737. foreach ( $conditions as $cond ) {
  738. $row->load( $cond[0] );
  739. $row->updateOrder( $cond[1] );
  740. }
  741. // clean any existing cache files
  742. mosCache::cleanCache( 'com_content' );
  743. $msg = 'New ordering saved';
  744. mosRedirect( 'index2.php?option=com_menus&menutype='. $menutype, $msg );
  745. }
  746. /**
  747. * Returns list of child items for a given set of ids from menu items supplied
  748. *
  749. */
  750. function josMenuChildrenRecurse( $mitems, $parents, $list, $maxlevel=20, $level=0 ) {
  751. // check to reduce recursive processing
  752. if ( $level <= $maxlevel && count( $parents ) ) {
  753. $children = array();
  754. foreach ( $parents as $id ) {
  755. foreach ( $mitems as $item ) {
  756. if ( $item->parent == $id ) {
  757. $children[] = $item->id;
  758. }
  759. }
  760. }
  761. // check to reduce recursive processing
  762. if ( count( $children ) ) {
  763. $list = josMenuChildrenRecurse( $mitems, $children, $list, $maxlevel, $level+1 );
  764. $list = array_merge( $list, $children );
  765. }
  766. }
  767. return $list;
  768. }
  769. ?>