| [ Index ] |
PHP Cross Reference of Joomla 1.5.26 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: item.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 MenusModelItem extends JModel 25 { 26 /** 27 * Menu Item ID 28 * 29 * @var int 30 */ 31 var $_id = null; 32 33 /** @var object JTable object */ 34 var $_table = null; 35 36 /** @var object JTable object */ 37 var $_url = null; 38 39 /** 40 * Overridden constructor 41 * @access protected 42 */ 43 function __construct() 44 { 45 parent::__construct(); 46 $url = JRequest::getVar('url', array(), '', 'array'); 47 if (isset($url['option'])) 48 { 49 $this->_url = 'index.php?option='.$url['option']; 50 unset($url['option']); 51 if (count($url)) { 52 foreach ($url as $k => $v) 53 { 54 $this->_url .= '&'.$k.'='.$v; 55 } 56 } 57 } 58 59 $this->setId(); 60 } 61 62 function setId() 63 { 64 $array = JRequest::getVar('cid', array(0), '', 'array'); 65 $this->_id = (int) $array[0]; 66 67 if (!$this->_id) { 68 $this->_id = JRequest::getInt('id', 0); 69 } 70 } 71 72 function &getItem() 73 { 74 static $item; 75 if (isset($item)) { 76 return $item; 77 } 78 79 $table =& $this->_getTable(); 80 81 // Load the current item if it has been defined 82 $edit = JRequest::getVar('edit',true); 83 $cid = JRequest::getVar( 'cid', array(0), '', 'array' ); 84 JArrayHelper::toInteger($cid, array(0)); 85 if ($edit) { 86 $table->load($cid[0]); 87 } 88 89 // Override the current item's type field if defined in the request 90 if ($type = JRequest::getString('type')) { 91 $table->type = $type; 92 } 93 94 // Override the current item's menutype field if defined in the request 95 if ($menutype = JRequest::getVar('menutype', '', '', 'menutype')) { 96 $table->menutype = $menutype; 97 } 98 99 switch ($table->type) 100 { 101 case 'separator': 102 $table->link = null; 103 $table->componentid = 0; 104 break; 105 case 'url': 106 $table->componentid = 0; 107 break; 108 case 'menulink': 109 $table->componentid = 0; 110 break; 111 case 'component': 112 // Override the current item's link field if defined in the request 113 if (!is_null($this->_url)) { 114 $table->link = $this->_url; 115 } 116 $url = str_replace('index.php?', '', $table->link); 117 $url = str_replace('&', '&', $url); 118 $table->linkparts = null; 119 if(strpos($url, '&') !== false) 120 { 121 $url = str_replace('&','&',$url); 122 } 123 124 parse_str($url, $table->linkparts); 125 126 $db = &$this->getDBO(); 127 if ($component = @$table->linkparts['option']) { 128 $query = 'SELECT `id`' . 129 ' FROM `#__components`' . 130 ' WHERE `link` <> \'\'' . 131 ' AND `parent` = 0' . 132 ' AND `option` = "'.$db->getEscaped($component).'"'; 133 $db->setQuery( $query ); 134 $table->componentid = $db->loadResult(); 135 } 136 break; 137 } 138 139 $item = $table; 140 return $item; 141 } 142 143 function &getExpansion() 144 { 145 $item = &$this->getItem(); 146 $return['option'] = JRequest::getCmd('expand'); 147 $menutype = JRequest::getVar('menutype', '', '', 'menutype'); 148 149 if ($return['option']) 150 { 151 require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_menus'.DS.'classes'.DS.'ilink.php'); 152 $handler = new iLink($return['option'], $item->id, $menutype); 153 $return['html'] = $handler->getTree(); 154 return $return; 155 } else { 156 $return['html'] = null; 157 } 158 return $return; 159 } 160 161 function &getUrlParams() 162 { 163 // Get the state parameters 164 $item =& $this->getItem(); 165 $params = new JParameter(''); 166 167 if ($state =& $this->_getStateXML()) 168 { 169 if (is_a($state, 'JSimpleXMLElement')) 170 { 171 $sp =& $state->getElementByPath('url'); 172 $params->setXML($sp); 173 if (isset($item->linkparts) && is_array($item->linkparts)) { 174 $params->loadArray($item->linkparts); 175 } 176 } 177 } 178 return $params; 179 } 180 181 function &getStateParams() 182 { 183 // Get the state parameters 184 $item =& $this->getItem(); 185 $params = new JParameter($item->params); 186 187 if ($state =& $this->_getStateXML()) 188 { 189 if (is_a($state, 'JSimpleXMLElement')) 190 { 191 $sp =& $state->getElementByPath('params'); 192 $params->setXML($sp); 193 } 194 } 195 return $params; 196 } 197 198 function &getAdvancedParams() 199 { 200 // Get the state parameters 201 $item =& $this->getItem(); 202 $params = new JParameter($item->params); 203 204 if ($state =& $this->_getStateXML()) 205 { 206 if (is_a($state, 'JSimpleXMLElement')) 207 { 208 $ap =& $state->getElementByPath('advanced'); 209 $params->setXML($ap); 210 } 211 } 212 return $params; 213 } 214 215 function &getComponentParams() 216 { 217 // Initialize variables 218 $params = null; 219 $item = &$this->getItem(); 220 221 if ($item->type == 'component') 222 { 223 $comp = &$this->getComponent(); 224 $option = preg_replace( '#\W#', '', $comp->option ); 225 $path = JPATH_ADMINISTRATOR.DS.'components'.DS.$option.DS.'config.xml'; 226 227 $params = new JParameter( $item->params ); 228 if (file_exists( $path )) 229 { 230 $xml =& JFactory::getXMLParser('Simple'); 231 if ($xml->loadFile($path)) 232 { 233 $document =& $xml->document; 234 235 // if hide is set, don't show the component configuration while editing menu item 236 $menu = $document->attributes('menu'); 237 if ( isset($menu) && $menu == 'hide' ) 238 { 239 $params = null; 240 return $params; 241 } 242 243 if (isset($document->params[0]->param)) 244 { 245 // We will collect the hidden elements in an array 246 // loop will mess up if we do it within the loop 247 $hide = array(); 248 for ($i=0,$n=count($document->params[0]->param); $i<$n; $i++) 249 { 250 if ($document->params[0]->param[$i]->attributes('menu') == 'hide') 251 { 252 $hide[] = &$document->params[0]->param[$i]; 253 } 254 else if ($document->params[0]->param[$i]->attributes('type') == 'radio' || $document->params[0]->param[$i]->attributes('type') == 'list') { 255 $document->params[0]->param[$i]->addAttribute('default', ''); 256 $document->params[0]->param[$i]->addAttribute('type', 'list'); 257 $child = &$document->params[0]->param[$i]->addChild('option', array('value' => '')); 258 $child->setData('Use Global'); 259 } 260 } 261 // Now remove any hidden elements 262 for ($i = 0, $n = count( $hide ); $i < $n; $i++) { 263 $document->params[0]->removeChild( $hide[$i] ); 264 } 265 } 266 $params->setXML( $document->params[0] ); 267 } 268 } 269 } 270 return $params; 271 } 272 273 function &getSystemParams() 274 { 275 // Initialize variables 276 $params = null; 277 $item = &$this->getItem(); 278 279 $params = new JParameter( $item->params ); 280 if ($item->type == 'component') { 281 $path = JPATH_BASE.DS.'components'.DS.'com_menus'.DS.'models'.DS.'metadata'.DS.'component.xml'; 282 if (file_exists( $path )) { 283 $xml =& JFactory::getXMLParser('Simple'); 284 if ($xml->loadFile($path)) { 285 $document =& $xml->document; 286 $params->setXML($document->getElementByPath('state/params')); 287 } 288 } 289 } 290 return $params; 291 } 292 293 /** 294 * Get the name of the current menu item 295 * 296 * @return string 297 * @access public 298 * @since 1.5 299 */ 300 function getStateName() 301 { 302 $state =& $this->_getStateXML(); 303 304 if ( ! is_a($state, 'JSimpleXMLElement')) 305 { 306 return null; 307 } 308 309 $name = null; 310 $sn =& $state->getElementByPath('name'); 311 if ($sn) { 312 $name = $sn->data(); 313 } 314 315 return JText::_($name); 316 } 317 318 /** 319 * Get the description of the current menu item 320 * 321 * @return string 322 * @access public 323 * @since 1.5 324 */ 325 function getStateDescription() 326 { 327 $state =& $this->_getStateXML(); 328 329 330 if ( ! is_a($state, 'JSimpleXMLElement')) 331 { 332 return null; 333 } 334 335 $description = null; 336 $sd =& $state->getElementByPath('description'); 337 if ($sd) { 338 $description = $sd->data(); 339 } 340 341 return JText::_($description); 342 } 343 344 /** 345 * Gets the componet table object related to this menu item 346 */ 347 function &getComponent() 348 { 349 $item =& $this->getItem(); 350 $id = $item->componentid; 351 $component = & JTable::getInstance( 'component'); 352 $component->load( $id ); 353 return $component; 354 } 355 356 function checkout($uid = null) 357 { 358 $id = JRequest::getVar('cid', array(0), '', 'array'); 359 JArrayHelper::toInteger( $id, array(0) ); 360 361 // Make sure we have a user id to checkout the article with 362 if (is_null($uid)) { 363 $user =& JFactory::getUser(); 364 $uid = $user->get('id'); 365 } 366 367 // Lets get to it and checkout the thing... 368 $item =& $this->getItem(); 369 if(!$item->checkout($uid, $id[0])) { 370 $this->setError($this->_db->getErrorMsg()); 371 return false; 372 } 373 374 return true; 375 } 376 377 function checkin() 378 { 379 if ($this->_id) { 380 $item =& $this->_getTable(); 381 382 if(!$item->checkin($this->_id)) { 383 $this->setError($this->_db->getErrorMsg()); 384 return false; 385 } 386 387 return true; 388 } 389 390 return false; 391 } 392 393 function store() 394 { 395 // Initialize variables 396 $db =& JFactory::getDBO(); 397 $row =& $this->getItem(); 398 $post = $this->_state->get( 'request' ); 399 400 switch ($post['type']) 401 { 402 case 'separator': 403 break; 404 case 'url': 405 break; 406 case 'menulink': 407 $post['link'] = 'index.php?Itemid='.$post['params']['menu_item']; 408 break; 409 case 'component': 410 break; 411 } 412 if (!$row->bind( $post )) { 413 echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n"; 414 return false; 415 } 416 417 if ($row->id > 0) 418 { 419 // existing item 420 $query = 'SELECT menutype FROM #__menu WHERE id = '.(int) $row->id; 421 $this->_db->setQuery( $query ); 422 $oldType = $this->_db->loadResult(); 423 if ($oldType != $row->menutype) { 424 // moved to another menu, disconnect the old parent 425 $row->parent = 0; 426 } 427 $query = 'SELECT parent FROM #__menu WHERE id = '.(int) $row->id; 428 $this->_db->setQuery( $query ); 429 $oldParent = $this->_db->loadResult(); 430 if ($oldParent != $row->parent) { 431 // we have changed parents, so we have to fix the submenu values 432 if ($row->parent != 0) { 433 $query = 'SELECT sublevel FROM #__menu WHERE id = '.(int) $row->parent; 434 $this->_db->setQuery( $query ); 435 $sublevel = $this->_db->loadResult() + 1; 436 } else { 437 $sublevel = 0; 438 } 439 $row->sublevel = $sublevel; 440 $this->_setSubLevel( array( (int) $row->id ), $sublevel ); 441 } 442 } 443 else 444 { 445 // if new item order last in appropriate group 446 $where = "menutype = " . $db->Quote($row->menutype) . " AND published >= 0 AND parent = ".(int) $row->parent; 447 $row->ordering = $row->getNextOrder( $where ); 448 449 if( $row->parent != 0 ) { 450 $query = 'SELECT sublevel FROM #__menu WHERE id = '. (int) $row->parent; 451 $this->_db->setQuery($query); 452 $row->sublevel = $this->_db->loadResult() + 1; 453 } 454 } 455 456 if (isset($post['urlparams']) && is_array($post['urlparams'])) 457 { 458 $pos = strpos( $row->link, '?' ); 459 if ($pos !== false) 460 { 461 $prefix = substr( $row->link, 0, $pos ); 462 $query = substr( $row->link, $pos+1 ); 463 464 $temp = array(); 465 if(strpos($query, '&') !== false) { 466 $query = str_replace('&', '&', $query); 467 } 468 parse_str( $query, $temp ); 469 $temp2 = array_merge( $temp, $post['urlparams'] ); 470 471 $temp3 = array(); 472 foreach ($temp2 as $k => $v) 473 { 474 if ( $k && strlen($v) ) 475 { 476 $temp3[] = $k.'='.$v; 477 } 478 } 479 $url = null; 480 $row->link = $prefix . '?' . implode( '&', $temp3 ); 481 } 482 } 483 484 if (!$row->check()) 485 { 486 echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n"; 487 return false; 488 } 489 490 if (!$row->store()) 491 { 492 echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n"; 493 return false; 494 } 495 496 $row->checkin(); 497 $row->reorder( 'menutype='.$db->Quote( $row->menutype ).' AND parent='.(int)$row->parent ); 498 499 // clean cache 500 MenusHelper::cleanCache(); 501 502 return true; 503 } 504 505 506 507 /** 508 * Delete one or more menu items 509 * @param mixed int or array of id values 510 */ 511 function delete( $ids ) 512 { 513 JArrayHelper::toInteger($ids); 514 515 $db = &$this->getDBO(); 516 517 if (count( $ids )) 518 { 519 // Delete associated module and template mappings 520 $where = 'WHERE menuid = ' . implode( ' OR menuid = ', $ids ); 521 522 $query = 'DELETE FROM #__modules_menu ' 523 . $where; 524 $db->setQuery( $query ); 525 if (!$db->query()) { 526 $this->setError( $menuTable->getErrorMsg() ); 527 return false; 528 } 529 530 $query = 'DELETE FROM #__templates_menu ' 531 . $where; 532 $db->setQuery( $query ); 533 if (!$db->query()) { 534 $this->setError( $menuTable->getErrorMsg() ); 535 return false; 536 } 537 538 // Set any alias menu types to not point to missing menu items 539 $query = 'UPDATE #__menu SET link = 0 WHERE type = \'menulink\' AND (link = '.implode( ' OR id = ', $ids ).')'; 540 $db->setQuery( $query ); 541 if (!$db->query()) { 542 $this->setError( $db->getErrorMsg() ); 543 return false; 544 } 545 546 // Delete the menu items 547 $where = 'WHERE id = ' . implode( ' OR id = ', $ids ); 548 549 $query = 'DELETE FROM #__menu ' . $where; 550 $db->setQuery( $query ); 551 if (!$db->query()) { 552 $this->setError( $db->getErrorMsg() ); 553 return false; 554 } 555 } 556 557 // clean cache 558 MenusHelper::cleanCache(); 559 560 return true; 561 } 562 563 /** 564 * Delete menu items by type 565 */ 566 function deleteByType( $type = '' ) 567 { 568 $db = &$this->getDBO(); 569 570 $query = 'SELECT id' . 571 ' FROM #__menu' . 572 ' WHERE menutype = ' . $db->Quote( $type ); 573 $db->setQuery( $query ); 574 $ids = $db->loadResultArray(); 575 576 if ($db->getErrorNum()) 577 { 578 $this->setError( $db->getErrorMsg() ); 579 return false; 580 } 581 582 return $this->delete( $ids ); 583 } 584 585 /** 586 * Returns the internal table object 587 * @return JTable 588 */ 589 function &_getTable() 590 { 591 if ($this->_table == null) { 592 $this->_table =& JTable::getInstance( 'menu'); 593 } 594 return $this->_table; 595 } 596 597 function &_getStateXML() 598 { 599 static $xml; 600 601 if (isset($xml)) { 602 return $xml; 603 } 604 $xml = null; 605 $xmlpath = null; 606 $item = &$this->getItem(); 607 608 switch ($item->type) 609 { 610 case 'separator': 611 $xmlpath = JPATH_BASE.DS.'components'.DS.'com_menus'.DS.'models'.DS.'metadata'.DS.'separator.xml'; 612 break; 613 case 'url': 614 $xmlpath = JPATH_BASE.DS.'components'.DS.'com_menus'.DS.'models'.DS.'metadata'.DS.'url.xml'; 615 break; 616 case 'menulink': 617 $xmlpath = JPATH_BASE.DS.'components'.DS.'com_menus'.DS.'models'.DS.'metadata'.DS.'menulink.xml'; 618 break; 619 case 'component': 620 default: 621 if (isset($item->linkparts['view'])) 622 { 623 // View is set... so we konw to look in view file 624 if (isset($item->linkparts['layout'])) { 625 $layout = $item->linkparts['layout']; 626 } else { 627 $layout = 'default'; 628 } 629 $lpath = JPATH_ROOT.DS.'components'.DS.$item->linkparts['option'].DS.'views'.DS.$item->linkparts['view'].DS.'tmpl'.DS.$layout.'.xml'; 630 $vpath = JPATH_ROOT.DS.'components'.DS.$item->linkparts['option'].DS.'views'.DS.$item->linkparts['view'].DS.'metadata.xml'; 631 if (file_exists($lpath)) { 632 $xmlpath = $lpath; 633 } elseif (file_exists($vpath)) { 634 $xmlpath = $vpath; 635 } 636 } 637 if (!$xmlpath && isset($item->linkparts['option'])) { 638 $xmlpath = JPATH_ROOT.DS.'components'.DS.$item->linkparts['option'].DS.'metadata.xml'; 639 if(!file_exists($xmlpath)) { 640 $xmlpath = JApplicationHelper::getPath('com_xml', $item->linkparts['option']); 641 } 642 } 643 break; 644 } 645 646 if (file_exists($xmlpath)) 647 { 648 $xml =& JFactory::getXMLParser('Simple'); 649 if ($xml->loadFile($xmlpath)) { 650 $this->_xml = &$xml; 651 $document =& $xml->document; 652 653 /* 654 * HANDLE NO OPTION CASE 655 */ 656 $menus =& $document->getElementByPath('menu'); 657 if (is_a($menus, 'JSimpleXMLElement') && $menus->attributes('options') == 'none') { 658 $xml =& $menus->getElementByPath('state'); 659 } else { 660 $xml =& $document->getElementByPath('state'); 661 } 662 663 // Handle error case... path doesn't exist 664 if (!is_a($xml, 'JSimpleXMLElement')) { 665 return $document; 666 } 667 668 /* 669 * HANDLE A SWITCH IF IT EXISTS 670 */ 671 if ($switch = $xml->attributes('switch')) 672 { 673 $default = $xml->attributes('default'); 674 // Handle switch 675 $switchVal = (isset($item->linkparts[$switch]))? $item->linkparts[$switch] : 'default'; 676 $found = false; 677 678 foreach ($xml->children() as $child) { 679 if ($child->name() == $switchVal) { 680 $xml =& $child; 681 $found = true; 682 break; 683 } 684 } 685 686 if (!$found) { 687 foreach ($xml->children() as $child) { 688 if ($child->name() == $default) { 689 $xml =& $child; 690 break; 691 } 692 } 693 } 694 } 695 696 /* 697 * HANDLE INCLUDED PARAMS 698 */ 699 $children = $xml->children(); 700 if (count($children) == 1) 701 { 702 if ($children[0]->name() == 'include') { 703 $ret =& $this->_getIncludedParams($children[0]); 704 if ($ret) { 705 $xml =& $ret; 706 } 707 } 708 } 709 710 if ($switch = $xml->attributes('switch')) 711 { 712 $default = $xml->attributes('default'); 713 // Handle switch 714 $switchVal = ($item->linkparts[$switch])? $item->linkparts[$switch] : 'default'; 715 $found = false; 716 717 foreach ($xml->children() as $child) { 718 if ($child->name() == $switchVal) { 719 $xml =& $child; 720 $found = true; 721 break; 722 } 723 } 724 725 if (!$found) { 726 foreach ($xml->children() as $child) { 727 if ($child->name() == $default) { 728 $xml =& $child; 729 break; 730 } 731 } 732 } 733 } 734 } 735 } 736 return $xml; 737 } 738 739 function &_getIncludedParams($include) 740 { 741 $tags = array(); 742 $state = null; 743 $source = $include->attributes('source'); 744 $path = $include->attributes('path'); 745 $item = &$this->getItem(); 746 747 preg_match_all( "/{([A-Za-z\-_]+)}/", $source, $tags); 748 if (isset($tags[1])) { 749 for ($i=0;$i<count($tags[1]);$i++) { 750 $source = str_replace($tags[0][$i], @$item->linkparts[$tags[1][$i]], $source); 751 } 752 } 753 754 // load the source xml file 755 if (file_exists( JPATH_ROOT.$source )) 756 { 757 $xml = & JFactory::getXMLParser('Simple'); 758 759 if ($xml->loadFile(JPATH_ROOT.$source)) { 760 $document = &$xml->document; 761 $state = $document->getElementByPath($path); 762 } 763 } 764 return $state; 765 } 766 767 /** 768 * Sets the sublevel for menu items 769 * 770 * @param array id values to set 771 * @param int level to assign to the sublevel 772 */ 773 function _setSubLevel( $cid, $level ) 774 { 775 JArrayHelper::toInteger($cid, array(0)); 776 777 $ids = implode( ',', $cid ); 778 779 $query = 'UPDATE #__menu SET sublevel = '.(int) $level 780 .' WHERE id IN ('.$ids.')'; 781 $this->_db->setQuery( $query ); 782 $this->_db->query(); 783 784 $query = 'SELECT id FROM #__menu WHERE parent IN ('.$ids.')'; 785 $this->_db->setQuery( $query ); 786 $cids = $this->_db->loadResultArray( 0 ); 787 788 if (!empty( $cids )) { 789 $this->_setSubLevel( $cids, $level + 1 ); 790 } 791 } 792 }
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 |