Skip to content

Commit e5c71cd

Browse files
Improvement: Make sure field controllers use component and area correctly.
1 parent 44f5410 commit e5c71cd

5 files changed

Lines changed: 81 additions & 17 deletions

File tree

classes/filter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ public static function return_filterjson(wunderbyte_table $table, string $cachek
129129

130130
$filterjson = ['categories' => []];
131131

132-
wbt_field_controller_info::instantiate_by_shortnames(array_keys($filtercolumns));
132+
wbt_field_controller_info::instantiate_by_shortnames(
133+
array_keys($filtercolumns),
134+
$table->customfieldcomponent,
135+
$table->customfieldarea
136+
);
137+
138+
// Pass customfield component and area through filtersettings so filter classes can use them.
139+
$filtersettings['_customfieldcomponent'] = $table->customfieldcomponent;
140+
$filtersettings['_customfieldarea'] = $table->customfieldarea;
133141

134142
foreach ($filtercolumns as $fckey => $values) {
135143
// Special treatment for key localizedname.

classes/filters/base.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ public static function add_to_categoryobject(array &$categoryobject, array $filt
305305

306306
if (isset($sortedarray[$valuekey]) && $sortedarray[$valuekey] === true) {
307307
// For custom fields, we get the actual string value from field controller.
308-
$fieldcontroller = wbt_field_controller_info::get_instance_by_shortname($fckey);
308+
$fieldcontroller = wbt_field_controller_info::get_instance_by_shortname(
309+
$fckey,
310+
$filtersettings['_customfieldcomponent'] ?? '',
311+
$filtersettings['_customfieldarea'] ?? ''
312+
);
309313
if (!empty($fieldcontroller)) {
310314
$cfstringvalueforvaluekey = $fieldcontroller->get_option_value_by_key($valuekey);
311315
if ($cfstringvalueforvaluekey == wbt_field_controller_info::WBTABLE_CUSTOMFIELD_VALUE_NOTFOUND) {

classes/filters/types/hierarchicalfilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ public static function add_to_categoryobject(array &$categoryobject, array $filt
135135
$categorycount = 0;
136136
foreach ($subcategoryarray as $valuekey => $valuevalue) {
137137
// For custom fields, we get the actual string value from field controller.
138-
$fieldcontroller = wbt_field_controller_info::get_instance_by_shortname($fckey);
138+
$fieldcontroller = wbt_field_controller_info::get_instance_by_shortname(
139+
$fckey,
140+
$filtersettings['_customfieldcomponent'] ?? '',
141+
$filtersettings['_customfieldarea'] ?? ''
142+
);
139143
if (!empty($fieldcontroller)) {
140144
$cfstringvalueforvaluekey = $fieldcontroller->get_option_value_by_key($valuekey);
141145
if ($cfstringvalueforvaluekey == wbt_field_controller_info::WBTABLE_CUSTOMFIELD_VALUE_NOTFOUND) {

classes/local/customfield/wbt_field_controller_info.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ public static function create(stdClass $record) {
6161
return new $class($record->id, $record);
6262
}
6363
// By default, we return the text controller.
64-
return new \local_wunderbyte_table\local\customfield\field\text\wbt_field_controller($record->id, $record);
64+
return new wbt_field_controller($record->id, $record);
6565
}
6666

6767
/**
6868
* Create instances of field controllers for all provided customfield shortnames.
6969
*
7070
* @param array $shortnames array of customfield shortnames
71+
* @param string $component optional component to filter by (e.g. 'mod_booking')
72+
* @param string $area optional area to filter by (e.g. 'booking')
7173
* @return void
7274
*/
73-
public static function instantiate_by_shortnames(array $shortnames) {
75+
public static function instantiate_by_shortnames(array $shortnames, string $component = '', string $area = '') {
7476
global $DB;
7577

7678
if (empty($shortnames)) {
@@ -79,15 +81,30 @@ public static function instantiate_by_shortnames(array $shortnames) {
7981

8082
[$inorequal, $inparams] = $DB->get_in_or_equal($shortnames, SQL_PARAMS_NAMED);
8183

84+
$where = "cf.shortname $inorequal";
85+
if (!empty($component)) {
86+
$inparams['cfcomponent'] = $component;
87+
$where .= ' AND cc.component = :cfcomponent';
88+
}
89+
if (!empty($area)) {
90+
$inparams['cfarea'] = $area;
91+
$where .= ' AND cc.area = :cfarea';
92+
}
93+
94+
// Order by cf.id DESC so the newest field per shortname is processed first.
8295
$sql = "SELECT cf.shortname AS filtercolumn, cf.*
83-
FROM {customfield_field} cf
84-
WHERE cf.shortname $inorequal";
96+
FROM {customfield_field} cf
97+
JOIN {customfield_category} cc ON cf.categoryid = cc.id
98+
WHERE $where
99+
ORDER BY cf.id DESC";
85100
$records = $DB->get_records_sql($sql, $inparams);
86101

87102
foreach ($records as $record) {
88-
// We only add the instance, if a field controller exists.
89-
if ($instance = self::create($record)) {
90-
self::$instances[$record->shortname] = $instance;
103+
// Only take the first (newest) instance per shortname.
104+
if (!isset(self::$instances[$record->shortname])) {
105+
if ($instance = self::create($record)) {
106+
self::$instances[$record->shortname] = $instance;
107+
}
91108
}
92109
}
93110
}
@@ -96,26 +113,43 @@ public static function instantiate_by_shortnames(array $shortnames) {
96113
* Get the field controller from the singleton $instances.
97114
*
98115
* @param string $shortname shortname of field controller customfield
116+
* @param string $component optional component to filter by (e.g. 'mod_booking')
117+
* @param string $area optional area to filter by (e.g. 'booking')
99118
* @return wbt_field_controller_base the field controller for the customfield record
100119
*/
101-
public static function get_instance_by_shortname(string $shortname) {
120+
public static function get_instance_by_shortname(string $shortname, string $component = '', string $area = '') {
102121
if (!empty(self::$instances[$shortname])) {
103122
return self::$instances[$shortname];
104123
} else {
105124
global $DB;
106-
$sql = "SELECT cf.shortname AS filtercolumn, cf.*
107-
FROM {customfield_field} cf
108-
WHERE cf.shortname = :shortname";
125+
126+
$where = 'cf.shortname = :shortname';
109127
$params = ['shortname' => $shortname];
110-
if ($record = $DB->get_record_sql($sql, $params)) {
111-
// We only add the instance, if a field controller exists.
128+
129+
if (!empty($component)) {
130+
$params['cfcomponent'] = $component;
131+
$where .= ' AND cc.component = :cfcomponent';
132+
}
133+
if (!empty($area)) {
134+
$params['cfarea'] = $area;
135+
$where .= ' AND cc.area = :cfarea';
136+
}
137+
138+
// Order by cf.id DESC and take the first record (newest) when not fully scoped.
139+
$sql = "SELECT cf.shortname AS filtercolumn, cf.*
140+
FROM {customfield_field} cf
141+
JOIN {customfield_category} cc ON cf.categoryid = cc.id
142+
WHERE $where
143+
ORDER BY cf.id DESC";
144+
145+
if ($record = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
112146
if ($instance = self::create($record)) {
113147
self::$instances[$record->shortname] = $instance;
114148
return $instance;
115149
}
116150
}
117151
}
118-
// Fallback: By default, we return text controller.
152+
// Fallback: By default, we return the text controller.
119153
return new wbt_field_controller();
120154
}
121155
}

classes/wunderbyte_table.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ class wunderbyte_table extends table_sql {
345345
*/
346346
public $filteronloadinactive = false;
347347

348+
/**
349+
* Optional customfield component for filtering (e.g. 'mod_booking').
350+
*
351+
* @var string
352+
*/
353+
public $customfieldcomponent = '';
354+
355+
/**
356+
* Optional customfield area for filtering (e.g. 'booking').
357+
*
358+
* @var string
359+
*/
360+
public $customfieldarea = '';
361+
348362
/**
349363
* Filter to be applied from URL
350364
*

0 commit comments

Comments
 (0)