mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
* add filter action for smart syntax parser
This commit is contained in:
parent
b395eaa76c
commit
a3113ed195
3 changed files with 91 additions and 3 deletions
|
|
@ -241,9 +241,9 @@ class TasksController extends ApiController {
|
|||
);
|
||||
if (Config::get('smartsyntax') != 0 && (false !== $a = $this->parseSmartSyntax($t['title'])))
|
||||
{
|
||||
$t['title'] = $a['title'];
|
||||
$t['prio'] = $a['prio'];
|
||||
$t['tags'] = $a['tags'];
|
||||
$t['title'] = (string) $a['title'];
|
||||
$t['prio'] = (int) $a['prio'];
|
||||
$t['tags'] = (string) $a['tags'];
|
||||
}
|
||||
$this->response->data = $t;
|
||||
}
|
||||
|
|
@ -803,6 +803,9 @@ class TasksController extends ApiController {
|
|||
if (count($tags) > 0) {
|
||||
$a['tags'] = implode( ',' , $tags );
|
||||
}
|
||||
|
||||
do_filter('parseSmartSyntax', $title, $a);
|
||||
|
||||
return $a;
|
||||
}
|
||||
|
||||
|
|
|
|||
84
src/includes/filters.php
Normal file
84
src/includes/filters.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
This file is a part of myTinyTodo.
|
||||
(C) Copyright 2023 Max Pozdeev <maxpozdeev@gmail.com>
|
||||
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
|
||||
*/
|
||||
|
||||
class MTTFilterCenter
|
||||
{
|
||||
private static $filters = [];
|
||||
|
||||
public static function addFilterCallbackForAction(string $action, callable $callback): bool
|
||||
{
|
||||
if (!isset(self::$filters[$action])) {
|
||||
self::$filters[$action] = [];
|
||||
}
|
||||
if (!in_array($callback, self::$filters[$action])) {
|
||||
// do not duplicate same callback
|
||||
self::$filters[$action][] = $callback;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function addFilterForAction(string $action, MTTFilterInterface $filter): bool
|
||||
{
|
||||
if (!isset(self::$filters[$action])) {
|
||||
self::$filters[$action] = [];
|
||||
}
|
||||
if (!in_array($filter, self::$filters[$action])) {
|
||||
// do not duplicate same filter
|
||||
self::$filters[$action][] = $filter;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function hasFiltersForAction(string $action): bool
|
||||
{
|
||||
if (isset(self::$filters[$action]) && count(self::$filters[$action]) > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function filter(string $action, $in, &$out): bool
|
||||
{
|
||||
if (!isset(self::$filters[$action]) || count(self::$filters[$action]) == 0) {
|
||||
return false;
|
||||
}
|
||||
foreach (self::$filters[$action] as $filter) {
|
||||
if ($filter instanceof MTTFilterInterface) {
|
||||
$filter->filter($in, $out);
|
||||
}
|
||||
else {
|
||||
$filter($in, $out);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
interface MTTFilterInterface
|
||||
{
|
||||
function filter($in, &$out);
|
||||
}
|
||||
|
||||
function add_filter(string $action, MTTFilterInterface $filter) {
|
||||
MTTFilterCenter::addFilterForAction($action, $filter);
|
||||
}
|
||||
|
||||
function add_filter_callback(string $action, callable $callback) {
|
||||
MTTFilterCenter::addFilterCallbackForAction($action, $callback);
|
||||
}
|
||||
|
||||
function do_filter(string $action, $in, &$out): bool {
|
||||
return MTTFilterCenter::filter($action, $in, $out);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -40,6 +40,7 @@ require_once(MTTINC. 'class.dbconnection.php');
|
|||
require_once(MTTINC. 'class.dbcore.php');
|
||||
require_once(MTTINC. 'class.config.php');
|
||||
require_once(MTTINC. 'notifications.php');
|
||||
require_once(MTTINC. 'filters.php');
|
||||
|
||||
configureDbConnection();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue