| [ Index ] |
PHP Cross Reference of Joomla 1.5.26 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: list.php 19343 2010-11-03 18:12:02Z 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 MenusModelList extends JModel 25 { 26 /** @var object JTable object */ 27 var $_table = null; 28 29 var $_pagination = null; 30 31 /** 32 * Returns the internal table object 33 * @return JTable 34 */ 35 function &getTable() 36 { 37 if ($this->_table == null) 38 { 39 $this->_table =& JTable::getInstance( 'menu'); 40 } 41 return $this->_table; 42 } 43 44 function &getItems() 45 { 46 global $mainframe; 47 48 static $items; 49 50 if (isset($items)) { 51 return $items; 52 } 53 54 $db =& $this->getDBO(); 55 56 $menutype = $mainframe->getUserStateFromRequest( 'com_menus.menutype', 'menutype', 'mainmenu', 'menutype' ); 57 $filter_order = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.filter_order', 'filter_order', 'm.ordering', 'cmd' ); 58 $filter_order_Dir = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.filter_order_Dir', 'filter_order_Dir', 'ASC', 'word' ); 59 $filter_state = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.filter_state', 'filter_state', '', 'word' ); 60 $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg( 'list_limit' ), 'int' ); 61 $limitstart = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.limitstart', 'limitstart', 0, 'int' ); 62 $levellimit = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.levellimit', 'levellimit', 10, 'int' ); 63 $search = $mainframe->getUserStateFromRequest( 'com_menus.'.$menutype.'.search', 'search', '', 'string' ); 64 if (strpos($search, '"') !== false) { 65 $search = str_replace(array('=', '<'), '', $search); 66 } 67 $search = JString::strtolower($search); 68 69 $and = ''; 70 if ( $filter_state ) 71 { 72 if ( $filter_state == 'P' ) { 73 $and = ' AND m.published = 1'; 74 } else if ($filter_state == 'U' ) { 75 $and = ' AND m.published = 0'; 76 } 77 } 78 79 // ensure $filter_order has a good value 80 if (!in_array($filter_order, array('m.name', 'm.published', 'm.ordering', 'groupname', 'm.type', 'm.id'))) { 81 $filter_order = 'm.ordering'; 82 } 83 84 if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC', ''))) { 85 $filter_order_Dir = 'ASC'; 86 } 87 88 // just in case filter_order get's messed up 89 if ($filter_order) { 90 $orderby = ' ORDER BY '.$filter_order .' '. $filter_order_Dir .', m.parent, m.ordering'; 91 } else { 92 $orderby = ' ORDER BY m.parent, m.ordering'; 93 } 94 95 // select the records 96 // note, since this is a tree we have to do the limits code-side 97 if ($search) { 98 $query = 'SELECT m.id' . 99 ' FROM #__menu AS m' . 100 ' WHERE menutype = '.$db->Quote($menutype) . 101 ' AND LOWER( m.name ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ) . 102 $and; 103 $db->setQuery( $query ); 104 $search_rows = $db->loadResultArray(); 105 } 106 107 $query = 'SELECT m.*, u.name AS editor, g.name AS groupname, c.publish_up, c.publish_down, com.name AS com_name' . 108 ' FROM #__menu AS m' . 109 ' LEFT JOIN #__users AS u ON u.id = m.checked_out' . 110 ' LEFT JOIN #__groups AS g ON g.id = m.access' . 111 ' LEFT JOIN #__content AS c ON c.id = m.componentid AND m.type = "content_typed"' . 112 ' LEFT JOIN #__components AS com ON com.id = m.componentid AND m.type = "component"' . 113 ' WHERE m.menutype = '.$db->Quote($menutype) . 114 ' AND m.published != -2' . 115 $and . 116 $orderby; 117 $db->setQuery( $query ); 118 $rows = $db->loadObjectList(); 119 120 // establish the hierarchy of the menu 121 $children = array(); 122 // first pass - collect children 123 foreach ($rows as $v ) 124 { 125 $pt = $v->parent; 126 $list = @$children[$pt] ? $children[$pt] : array(); 127 array_push( $list, $v ); 128 $children[$pt] = $list; 129 } 130 // second pass - get an indent list of the items 131 $list = JHTML::_('menu.treerecurse', 0, '', array(), $children, max( 0, $levellimit-1 ) ); 132 // eventually only pick out the searched items. 133 if ($search) { 134 $list1 = array(); 135 136 foreach ($search_rows as $sid ) 137 { 138 foreach ($list as $item) 139 { 140 if ($item->id == $sid) { 141 $list1[] = $item; 142 } 143 } 144 } 145 // replace full list with found items 146 $list = $list1; 147 } 148 149 $total = count( $list ); 150 151 jimport('joomla.html.pagination'); 152 $this->_pagination = new JPagination( $total, $limitstart, $limit ); 153 154 // slice out elements based on limits 155 $list = array_slice( $list, $this->_pagination->limitstart, $this->_pagination->limit ); 156 157 $i = 0; 158 $query = array(); 159 foreach ( $list as $mitem ) 160 { 161 $edit = ''; 162 switch ( $mitem->type ) 163 { 164 case 'separator': 165 $list[$i]->descrip = JText::_('Separator'); 166 break; 167 168 case 'url': 169 $list[$i]->descrip = JText::_('URL'); 170 break; 171 172 case 'menulink': 173 $list[$i]->descrip = JText::_('Menu Link'); 174 break; 175 176 case 'component': 177 $list[$i]->descrip = JText::_('Component'); 178 $query = parse_url($list[$i]->link); 179 $view = array(); 180 if(isset($query['query'])) { 181 if(strpos($query['query'], '&') !== false) 182 { 183 $query['query'] = str_replace('&','&',$query['query']); 184 } 185 parse_str($query['query'], $view); 186 } 187 $list[$i]->view = JText::_($list[$i]->com_name); 188 if (isset($view['view'])) 189 { 190 $list[$i]->view .= ' » '.JText::_(ucfirst($view['view'])); 191 } 192 if (isset($view['layout'])) 193 { 194 $list[$i]->view .= ' / '.JText::_(ucfirst($view['layout'])); 195 } 196 if (isset($view['task']) && !isset($view['view'])) 197 { 198 $list[$i]->view .= ' :: '.JText::_(ucfirst($view['task'])); 199 } 200 break; 201 202 default: 203 $list[$i]->descrip = JText::_('Unknown'); 204 break; 205 } 206 $i++; 207 } 208 209 $items = $list; 210 return $items; 211 } 212 213 function &getPagination() 214 { 215 if ($this->_pagination == null) { 216 $this->getItems(); 217 } 218 return $this->_pagination; 219 } 220 221 /** 222 * Form for copying item(s) to a specific menu 223 */ 224 function getItemsFromRequest() 225 { 226 static $items; 227 228 if (isset($items)) { 229 return $items; 230 } 231 232 $cid = JRequest::getVar( 'cid', array(), 'post', 'array' ); 233 JArrayHelper::toInteger($cid); 234 235 if (count($cid) < 1) { 236 $this->setError(JText::_( 'Select an item to move')); 237 return false; 238 } 239 240 // Query to list the selected menu items 241 $db =& $this->getDBO(); 242 $cids = implode( ',', $cid ); 243 $query = 'SELECT `id`, `name`' . 244 ' FROM `#__menu`' . 245 ' WHERE `id` IN ( '.$cids.' )'; 246 247 $db->setQuery( $query ); 248 $items = $db->loadObjectList(); 249 250 return $items; 251 } 252 253 /** 254 * Gets the componet table object related to this menu item 255 */ 256 function &getComponent() 257 { 258 $id = $this->_table->componentid; 259 $component = & JTable::getInstance( 'component'); 260 $component->load( $id ); 261 return $component; 262 } 263 264 /** 265 * Save the item(s) to the menu selected 266 */ 267 function copy( $items, $menu ) 268 { 269 $curr =& JTable::getInstance('menu'); 270 $itemref = array(); 271 foreach ($items as $id) 272 { 273 $curr->load( $id ); 274 $curr->id = NULL; 275 $curr->home = 0; 276 if ( !$curr->store() ) { 277 $this->setError($curr->getError()); 278 return false; 279 } 280 $itemref[] = array($id, $curr->id); 281 } 282 foreach ($itemref as $ref) 283 { 284 $curr->load( $ref[1] ); 285 if ($curr->parent!=0) { 286 $found = false; 287 foreach ( $itemref as $ref2 ) 288 { 289 if ($curr->parent == $ref2[0]) { 290 $curr->parent = $ref2[1]; 291 $found = true; 292 break; 293 } // if 294 } 295 if (!$found && $curr->menutype!=$menu) { 296 $curr->parent = 0; 297 } 298 } 299 $curr->menutype = $menu; 300 $curr->ordering = '9999'; 301 $curr->home = 0; 302 if ( !$curr->store() ) { 303 $this->setError($curr->getError()); 304 return false; 305 } 306 $curr->reorder( 'menutype = '.$this->_db->Quote($curr->menutype).' AND parent = '.(int) $curr->parent ); 307 } // foreach 308 309 //Now, we need to rebuild sublevels... 310 $this->_rebuildSubLevel(); 311 312 // clean cache 313 MenusHelper::cleanCache(); 314 315 return true; 316 } 317 318 function move($items, $menu) 319 { 320 // Add all children to the list 321 foreach ($items as $id) 322 { 323 $this->_addChildren($id, $items); 324 } 325 326 $row =& $this->getTable(); 327 $ordering = 1000000; 328 $firstroot = 0; 329 foreach ($items as $id) { 330 $row->load( $id ); 331 332 // is it moved together with his parent? 333 $found = false; 334 if ($row->parent != 0) { 335 foreach ($items as $idx) 336 { 337 if ($idx == $row->parent) { 338 $found = true; 339 break; 340 } // if 341 } 342 } 343 if (!$found) { 344 $row->parent = 0; 345 $row->ordering = $ordering++; 346 if (!$firstroot) $firstroot = $row->id; 347 } // if 348 349 $row->menutype = $menu; 350 if ( !$row->store() ) { 351 $this->setError($row->getError()); 352 return false; 353 } // if 354 } // foreach 355 356 if ($firstroot) { 357 $row->load( $firstroot ); 358 $row->reorder( 'menutype = '.$this->_db->Quote($row->menutype).' AND parent = '.(int) $row->parent ); 359 } // if 360 361 //Rebuild sublevel 362 $this->_rebuildSubLevel(); 363 364 // clean cache 365 MenusHelper::cleanCache(); 366 367 return true; 368 } 369 370 function toTrash($items) 371 { 372 $db =& $this->getDBO(); 373 $nd = $db->getNullDate(); 374 $state = -2; 375 $row =& $this->getTable(); 376 $default = 0; 377 378 // Add all children to the list 379 foreach ($items as $id) 380 { 381 //Check if it's the default item 382 $row->load( $id ); 383 if ($row->home != 1) { 384 $this->_addChildren($id, $items); 385 } else { 386 unset($items[$default]); 387 JError::raiseWarning( 'SOME_ERROR_CODE', JText::_('You cannot trash the default menu item')); 388 } 389 $default++; 390 } 391 if (!empty($items)) { 392 // Sent menu items to the trash 393 JArrayHelper::toInteger($items, array(0)); 394 $where = ' WHERE (id = ' . implode( ' OR id = ', $items ) . ') AND home = 0'; 395 $query = 'UPDATE #__menu' . 396 ' SET published = '.(int) $state.', parent = 0, ordering = 0, checked_out = 0, checked_out_time = '.$db->Quote($nd) . 397 $where; 398 $db->setQuery( $query ); 399 if (!$db->query()) { 400 $this->setError( $db->getErrorMsg() ); 401 return false; 402 } 403 } 404 405 // clean cache 406 MenusHelper::cleanCache(); 407 408 return count($items); 409 } 410 411 function fromTrash($items) 412 { 413 $db =& $this->getDBO(); 414 $nd = $db->getNullDate(); 415 $state = 0; 416 417 // Add all children to the list 418 foreach ($items as $id) 419 { 420 $this->_addChildren($id, $items); 421 } 422 423 // Sent menu items to the trash 424 JArrayHelper::toInteger($items, array(0)); 425 $where = ' WHERE id = ' . implode( ' OR id = ', $items ); 426 $query = 'UPDATE #__menu' . 427 ' SET published = '.(int) $state.', parent = 0, ordering = 99999, checked_out = 0, checked_out_time = '.$db->Quote($nd) . 428 $where; 429 $db->setQuery( $query ); 430 if (!$db->query()) { 431 $this->setError( $db->getErrorMsg() ); 432 return false; 433 } 434 435 // clean cache (require helper because method can be called from com_trash) 436 require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_menus'.DS.'helpers'.DS.'helper.php' ); 437 MenusHelper::cleanCache(); 438 439 return count($items); 440 } 441 442 /** 443 * Set the state of selected menu items 444 */ 445 function setHome( $item ) 446 { 447 $db =& $this->getDBO(); 448 449 // Clear home field for all other items 450 $query = 'UPDATE #__menu' . 451 ' SET home = 0' . 452 ' WHERE 1'; 453 $db->setQuery( $query ); 454 if ( !$db->query() ) { 455 $this->setError($db->getErrorMsg()); 456 return false; 457 } 458 459 // Set the given item to home 460 $query = 'UPDATE #__menu' . 461 ' SET home = 1' . 462 ' WHERE id = '.(int) $item; 463 $db->setQuery( $query ); 464 if ( !$db->query() ) { 465 $this->setError($db->getErrorMsg()); 466 return false; 467 } 468 469 // clean cache 470 MenusHelper::cleanCache(); 471 472 return true; 473 } 474 475 /** 476 * Set the state of selected menu items 477 */ 478 function setItemState( $items, $state ) 479 { 480 if(is_array($items)) 481 { 482 $row =& $this->getTable(); 483 foreach ($items as $id) 484 { 485 $row->load( $id ); 486 487 if ($row->home != 1) { 488 $row->published = $state; 489 490 if ($state != 1) { 491 // Set any alias menu types to not point to unpublished menu items 492 $db = &$this->getDBO(); 493 $query = 'UPDATE #__menu SET link = 0 WHERE type = \'menulink\' AND link = '.(int)$id; 494 $db->setQuery( $query ); 495 if (!$db->query()) { 496 $this->setError( $db->getErrorMsg() ); 497 return false; 498 } 499 } 500 501 if (!$row->check()) { 502 $this->setError($row->getError()); 503 return false; 504 } 505 if (!$row->store()) { 506 $this->setError($row->getError()); 507 return false; 508 } 509 } else { 510 JError::raiseWarning( 'SOME_ERROR_CODE', JText::_('You cannot unpublish the default menu item')); 511 return false; 512 } 513 } 514 } 515 516 // clean cache 517 MenusHelper::cleanCache(); 518 519 return true; 520 } 521 522 /** 523 * Set the access of selected menu items 524 */ 525 function setAccess( $items, $access ) 526 { 527 $row =& $this->getTable(); 528 foreach ($items as $id) 529 { 530 $row->load( $id ); 531 $row->access = $access; 532 533 // Set any alias menu types to not point to unpublished menu items 534 $db = &$this->getDBO(); 535 $query = 'UPDATE #__menu SET link = 0 WHERE type = \'menulink\' AND access < '.(int)$access.' AND link = '.(int)$id; 536 $db->setQuery( $query ); 537 if (!$db->query()) { 538 $this->setError( $db->getErrorMsg() ); 539 return false; 540 } 541 542 if (!$row->check()) { 543 $this->setError($row->getError()); 544 return false; 545 } 546 if (!$row->store()) { 547 $this->setError($row->getError()); 548 return false; 549 } 550 } 551 552 // clean cache 553 MenusHelper::cleanCache(); 554 555 return true; 556 } 557 558 function orderItem($item, $movement) 559 { 560 $row =& $this->getTable(); 561 $row->load( $item ); 562 if (!$row->move( $movement, 'menutype = '.$this->_db->Quote($row->menutype).' AND parent = '.(int) $row->parent )) { 563 $this->setError($row->getError()); 564 return false; 565 } 566 567 // clean cache 568 MenusHelper::cleanCache(); 569 570 return true; 571 } 572 573 function setOrder($items, $menutype) 574 { 575 $total = count( $items ); 576 $row =& $this->getTable(); 577 $groupings = array(); 578 579 $order = JRequest::getVar( 'order', array(), 'post', 'array' ); 580 JArrayHelper::toInteger($order); 581 582 // update ordering values 583 for( $i=0; $i < $total; $i++ ) { 584 $row->load( $items[$i] ); 585 // track parents 586 $groupings[] = $row->parent; 587 if ($row->ordering != $order[$i]) { 588 $row->ordering = $order[$i]; 589 if (!$row->store()) { 590 $this->setError($row->getError()); 591 return false; 592 } 593 } // if 594 } // for 595 596 // execute updateOrder for each parent group 597 $groupings = array_unique( $groupings ); 598 foreach ($groupings as $group){ 599 $row->reorder('menutype = '.$this->_db->Quote($menutype).' AND parent = '.(int) $group.' AND published >=0'); 600 } 601 602 // clean cache 603 MenusHelper::cleanCache(); 604 605 return true; 606 } 607 608 /** 609 * Delete one or more menu items 610 * @param mixed int or array of id values 611 */ 612 function delete( $ids ) 613 { 614 JArrayHelper::toInteger($ids); 615 616 if (!empty( $ids )) { 617 618 // Add all children to the list 619 foreach ($ids as $id) 620 { 621 $this->_addChildren($id, $ids); 622 } 623 624 $db = &$this->getDBO(); 625 626 // Delete associated module and template mappings 627 $where = 'WHERE menuid = ' . implode( ' OR menuid = ', $ids ); 628 629 $query = 'DELETE FROM #__modules_menu ' 630 . $where; 631 $db->setQuery( $query ); 632 if (!$db->query()) { 633 $this->setError( $menuTable->getErrorMsg() ); 634 return false; 635 } 636 637 $query = 'DELETE FROM #__templates_menu ' 638 . $where; 639 $db->setQuery( $query ); 640 if (!$db->query()) { 641 $this->setError( $menuTable->getErrorMsg() ); 642 return false; 643 } 644 645 // Set any alias menu types to not point to missing menu items 646 $query = 'UPDATE #__menu SET link = 0 WHERE type = \'menulink\' AND (link = '.implode( ' OR id = ', $ids ).')'; 647 $db->setQuery( $query ); 648 if (!$db->query()) { 649 $this->setError( $db->getErrorMsg() ); 650 return false; 651 } 652 653 // Delete the menu items 654 $where = 'WHERE id = ' . implode( ' OR id = ', $ids ); 655 656 $query = 'DELETE FROM #__menu ' . $where; 657 $db->setQuery( $query ); 658 if (!$db->query()) { 659 $this->setError( $db->getErrorMsg() ); 660 return false; 661 } 662 } 663 664 // clean cache 665 MenusHelper::cleanCache(); 666 667 return true; 668 } 669 670 /** 671 * Delete menu items by type 672 */ 673 function deleteByType( $type = '' ) 674 { 675 $db = &$this->getDBO(); 676 677 $query = 'SELECT id' . 678 ' FROM #__menu' . 679 ' WHERE menutype = ' . $db->Quote( $type ); 680 $db->setQuery( $query ); 681 $ids = $db->loadResultArray(); 682 683 if ($db->getErrorNum()) { 684 $this->setError( $db->getErrorMsg() ); 685 return false; 686 } 687 688 return $this->delete( $ids ); 689 } 690 691 function _addChildren($id, &$list) 692 { 693 // Initialize variables 694 $return = true; 695 696 // Get all rows with parent of $id 697 $db =& $this->getDBO(); 698 $query = 'SELECT id' . 699 ' FROM #__menu' . 700 ' WHERE parent = '.(int) $id; 701 $db->setQuery( $query ); 702 $rows = $db->loadObjectList(); 703 704 // Make sure there aren't any errors 705 if ($db->getErrorNum()) { 706 $this->setError($db->getErrorMsg()); 707 return false; 708 } 709 710 // Recursively iterate through all children... kinda messy 711 // TODO: Cleanup this method 712 foreach ($rows as $row) 713 { 714 $found = false; 715 foreach ($list as $idx) 716 { 717 if ($idx == $row->id) { 718 $found = true; 719 break; 720 } 721 } 722 if (!$found) { 723 $list[] = $row->id; 724 } 725 $return = $this->_addChildren($row->id, $list); 726 } 727 return $return; 728 } 729 730 /* 731 * Rebuild the sublevel field for items in the menu (if called with 2nd param = 0 or no params, it will rebuild entire menu tree's sublevel 732 * @param array of menu item ids to change level to 733 * @param int level to set the menu items to (based on parent 734 */ 735 function _rebuildSubLevel($cid = array(0), $level = 0) 736 { 737 JArrayHelper::toInteger($cid, array(0)); 738 $db =& $this->getDBO(); 739 $ids = implode( ',', $cid ); 740 $cids = array(); 741 if($level == 0) { 742 $query = 'UPDATE #__menu SET sublevel = 0 WHERE parent = 0'; 743 $db->setQuery($query); 744 $db->query(); 745 $query = 'SELECT id FROM #__menu WHERE parent = 0'; 746 $db->setQuery($query); 747 $cids = $db->loadResultArray(0); 748 } else { 749 $query = 'UPDATE #__menu SET sublevel = '.(int) $level 750 .' WHERE parent IN ('.$ids.')'; 751 $db->setQuery( $query ); 752 $db->query(); 753 $query = 'SELECT id FROM #__menu WHERE parent IN ('.$ids.')'; 754 $db->setQuery( $query ); 755 $cids = $db->loadResultArray( 0 ); 756 } 757 if (!empty( $cids )) { 758 $this->_rebuildSubLevel( $cids, $level + 1 ); 759 } 760 } 761 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Mar 28 15:54:07 2012 | Cross-referenced by PHPXref 0.7.1 |