1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage form
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * Generic field header, with drop down filters based on a SQL query
13 *
14 * @package FrameworkOnFramework
15 * @since 2.0
16 */
17 class FOFFormHeaderFieldsql extends FOFFormHeaderFieldselectable
18 {
19 /**
20 * Create objects for the options
21 *
22 * @return array The array of option objects
23 */
24 protected function getOptions()
25 {
26 $options = array();
27
28 // Initialize some field attributes.
29 $key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
30 $value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
31 $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
32 $query = (string) $this->element['query'];
33
34 // Get the database object.
35 $db = FOFPlatform::getInstance()->getDbo();
36
37 // Set the query and get the result list.
38 $db->setQuery($query);
39 $items = $db->loadObjectlist();
40
41 // Build the field options.
42 if (!empty($items))
43 {
44 foreach ($items as $item)
45 {
46 if ($translate == true)
47 {
48 $options[] = JHtml::_('select.option', $item->$key, JText::_($item->$value));
49 }
50 else
51 {
52 $options[] = JHtml::_('select.option', $item->$key, $item->$value);
53 }
54 }
55 }
56
57 // Merge any additional options in the XML definition.
58 $options = array_merge(parent::getOptions(), $options);
59
60 return $options;
61 }
62 }
63