PageRenderTime 112ms CodeModel.GetById 47ms RepoModel.GetById 1ms app.codeStats 0ms

/master/tmp/install_4d8110c1cce79/AddToMenu-v1.6.0/files/elements/plugins/system/nonumberelements/elements/menuitems.php

https://github.com/sherdog/GitWitty
PHP | 191 lines | 139 code | 22 blank | 30 comment | 31 complexity | 5b716698dc672b389530351bc7ff9827 MD5 | raw file
  1. <?php
  2. /**
  3. * Element: MenuItems
  4. * Display a menuitem field with a button
  5. *
  6. * @package NoNumber! Elements
  7. * @version 2.2.2
  8. *
  9. * @author Peter van Westen <peter@nonumber.nl>
  10. * @link http://www.nonumber.nl
  11. * @copyright Copyright © 2011 NoNumber! All Rights Reserved
  12. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  13. */
  14. // Ensure this file is being included by a parent file
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16. /**
  17. * MenuItems Element
  18. */
  19. class JElementMenuItems extends JElement
  20. {
  21. /**
  22. * Element name
  23. *
  24. * @access protected
  25. * @var string
  26. */
  27. var $_name = 'MenuItems';
  28. function fetchElement( $name, $value, &$node, $control_name )
  29. {
  30. JHTML::_( 'behavior.modal', 'a.modal' );
  31. $size = $node->attributes( 'size' );
  32. $multiple = $this->def( $node->attributes( 'multiple'), 1 );
  33. $showinput = $node->attributes( 'showinput' );
  34. $state = $node->attributes( 'state' );
  35. $disable = $node->attributes( 'disable' );
  36. $db =& JFactory::getDBO();
  37. // load the list of menu types
  38. $query = 'SELECT menutype, title'
  39. .' FROM #__menu_types'
  40. .' ORDER BY title'
  41. ;
  42. $db->setQuery( $query );
  43. $menuTypes = $db->loadObjectList();
  44. // load the list of menu items
  45. if ( $state != '' ) {
  46. $where = 'WHERE published = '.(int) $state;
  47. } else {
  48. $where = 'WHERE published != -2';
  49. }
  50. $query = 'SELECT id, parent, name, menutype, type, published'
  51. .' FROM #__menu'
  52. .' '.$where
  53. .' ORDER BY menutype, parent, ordering'
  54. ;
  55. $db->setQuery($query);
  56. $menuItems = $db->loadObjectList();
  57. // establish the hierarchy of the menu
  58. $children = array();
  59. if ($menuItems)
  60. {
  61. // first pass - collect children
  62. foreach ($menuItems as $v)
  63. {
  64. $pt = $v->parent;
  65. $list = @$children[$pt] ? $children[$pt] : array();
  66. array_push( $list, $v );
  67. $children[$pt] = $list;
  68. }
  69. }
  70. // second pass - get an indent list of the items
  71. require_once JPATH_LIBRARIES.DS.'joomla'.DS.'html'.DS.'html'.DS.'menu.php';
  72. $list = JHTMLMenu::treerecurse( 0, '', array(), $children, 9999, 0, 0 );
  73. // assemble into menutype groups
  74. $groupedList = array();
  75. foreach ( $list as $k => $v ) {
  76. $groupedList[$v->menutype][] =& $list[$k];
  77. }
  78. // assemble menu items to the array
  79. $options = array();
  80. $count = 0;
  81. foreach ($menuTypes as $type) {
  82. if (isset( $groupedList[$type->menutype] )) {
  83. if ( $count > 0 ) {
  84. $options[] = JHTML::_('select.option', '-', '&nbsp;', 'value', 'text', true);
  85. }
  86. $count++;
  87. $options[] = JHTML::_('select.option', $type->menutype, '[ '.$type->title.' ]', 'value', 'text', true );
  88. $n = count( $groupedList[$type->menutype] );
  89. for ($i = 0; $i < $n; $i++)
  90. {
  91. $item =& $groupedList[$type->menutype][$i];
  92. //If menutype is changed but item is not saved yet, use the new type in the list
  93. if ( JRequest::getString( 'option', '', 'get' ) == 'com_menus' ) {
  94. $currentItemArray = JRequest::getVar( 'cid', array(0), '', 'array' );
  95. $currentItemId = (int) $currentItemArray['0'];
  96. $currentItemType = JRequest::getString( 'type', $item->type, 'get' );
  97. if ( $currentItemId == $item->id && $currentItemType != $item->type ) {
  98. $item->type = $currentItemType;
  99. }
  100. }
  101. $disable = strpos( $disable, $item->type ) !== false ? true : false;
  102. $item_name = $item->treename;
  103. $item_id = $item->id;
  104. $style = 'padding-left:1em;';
  105. if ( $item->published == 0 && !( $state === 0 ) ) {
  106. $item_name = '*'.$item_name.' ('.JText::_( 'Unpublished' ).')';
  107. $style .= 'font-style:italic;';
  108. }
  109. if ( $showinput) {
  110. $item_name .= ' ['.$item->id.']';
  111. }
  112. if ( $style ) {
  113. $item_name = '[[:'.$style.':]]'.$item_name;
  114. }
  115. $options[] = JHTML::_( 'select.option', $item_id, $item_name, 'value', 'text', $disable );
  116. }
  117. }
  118. }
  119. $attribs = 'class="inputbox"';
  120. if ( $showinput) {
  121. array_unshift( $options,JHTML::_( 'select.option', '-', '&nbsp;', 'value', 'text', true) );
  122. array_unshift( $options, JHTML::_( 'select.option', '-', '- '.JText::_('Select Item').' -') );
  123. if( $multiple ) {
  124. $onchange = 'if ( this.value ) { if ( '.$control_name.$name.'.value ) { '.$control_name.$name.'.value+=\',\'; } '.$control_name.$name.'.value+=this.value; } this.value=\'\';';
  125. } else {
  126. $onchange = 'if ( this.value ) { '.$control_name.$name.'.value=this.value;'.$control_name.$name.'_text.value=this.options[this.selectedIndex].innerHTML.replace( /^((&|&amp;|&#160;)nbsp;|-)*/gm, \'\' ).trim(); } this.value=\'\';';
  127. }
  128. $attribs .= ' onchange="'.$onchange.'"';
  129. $html = '<table cellpadding="0" cellspacing="0"><tr><td style="padding: 0px;">'."\n";
  130. if( !$multiple ) {
  131. $value_name = $value;
  132. if ( $value ) {
  133. foreach ( $menuItems as $item ) {
  134. if ( $item->id == $value ) {
  135. $value_name = $item->name.' ['.$value.']';;
  136. break;
  137. }
  138. }
  139. }
  140. $html .= '<input type="text" id="'.$control_name.$name.'_text" value="'.$value_name.'" class="inputbox" size="'.$size.'" disabled="disabled" />';
  141. $html .= '<input type="hidden" name="'.$control_name.'['.$name.']" id="'.$control_name.$name.'" value="'.$value.'" />';
  142. } else {
  143. $html .= '<input type="text" name="'.$control_name.'['.$name.']" id="'.$control_name.$name.'" value="'.$value.'" class="inputbox" size="'.$size.'" />';
  144. }
  145. $html .= '</td><td style="padding: 0px;"padding-left: 5px;>'."\n";
  146. $html .= JHTML::_('select.genericlist', $options, '', $attribs, 'value', 'text', '', '');
  147. $html .= '</td></tr></table>'."\n";
  148. } else {
  149. if( $size ) {
  150. $attribs .= ' size="'.$size.'"';
  151. }else {
  152. $attribs .= ' size="'.( ( count( $options) > 10 ) ? 10 : count( $options) ).'"';
  153. }
  154. if( $multiple ) {
  155. if ( !is_array( $value ) ) {
  156. $value = explode( ',', $value );
  157. }
  158. $attribs .= ' multiple="multiple"';
  159. }
  160. $html = JHTML::_( 'select.genericlist', $options, ''.$control_name.'['.$name.'][]', $attribs, 'value', 'text', $value, $control_name.$name );
  161. }
  162. $html = preg_replace( '#>\[\[\:(.*?)\:\]\]#si', ' style="\1">', $html );
  163. return $html;
  164. }
  165. function def( $val, $default )
  166. {
  167. return ( $val == '' ) ? $default : $val;
  168. }
  169. }