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