Skip to content

Commit

Permalink
Improvement: Add possibility to pre-filter shortcode for any customfi…
Browse files Browse the repository at this point in the history
…eld (#752).
  • Loading branch information
ibernhardf committed Dec 19, 2024
1 parent f5c3a09 commit 4db1bea
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion classes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ public static function courselist($shortcode, $args, $content, $env, $next) {

$wherearray['bookingid'] = (int)$booking->id;

// Additional where condition for both card and list views.
$additionalwhere = self::set_wherearray_from_arguments($args, $wherearray) ?? '';

list($fields, $from, $where, $params, $filter) =
booking::get_options_filter_sql(0, 0, '', null, null, [], $wherearray);
booking::get_options_filter_sql(0, 0, '', null, null, [], $wherearray, null,
[MOD_BOOKING_STATUSPARAM_BOOKED], $additionalwhere);

// By default, we do not show booking options that lie in the past.
// Shortcode arg values get transmitted as string, so also check for "false" and "0".
Expand Down Expand Up @@ -833,4 +837,63 @@ private static function apply_bookinginstance_filter(&$table) {
$instancefilter->add_options($filterarray);
$table->add_filter($instancefilter);
}

/**
* Modify there wherearray via arguments.
*
* @param array $args
*
* @return string
*
*/
private static function set_wherearray_from_arguments(array &$args, &$wherearray) {

global $DB;

$customfields = booking_handler::get_customfields();
// Set given customfields (shortnames) as arguments.
$fields = [];
$additonalwhere = '';
if (!empty($customfields) && !empty($args)) {
foreach ($args as $key => $value) {
foreach ($customfields as $customfield) {
if ($customfield->shortname == $key) {
$configdata = json_decode($customfield->configdata ?? '[]');

if (!empty($configdata->multiselect)) {
if (!empty($additonalwhere)) {
$additonalwhere .= " AND ";
}

$values = explode(',', $value);

if (!empty($values)) {
$additonalwhere .= " ( ";
}

foreach ($values as $vkey => $vvalue) {

$additonalwhere .= $vkey > 0 ? ' OR ' : '';
$vvalue = "'%$vvalue%'";
$additonalwhere .= " $key LIKE $vvalue ";
}

if (!empty($values)) {
$additonalwhere .= " ) ";
}

} else {
$argument = strip_tags($value);
$argument = trim($argument);
$wherearray[$key] = $argument;
}

break;
}
}
}
}

return $additonalwhere;
}
}

0 comments on commit 4db1bea

Please sign in to comment.