[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/form/fields/ -> sql.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Form
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  JFormHelper::loadFieldClass('list');
  13  
  14  /**
  15   * Supports an custom SQL select list
  16   *
  17   * @package     Joomla.Platform
  18   * @subpackage  Form
  19   * @since       11.1
  20   */
  21  class JFormFieldSQL extends JFormFieldList
  22  {
  23      /**
  24       * The form field type.
  25       *
  26       * @var    string
  27       * @since  11.1
  28       */
  29      public $type = 'SQL';
  30  
  31      /**
  32       * Method to get the custom field options.
  33       * Use the query attribute to supply a query to generate the list.
  34       *
  35       * @return  array  The field option objects.
  36       *
  37       * @since   11.1
  38       */
  39  	protected function getOptions()
  40      {
  41          // Initialize variables.
  42          $options = array();
  43  
  44          // Initialize some field attributes.
  45          $key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
  46          $value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
  47          $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
  48          $query = (string) $this->element['query'];
  49  
  50          // Get the database object.
  51          $db = JFactory::getDBO();
  52  
  53          // Set the query and get the result list.
  54          $db->setQuery($query);
  55          $items = $db->loadObjectlist();
  56  
  57          // Check for an error.
  58          if ($db->getErrorNum())
  59          {
  60              JError::raiseWarning(500, $db->getErrorMsg());
  61              return $options;
  62          }
  63  
  64          // Build the field options.
  65          if (!empty($items))
  66          {
  67              foreach ($items as $item)
  68              {
  69                  if ($translate == true)
  70                  {
  71                      $options[] = JHtml::_('select.option', $item->$key, JText::_($item->$value));
  72                  }
  73                  else
  74                  {
  75                      $options[] = JHtml::_('select.option', $item->$key, $item->$value);
  76                  }
  77              }
  78          }
  79  
  80          // Merge any additional options in the XML definition.
  81          $options = array_merge(parent::getOptions(), $options);
  82  
  83          return $options;
  84      }
  85  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1