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