joomla_test/libraries/joomla/form/fields/sql.php
2020-01-02 22:20:31 +07:00

78 lines
1.8 KiB
PHP

<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('list');
/**
* Supports an custom SQL select list
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
class JFormFieldSQL extends JFormFieldList
{
/**
* The form field type.
*
* @var string
* @since 11.1
*/
public $type = 'SQL';
/**
* Method to get the custom field options.
* Use the query attribute to supply a query to generate the list.
*
* @return array The field option objects.
*
* @since 11.1
*/
protected function getOptions()
{
$options = array();
// Initialize some field attributes.
$key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
$value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
$translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
$query = (string) $this->element['query'];
// Get the database object.
$db = JFactory::getDbo();
// Set the query and get the result list.
$db->setQuery($query);
$items = $db->loadObjectlist();
// Build the field options.
if (!empty($items))
{
foreach ($items as $item)
{
if ($translate == true)
{
$options[] = JHtml::_('select.option', $item->$key, JText::_($item->$value));
}
else
{
$options[] = JHtml::_('select.option', $item->$key, $item->$value);
}
}
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
return $options;
}
}