<?php

/**
* @version 2.7.7
* @package Joomla 1.6
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
 
jimport('joomla.form.formfield');
 
// The class name must always be the same as the filename (in camel case)
class JFormFieldcatlist extends JFormField {
 
	//The field class must know its own type through the variable $type.
	protected $type = 'catlist';
 
	//public function getLabel() {
		// code that returns HTML that will be shown as the label
	//}
 
	public function getInput() {
		
		$db = &JFactory::getDBO();

	    $class = "";

		$query = "SELECT  categoryId , categoryName , parent_id , level 

    FROM #__dtregister_categories c where parent_id > 0  order by  lft asc";

		$db->setQuery($query);

		$rows1 = $db->loadObjectList();
       
		$result = '<select name="jform[request]['.$this->name.']" id="jform_request_cat" class="'.$class.'">';
        $value = $this->value;
		if(is_array($value)){

		    $all_cats = in_array( -1, $value )?"selected":'';

		}elseif($value){

		    $all_cats = ( -1 == $value )?"selected":'';

		}else{

		    $all_cats = "";

		}

        $options=array();
        $children = array();
        foreach ($rows1 as $v )
		{
			$pt = $v->parent_id;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}
		
	  $options=array();
		$options[]=JHTML::_('select.option',"","Select Category");
		
		foreach($rows1 as $pcategory){
		   $options[]=JHTML::_('select.option',$pcategory->categoryId,str_repeat("-",$pcategory->level - 1).$pcategory->categoryName);
		  
		}
		return  JHTML::_('select.genericlist', $options, $this->name,'',"value","text",$value);
		
	}
}
?>