Advertisement
manchumahara

Joomla K2 Category Listing Custom Param/Form Field

May 2nd, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.50 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @version Id: k2cat.php $
  4.  * @package Joomla
  5.  * @subpackage  Content
  6.  * @copyright   Copyright (C)2011-2013 codeboxr.com. All rights reserved.
  7.  * @license GNU/GPL, http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  8.  * @help:      http://codeboxr.com/product/all-in-one-social-comment-embed-system-content-plugin-for-joomla
  9.  */
  10.  
  11. // no direct access
  12. defined('_JEXEC') or die('Restricted access');
  13.  
  14. if(version_compare(JVERSION,'3.0.0','ge')):
  15.     jimport('joomla.form.formfield');
  16.     class JFormFieldK2cat extends JFormField {
  17.  
  18.             protected $type = 'k2cat';
  19.  
  20.             public function getInput(){
  21.                 //$this->name, $this->value, $this->element, $this->options['control']
  22.                 //var_dump($this);
  23.                 //$value =
  24.                 $db = JFactory::getDBO();
  25.                 if(JFile::exists(JPATH_ROOT.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_k2'.DIRECTORY_SEPARATOR.'k2.php')):
  26.                     $query = 'SELECT m.* FROM #__k2_categories m WHERE published=1 AND trash = 0 ORDER BY parent, ordering';
  27.                     $db->setQuery( $query );
  28.                     $mitems = $db->loadObjectList();
  29.                     //var_dump($mitems);
  30.                     $children = array();
  31.                     if ($mitems){
  32.                         foreach ( $mitems as $v ){
  33.                             if(K2_JVERSION != '16'){
  34.                                     $v->title = $v->name;
  35.                                     $v->parent_id = $v->parent;
  36.                             }
  37.                             $pt = $v->parent;
  38.                             $list = @$children[$pt] ? $children[$pt] : array();
  39.                             array_push( $list, $v );
  40.                             $children[$pt] = $list;
  41.                         }
  42.                     }
  43.                     $list = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0 );
  44.                     $mitems = array();
  45.  
  46.                     foreach ( $list as $item ) {
  47.                         $item->treename = JString::str_ireplace('&#160;', '- ', $item->treename);
  48.                         $mitems[] = JHTML::_('select.option',  $item->id, '   '.$item->treename );
  49.                     }                
  50.                
  51.                 endif;
  52.  
  53.                 $fieldName = $this->name.'[]';
  54.  
  55.  
  56.                 //$doc->addScriptDeclaration($js);
  57.                 $output = JHTML::_('select.genericlist',  $mitems, $fieldName, 'class="inputbox" style="width:90%;" multiple="multiple" size="10"', 'value', 'text', $this->value );
  58.                 return $output;
  59.                 //return JElementK2cat::fetchElement($this->name, $this->value, $this->element, $this->options['control']);
  60.             }
  61.     }
  62. else:    
  63.     if(version_compare(JVERSION,'1.6.0','ge')) {
  64.             jimport('joomla.form.formfield');
  65.             class JFormFieldK2cat extends JFormField {
  66.  
  67.                     var $type = 'k2cat';
  68.  
  69.                     function getInput(){
  70.                             return JElementK2cat::fetchElement($this->name, $this->value, $this->element, $this->options['control']);
  71.                     }
  72.             }
  73.     }
  74.  
  75.     jimport('joomla.html.parameter.element');
  76.  
  77.     class JElementK2cat extends JElement
  78.     {
  79.  
  80.             var $_name = 'k2cat';
  81.  
  82.             function fetchElement($name, $value, &$node, $control_name){
  83.  
  84.                     $db = JFactory::getDBO();
  85.                    
  86.                     if(JFile::exists(JPATH_ROOT.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_k2'.DIRECTORY_SEPARATOR.'k2.php')):
  87.                        
  88.                    
  89.                         $query = 'SELECT m.* FROM #__k2_categories m WHERE published=1 AND trash = 0 ORDER BY parent, ordering';
  90.                         $db->setQuery( $query );
  91.                         $mitems = $db->loadObjectList();
  92.                         $children = array();
  93.                         if ($mitems){
  94.                             foreach ( $mitems as $v ){
  95.                                 if(K2_JVERSION != '16'){
  96.                                         $v->title = $v->name;
  97.                                         $v->parent_id = $v->parent;
  98.                                 }
  99.                                 $pt = $v->parent;
  100.                                 $list = @$children[$pt] ? $children[$pt] : array();
  101.                                 array_push( $list, $v );
  102.                                 $children[$pt] = $list;
  103.                             }
  104.                         }
  105.                         $list = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0 );
  106.                         $mitems = array();
  107.  
  108.                         foreach ( $list as $item ) {
  109.                             $item->treename = JString::str_ireplace('&#160;', '- ', $item->treename);
  110.                             $mitems[] = JHTML::_('select.option',  $item->id, '   '.$item->treename );
  111.                         }                      
  112.                     endif;
  113.                    
  114.                     if(version_compare(JVERSION,'1.6.0','ge')) {
  115.                         $fieldName = $name.'[]';
  116.                     }
  117.                     else {
  118.                         $fieldName = $control_name.'['.$name.'][]';
  119.                     }
  120.  
  121.                     $output= JHTML::_('select.genericlist',  $mitems, $fieldName, 'class="inputbox" style="width:90%;" multiple="multiple" size="10"', 'value', 'text', $value );
  122.                     return $output;
  123.             }
  124.     }
  125. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement