zoneclearFeedServer/src/PostsFilter.php

165 lines
4 KiB
PHP
Raw Normal View History

2022-12-10 21:20:42 +00:00
<?php
2023-10-14 13:41:43 +00:00
2023-05-07 22:40:58 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\zoneclearFeedServer;
use ArrayObject;
2023-10-14 13:41:43 +00:00
use Dotclear\App;
2023-08-16 20:03:40 +00:00
use Dotclear\Core\Backend\Combos;
use Dotclear\Core\Backend\Filter\{
Filter,
Filters,
FiltersLibrary
};
2023-05-07 22:40:58 +00:00
use Dotclear\Helper\Html\Html;
use Exception;
2022-12-10 21:20:42 +00:00
/**
2023-10-14 13:41:43 +00:00
* @brief zoneclearFeedServer backend feeds posts list filter.
* @ingroup zoneclearFeedServer
*
* @author Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
2022-12-10 21:20:42 +00:00
*/
2023-08-16 20:03:40 +00:00
class PostsFilter extends Filters
2022-12-10 21:20:42 +00:00
{
public function __construct()
{
2023-05-07 22:40:58 +00:00
// use user posts pref
parent::__construct('posts');
2022-12-10 21:20:42 +00:00
2023-05-07 22:40:58 +00:00
$filters = new ArrayObject([
2023-08-16 20:03:40 +00:00
FiltersLibrary::getPageFilter(),
2022-12-10 21:20:42 +00:00
$this->getPostUserFilter(),
$this->getPostCategoriesFilter(),
$this->getPostStatusFilter(),
$this->getPostMonthFilter(),
]);
# --BEHAVIOR-- zcfsPostFilter
2023-10-14 13:41:43 +00:00
App::behavior()->callBehavior('zcfsPostFilter', $filters);
2022-12-10 21:20:42 +00:00
$filters = $filters->getArrayCopy();
$this->add($filters);
}
/**
* Posts users select
2023-06-30 20:55:59 +00:00
*
2023-08-16 20:03:40 +00:00
* @return null|Filter
2022-12-10 21:20:42 +00:00
*/
2023-06-30 20:55:59 +00:00
public function getPostUserFilter()
2022-12-10 21:20:42 +00:00
{
$users = null;
try {
2023-10-14 13:41:43 +00:00
$users = App::blog()->getPostsUsers();
2023-11-04 21:45:37 +00:00
if ($users->isEmpty()) {
2022-12-10 21:20:42 +00:00
return null;
}
} catch (Exception $e) {
2023-10-14 13:41:43 +00:00
App::error()->add($e->getMessage());
2022-12-10 21:20:42 +00:00
return null;
}
2023-08-16 20:03:40 +00:00
$combo = Combos::getUsersCombo($users);
2023-10-14 13:41:43 +00:00
App::lexical()->lexicalKeySort($combo);
2022-12-10 21:20:42 +00:00
2023-08-16 20:03:40 +00:00
return (new Filter('user_id'))
2022-12-10 21:20:42 +00:00
->param()
->title(__('Author:'))
->options(array_merge(
['-' => ''],
$combo
))
->prime(true);
}
/**
* Posts categories select
2023-06-30 20:55:59 +00:00
*
2023-08-16 20:03:40 +00:00
* @return null|Filter
2022-12-10 21:20:42 +00:00
*/
2023-06-30 20:55:59 +00:00
public function getPostCategoriesFilter()
2022-12-10 21:20:42 +00:00
{
$categories = null;
try {
2023-10-14 13:41:43 +00:00
$categories = App::blog()->getCategories();
2023-11-04 21:45:37 +00:00
if ($categories->isEmpty()) {
2022-12-10 21:20:42 +00:00
return null;
}
} catch (Exception $e) {
2023-10-14 13:41:43 +00:00
App::error()->add($e->getMessage());
2022-12-10 21:20:42 +00:00
return null;
}
$combo = [
'-' => '',
__('(No cat)') => 'NULL',
];
while ($categories->fetch()) {
2023-05-07 22:40:58 +00:00
if (is_numeric($categories->f('level')) && is_string($categories->f('cat_title'))) {
$combo[
str_repeat('&nbsp;', ((int) $categories->f('level') - 1) * 4) .
Html::escapeHTML($categories->f('cat_title')) . ' (' . $categories->f('nb_post') . ')'
] = $categories->f('cat_id');
}
2022-12-10 21:20:42 +00:00
}
2023-08-16 20:03:40 +00:00
return (new Filter('cat_id'))
2022-12-10 21:20:42 +00:00
->param()
->title(__('Category:'))
->options($combo)
->prime(true);
}
/**
* Posts status select
*/
2023-08-16 20:03:40 +00:00
public function getPostStatusFilter(): Filter
2022-12-10 21:20:42 +00:00
{
2023-08-16 20:03:40 +00:00
return (new Filter('status'))
2022-12-10 21:20:42 +00:00
->param('post_status')
->title(__('Status:'))
->options(array_merge(
['-' => ''],
2023-08-16 20:03:40 +00:00
Combos::getPostStatusesCombo()
2022-12-10 21:20:42 +00:00
));
}
/**
* Posts by month select
2023-06-30 20:55:59 +00:00
*
2023-08-16 20:03:40 +00:00
* @return null|Filter
2022-12-10 21:20:42 +00:00
*/
2023-06-30 20:55:59 +00:00
public function getPostMonthFilter()
2022-12-10 21:20:42 +00:00
{
$dates = null;
try {
2023-10-14 13:41:43 +00:00
$dates = App::blog()->getDates(['type' => 'month']);
2023-11-04 21:45:37 +00:00
if ($dates->isEmpty()) {
2022-12-10 21:20:42 +00:00
return null;
}
} catch (Exception $e) {
2023-10-14 13:41:43 +00:00
App::error()->add($e->getMessage());
2022-12-10 21:20:42 +00:00
return null;
}
2023-08-16 20:03:40 +00:00
return (new Filter('month'))
2022-12-10 21:20:42 +00:00
->param('post_month', function ($f) { return substr($f[0], 4, 2); })
->param('post_year', function ($f) { return substr($f[0], 0, 4); })
->title(__('Month:'))
->options(array_merge(
['-' => ''],
2023-08-16 20:03:40 +00:00
Combos::getDatesCombo($dates)
2022-12-10 21:20:42 +00:00
));
}
}