fixes #2211 integrate new redesign for date posted filter

filter on date_posted custom values (specific years, months or days) along side last 7, 30 days, 3 and 6 months
following redesign by alice
This commit is contained in:
HWFord
2024-09-04 11:30:44 +02:00
committed by GitHub
parent b6789d4de9
commit b3151e0129
9 changed files with 589 additions and 92 deletions
+52 -7
View File
@@ -397,8 +397,9 @@ SELECT
//
// date_posted
//
if (!empty($search['fields']['date_posted']))
if (!empty($search['fields']['date_posted']['preset']))
{
$has_filters_filled = true;
$options = array(
@@ -407,17 +408,60 @@ SELECT
'30d' => '30 DAY',
'3m' => '3 MONTH',
'6m' => '6 MONTH',
'1y' => '1 YEAR',
);
if (isset($options[ $search['fields']['date_posted'] ]))
if (isset($options[ $search['fields']['date_posted']['preset'] ]) and 'custom' != $search['fields']['date_posted']['preset'])
{
$date_posted_clause = 'date_available > SUBDATE(NOW(), INTERVAL '.$options[ $search['fields']['date_posted'] ].')';
$date_posted_clause = 'date_available > SUBDATE(NOW(), INTERVAL '.$options[ $search['fields']['date_posted']['preset'] ].')';
}
elseif (preg_match('/^y(\d+)$/', $search['fields']['date_posted'], $matches))
elseif ('custom' == $search['fields']['date_posted']['preset'] and isset($search['fields']['date_posted']['custom']))
{
// that is for y2023 = all photos posted in 2023
$date_posted_clause = 'YEAR(date_available) = '.$matches[1];
$date_posted_subclauses = array();
$custom_dates = array_flip($search['fields']['date_posted']['custom']);
foreach (array_keys($custom_dates) as $custom_date)
{
// in real-life tests, we have determined "where year(date_available) = 2024" was
// far less (4 times less) than "where date_available between '2024-01-01 00:00:00' and '2024-12-31 23:59:59'"
// so let's find the begin/end for each custom date
// ... and also, no need to search for images of 2023-10-16 if 2023-10 is already requested
$begin = $end = null;
$ymd = substr($custom_date, 0, 1);
if ('y' == $ymd)
{
$year = substr($custom_date, 1);
$begin = $year.'-01-01 00:00:00';
$end = $year.'-12-31 23:59:59';
}
elseif ('m' == $ymd)
{
list($year, $month) = explode('-', substr($custom_date, 1));
if (!isset($custom_dates['y'.$year]))
{
$begin = $year.'-'.$month.'-01 00:00:00';
$end = $year.'-'.$month.'-'.cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year).' 23:59:59';
}
}
elseif ('d' == $ymd)
{
list($year, $month, $day) = explode('-', substr($custom_date, 1));
if (!isset($custom_dates['y'.$year]) and !isset($custom_dates['m'.$year.'-'.$month]))
{
$begin = $year.'-'.$month.'-'.$day.' 00:00:00';
$end = $year.'-'.$month.'-'.$day.' 23:59:59';
}
}
if (!empty($begin))
{
$date_posted_subclauses[] = 'date_available BETWEEN "'.$begin.'" AND "'.$end.'"';
}
}
$date_posted_clause = '('.implode(' OR ', prepend_append_array_items($date_posted_subclauses, '(', ')')).')';
}
$query = '
@@ -428,6 +472,7 @@ SELECT
WHERE '.$date_posted_clause.'
'.$forbidden.'
;';
$image_ids_for_filter['date_posted'] = query2array($query, null, 'id');
}
+60 -4
View File
@@ -859,14 +859,70 @@ function ws_images_filteredSearch_create($params, $service)
$search['fields']['added_by'] = $params['added_by'];
}
if (isset($params['date_posted']))
if (isset($params['date_posted_preset']))
{
if (!preg_match('/^(24h|7d|30d|3m|6m|y\d+|)$/', $params['date_posted']))
if (!preg_match('/^(24h|7d|30d|3m|6m|custom|)$/', $params['date_posted_preset']))
{
return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid parameter date_posted');
return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid parameter date_posted_preset');
}
$search['fields']['date_posted'] = $params['date_posted'];
@$search['fields']['date_posted']['preset'] = $params['date_posted_preset'];
if ('custom' == $search['fields']['date_posted']['preset'] and empty($params['date_posted_custom']))
{
return new PwgError(WS_ERR_INVALID_PARAM, 'date_posted_custom is missing');
}
}
if (isset($params['date_posted_custom']))
{
if (!isset($search['fields']['date_posted']['preset']) or $search['fields']['date_posted']['preset'] != 'custom')
{
return new PwgError(WS_ERR_INVALID_PARAM, 'date_posted_custom provided date_posted_preset is not custom');
}
foreach ($params['date_posted_custom'] as $date)
{
$correct_format = false;
$ymd = substr($date, 0, 1);
if ('y' == $ymd)
{
if (preg_match('/^y(\d{4})$/', $date, $matches))
{
$correct_format = true;
}
}
elseif ('m' == $ymd)
{
if (preg_match('/^m(\d{4}-\d{2})$/', $date, $matches))
{
list($year, $month) = explode('-', $matches[1]);
if ($month >= 1 and $month <= 12)
{
$correct_format = true;
}
}
}
elseif ('d' == $ymd)
{
if (preg_match('/^d(\d{4}-\d{2}-\d{2})$/', $date, $matches))
{
list($year, $month, $day) = explode('-', $matches[1]);
if ($month >= 1 and $month <= 12 and $day >= 1 and $day <= cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year))
{
$correct_format = true;
}
}
}
if (!$correct_format)
{
return new PwgError(WS_ERR_INVALID_PARAM, 'date_posted_custom, invalid option '.$date);
}
@$search['fields']['date_posted']['custom'][] = $date;
}
}
if (isset($params['ratios']))