Source for file sql.php

Documentation is available at sql.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Form
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 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.  
  13. /**
  14.  * Supports an custom SQL select list
  15.  *
  16.  * @package     Joomla.Platform
  17.  * @subpackage  Form
  18.  * @since       11.1
  19.  */
  20. class JFormFieldSQL extends JFormFieldList
  21. {
  22.     /**
  23.      * The form field type.
  24.      *
  25.      * @var    string 
  26.      * @since  11.1
  27.      */
  28.     public $type = 'SQL';
  29.  
  30.     /**
  31.      * The keyField.
  32.      *
  33.      * @var    string 
  34.      * @since  3.2
  35.      */
  36.     protected $keyField;
  37.  
  38.     /**
  39.      * The valueField.
  40.      *
  41.      * @var    string 
  42.      * @since  3.2
  43.      */
  44.     protected $valueField;
  45.  
  46.     /**
  47.      * The translate.
  48.      *
  49.      * @var    boolean 
  50.      * @since  3.2
  51.      */
  52.     protected $translate = false;
  53.  
  54.     /**
  55.      * The query.
  56.      *
  57.      * @var    string 
  58.      * @since  3.2
  59.      */
  60.     protected $query;
  61.  
  62.     /**
  63.      * Method to get certain otherwise inaccessible properties from the form field object.
  64.      *
  65.      * @param   string  $name  The property name for which to the the value.
  66.      *
  67.      * @return  mixed  The property value or null.
  68.      *
  69.      * @since   3.2
  70.      */
  71.     public function __get($name)
  72.     {
  73.         switch ($name)
  74.         {
  75.             case 'keyField':
  76.             case 'valueField':
  77.             case 'translate':
  78.             case 'query':
  79.                 return $this->$name;
  80.         }
  81.  
  82.         return parent::__get($name);
  83.     }
  84.  
  85.     /**
  86.      * Method to set certain otherwise inaccessible properties of the form field object.
  87.      *
  88.      * @param   string  $name   The property name for which to the the value.
  89.      * @param   mixed   $value  The value of the property.
  90.      *
  91.      * @return  void 
  92.      *
  93.      * @since   3.2
  94.      */
  95.     public function __set($name$value)
  96.     {
  97.         switch ($name)
  98.         {
  99.             case 'keyField':
  100.             case 'valueField':
  101.             case 'translate':
  102.             case 'query':
  103.                 $this->$name = (string) $value;
  104.                 break;
  105.  
  106.             default:
  107.                 parent::__set($name$value);
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Method to attach a JForm object to the field.
  113.      *
  114.      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
  115.      * @param   mixed             $value    The form field value to validate.
  116.      * @param   string            $group    The field name group control value. This acts as as an array container for the field.
  117.      *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  118.      *                                       full field name would end up being "bar[foo]".
  119.      *
  120.      * @return  boolean  True on success.
  121.      *
  122.      * @see     JFormField::setup()
  123.      * @since   3.2
  124.      */
  125.     public function setup(SimpleXMLElement $element$value$group null)
  126.     {
  127.         $return parent::setup($element$value$group);
  128.  
  129.         if ($return)
  130.         {
  131.             $this->keyField   = $this->element['key_field'? (string) $this->element['key_field''value';
  132.             $this->valueField = $this->element['value_field'? (string) $this->element['value_field': (string) $this->element['name'];
  133.             $this->translate  = $this->element['translate'? (string) $this->element['translate'false;
  134.             $this->query      = (string) $this->element['query'];
  135.         }
  136.  
  137.         return $return;
  138.     }
  139.  
  140.     /**
  141.      * Method to get the custom field options.
  142.      * Use the query attribute to supply a query to generate the list.
  143.      *
  144.      * @return  array  The field option objects.
  145.      *
  146.      * @since   11.1
  147.      */
  148.     protected function getOptions()
  149.     {
  150.         $options array();
  151.  
  152.         // Initialize some field attributes.
  153.         $key   $this->keyField;
  154.         $value $this->valueField;
  155.  
  156.         // Get the database object.
  157.         $db JFactory::getDbo();
  158.  
  159.         // Set the query and get the result list.
  160.         $db->setQuery($this->query);
  161.         $items $db->loadObjectlist();
  162.  
  163.         // Build the field options.
  164.         if (!empty($items))
  165.         {
  166.             foreach ($items as $item)
  167.             {
  168.                 if ($this->translate == true)
  169.                 {
  170.                     $options[JHtml::_('select.option'$item->$keyJText::_($item->$value));
  171.                 }
  172.                 else
  173.                 {
  174.                     $options[JHtml::_('select.option'$item->$key$item->$value);
  175.                 }
  176.             }
  177.         }
  178.  
  179.         // Merge any additional options in the XML definition.
  180.         $options array_merge(parent::getOptions()$options);
  181.  
  182.         return $options;
  183.     }
  184. }

Documentation generated on Tue, 19 Nov 2013 15:14:03 +0100 by phpDocumentor 1.4.3