<?php
/*
 * @package		Joomla.Framework
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 *
 * @component Phoca Component
 * @copyright Copyright (C) Jan Pavelka www.phoca.cz
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License version 2 or later;
 */
defined('_JEXEC') or die();
jimport( 'joomla.filesystem.file' );
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
class JFormFieldPhocaGalleryCategory extends JFormField
{
	protected $type 		= 'PhocaGallery';

	protected function getInput() {
	
		if (! class_exists('PhocaGalleryLoader')) {
			$file = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocagallery'.DS.'libraries'.DS.'loader.php';
			if (JFile::exists($file)) {
				require_once( $file);
				phocagalleryimport('phocagallery.html.category');
			} else {
				$tree = array();
				array_unshift($tree, JHTML::_('select.option', '', '- '.JText::_('TPL_PHOCA_BILD3_SELECT_CATEGORY').' -', 'value', 'text'));
				return JHTML::_('select.genericlist',  $tree,  $this->name, 'class="inputbox"', 'value', 'text', $this->value, $this->id );
			}
		} else {
			phocagalleryimport('phocagallery.html.category');
		}
		
		$db = JFactory::getDBO();

       //build the list of categories
		$query = 'SELECT a.title AS text, a.id AS value, a.parent_id as parentid'
		. ' FROM #__phocagallery_categories AS a'
		. ' WHERE a.published = 1'
		. ' ORDER BY a.ordering';
		$db->setQuery( $query );
		$phocagallerys = $db->loadObjectList();
	
		// TODO - check for other views than category edit
		$view 	= JRequest::getVar( 'view' );
		$catId	= -1;
		if ($view == 'phocagalleryc') {
			$id 	= $this->form->getValue('id'); // id of current category
			if ((int)$id > 0) {
				$catId = $id;
			}
		}
		
		$tree = array();
		$text = '';
		if ( class_exists('PhocaGalleryCategory')) {
			$tree = PhocaGalleryCategory::CategoryTreeOption($phocagallerys, $tree, 0, $text, $catId);
		}
		array_unshift($tree, JHTML::_('select.option', '', '- '.JText::_('TPL_PHOCA_BILD3_SELECT_CATEGORY').' -', 'value', 'text'));
		
		return JHTML::_('select.genericlist',  $tree,  $this->name, 'class="inputbox"', 'value', 'text', $this->value, $this->id );
	}
}
?>