faysalmirmd

K2 Component category Selection Custom Field- Joomla

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