fixes #2227 add creation date filter

Same logic as date posted
This commit is contained in:
HWFord
2024-09-17 16:50:38 +02:00
parent 2783a8b848
commit 2d2baa7052
9 changed files with 635 additions and 27 deletions
+82
View File
@@ -476,6 +476,88 @@ SELECT
$image_ids_for_filter['date_posted'] = query2array($query, null, 'id');
}
//
// date_created
//
if (!empty($search['fields']['date_created']['preset']))
{
$has_filters_filled = true;
$options = array(
'24h' => '24 HOUR',
'7d' => '7 DAY',
'30d' => '30 DAY',
'3m' => '3 MONTH',
'6m' => '6 MONTH',
);
if (isset($options[ $search['fields']['date_created']['preset'] ]) and 'custom' != $search['fields']['date_created']['preset'])
{
$date_created_clause = 'date_creation > SUBDATE(NOW(), INTERVAL '.$options[ $search['fields']['date_created']['preset'] ].')';
}
elseif ('custom' == $search['fields']['date_created']['preset'] and isset($search['fields']['date_created']['custom']))
{
$date_created_subclauses = array();
$custom_dates = array_flip($search['fields']['date_created']['custom']);
foreach (array_keys($custom_dates) as $custom_date)
{
// in real-life tests, we have determined "where year(date_creation) = 2024" was
// far less (4 times less) than "where date_creation 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_created_subclauses[] = 'date_creation BETWEEN "'.$begin.'" AND "'.$end.'"';
}
}
$date_created_clause = '('.implode(' OR ', prepend_append_array_items($date_created_subclauses, '(', ')')).')';
}
$query = '
SELECT
DISTINCT(id)
FROM '.IMAGES_TABLE.' AS i
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
WHERE '.$date_created_clause.'
'.$forbidden.'
;';
$image_ids_for_filter['date_created'] = query2array($query, null, 'id');
}
//
// ratios
//
+106
View File
@@ -222,6 +222,112 @@ SELECT
$template->assign('DATE_POSTED', $counters);
}
if (isset($my_search['fields']['date_created']))
{
$filter_clause = get_clause_for_filter('date_created');
$cache_key = $persistent_cache->make_key('filter_date_created'.$user['id'].$user['cache_update_time']);
$set_persistent_cache = !preg_match('/^image_id IN/', $filter_clause) and !$persistent_cache->get($cache_key, $date_created);
if (!isset($date_created))
{
$query = '
SELECT
SUBDATE(NOW(), INTERVAL 24 HOUR) AS 24h,
SUBDATE(NOW(), INTERVAL 7 DAY) AS 7d,
SUBDATE(NOW(), INTERVAL 30 DAY) AS 30d,
SUBDATE(NOW(), INTERVAL 3 MONTH) AS 3m,
SUBDATE(NOW(), INTERVAL 6 MONTH) AS 6m
;';
$thresholds = query2array($query)[0];
$query = '
SELECT
DISTINCT id,
date_creation as date
FROM '.IMAGES_TABLE.' AS i
JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id
WHERE '.$filter_clause.'
;';
$list_of_dates = array();
$pre_counters = array();
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if(!empty($row['date'])){
foreach ($thresholds as $threshold => $date_limit)
{
if ($row['date'] > $date_limit)
{
@$pre_counters[$threshold]++;
}
}
list($date_without_time) = explode(' ', $row['date']);
list($y, $m) = explode('-', $date_without_time);
@$list_of_dates[$y]['months'][$y.'-'.$m]['days'][$date_without_time]['count']++;
@$list_of_dates[$y]['months'][$y.'-'.$m]['count']++;
@$list_of_dates[$y]['count']++;
}
}
$date_created = array(
'pre_counters' => $pre_counters,
'list_of_dates' => $list_of_dates,
);
if ($set_persistent_cache)
{
// for this filter, we do not store in cache the $filter_rows : for a big gallery it may
// take more than 10MB. It is smarter to store in cache the result of the computation,
// which is just around 100 bytes.
$persistent_cache->set($cache_key, $date_created);
}
}
$label_for_threshold = array(
'24h' => l10n('last 24 hours'),
'7d' => l10n('last 7 days'),
'30d' => l10n('last 30 days'),
'3m' => l10n('last 3 months'),
'6m' => l10n('last 6 months'),
);
$counters = array();
foreach (array_keys($label_for_threshold) as $threshold)
{
if (isset($date_created['pre_counters'][$threshold]))
{
$counters[$threshold] = array(
'label' => $label_for_threshold[$threshold],
'counter' => $date_created['pre_counters'][$threshold],
);
}
}
foreach (array_keys($date_created['list_of_dates']) as $y)
{
$date_created['list_of_dates'][$y]['label'] = l10n('year %d', $y);
foreach (array_keys($date_created['list_of_dates'][$y]['months']) as $ym)
{
list(,$m) = explode('-', $ym);
$date_created['list_of_dates'][$y]['months'][$ym]['label'] = $lang['month'][(int)$m].' '.$y;
foreach (array_keys($date_created['list_of_dates'][$y]['months'][$ym]['days']) as $ymd)
{
list(,,$d) = explode('-', $ymd);
$date_created['list_of_dates'][$y]['months'][$ym]['days'][$ymd]['label'] = format_date($ymd);
}
}
}
$template->assign('LIST_DATE_CREATED', $date_created['list_of_dates']);
$template->assign('DATE_CREATED', $counters);
}
if (isset($my_search['fields']['added_by']))
{
$filter_clause = get_clause_for_filter('added_by');
+66
View File
@@ -925,6 +925,72 @@ function ws_images_filteredSearch_create($params, $service)
}
}
if (isset($params['date_created_preset']))
{
if (!preg_match('/^(24h|7d|30d|3m|6m|custom|)$/', $params['date_created_preset']))
{
return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid parameter date_created_preset');
}
@$search['fields']['date_created']['preset'] = $params['date_created_preset'];
if ('custom' == $search['fields']['date_created']['preset'] and empty($params['date_created_custom']))
{
return new PwgError(WS_ERR_INVALID_PARAM, 'date_created_custom is missing');
}
}
if (isset($params['date_created_custom']))
{
if (!isset($search['fields']['date_created']['preset']) or $search['fields']['date_created']['preset'] != 'custom')
{
return new PwgError(WS_ERR_INVALID_PARAM, 'date_created_custom provided date_created_preset is not custom');
}
foreach ($params['date_created_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_created_custom, invalid option '.$date);
}
@$search['fields']['date_created']['custom'][] = $date;
}
}
if (isset($params['ratios']))
{
foreach ($params['ratios'] as $ext)