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 /**
13 * Ordering field.
14 *
15 * @since 3.2
16 */
17 class JFormFieldOrdering extends JFormField
18 {
19 /**
20 * The form field type.
21 *
22 * @var string
23 * @since 3.2
24 */
25 protected $type = 'Ordering';
26
27 /**
28 * The form field content type.
29 *
30 * @var string
31 * @since 3.2
32 */
33 protected $contentType;
34
35 /**
36 * Method to get certain otherwise inaccessible properties from the form field object.
37 *
38 * @param string $name The property name for which to the the value.
39 *
40 * @return mixed The property value or null.
41 *
42 * @since 3.2
43 */
44 public function __get($name)
45 {
46 switch ($name)
47 {
48 case 'contentType':
49 return $this->contentType;
50 }
51
52 return parent::__get($name);
53 }
54
55 /**
56 * Method to set certain otherwise inaccessible properties of the form field object.
57 *
58 * @param string $name The property name for which to the the value.
59 * @param mixed $value The value of the property.
60 *
61 * @return void
62 *
63 * @since 3.2
64 */
65 public function __set($name, $value)
66 {
67 switch ($name)
68 {
69 case 'contentType':
70 $this->contentType = (string) $value;
71 break;
72
73 default:
74 parent::__set($name, $value);
75 }
76 }
77
78 /**
79 * Method to attach a JForm object to the field.
80 *
81 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
82 * @param mixed $value The form field value to validate.
83 * @param string $group The field name group control value. This acts as an array container for the field.
84 * For example if the field has name="foo" and the group value is set to "bar" then the
85 * full field name would end up being "bar[foo]".
86 *
87 * @return boolean True on success.
88 *
89 * @see JFormField::setup()
90 * @since 3.2
91 */
92 public function setup(SimpleXMLElement $element, $value, $group = null)
93 {
94 $result = parent::setup($element, $value, $group);
95
96 if ($result === true)
97 {
98 $this->contentType = (string) $this->element['content_type'];
99 }
100
101 return $result;
102 }
103
104 /**
105 * Method to get the field input markup.
106 *
107 * @return string The field input markup.
108 *
109 * @since 3.2
110 */
111 protected function getInput()
112 {
113 $html = array();
114 $attr = '';
115
116 // Initialize some field attributes.
117 $attr .= !empty($this->class) ? ' class="' . $this->class . '"' : '';
118 $attr .= $this->disabled ? ' disabled' : '';
119 $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
120
121 // Initialize JavaScript field attributes.
122 $attr .= !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
123
124 $itemId = (int) $this->getItemId();
125
126 $query = $this->getQuery();
127
128 // Create a read-only list (no name) with a hidden input to store the value.
129 if ($this->readonly)
130 {
131 $html[] = JHtml::_('list.ordering', '', $query, trim($attr), $this->value, $itemId ? 0 : 1);
132 $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
133 }
134 else
135 {
136 // Create a regular list.
137 $html[] = JHtml::_('list.ordering', $this->name, $query, trim($attr), $this->value, $itemId ? 0 : 1);
138 }
139
140 return implode($html);
141 }
142
143 /**
144 * Builds the query for the ordering list.
145 *
146 * @return JDatabaseQuery The query for the ordering form field
147 *
148 * @since 3.2
149 */
150 protected function getQuery()
151 {
152 $categoryId = (int) $this->form->getValue('catid');
153 $ucmType = new JUcmType;
154 $ucmRow = $ucmType->getType($ucmType->getTypeId($this->contentType));
155 $ucmMapCommon = json_decode($ucmRow->field_mappings)->common;
156
157 if (is_object($ucmMapCommon))
158 {
159 $ordering = $ucmMapCommon->core_ordering;
160 $title = $ucmMapCommon->core_title;
161 }
162 elseif (is_array($ucmMapCommon))
163 {
164 $ordering = $ucmMapCommon[0]->core_ordering;
165 $title = $ucmMapCommon[0]->core_title;
166 }
167
168 $db = JFactory::getDbo();
169 $query = $db->getQuery(true);
170 $query->select(array($db->quoteName($ordering, 'value'), $db->quoteName($title, 'text')))
171 ->from($db->quoteName(json_decode($ucmRow->table)->special->dbtable))
172 ->where($db->quoteName('catid') . ' = ' . (int) $categoryId)
173 ->order('ordering');
174
175 return $query;
176 }
177
178 /**
179 * Retrieves the current Item's Id.
180 *
181 * @return integer The current item ID
182 *
183 * @since 3.2
184 */
185 protected function getItemId()
186 {
187 return (int) $this->form->getValue('id');
188 }
189 }
190