From 88e539f51daaf8d443409cd09a3d2025a1b99bec Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 28 Oct 2021 16:07:25 -0600 Subject: [PATCH] Calendar: Enable filtering by customfield in list view --- calendar/inc/class.calendar_so.inc.php | 38 +++++++++++++++++++--- calendar/inc/class.calendar_uilist.inc.php | 8 +++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/calendar/inc/class.calendar_so.inc.php b/calendar/inc/class.calendar_so.inc.php index 386c64cd21..c97bc7573a 100644 --- a/calendar/inc/class.calendar_so.inc.php +++ b/calendar/inc/class.calendar_so.inc.php @@ -785,7 +785,7 @@ class calendar_so } if (!empty($params['sql_filter'])) { - if (is_string($params['sql_filter'])) + if(is_string($params['sql_filter'])) { $where[] = $params['sql_filter']; } @@ -794,15 +794,45 @@ class calendar_so $where = array_merge($where, $params['sql_filter']); } } + if(array_filter($where, fn($key) => str_contains($key, '#'), ARRAY_FILTER_USE_KEY)) + { + $custom_fields = Api\Storage\Customfields::get('calendar'); + foreach($where as $col => $data) + { + if($col[0] == '#' && $data) + { + unset($where[$col]); + $filtermethod = " $this->cal_table.cal_id IN (SELECT DISTINCT cal_id FROM $this->extra_table WHERE "; + if($custom_fields[substr($col, 1)]['type'] == 'select' && $custom_fields[substr($col, 1)]['rows'] > 1) + { + // Multi-select - any entry with the filter value selected matches + $filtermethod .= $this->db->expression($this->extra_table, array( + 'cal_extra_name' => substr($col, 1), + $this->db->concat("','", 'cal_extra_value', "','") . ' ' . $this->db->capabilities[Api\Db::CAPABILITY_CASE_INSENSITIV_LIKE] . ' ' . $this->db->quote('%,' . $data . ',%'), + )) . ')'; + } + else + { + $filtermethod .= $this->db->expression($this->extra_table, array( + 'cal_extra_name' => substr($col, 1), + 'cal_extra_value' => $data, + )) . ')'; + } + $where[] = $filtermethod; + } + } + } + $useUnionQuery = $this->db->capabilities['distinct_on_text'] && $this->db->capabilities['union']; - if ($users) + + if($users) { $users_by_type = array(); foreach((array)$users as $user) { - if (is_numeric($user)) + if(is_numeric($user)) { - $users_by_type['u'][] = (int) $user; + $users_by_type['u'][] = (int)$user; } else { diff --git a/calendar/inc/class.calendar_uilist.inc.php b/calendar/inc/class.calendar_uilist.inc.php index 2379c1c7c8..a7788c1914 100644 --- a/calendar/inc/class.calendar_uilist.inc.php +++ b/calendar/inc/class.calendar_uilist.inc.php @@ -408,14 +408,18 @@ class calendar_uilist extends calendar_ui $col_filter = array(); foreach($params['col_filter'] as $name => $val) { - if (!in_array($name, array('participant','row_id')) && (string)$val !== '') + if(!in_array($name, array('participant', 'row_id')) && (string)$val !== '') { $col_filter[$name] = $val; } - else if ( $name == 'row_id' && (string)$val !== '') + elseif($name == 'row_id' && (string)$val !== '') { $col_filter['cal_id'] = $val; } + if($name[0] == '#') + { + $search_params['cfs'][] = $name; + } } // Videocalls if(array_key_exists('include_videocalls',$params['col_filter']))