/www/plugins/system/nonumberelements/fields/categoriesfc.php

https://github.com/amet17/webstar · PHP · 131 lines · 78 code · 19 blank · 34 comment · 8 complexity · 5082b1ced26a9dc77eb509b891a441d2 MD5 · raw file

  1. <?php
  2. /**
  3. * Element: CategoriesFC
  4. * Displays a multiselectbox of available Flexicontent categories
  5. *
  6. * @package NoNumber! Elements
  7. * @version 2.9.1
  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. // No direct access
  15. defined( '_JEXEC' ) or die();
  16. /**
  17. * CategoriesFC Element
  18. */
  19. class nnElementCategoriesFC
  20. {
  21. var $_version = '2.9.1';
  22. function getInput( $name, $id, $value, $params, $children, $j15 = 0 )
  23. {
  24. $this->params = $params;
  25. if ( !file_exists( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'admin.flexicontent.php' ) ) {
  26. return 'Flexicontent files not found...';
  27. }
  28. $db =& JFactory::getDBO();
  29. $sql = "SHOW tables like '".$db->getPrefix()."flexicontent_cats_item_relations'";
  30. $db->setQuery( $sql );
  31. $tables = $db->loadObjectList();
  32. if ( !count( $tables ) ) {
  33. return 'Flexicontent category-item relations table not found in database...';
  34. }
  35. $size = (int) $this->def( 'size' );
  36. $multiple = $this->def( 'multiple' );
  37. $get_categories = $this->def( 'getcategories', 1 );
  38. if ( !is_array( $value ) ) {
  39. $value = explode( ',', $value );
  40. }
  41. $flexicomp_params =& JComponentHelper::getParams( 'com_flexicontent' );
  42. $flexi_section = $flexicomp_params->get( 'flexi_section' );
  43. $sql = 'SELECT id, parent_id as parent, title as name'
  44. .' FROM #__categories'
  45. .' WHERE published = 1'
  46. .' AND section = '.$flexi_section
  47. .' ORDER BY ordering';
  48. $db->setQuery($sql);
  49. $menuItems = $db->loadObjectList();
  50. // establish the hierarchy of the menu
  51. // TODO: use node model
  52. $children = array();
  53. if ( $menuItems ) {
  54. // first pass - collect children
  55. foreach ( $menuItems as $v ) {
  56. $pt = $v->parent;
  57. $list = @$children[$pt] ? $children[$pt] : array();
  58. array_push( $list, $v );
  59. $children[$pt] = $list;
  60. }
  61. }
  62. // second pass - get an indent list of the items
  63. require_once JPATH_LIBRARIES.DS.'joomla'.DS.'html'.DS.'html'.DS.'menu.php';
  64. $list = JHTMLMenu::treerecurse( 0, '', array(), $children, 9999, 0, 1 );
  65. // assemble items to the array
  66. $options = array();
  67. foreach ( $list as $item ) {
  68. $item_name = preg_replace( '#^((&nbsp;)*)- #', '\1', str_replace( '&#160;', '&nbsp;', $item->treename ) );
  69. $options[] = JHTML::_( 'select.option', $item->id, $item_name, 'value', 'text', 0 );
  70. }
  71. require_once JPATH_PLUGINS.DS.'system'.DS.'nonumberelements'.DS.'helpers'.DS.'html.php';
  72. return nnHTML::selectlist( $options, $name, $value, $id, $size, $multiple, 0, $j15 );
  73. }
  74. private function def( $val, $default = '' )
  75. {
  76. return ( isset( $this->params[$val] ) && (string) $this->params[$val] != '' ) ? (string) $this->params[$val] : $default;
  77. }
  78. }
  79. if ( version_compare( JVERSION, '1.6.0', 'l' ) ) {
  80. // For Joomla 1.5
  81. class JElementNN_CategoriesFC extends JElement
  82. {
  83. /**
  84. * Element name
  85. *
  86. * @access protected
  87. * @var string
  88. */
  89. var $_name = 'CategoriesFC';
  90. function fetchElement( $name, $value, &$node, $control_name )
  91. {
  92. $this->_nnelement = new nnElementCategoriesFC();
  93. return $this->_nnelement->getInput( $control_name.'['.$name.']', $control_name.$name, $value, $node->attributes(), $node->children(), 1 );
  94. }
  95. }
  96. } else {
  97. // For Joomla 1.6
  98. class JFormFieldNN_CategoriesFC extends JFormField
  99. {
  100. /**
  101. * The form field type
  102. *
  103. * @var string
  104. */
  105. public $type = 'CategoriesFC';
  106. protected function getInput()
  107. {
  108. $this->_nnelement = new nnElementCategoriesFC();
  109. return $this->_nnelement->getInput( $this->name, $this->id, $this->value, $this->element->attributes(), $this->element->children() );
  110. }
  111. }
  112. }