diff --git a/libraries/joomla/form/fields/sql.php b/libraries/joomla/form/fields/sql.php index 38be7d7a96486..53f319b536713 100644 --- a/libraries/joomla/form/fields/sql.php +++ b/libraries/joomla/form/fields/sql.php @@ -129,15 +129,132 @@ public function setup(SimpleXMLElement $element, $value, $group = null) if ($return) { - $this->keyField = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value'; - $this->valueField = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name']; - $this->translate = $this->element['translate'] ? (string) $this->element['translate'] : false; - $this->query = (string) $this->element['query']; + // Check if its using the old way + $this->query = (string) $this->element['query']; + + if (empty($this->query)) + { + // Get the query from the form + $query = array(); + $defaults = array(); + + $query['select'] = (string) $this->element['sql_select']; + + $query['from'] = (string) $this->element['sql_from']; + + $query['join'] = isset($this->element['sql_join']) ? (string) $this->element['sql_join'] : ''; + + $query['where'] = isset($this->element['sql_where']) ? (string) $this->element['sql_where'] : ''; + + $query['group'] = isset($this->element['sql_group']) ? (string) $this->element['sql_group'] : ''; + + $query['order'] = (string) $this->element['sql_order']; + + // Get the filters + $filters = isset($this->element['sql_filter']) ? explode(",", $this->element['sql_filter']) : ''; + + // Get the default value for query if empty + if (is_array($filters)) + { + foreach ($filters as $key => $val) + { + $name = "sql_default_{$val}"; + $attrib = (string) $this->element[$name]; + + if (!empty($attrib)) + { + $defaults[$val] = $attrib; + } + } + } + + // Process the query + $this->query = $this->processQuery($query, $filters, $defaults); + } + + $this->keyField = isset($this->element['key_field']) ? (string) $this->element['key_field'] : 'value'; + $this->valueField = isset($this->element['value_field']) ? (string) $this->element['value_field'] : (string) $this->element['name']; + $this->translate = isset($this->element['translate']) ? (string) $this->element['translate'] : false; + $this->header = $this->element['header'] ? (string) $this->element['header'] : false; } return $return; } + /** + * Method to process the query from form. + * + * @param array $conditions The conditions from the form. + * @param string $filters The columns to filter. + * @param array $defaults The defaults value to set if condition is empty. + * + * @return $query The query object. + * + * @since 3.4 + */ + protected function processQuery($conditions, $filters, $defaults) + { + // Get the database object. + $db = JFactory::getDbo(); + + // Get the query object + $query = $db->getQuery(true); + + // Select fields + $query->select($conditions['select']); + + // From selected table + $query->from($conditions['from']); + + // Join over the groups + if (!empty($conditions['join'])) + { + $query->join('LEFT', $conditions['join']); + } + + // Where condition + if (!empty($conditions['where'])) + { + $query->where($conditions['where']); + } + + // Group by + if (!empty($conditions['group'])) + { + $query->group($conditions['group']); + } + + // Process the filters + if (is_array($filters)) + { + $html_filters = JFactory::getApplication()->getUserStateFromRequest($this->context . '.filter', 'filter', array(), 'array'); + + foreach ($filters as $k => $value) + { + if (!empty($html_filters[$value])) + { + $escape = $db->quote($db->escape($html_filters[$value]), false); + + $query->where("{$value} = {$escape}"); + } + elseif (!empty($defaults[$value])) + { + $escape = $db->quote($db->escape($defaults[$value]), false); + + $query->where("{$value} = {$escape}"); + } + } + } + + // Add order to query + if (!empty($conditions['order'])) + { + $query->order($conditions['order']); + } + + return $query; + } + /** * Method to get the custom field options. * Use the query attribute to supply a query to generate the list. @@ -153,6 +270,7 @@ protected function getOptions() // Initialize some field attributes. $key = $this->keyField; $value = $this->valueField; + $header = $this->header; // Get the database object. $db = JFactory::getDbo(); @@ -161,6 +279,13 @@ protected function getOptions() $db->setQuery($this->query); $items = $db->loadObjectlist(); + // Add header. + if (!empty($header)) + { + $header_title = JText::_($header); + $options[] = JHtml::_('select.option', '', $header_title); + } + // Build the field options. if (!empty($items)) {