From 92502d8caa37296177cf585a882af3dda2723ea0 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 29 May 2020 11:25:37 +0300 Subject: [PATCH] search user on like %...% --- lib/Alchemy/Phrasea/Helper/User/Manage.php | 4 +- lib/classes/User/Query.php | 51 ++++++++++++++++------ templates/web/admin/users.html.twig | 4 +- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/Alchemy/Phrasea/Helper/User/Manage.php b/lib/Alchemy/Phrasea/Helper/User/Manage.php index bb6724fcbf..ecaa32547b 100644 --- a/lib/Alchemy/Phrasea/Helper/User/Manage.php +++ b/lib/Alchemy/Phrasea/Helper/User/Manage.php @@ -87,6 +87,7 @@ class Manage extends Helper $this->query_parms = [ 'inactives' => $this->request->get('inactives'), 'like_field' => $this->request->get('like_field'), + 'like_type' => $this->request->get('like_type'), 'like_value' => $this->request->get('like_value'), 'sbas_id' => $this->request->get('sbas_id'), 'base_id' => $this->request->get('base_id'), @@ -98,6 +99,7 @@ class Manage extends Helper 'offset_start' => $offset_start, ]; + /** @var \User_Query $query */ $query = $this->app['phraseanet.user-query']; if (is_array($this->query_parms['base_id'])) @@ -106,7 +108,7 @@ class Manage extends Helper $query->on_sbas_ids($this->query_parms['sbas_id']); $results = $query->sort_by($this->query_parms["srt"], $this->query_parms["ord"]) - ->like($this->query_parms['like_field'], $this->query_parms['like_value']) + ->like($this->query_parms['like_field'], $this->query_parms['like_value'], $this->query_parms['like_type']) ->last_model_is($this->query_parms['last_model']) ->get_inactives($this->query_parms['inactives']) ->include_templates(true) diff --git a/lib/classes/User/Query.php b/lib/classes/User/Query.php index e0ec9edfa5..56e366d1bc 100644 --- a/lib/classes/User/Query.php +++ b/lib/classes/User/Query.php @@ -36,11 +36,14 @@ class User_Query const LIKE_COUNTRY = 'country'; const LIKE_MATCH_AND = 'AND'; const LIKE_MATCH_OR = 'OR'; + const LIKE_TYPE_START = 'like_start'; + const LIKE_TYPE_CONTAINS = 'like_contains'; protected $app; protected $results = []; protected $sort = []; protected $like_field = []; + protected $like_type = 'like_start'; protected $have_rights = null; protected $have_not_rights = null; protected $like_match = 'OR'; @@ -400,12 +403,14 @@ class User_Query * * @param $like_field * @param $like_value + * @param $like_type * * @return $this */ - public function like($like_field, $like_value) + public function like($like_field, $like_value, $like_type = self::LIKE_TYPE_START) { $this->like_field[trim($like_field)] = trim($like_value); + $this->like_type = $like_type; $this->total = $this->page = $this->total_page = null; return $this; @@ -994,13 +999,24 @@ class User_Query if (trim($like_val) === '') continue; - $queries[] = sprintf( - ' (Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci OR Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci) ' - , self::LIKE_FIRSTNAME - , str_replace(['"', '%'], ['\"', '\%'], $like_val) - , self::LIKE_LASTNAME - , str_replace(['"', '%'], ['\"', '\%'], $like_val) - ); + if ($this->like_type == self::LIKE_TYPE_CONTAINS) { + $queries[] = sprintf( + ' (Users.`%s` LIKE "%%%s%%" COLLATE utf8_unicode_ci OR Users.`%s` LIKE "%%%s%%" COLLATE utf8_unicode_ci) ' + , self::LIKE_FIRSTNAME + , str_replace(['"', '%'], ['\"', '\%'], $like_val) + , self::LIKE_LASTNAME + , str_replace(['"', '%'], ['\"', '\%'], $like_val) + ); + } else { + $queries[] = sprintf( + ' (Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci OR Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci) ' + , self::LIKE_FIRSTNAME + , str_replace(['"', '%'], ['\"', '\%'], $like_val) + , self::LIKE_LASTNAME + , str_replace(['"', '%'], ['\"', '\%'], $like_val) + ); + } + } if (count($queries) > 0) { @@ -1013,11 +1029,20 @@ class User_Query case self::LIKE_EMAIL: case self::LIKE_LOGIN: case self::LIKE_COUNTRY: - $sql_like[] = sprintf( - ' Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci ' - , $like_field - , str_replace(['"', '%'], ['\"', '\%'], $like_value) - ); + if ($this->like_type == self::LIKE_TYPE_CONTAINS) { + $sql_like[] = sprintf( + ' Users.`%s` LIKE "%%%s%%" COLLATE utf8_unicode_ci ' + , $like_field + , str_replace(['"', '%'], ['\"', '\%'], $like_value) + ); + } else { + $sql_like[] = sprintf( + ' Users.`%s` LIKE "%s%%" COLLATE utf8_unicode_ci ' + , $like_field + , str_replace(['"', '%'], ['\"', '\%'], $like_value) + ); + } + break; default; break; diff --git a/templates/web/admin/users.html.twig b/templates/web/admin/users.html.twig index 31e9f58041..48f3fac5d2 100644 --- a/templates/web/admin/users.html.twig +++ b/templates/web/admin/users.html.twig @@ -47,8 +47,8 @@ {{ 'Last applied template' | trans }}