<?php

/**
* @version 2.8.0
* @package Joomla 2.5
* @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');
include_once(JPATH_SITE."/administrator/components/com_dtregister/lib/dtcommon.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/lib/defines.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/event.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/category.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/config.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/helpers/dtregister.php");
// The class name must always be the same as the filename (in camel case)
class JFormFieldeventlist extends JFormField {
	
	protected $type = 'eventlist';
	
	public function getInput() { 
		$category = DtrModel::getInstance('category','DtregisterModel');
		$categoryTable = $category->table;
		$event = DtrModel::getInstance('event','DtregisterModel');
		$eventTable = $event->table;
		$user =& JFactory::getUser();
        $access_levels = $user->authorisedLevels();
       
$where = array();
     $rows = $eventTable->findAllByCategoryTree(null,implode(' and ',array_filter($where))," c.lft asc, b.ordering ASC ");
	$currCat=NULL;

	// Display an event on each row

    $k = 0;

	$prevCat = NUll;
	$parent = $categoryTable;
	$opts = array();
	$old_name = "";
	foreach($rows as $row) {
		$currCat = $row->category;
		if($currCat!=$prevCat || $currCat==""){

			if (isset($arrCategory[$row->category]) && $arrCategory[$row->category]){

				$parent->categoryName = "";
				
				$parent->load($row->category);
				$parent->categoryName = "";
                $subclass = '';
				if($parent->parent_id > 0){
                    $subclass = 'class="subcategory"';
					$parent->load($parent->parent_id);

					$parent->categoryName .=" >> ";
					
				}
				if($old_name != "") {
				   $opts[] = JHtml::_('select.optgroup',$old_name);
			    }
                $opts[] = JHtml::_('select.optgroup', $parent->categoryName.$arrCategory[$row->category]);
				$old_name = $parent->categoryName.$arrCategory[$row->category];
				
			}
            
		}
		$opts[] = JHtml::_('select.option', $row->slabId, $row->title);
        
		$prevCat = $currCat;
	}
	
	return JHtml::_('select.genericlist', $opts, $this->name,'',"value","text",$this->value);
	
	}
	
}
?>