1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * Form Field class for the Joomla Framework.
16 *
17 * @since 11.4
18 */
19 class JFormFieldPlugins extends JFormFieldList
20 {
21 /**
22 * The field type.
23 *
24 * @var string
25 * @since 11.4
26 */
27 protected $type = 'Plugins';
28
29 /**
30 * The path to folder for plugins.
31 *
32 * @var string
33 * @since 3.2
34 */
35 protected $folder;
36
37 /**
38 * Method to get certain otherwise inaccessible properties from the form field object.
39 *
40 * @param string $name The property name for which to the the value.
41 *
42 * @return mixed The property value or null.
43 *
44 * @since 3.2
45 */
46 public function __get($name)
47 {
48 switch ($name)
49 {
50 case 'folder':
51 return $this->folder;
52 }
53
54 return parent::__get($name);
55 }
56
57 /**
58 * Method to set certain otherwise inaccessible properties of the form field object.
59 *
60 * @param string $name The property name for which to the the value.
61 * @param mixed $value The value of the property.
62 *
63 * @return void
64 *
65 * @since 3.2
66 */
67 public function __set($name, $value)
68 {
69 switch ($name)
70 {
71 case 'folder':
72 $this->folder = (string) $value;
73 break;
74
75 default:
76 parent::__set($name, $value);
77 }
78 }
79
80 /**
81 * Method to attach a JForm object to the field.
82 *
83 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
84 * @param mixed $value The form field value to validate.
85 * @param string $group The field name group control value. This acts as an array container for the field.
86 * For example if the field has name="foo" and the group value is set to "bar" then the
87 * full field name would end up being "bar[foo]".
88 *
89 * @return boolean True on success.
90 *
91 * @see JFormField::setup()
92 * @since 3.2
93 */
94 public function setup(SimpleXMLElement $element, $value, $group = null)
95 {
96 $return = parent::setup($element, $value, $group);
97
98 if ($return)
99 {
100 $this->folder = (string) $this->element['folder'];
101 }
102
103 return $return;
104 }
105
106 /**
107 * Method to get a list of options for a list input.
108 *
109 * @return array An array of JHtml options.
110 *
111 * @since 11.4
112 */
113 protected function getOptions()
114 {
115 $folder = $this->folder;
116
117 if (!empty($folder))
118 {
119 // Get list of plugins
120 $db = JFactory::getDbo();
121 $query = $db->getQuery(true)
122 ->select('element AS value, name AS text')
123 ->from('#__extensions')
124 ->where('folder = ' . $db->quote($folder))
125 ->where('enabled = 1')
126 ->order('ordering, name');
127 $db->setQuery($query);
128
129 $options = $db->loadObjectList();
130
131 $lang = JFactory::getLanguage();
132
133 foreach ($options as $i => $item)
134 {
135 $source = JPATH_PLUGINS . '/' . $folder . '/' . $item->value;
136 $extension = 'plg_' . $folder . '_' . $item->value;
137 $lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, true)
138 || $lang->load($extension . '.sys', $source, null, false, true);
139 $options[$i]->text = JText::_($item->text);
140 }
141 }
142 else
143 {
144 JLog::add(JText::_('JFRAMEWORK_FORM_FIELDS_PLUGINS_ERROR_FOLDER_EMPTY'), JLog::WARNING, 'jerror');
145 }
146
147 // Merge any additional options in the XML definition.
148 $options = array_merge(parent::getOptions(), $options);
149
150 return $options;
151 }
152 }
153