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