<?php
/*
 * ARI Smart Content
 *
 * @package		ARI Smart Content
 * @version		1.0.0
 * @author		ARI Soft
 * @copyright	Copyright (c) 2009 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

defined('_JEXEC') or die ('Restricted access');

jimport('joomla.html.html');
jimport('joomla.form.formfield');

require_once dirname(__FILE__) . '/../kernel/class.AriKernel.php';

require_once JPATH_ADMINISTRATOR . '/components/com_arismartcontent/defines.php';

class JFormFieldIntegrationplugin extends JFormField
{
	protected $type = 'Integrationplugin';

	function getInput()
	{	
		return $this->fetchElement($this->element['name'], $this->value, $this->element, $this->name);
	}

	function fetchElement($name, $value, &$node, $control_name)
	{
		$type = (string)$node['plugintype'];
		$lblNone = (string)$node['nonelabel'];
		$ctrlId = str_replace(array('[', ']'), array('_', ''), $control_name);
		$plugins = $this->getPlugins($type, $lblNone);
		
		return JHTML::_(
			'select.genericlist', 
			$plugins, 
			$control_name, 
			'class="inputbox"', 
			'element', 
			'name', 
			$value,
			$ctrlId
		);
	}

	function getPlugins($type, $noneLabel = null)
	{
		$db = JFactory::getDBO();

		$db->setQuery(
			sprintf(
				'SELECT name,element FROM #__extensions WHERE type = "plugin" AND folder = %1$s %2$s ORDER BY name',
				$db->Quote(ARISMARTCONTENT_INTEGRATIONPLUGIN_TYPE),
				$type ? ' AND element LIKE CONCAT("%\_",' . $db->Quote($type) . ')' : ''
			)
		);
		$plugins = $db->loadObjectList();

		if ($noneLabel)
		{
			if (!is_array($plugins))
				$plugins = array();
				
			$noneItem = new stdClass();
			$noneItem->name = JText::_($noneLabel);
			$noneItem->element = '';
			
			array_unshift($plugins, $noneItem);
		}
		
		return $plugins;
	}
}