1 <?php
2 /**
3 * @package Joomla.Libraries
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.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JFormHelper::loadFieldClass('groupedlist');
13
14 /**
15 * Supports a select grouped list of template styles
16 *
17 * @since 1.6
18 */
19 class JFormFieldTemplatestyle extends JFormFieldGroupedList
20 {
21 /**
22 * The form field type.
23 *
24 * @var string
25 * @since 1.6
26 */
27 public $type = 'TemplateStyle';
28
29 /**
30 * The client name.
31 *
32 * @var mixed
33 * @since 3.2
34 */
35 protected $clientName;
36
37 /**
38 * The template.
39 *
40 * @var mixed
41 * @since 3.2
42 */
43 protected $template;
44
45 /**
46 * Method to get certain otherwise inaccessible properties from the form field object.
47 *
48 * @param string $name The property name for which to the the value.
49 *
50 * @return mixed The property value or null.
51 *
52 * @since 3.2
53 */
54 public function __get($name)
55 {
56 switch ($name)
57 {
58 case 'clientName':
59 case 'template':
60 return $this->$name;
61 }
62
63 return parent::__get($name);
64 }
65
66 /**
67 * Method to set certain otherwise inaccessible properties of the form field object.
68 *
69 * @param string $name The property name for which to the the value.
70 * @param mixed $value The value of the property.
71 *
72 * @return void
73 *
74 * @since 3.2
75 */
76 public function __set($name, $value)
77 {
78 switch ($name)
79 {
80 case 'clientName':
81 case 'template':
82 $this->$name = (string) $value;
83 break;
84
85 default:
86 parent::__set($name, $value);
87 }
88 }
89
90 /**
91 * Method to attach a JForm object to the field.
92 *
93 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
94 * @param mixed $value The form field value to validate.
95 * @param string $group The field name group control value. This acts as an array container for the field.
96 * For example if the field has name="foo" and the group value is set to "bar" then the
97 * full field name would end up being "bar[foo]".
98 *
99 * @return boolean True on success.
100 *
101 * @see JFormField::setup()
102 * @since 3.2
103 */
104 public function setup(SimpleXMLElement $element, $value, $group = null)
105 {
106 $result = parent::setup($element, $value, $group);
107
108 if ($result === true)
109 {
110 // Get the clientName template.
111 $this->clientName = $this->element['client'] ? (string) $this->element['client'] : 'site';
112 $this->template = (string) $this->element['template'];
113 }
114
115 return $result;
116 }
117
118 /**
119 * Method to get the list of template style options grouped by template.
120 * Use the client attribute to specify a specific client.
121 * Use the template attribute to specify a specific template
122 *
123 * @return array The field option objects as a nested array in groups.
124 *
125 * @since 1.6
126 */
127 protected function getGroups()
128 {
129 $groups = array();
130 $lang = JFactory::getLanguage();
131
132 // Get the client and client_id.
133 $client = JApplicationHelper::getClientInfo($this->clientName, true);
134
135 // Get the template.
136 $template = $this->template;
137
138 // Get the database object and a new query object.
139 $db = JFactory::getDbo();
140 $query = $db->getQuery(true);
141
142 // Build the query.
143 $query->select('s.id, s.title, e.name as name, s.template')
144 ->from('#__template_styles as s')
145 ->where('s.client_id = ' . (int) $client->id)
146 ->order('template')
147 ->order('title');
148
149 if ($template)
150 {
151 $query->where('s.template = ' . $db->quote($template));
152 }
153
154 $query->join('LEFT', '#__extensions as e on e.element=s.template')
155 ->where('e.enabled = 1')
156 ->where($db->quoteName('e.type') . ' = ' . $db->quote('template'));
157
158 // Set the query and load the styles.
159 $db->setQuery($query);
160 $styles = $db->loadObjectList();
161
162 // Build the grouped list array.
163 if ($styles)
164 {
165 foreach ($styles as $style)
166 {
167 $template = $style->template;
168 $lang->load('tpl_' . $template . '.sys', $client->path, null, false, true)
169 || $lang->load('tpl_' . $template . '.sys', $client->path . '/templates/' . $template, null, false, true);
170 $name = JText::_($style->name);
171
172 // Initialize the group if necessary.
173 if (!isset($groups[$name]))
174 {
175 $groups[$name] = array();
176 }
177
178 $groups[$name][] = JHtml::_('select.option', $style->id, $style->title);
179 }
180 }
181
182 // Merge any additional groups in the XML definition.
183 $groups = array_merge(parent::getGroups(), $groups);
184
185 return $groups;
186 }
187 }
188