<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) 2010 Blue Constant Media LTD, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://design-joomla.eu
 * @author email contact@design-joomla.eu
 */

use Joomla\Registry\Registry;
use Joomla\CMS\Layout\FileLayout;

defined('JPATH_BASE') or die;

JFormHelper::loadFieldClass('list');

class JFormFieldDJItems extends Joomla\CMS\Form\Field\ListField
{
	public $type = 'DJItems';
	
	protected $layout = 'com_djcatalog2.form.field.fancycombo-items';
	
	
	/**
	 * Flag to work with nested tag field
	 *
	 * @var    boolean
	 * @since  3.1
	 */
	public $isNested = null;
	
	/**
	 * com_tags parameters
	 *
	 * @var    JRegistry
	 * @since  3.1
	 */
	protected $comParams = null;
	
	/**
	 * Constructor
	 *
	 * @since  3.1
	 */
	public function __construct()
	{
		parent::__construct();
		
		// Load com_tags config
		$this->comParams = JComponentHelper::getParams('com_djcatalog2');
	}
	
	protected function getLayoutPaths()
	{
		$renderer = new FileLayout('default');
		
		$paths =  $renderer->getDefaultIncludePaths();
		$paths[] = JPATH_ROOT.'/administrator/components/com_djcatalog2/layouts';
		
		return $paths;
	}
	
	protected function getOptions()
	{
		$options = array();
		$user = JFactory::getUser();
		
		$method = isset($this->element['ajax']) ? 'ajax' : 'static';
		
		$db = JFactory::getDbo();
		if ($method == 'ajax') {
			$values = $this->value;
			if (!is_array($values) && !empty($values))
			{
				if (is_string($values))
				{
					$values = explode(',', $values);
				}
			}
			
			if (is_array($values) && count($values)) {
				$db->setQuery('select id as value, concat(name, " ", " [", id, "]") as text from #__djc2_items WHERE id IN ('.implode(',', $values).') order by name asc');
				$options = $db->loadObjectList();
			}
			
		} else {
			$db->setQuery('select id as value, concat(name, " ", " [", id, "]") as text from #__djc2_items order by name asc');
			$options = $db->loadObjectList();
		}
		
		return $options;
	}
}
