Merge commit 'origin/master' into hruid
[platal.git] / include / userset.inc.php
CommitLineData
8c4a0c30 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
8c4a0c30 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
8c4a0c30 22require_once('user.func.inc.php');
23
24global $globals;
25
35fa92e8 26@$globals->search->result_where_statement = '
8c4a0c30 27 LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0)
28 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
29 LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1)
30 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
31 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id)
32 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
33 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
34 LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2)
35 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut))
36 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
37 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
38 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = \'active\')';
39
40class UserSet extends PlSet
41{
42 public function __construct($joins = '', $where = '')
43 {
44 global $globals;
45 parent::__construct('auth_user_md5 AS u',
eaf30d86
PH
46 (!empty($GLOBALS['IS_XNET_SITE']) ?
47 'INNER JOIN groupex.membres AS gxm ON (u.user_id = gxm.uid
8c4a0c30 48 AND gxm.asso_id = ' . $globals->asso('id') . ') ' : '')
eaf30d86 49 . 'LEFT JOIN auth_user_quick AS q USING (user_id)
9d66aa4a 50 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type = \'a_vie\')
8c4a0c30 51 ' . $joins,
52 $where,
53 'u.user_id');
54 }
55}
56
57class SearchSet extends UserSet
58{
86b5c8f0 59 public $advanced = false;
35fa92e8 60 private $score = null;
8c4a0c30 61 private $order = null;
62 private $quick = false;
63
a2aa8436 64 public function __construct($quick = false, $no_search = false, $join = '', $where = '')
8c4a0c30 65 {
adb07f6f 66 Platal::load('search', 'search.inc.php');
8c4a0c30 67 if ($no_search) {
68 return;
69 }
70
71 $this->quick = $quick;
72 if ($quick) {
a2aa8436 73 $this->getQuick($join, $where);
8c4a0c30 74 } else {
a2aa8436 75 $this->getAdvanced($join, $where);
8c4a0c30 76 }
77 }
78
a2aa8436 79 private function getQuick($join, $where)
8c4a0c30 80 {
adb07f6f 81 Platal::load('search', 'search.inc.php');
8c4a0c30 82 global $globals;
83 if (!S::logged()) {
84 Env::kill('with_soundex');
85 }
86 $qSearch = new QuickSearch('quick');
87 $fields = new SFieldGroup(true, array($qSearch));
88 if ($qSearch->isEmpty()) {
8d118e58 89 new ThrowError('Aucun critère de recherche n\'est spécifié.');
8c4a0c30 90 }
35fa92e8 91 $this->score = $qSearch->get_score_statement();
3b2f9d11 92 $pwhere = $fields->get_where_statement();
93 if (trim($pwhere)) {
94 if (trim($where)) {
95 $where .= ' AND ';
96 }
97 $where .= $pwhere;
98 }
99 if (S::logged() && Env::has('nonins')) {
100 if (trim($where)) {
101 $where .= ' AND ';
102 }
103 $where .= 'u.perms="pending" AND u.deces=0';
104 }
105 parent::__construct($join . ' ' . $fields->get_select_statement(), $where);
8c4a0c30 106
107 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
108 'u.promo DESC, NomSortKey, prenom')));
109 }
110
63fac48e 111 private function getAdvanced($join, $where)
8c4a0c30 112 {
113 global $globals;
86b5c8f0 114 $this->advanced = true;
8c4a0c30 115 $fields = new SFieldGroup(true, advancedSearchFromInput());
116 if ($fields->too_large()) {
117 new ThrowError('Recherche trop générale.');
118 }
137e819f
FB
119 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
120 @$where . ' ' . $fields->get_where_statement());
8c4a0c30 121 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
122 'promo DESC, NomSortKey, prenom')));
123 }
35fa92e8 124
125 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
126 {
127 if ($this->score) {
128 $fields .= ', ' . $this->score;
129 }
130 return parent::get($fields, $joins, $where, $groupby, $order, $limitcount, $limitfrom);
131 }
8c4a0c30 132}
133
1cc0afe7 134class ArraySet extends UserSet
135{
136 public function __construct(array $users)
137 {
138 $where = $this->getUids($users);
139 if ($where) {
7586ae0b 140 $where = "u.hruid IN ($where)";
1cc0afe7 141 } else {
142 $where = " 0 ";
143 }
144 parent::__construct('', $where);
145 }
146
147 private function getUids(array $users)
148 {
4f25bc90 149 $users = User::getBulkHruid($users, array('User', '_silent_user_callback'));
1cc0afe7 150 if (is_null($users)) {
151 return '';
152 }
153 return '\'' . implode('\', \'', $users) . '\'';
154 }
155}
156
8c4a0c30 157class MinificheView extends MultipageView
158{
159 public function __construct(PlSet &$set, $data, array $params)
160 {
161 require_once 'applis.func.inc.php';
162 global $globals;
163 $this->entriesPerPage = $globals->search->per_page;
35fa92e8 164 if (@$params['with_score']) {
acbd9804 165 $this->addSortKey('score', array('-score', '-date', '-promo', 'nom', 'prenom'), 'pertinence');
35fa92e8 166 }
167 $this->addSortKey('name', array('nom', 'prenom'), 'nom');
168 $this->addSortKey('promo', array('-promo', 'nom', 'prenom'), 'promotion');
acbd9804 169 $this->addSortKey('date_mod', array('-date', '-promo', 'nom', 'prenom'), 'dernière modification');
8c4a0c30 170 parent::__construct($set, $data, $params);
171 }
172
173 public function fields()
174 {
175 return "u.user_id AS id,
176 u.*, a.alias AS forlife,
177 u.perms != 'pending' AS inscrit,
178 u.perms != 'pending' AS wasinscrit,
179 u.deces != 0 AS dcd, u.deces, u.matricule_ax,
180 FIND_IN_SET('femme', u.flags) AS sexe,
fd9a8382 181 e.entreprise, e.web AS job_web, es.label AS secteur, ef.fonction_fr AS fonction,
8c4a0c30 182 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
183 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
184 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
185 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
186 IF(u.nom_usage<>'',u.nom_usage,u.nom) AS sortkey,
0e5ec860
VZ
187 (COUNT(em.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif" .
188 (S::logged() ? ", c.contact AS contact" : '');
8c4a0c30 189 }
190
191 public function joins()
192 {
f3af95c0 193 return "LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id".(S::logged() ? "" : " AND e.pub = 'public'").")
8c4a0c30 194 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
195 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
196 LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2)
197 LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0)
198 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
199 LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1)
200 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
201 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid
f3af95c0 202 AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : " AND adr.pub = 'public'").")
8c4a0c30 203 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
204 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
86b5c8f0 205 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = 'active')" .
eaf30d86 206 (S::logged() ?
86b5c8f0 207 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
208 : "");
8c4a0c30 209 }
210
b71b2a36
SJ
211 public function bounds()
212 {
213 $order = Env::v('order', $this->defaultkey);
214 $show_bounds = 0;
215 if (($order == "name") || ($order == "-name")) {
216 $this->bound_field = "nom";
217 $show_bounds = 1;
218 } elseif (($order == "promo") || ($order == "-promo")) {
219 $this->bound_field = "promo";
220 $show_bounds = -1;
221 }
222 if ($order{0} == '-') {
223 $show_bounds = -$show_bounds;
224 }
225 return $show_bounds;
226 }
227
8c4a0c30 228 public function templateName()
229 {
230 return 'include/plview.minifiche.tpl';
231 }
232}
233
ff3eb9b7 234class MentorView extends MultipageView
235{
236 public function __construct(PlSet &$set, $data, array $params)
237 {
eaf30d86 238 $this->entriesPerPage = 10;
ff3eb9b7 239 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
eaf30d86
PH
240 $this->addSortKey('name', array('nom', 'prenom'), 'nom');
241 $this->addSortKey('promo', array('-promo', 'nom', 'prenom'), 'promotion');
242 $this->addSortKey('date_mod', array('-date', '-promo', 'nom', 'prenom'), 'dernière modification');
243 parent::__construct($set, $data, $params);
ff3eb9b7 244 }
245
246 public function fields()
247 {
248 return "m.uid, u.prenom, u.nom, u.promo,
e1ce18d2 249 a.alias AS forlife, m.expertise, mp.pid,
ff3eb9b7 250 ms.secteur, ms.ss_secteur";
251 }
252
b71b2a36
SJ
253 public function bounds()
254 {
255 $order = Env::v('order', $this->defaultkey);
256 $show_bounds = 0;
257 if (($order == "name") || ($order == "-name")) {
258 $this->bound_field = "nom";
259 $show_bounds = 1;
260 } elseif (($order == "promo") || ($order == "-promo")) {
261 $this->bound_field = "promo";
262 $show_bounds = -1;
263 }
264 if ($order{0} == '-') {
265 $show_bounds = -$show_bounds;
266 }
267 return $show_bounds;
268 }
269
ff3eb9b7 270 public function templateName()
271 {
272 return 'include/plview.referent.tpl';
273 }
274}
275
8c4a0c30 276class TrombiView extends MultipageView
277{
278 public function __construct(PlSet &$set, $data, array $params)
279 {
280 $this->entriesPerPage = 24;
281 $this->order = explode(',', Env::v('order', 'nom,prenom,promo'));
35fa92e8 282 if (@$params['with_score']) {
283 $this->addSortKey('score', array('-score', '-watch_last', '-promo', 'nom', 'prenom'), 'pertinence');
284 }
285 $this->addSortKey('name', array('nom', 'prenom'), 'nom');
286 $this->addSortKey('promo', array('-promo', 'nom', 'prenom'), 'promotion');
8c4a0c30 287 parent::__construct($set, $data, $params);
288 }
289
290 public function fields()
291 {
292 return "u.user_id, IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, u.prenom, u.promo, a.alias AS forlife ";
293 }
294
295 public function joins()
296 {
297 return "INNER JOIN photo AS p ON (p.uid = u.user_id) ";
298 }
299
b71b2a36
SJ
300 public function bounds()
301 {
302 $order = Env::v('order', $this->defaultkey);
303 $show_bounds = 0;
304 if (($order == "name") || ($order == "-name")) {
305 $this->bound_field = "nom";
306 $show_bounds = 1;
307 } elseif (($order == "promo") || ($order == "-promo")) {
308 $this->bound_field = "promo";
309 $show_bounds = -1;
310 }
311 if ($order{0} == '-') {
312 $show_bounds = -$show_bounds;
313 }
314 return $show_bounds;
315 }
316
8c4a0c30 317 public function templateName()
318 {
319 return 'include/plview.trombi.tpl';
320 }
321
04334c61 322 public function apply(PlPage &$page)
8c4a0c30 323 {
324 if (!empty($GLOBALS['IS_XNET_SITE'])) {
325 global $globals;
326 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
327 }
328 return parent::apply($page);
329 }
330}
331
332class GeolocView implements PlView
333{
334 private $set;
335 private $type;
336 private $params;
337
338 public function __construct(PlSet &$set, $data, array $params)
339 {
340 $this->params = $params;
341 $this->set =& $set;
342 $this->type = $data;
343 }
344
345 private function use_map()
346 {
347 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
348 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
349 }
350
35fa92e8 351 public function args()
8c4a0c30 352 {
35fa92e8 353 $args = $this->set->args();
354 unset($args['initfile']);
355 unset($args['mapid']);
356 return $args;
8c4a0c30 357 }
358
04334c61 359 public function apply(PlPage &$page)
8c4a0c30 360 {
361 require_once 'geoloc.inc.php';
362 require_once '../modules/search/search.inc.php';
363
364 switch ($this->type) {
365 case 'icon.swf':
366 header("Content-type: application/x-shockwave-flash");
367 header("Pragma:");
368 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
369 exit;
370
371 case 'dynamap.swf':
372 header("Content-type: application/x-shockwave-flash");
373 header("Pragma:");
374 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
375 exit;
376
377 case 'init':
378 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
379 header('Content-Type: text/xml');
380 header('Pragma:');
381 if (!empty($GLOBALS['IS_XNET_SITE'])) {
382 $page->assign('background', 0xF2E9D0);
383 }
8c4a0c30 384 break;
385
386 case 'city':
387 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
388 header('Content-Type: text/xml');
389 header('Pragma:');
a2aa8436 390 $only_current = Env::v('only_current', false)? ' AND FIND_IN_SET(\'active\', adrf.statut)' : '';
8c4a0c30 391 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, u.promo, al.alias',
a2aa8436 392 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id $only_current)
8c4a0c30 393 LEFT JOIN aliases AS al ON (u.user_id = al.id
010268b2 394 AND FIND_IN_SET('bestalias', al.flags))
97eff0ff 395 INNER JOIN adresses AS avg ON (" . getadr_join('avg') . ")",
8c4a0c30 396 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
397 $page->assign('users', $it);
398 break;
399
400 case 'country':
401 if (Env::has('debug')) {
402 $page->changeTpl('geoloc/country.tpl', SIMPLE);
403 } else {
404 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
405 header('Content-Type: text/xml');
406 header('Pragma:');
407 }
8c4a0c30 408 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
409 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
410 $page->assign('countries', $countries);
411 $page->assign('cities', $cities);
412 break;
413
414 default:
415 global $globals;
416 if (!$this->use_map()) {
417 $page->assign('request_geodesix', true);
418 }
a2aa8436 419 $page->assign('annu', @$this->params['with_annu']);
8c4a0c30 420 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
421 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
8c4a0c30 422 return 'include/plview.geoloc.tpl';
423 }
424 }
425}
426
92e80560
VZ
427class GadgetView implements PlView
428{
429 public function __construct(PlSet &$set, $data, array $params)
430 {
431 $this->set =& $set;
432 }
433
434 public function fields()
435 {
436 return "u.user_id AS id,
437 u.*, a.alias AS forlife," .
438 (S::logged() ? "q.profile_mobile AS mobile, " : "IF(q.profile_mobile_pub = 'public', q.profile_mobile, NULL) as mobile, ") .
439 "u.perms != 'pending' AS inscrit,
440 u.perms != 'pending' AS wasinscrit,
441 u.deces != 0 AS dcd, u.deces,
442 FIND_IN_SET('femme', u.flags) AS sexe,
443 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region" .
444 (S::logged() ? ", c.contact AS contact" : '');
445 }
446
447 public function joins()
448 {
449 return "LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : " AND adr.pub = 'public'").")
450 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
451 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)" .
452 (S::logged() ?
453 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
454 : "");
455 }
456
04334c61 457 public function apply(PlPage &$page)
92e80560
VZ
458 {
459 $page->assign_by_ref('set',
460 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
461 }
462
463 public function args()
464 {
465 return null;
466 }
467}
468
8c4a0c30 469// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
470?>