Skip to content

Commit

Permalink
Fix catalog search query BT#16817
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Jun 8, 2020
1 parent bbad3eb commit b70f0ce
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
1 change: 1 addition & 0 deletions main/auth/courses.php
Expand Up @@ -264,6 +264,7 @@
$countCoursesInCategory = count($courses);
} else {
$values = $_REQUEST;

if ($allowExtraFields) {
$extraResult = $extraField->processExtraFieldSearch($values, $form, 'course', 'AND');
$conditions = $extraResult['condition'];
Expand Down
1 change: 1 addition & 0 deletions main/inc/lib/CoursesAndSessionsCatalog.class.php
Expand Up @@ -671,6 +671,7 @@ public static function searchAndSortCourses(
}
unset($course);
}

// do we have $course['groupKey']['subKey'] to sort on, such as 'point_info/users' ?
foreach ($sortKeys as $key) {
if (false !== strpos($key, '/')) {
Expand Down
117 changes: 58 additions & 59 deletions main/inc/lib/extra_field.lib.php
Expand Up @@ -2486,10 +2486,7 @@ public function processExtraFieldSearch($values, $form, $alias, $condition = 'OR
}
}

$extraFieldsAll = $this->get_all(
['visible_to_self = ? AND filter = ?' => [1, 1]],
'option_order'
);
$extraFieldsAll = $this->get_all(['visible_to_self = ? AND filter = ?' => [1, 1]], 'option_order');
$extraFieldsType = array_column($extraFieldsAll, 'field_type', 'variable');
$extraFields = array_column($extraFieldsAll, 'variable');
$filter = new stdClass();
Expand Down Expand Up @@ -2562,8 +2559,8 @@ public function processExtraFieldSearch($values, $form, $alias, $condition = 'OR
*/
public function getExtraFieldRules($filters, $stringToSearch = 'extra_', $condition = 'OR')
{
$extra_fields = [];
$condition_array = [];
$extraFields = [];
$conditionArray = [];

// Getting double select if exists
$double_select = [];
Expand Down Expand Up @@ -2591,7 +2588,7 @@ public function getExtraFieldRules($filters, $stringToSearch = 'extra_', $condit
// normal fields
$field = $rule->field;
if (isset($rule->data) && is_string($rule->data) && -1 != $rule->data) {
$condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
$conditionArray[] = $this->get_where_clause($field, $rule->op, $rule->data);
}
} else {
// Extra fields
Expand All @@ -2600,47 +2597,61 @@ public function getExtraFieldRules($filters, $stringToSearch = 'extra_', $condit
$original_field = str_replace($stringToSearch, '', $rule->field);
$field_option = $this->get_handler_field_info_by_field_variable($original_field);

if (self::FIELD_TYPE_DOUBLE_SELECT == $field_option['field_type']) {
if (isset($double_select[$rule->field])) {
$data = explode('#', $rule->data);
$rule->data = $data[1].'::'.$double_select[$rule->field];
} else {
// only was sent 1 select
if (is_string($rule->data)) {
switch ($field_option['field_type']) {
case self::FIELD_TYPE_DOUBLE_SELECT:
if (isset($double_select[$rule->field])) {
$data = explode('#', $rule->data);
$rule->data = $data[1];
$rule->data = $data[1].'::'.$double_select[$rule->field];
} else {
// only was sent 1 select
if (is_string($rule->data)) {
$data = explode('#', $rule->data);
$rule->data = $data[1];
}
}
}

if (!isset($rule->data)) {
$condition_array[] = ' ('
.$this->get_where_clause($rule->field, $rule->op, $rule->data)
.') ';
$extra_fields[] = ['field' => $rule->field, 'id' => $field_option['id']];
}
} else {
if (isset($rule->data)) {
if (isset($rule->data) && is_int($rule->data) && -1 == $rule->data) {
continue;
if (!isset($rule->data)) {
$conditionArray[] = ' ('
.$this->get_where_clause($rule->field, $rule->op, $rule->data)
.') ';
$extraFields[] = ['field' => $rule->field, 'id' => $field_option['id']];
}
/*var_dump($rule->data);
foreach ($rule->data as $option) {
}*/
$where = $this->get_where_clause($rule->field, $rule->op, $rule->data, 'OR');
$condition_array[] = " ( $where ) ";

$extra_fields[] = [
'field' => $rule->field,
'id' => $field_option['id'],
'data' => $rule->data,
];
}
break;
case self::FIELD_TYPE_TAG:
if (isset($rule->data)) {
if (is_int($rule->data) && -1 == $rule->data) {
break;
}
// Where will be injected in the parseConditions()
//$where = $this->get_where_clause($rule->field, $rule->op, $rule->data, 'OR');
//$conditionArray[] = " ( $where ) ";
$extraFields[] = [
'field' => $rule->field,
'id' => $field_option['id'],
'data' => $rule->data,
];
}
break;
default:
if (isset($rule->data)) {
if (is_int($rule->data) && -1 == $rule->data) {
break;
}
$where = $this->get_where_clause($rule->field, $rule->op, $rule->data, 'OR');
$conditionArray[] = " ( $where ) ";
$extraFields[] = [
'field' => $rule->field,
'id' => $field_option['id'],
'data' => $rule->data,
];
}
break;
}
} else {
$my_field = str_replace('_second', '', $rule->field);
$original_field = str_replace($stringToSearch, '', $my_field);
$field_option = $this->get_handler_field_info_by_field_variable($original_field);
$extra_fields[] = [
$extraFields[] = [
'field' => $rule->field,
'id' => $field_option['id'],
];
Expand All @@ -2649,17 +2660,7 @@ public function getExtraFieldRules($filters, $stringToSearch = 'extra_', $condit
}
}

/*var_dump(
[
'extra_fields' => $extra_fields,
'condition_array' => $condition_array,
]
);*/

return [
'extra_fields' => $extra_fields,
'condition_array' => $condition_array,
];
return ['extra_fields' => $extraFields, 'condition_array' => $conditionArray,];
}

/**
Expand Down Expand Up @@ -2737,7 +2738,7 @@ public function parseConditions($options, $alias = 's')
case self::FIELD_TYPE_DOUBLE_SELECT:
$inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
break;
case self::FIELD_TYPE_TAG:
case self::FIELD_TYPE_TAG:
// If using OR
// If using AND
$newCounter = 1;
Expand Down Expand Up @@ -2780,7 +2781,6 @@ public function parseConditions($options, $alias = 's')
$inject_joins = null;
$inject_where = null;
$where = null;

if (!empty($options['where'])) {
if (!empty($options['extra'])) {
// Removing double 1=1
Expand Down Expand Up @@ -2810,8 +2810,8 @@ public function parseConditions($options, $alias = 's')
break;
case self::FIELD_TYPE_TAG:
$newCounter = 1;
$whereTag = [];
if (isset($extra_info['data']) && !empty($extra_info['data'])) {
$whereTag = [];
foreach ($extra_info['data'] as $data) {
$data = Database::escape_string($data);
$key = $counter.$newCounter;
Expand All @@ -2827,10 +2827,9 @@ public function parseConditions($options, $alias = 's')
";
$newCounter++;
}
}

if (!empty($whereTag)) {
$options['where'] = ' ('.implode(' AND ', $whereTag).') ';
if (!empty($whereTag)) {
$options['where'] .= ' AND ('.implode(' AND ', $whereTag).') ';
}
}
break;
default:
Expand All @@ -2849,11 +2848,11 @@ public function parseConditions($options, $alias = 's')
$where .= ' AND '.$options['where'];
}

$order = null;
$order = '';
if (!empty($options['order'])) {
$order = ' ORDER BY '.$options['order'];
}
$limit = null;
$limit = '';
if (!empty($options['limit'])) {
$limit = ' LIMIT '.$options['limit'];
}
Expand Down

0 comments on commit b70f0ce

Please sign in to comment.