From 5600b2feb94fb836d9d0359d74ab8d6f729908c0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Tue, 26 Apr 2011 13:06:35 +0200 Subject: [PATCH 1/1] Adds search on firstname/lastname/nickname (Closes #1469). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- ChangeLog | 1 + classes/profile.php | 10 +++++----- classes/userfilter/conditions.inc.php | 7 ++++++- include/ufbuilder.inc.php | 22 ++++++++++++++++++++-- templates/search/adv.form.tpl | 6 ++++++ upgrade/1.1.1/10_names.sql | 15 +++++++++++++++ upgrade/1.1.1/README | 2 ++ 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 upgrade/1.1.1/10_names.sql diff --git a/ChangeLog b/ChangeLog index 045ad1d..001e140 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,7 @@ Bug/Wish: * Search: - #911: Adds search on corps -JAC - #990: Adds search on broken redirections -JAC + - #1469: Adds search on firstname/lastname/nickname -JAC - #1470: Fixes autocomplete on countries -JAC * XnetList: diff --git a/classes/profile.php b/classes/profile.php index 249de86..e63bfe5 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -1092,13 +1092,13 @@ class Profile implements PlExportable if (!is_array($pids)) { $pids = array($pids); } - $keys = XDB::iterator("(SELECT n.pid AS pid, n.name AS name, e.score AS score, + $keys = XDB::iterator("(SELECT n.pid AS pid, n.name AS name, e.score AS score, e.general_type, IF(FIND_IN_SET('public', e.flags), 'public', '') AS public FROM profile_name AS n INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags)) UNION - (SELECT n.pid AS pid, n.particle AS name, 0 AS score, + (SELECT n.pid AS pid, n.particle AS name, 0 AS score, e.general_type, IF(FIND_IN_SET('public', e.flags), 'public', '') AS public FROM profile_name AS n INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) @@ -1124,9 +1124,9 @@ class Profile implements PlExportable $token = ''; foreach ($toks as $tok) { $token = $tok . $token; - $names["$pid-$token"] = XDB::format('({?}, {?}, {?}, {?}, {?})', + $names["$pid-$token"] = XDB::format('({?}, {?}, {?}, {?}, {?}, {?})', $token, $pid, soundex_fr($token), - $eltScore, $key['public']); + $eltScore, $key['public'], $key['general_type']); } } if ($transaction) { @@ -1136,7 +1136,7 @@ class Profile implements PlExportable WHERE pid IN {?}', $pids); if (count($names) > 0) { - XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags) + XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type) VALUES ' . implode(', ', $names)); } if ($transaction) { diff --git a/classes/userfilter/conditions.inc.php b/classes/userfilter/conditions.inc.php index e5c347b..54932f9 100644 --- a/classes/userfilter/conditions.inc.php +++ b/classes/userfilter/conditions.inc.php @@ -663,8 +663,9 @@ class UFC_NameTokens extends UserFilterCondition private $flags; private $soundex; private $exact; + private $general_type; - public function __construct($tokens, $flags = array(), $soundex = false, $exact = false) + public function __construct($tokens, $flags = array(), $soundex = false, $exact = false, $general_type = '') { if (is_array($tokens)) { $this->tokens = $tokens; @@ -678,6 +679,7 @@ class UFC_NameTokens extends UserFilterCondition } $this->soundex = $soundex; $this->exact = $exact; + $this->general_type = $general_type; } public function buildCondition(PlFilter $uf) @@ -695,6 +697,9 @@ class UFC_NameTokens extends UserFilterCondition if ($this->flags != null) { $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags); } + if ($this->general_type) { + $c .= XDB::format(' AND ' . $sub . '.general_type = {?}', $this->general_type); + } $conds[] = $c; } diff --git a/include/ufbuilder.inc.php b/include/ufbuilder.inc.php index 457bd09..e329bbc 100644 --- a/include/ufbuilder.inc.php +++ b/include/ufbuilder.inc.php @@ -339,7 +339,7 @@ class UFB_AdvancedSearch extends UserFilterBuilder public function __construct($include_admin = false, $include_ax = false, $envprefix = '') { $fields = array( - new UFBF_Name('name', 'Nom'), + new UFBF_Name('name', 'Nom', 'name_type'), new UFBF_Promo('promo1', 'Promotion', 'egal1', 'edu_type'), new UFBF_Promo('promo2', 'Promotion', 'egal2', 'edu_type'), new UFBF_Sex('woman', 'Sexe'), @@ -836,6 +836,15 @@ class UFBF_SchoolIds extends UFB_Field // {{{ class UFBF_Name class UFBF_Name extends UFBF_Text { + private $envfieldtype; + private $type; + + public function __construct($envfield, $formtext = '', $envfieldtype) + { + parent::__construct($envfield, $formtext); + $this->envfieldtype = $envfieldtype; + } + protected function check(UserFilterBuilder $ufb) { if (!parent::check($ufb)) { @@ -848,12 +857,21 @@ class UFBF_Name extends UFBF_Text if (count($this->val) == 0) { $this->empty = true; } + $this->type = $ufb->v($this->envfieldtype); + if (!in_array($this->type, array('', 'lastname', 'firstname', 'nickname'))) { + return $this->raise("Le critère {$this->type} n'est pas valide pour le champ %s"); + } return true; } protected function buildUFC(UserFilterBuilder $ufb) { - return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact')); + return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact'), $this->type); + } + + public function getEnvFieldNames() + { + return array($this->envfield, $this->envfieldtype); } } // }}} diff --git a/templates/search/adv.form.tpl b/templates/search/adv.form.tpl index 81e08bb..4d47703 100644 --- a/templates/search/adv.form.tpl +++ b/templates/search/adv.form.tpl @@ -277,6 +277,12 @@ function cleanForm(f) { + diff --git a/upgrade/1.1.1/10_names.sql b/upgrade/1.1.1/10_names.sql new file mode 100644 index 0000000..6a6071d --- /dev/null +++ b/upgrade/1.1.1/10_names.sql @@ -0,0 +1,15 @@ +ALTER TABLE profile_name_enum ADD COLUMN general_type ENUM('lastname', 'firstname', 'nickname') NOT NULL DEFAULT 'lastname'; + +UPDATE profile_name_enum + SET general_type = 'lastname' + WHERE type LIKE 'lastname%' OR type LIKE 'name_%'; +UPDATE profile_name_enum + SET general_type = 'firstname' + WHERE type LIKE 'firstname%'; +UPDATE profile_name_enum + SET general_type = 'nickname' + WHERE type IN ('pseudonym', 'nickname'); + +ALTER TABLE search_name ADD COLUMN general_type ENUM('lastname', 'firstname', 'nickname') NOT NULL DEFAULT 'lastname'; + +-- vim:set syntax=mysql: diff --git a/upgrade/1.1.1/README b/upgrade/1.1.1/README index 936e205..890317b 100644 --- a/upgrade/1.1.1/README +++ b/upgrade/1.1.1/README @@ -1,3 +1,5 @@ The following variable should be set: [Core] baseurl_shortener = "http://u.w4x.org/" + +Run bin/search.rebuild_db.php after release to enable search on firstname/lastname/nickname. -- 2.1.4