09824164 |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2006 Polytechnique.org * |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the Free Software * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
21 | |
22 | class SearchModule extends PLModule |
23 | { |
24 | function handlers() |
25 | { |
26 | return array( |
27 | 'search' => $this->make_hook('quick', AUTH_PUBLIC), |
28 | 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE), |
29 | ); |
30 | } |
31 | |
8d8f7607 |
32 | function on_subscribe($forlife, $uid, $promo, $pass) |
33 | { |
34 | require_once 'user.func.inc.php'; |
35 | user_reindex($uid); |
36 | } |
37 | |
09824164 |
38 | function get_quick($offset, $limit, $order) |
39 | { |
40 | global $globals; |
41 | $qSearch = new QuickSearch('quick'); |
42 | $fields = new SFieldGroup(true, array($qSearch)); |
43 | |
44 | if ($qSearch->isempty()) { |
45 | new ThrowError('Recherche trop générale.'); |
46 | } |
47 | |
48 | $sql = 'SELECT SQL_CALC_FOUND_ROWS |
49 | UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom, |
50 | IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom, |
51 | '.$globals->search->result_fields.' |
52 | c.uid AS contact, w.ni_id AS watch, |
53 | '.$qSearch->get_score_statement().' |
54 | FROM auth_user_md5 AS u |
55 | '.$fields->get_select_statement().' |
56 | LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id) |
57 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
6b590724 |
58 | LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).' |
09824164 |
59 | AND c.contact=u.user_id) |
60 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
6b590724 |
61 | AND w.uid='.S::i('uid', -1).') |
09824164 |
62 | '.$globals->search->result_where_statement.' |
63 | WHERE '.$fields->get_where_statement() |
cab08090 |
64 | .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '') |
09824164 |
65 | .' |
66 | GROUP BY u.user_id |
67 | ORDER BY '.($order?($order.', '):'') |
68 | .implode(',',array_filter(array($fields->get_order_statement(), |
69 | 'u.promo DESC, NomSortKey, prenom'))).' |
70 | LIMIT '.$offset * $globals->search->per_page.',' |
71 | .$globals->search->per_page; |
08cce2ff |
72 | $list = XDB::iterator($sql); |
73 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
74 | $nb_tot = $res->fetchOneCell(); |
75 | return array($list, $nb_tot); |
76 | } |
77 | |
78 | function form_prepare() |
79 | { |
d25e0f0b |
80 | global $page; |
81 | |
09824164 |
82 | $page->assign('formulaire',1); |
83 | $page->assign('choix_nats', |
08cce2ff |
84 | XDB::iterator('SELECT a2 AS id,IF(nat=\'\',pays,nat) AS text |
09824164 |
85 | FROM geoloc_pays ORDER BY text')); |
86 | $page->assign('choix_postes', |
08cce2ff |
87 | XDB::iterator('SELECT id,fonction_fr FROM fonctions_def |
09824164 |
88 | ORDER BY fonction_fr')); |
89 | $page->assign('choix_binets', |
08cce2ff |
90 | XDB::iterator('SELECT id,text FROM binets_def ORDER BY text')); |
09824164 |
91 | $page->assign('choix_groupesx', |
08cce2ff |
92 | XDB::iterator('SELECT id,text FROM groupesx_def ORDER BY text')); |
09824164 |
93 | $page->assign('choix_sections', |
08cce2ff |
94 | XDB::iterator('SELECT id,text FROM sections ORDER BY text')); |
09824164 |
95 | $page->assign('choix_schools', |
08cce2ff |
96 | XDB::iterator('SELECT id,text FROM applis_def ORDER BY text')); |
09824164 |
97 | $page->assign('choix_secteurs', |
08cce2ff |
98 | XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label')); |
09824164 |
99 | |
100 | if (Env::has('school')) { |
5e2307dc |
101 | $sql = 'SELECT type FROM applis_def WHERE id='.Env::i('school'); |
09824164 |
102 | } else { |
103 | $sql = 'DESCRIBE applis_def type'; |
104 | } |
105 | |
08cce2ff |
106 | $res = XDB::query($sql); |
09824164 |
107 | $row = $res->fetchOneRow(); |
108 | if (Env::has('school')) { |
109 | $types = $row[0]; |
110 | } else { |
111 | $types = explode('(',$row[1]); |
112 | $types = str_replace("'","",substr($types[1],0,-1)); |
113 | } |
114 | $page->assign('choix_diplomas', explode(',',$types)); |
115 | } |
116 | |
117 | function get_advanced($offset, $limit, $order) |
118 | { |
119 | $fields = new SFieldGroup(true, advancedSearchFromInput()); |
120 | if ($fields->too_large()) { |
121 | $this->form_prepare(); |
122 | new ThrowError('Recherche trop générale.'); |
123 | } |
124 | global $globals, $page; |
125 | |
126 | $page->assign('search_vars', $fields->get_url()); |
127 | |
128 | $where = $fields->get_where_statement(); |
129 | if ($where) { |
130 | $where = "WHERE $where"; |
131 | } |
132 | $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT |
133 | u.nom, u.prenom, |
134 | '.$globals->search->result_fields.' |
135 | c.uid AS contact, |
136 | w.ni_id AS watch |
137 | FROM auth_user_md5 AS u |
138 | LEFT JOIN auth_user_quick AS q USING(user_id) |
139 | '.$fields->get_select_statement().' |
140 | '.(Env::has('only_referent') ? ' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').' |
141 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
cab08090 |
142 | LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').' |
09824164 |
143 | AND c.contact=u.user_id) |
144 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
cab08090 |
145 | AND w.uid='.S::v('uid').') |
09824164 |
146 | '.$globals->search->result_where_statement." |
147 | $where |
148 | ORDER BY ".($order?($order.', '):'') |
149 | .implode(',',array_filter(array($fields->get_order_statement(), |
150 | 'promo DESC, NomSortKey, prenom'))).' |
151 | LIMIT '.($offset * $limit).','.$limit; |
08cce2ff |
152 | $liste = XDB::iterator($sql); |
153 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
154 | $nb_tot = $res->fetchOneCell(); |
155 | return Array($liste, $nb_tot); |
156 | } |
157 | |
158 | function handler_quick(&$page) |
159 | { |
160 | global $globals; |
161 | |
162 | require_once 'search.inc.php'; |
163 | |
164 | $page->changeTpl('search/index.tpl'); |
165 | |
166 | $page->assign('xorg_title','Polytechnique.org - Annuaire'); |
167 | require_once("applis.func.inc.php"); |
168 | require_once("geoloc.inc.php"); |
169 | |
170 | $page->assign('baseurl', $globals->baseurl); |
171 | |
172 | if (Env::has('quick')) { |
173 | $page->assign('formulaire', 0); |
174 | |
175 | $search = new XOrgSearch(array($this, 'get_quick')); |
176 | $search->setNbLines($globals->search->per_page); |
177 | $search->addOrder('score', 'score', false, 'pertinence', AUTH_PUBLIC, true); |
178 | |
179 | $nb_tot = $search->show(); |
180 | |
cab08090 |
181 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
09824164 |
182 | new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.'); |
183 | } elseif ($nb_tot > $globals->search->private_max) { |
184 | new ThrowError('Recherche trop générale'); |
185 | } elseif (empty($nb_tot)) { |
186 | new ThrowError('il n\'existe personne correspondant à ces critères dans la base !'); |
187 | } |
188 | } else { |
189 | $page->assign('formulaire',1); |
190 | } |
191 | |
192 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
193 | } |
194 | |
195 | function handler_advanced(&$page, $mode = null) |
196 | { |
197 | global $globals; |
198 | |
199 | require_once 'search.inc.php' ; |
200 | require_once 'applis.func.inc.php'; |
201 | require_once 'geoloc.inc.php'; |
202 | |
203 | |
62a66dfc |
204 | $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED); |
09824164 |
205 | |
206 | $page->assign('advanced',1); |
207 | $page->assign('public_directory',0); |
09824164 |
208 | |
209 | if (!Env::has('rechercher')) { |
210 | $this->form_prepare(); |
211 | } else { |
212 | |
d095003a |
213 | $search = new XOrgSearch(array($this, 'get_advanced')); |
09824164 |
214 | $search->setNbLines($globals->search->per_page); |
215 | |
216 | $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0))); |
217 | if (Env::has('with_soundex')) { |
218 | $page->assign('with_soundex', $search->make_url(Array())."&with_soundex=1"); |
219 | } |
220 | |
221 | $nb_tot = $search->show(); |
222 | |
223 | if ($nb_tot > $globals->search->private_max) { |
224 | $this->form_prepare(); |
225 | new ThrowError('Recherche trop générale'); |
226 | } |
227 | |
228 | } |
229 | |
230 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
231 | } |
232 | } |
233 | |
234 | ?> |