e81fef25a6d6a47e897389578e9b5860da9f95af
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 require_once('user.func.inc.php');
23
24 global $globals;
25
26 @$globals->search->result_where_statement = '
27 LEFT JOIN profile_education AS edu ON (u.user_id = edu.uid)
28 LEFT JOIN profile_education_enum AS ede ON (ede.id = edu.eduid)
29 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id)
30 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
31 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
32 LEFT JOIN geoloc_pays AS n1 ON (u.nationalite = n1.a2)
33 LEFT JOIN geoloc_pays AS n2 ON (u.nationalite2 = n2.a2)
34 LEFT JOIN geoloc_pays AS n3 ON (u.nationalite2 = n3.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
40 class UserSet extends PlSet
41 {
42 public function __construct($joins = '', $where = '')
43 {
44 global $globals;
45 parent::__construct('auth_user_md5 AS u',
46 (!empty($GLOBALS['IS_XNET_SITE']) ?
47 'INNER JOIN groupex.membres AS gxm ON (u.user_id = gxm.uid
48 AND gxm.asso_id = ' . $globals->asso('id') . ') ' : '')
49 . 'LEFT JOIN auth_user_quick AS q USING (user_id)' . $joins,
50 $where,
51 'u.user_id');
52 }
53 }
54
55 class SearchSet extends UserSet
56 {
57 public $advanced = false;
58 private $score = null;
59 private $order = null;
60 private $quick = false;
61
62 public function __construct($quick = false, $no_search = false, $join = '', $where = '')
63 {
64 Platal::load('search', 'search.inc.php');
65 if ($no_search) {
66 return;
67 }
68
69 $this->quick = $quick;
70 if ($quick) {
71 $this->getQuick($join, $where);
72 } else {
73 $this->getAdvanced($join, $where);
74 }
75 }
76
77 private function getQuick($join, $where)
78 {
79 Platal::load('search', 'search.inc.php');
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()) {
87 new ThrowError('Aucun critère de recherche n\'est spécifié.');
88 }
89 $this->score = $qSearch->get_score_statement();
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);
104
105 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
106 'u.promo DESC, NomSortKey, prenom')));
107 }
108
109 private function getAdvanced($join, $where)
110 {
111 global $globals;
112 $this->advanced = true;
113 $fields = new SFieldGroup(true, advancedSearchFromInput());
114 if ($fields->too_large()) {
115 new ThrowError('Recherche trop générale.');
116 }
117 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
118 @$where . ' ' . $fields->get_where_statement());
119 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
120 'promo DESC, NomSortKey, prenom')));
121 }
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 }
130 }
131
132 class ArraySet extends UserSet
133 {
134 public function __construct(array $users)
135 {
136 $where = $this->getUids($users);
137 if ($where) {
138 $where = "u.hruid IN ($where)";
139 } else {
140 $where = " 0 ";
141 }
142 parent::__construct('', $where);
143 }
144
145 private function getUids(array $users)
146 {
147 $users = User::getBulkHruid($users, array('User', '_silent_user_callback'));
148 if (is_null($users)) {
149 return '';
150 }
151 return '\'' . implode('\', \'', $users) . '\'';
152 }
153 }
154
155 class MinificheView extends MultipageView
156 {
157 public function __construct(PlSet &$set, $data, array $params)
158 {
159 require_once 'education.func.inc.php';
160 global $globals;
161 $this->entriesPerPage = $globals->search->per_page;
162 if (@$params['with_score']) {
163 $this->addSortKey('score', array('-score', '-date', '-promo', 'name_sort'), 'pertinence');
164 }
165 $this->addSortKey('name', array('name_sort'), 'nom');
166 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
167 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
168 parent::__construct($set, $data, $params);
169 }
170
171 public function fields()
172 {
173 global $globals;
174 return "u.user_id AS id, u.*,
175 CONCAT(a.alias, '@{$globals->mail->domain}') AS bestemail,
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,
180 e.entreprise, e.web AS job_web, es.label AS secteur, ef.fonction_fr AS fonction,
181 IF(n1.nat='',n1.pays,n1.nat) AS nat1, n1.a2 AS iso3166_1,
182 IF(n2.nat='',n2.pays,n2.nat) AS nat2, n2.a2 AS iso3166_2,
183 IF(n3.nat='',n3.pays,n3.nat) AS nat3, n3.a2 AS iso3166_3,
184 ede0.name AS eduname0, ede0.url AS eduurl0, edd0.degree AS edudegree0,
185 edu0.grad_year AS edugrad_year0, f0.field AS edufield0, edu0.program AS eduprogram0,
186 ede1.name AS eduname1, ede1.url AS eduurl1, edd1.degree AS edudegree1,
187 edu1.grad_year AS edugrad_year1, f1.field AS edufield1, edu1.program AS eduprogram1,
188 ede2.name AS eduname2, ede2.url AS eduurl2, edd2.degree AS edudegree2,
189 edu2.grad_year AS edugrad_year2, f2.field AS edufield2, edu2.program AS eduprogram2,
190 ede3.name AS eduname3, ede3.url AS eduurl3, edd3.degree AS edudegree3,
191 edu3.grad_year AS edugrad_year3, f3.field AS edufield3, edu3.program AS eduprogram3,
192 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
193 (COUNT(em.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif,
194 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort" .
195 (S::logged() ? ", c.contact AS contact" : '');
196 }
197
198 public function joins()
199 {
200 return "LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id".(S::logged() ? "" : " AND e.pub = 'public'").")
201 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
202 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
203 LEFT JOIN geoloc_pays AS n1 ON (u.nationalite = n1.a2)
204 LEFT JOIN geoloc_pays AS n2 ON (u.nationalite2 = n2.a2)
205 LEFT JOIN geoloc_pays AS n3 ON (u.nationalite3 = n3.a2)
206 LEFT JOIN profile_education AS edu0 ON (u.user_id = edu0.uid AND edu0.id = 0)
207 LEFT JOIN profile_education_enum AS ede0 ON (ede0.id = edu0.eduid)
208 LEFT JOIN profile_education_degree_enum AS edd0 ON (edd0.id = edu0.degreeid)
209 LEFT JOIN profile_education_field_enum AS f0 ON (f0.id = edu0.fieldid)
210 LEFT JOIN profile_education AS edu1 ON (u.user_id = edu1.uid AND edu1.id = 1)
211 LEFT JOIN profile_education_enum AS ede1 ON (ede1.id = edu1.eduid)
212 LEFT JOIN profile_education_degree_enum AS edd1 ON (edd1.id = edu1.degreeid)
213 LEFT JOIN profile_education_field_enum AS f1 ON (f1.id = edu1.fieldid)
214 LEFT JOIN profile_education AS edu2 ON (u.user_id = edu2.uid AND edu2.id = 2)
215 LEFT JOIN profile_education_enum AS ede2 ON (ede2.id = edu2.eduid)
216 LEFT JOIN profile_education_degree_enum AS edd2 ON (edd2.id = edu2.degreeid)
217 LEFT JOIN profile_education_field_enum AS f2 ON (f2.id = edu2.fieldid)
218 LEFT JOIN profile_education AS edu3 ON (u.user_id = edu3.uid AND edu3.id = 3)
219 LEFT JOIN profile_education_enum AS ede3 ON (ede3.id = edu3.eduid)
220 LEFT JOIN profile_education_degree_enum AS edd3 ON (edd3.id = edu3.degreeid)
221 LEFT JOIN profile_education_field_enum AS f3 ON (f3.id = edu3.fieldid)
222 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid
223 AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : "
224 AND adr.pub = 'public'").")
225 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
226 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
227 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = 'active')
228 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)" .
229 (S::logged() ?
230 "LEFT JOIN contacts AS c ON (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
231 : "");
232 }
233
234 public function bounds()
235 {
236 $order = Env::v('order', $this->defaultkey);
237 $show_bounds = 0;
238 if (($order == "name") || ($order == "-name")) {
239 $this->bound_field = "nom";
240 $show_bounds = 1;
241 } elseif (($order == "promo") || ($order == "-promo")) {
242 $this->bound_field = "promo";
243 $show_bounds = -1;
244 }
245 if ($order{0} == '-') {
246 $show_bounds = -$show_bounds;
247 }
248 return $show_bounds;
249 }
250
251 public function templateName()
252 {
253 return 'include/plview.minifiche.tpl';
254 }
255 }
256
257 class MentorView extends MultipageView
258 {
259 public function __construct(PlSet &$set, $data, array $params)
260 {
261 $this->entriesPerPage = 10;
262 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
263 $this->addSortKey('name', array('name_sort'), 'nom');
264 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
265 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
266 parent::__construct($set, $data, $params);
267 }
268
269 public function fields()
270 {
271 return "m.uid, u.promo, u.hruid,
272 m.expertise, mp.pid, ms.secteur, ms.ss_secteur,
273 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort";
274 }
275
276 public function joins()
277 {
278 return "INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
279 }
280
281 public function bounds()
282 {
283 $order = Env::v('order', $this->defaultkey);
284 $show_bounds = 0;
285 if (($order == "name") || ($order == "-name")) {
286 $this->bound_field = "nom";
287 $show_bounds = 1;
288 } elseif (($order == "promo") || ($order == "-promo")) {
289 $this->bound_field = "promo";
290 $show_bounds = -1;
291 }
292 if ($order{0} == '-') {
293 $show_bounds = -$show_bounds;
294 }
295 return $show_bounds;
296 }
297
298 public function templateName()
299 {
300 return 'include/plview.referent.tpl';
301 }
302 }
303
304 class TrombiView extends MultipageView
305 {
306 public function __construct(PlSet &$set, $data, array $params)
307 {
308 $this->entriesPerPage = 24;
309 $this->order = explode(',', Env::v('order', 'name_sort'));
310 if (@$params['with_score']) {
311 $this->addSortKey('score', array('-score', '-watch_last', '-promo', 'name_sort'), 'pertinence');
312 }
313 $this->addSortKey('name', array('name_sort'), 'nom');
314 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
315 parent::__construct($set, $data, $params);
316 }
317
318 public function fields()
319 {
320 return "u.user_id, nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort, u.promo, u.hruid ";
321 }
322
323 public function joins()
324 {
325 return "INNER JOIN photo AS p ON (p.uid = u.user_id)
326 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
327 }
328
329 public function bounds()
330 {
331 $order = Env::v('order', $this->defaultkey);
332 $show_bounds = 0;
333 if (($order == "name") || ($order == "-name")) {
334 $this->bound_field = "nom";
335 $show_bounds = 1;
336 } elseif (($order == "promo") || ($order == "-promo")) {
337 $this->bound_field = "promo";
338 $show_bounds = -1;
339 }
340 if ($order{0} == '-') {
341 $show_bounds = -$show_bounds;
342 }
343 return $show_bounds;
344 }
345
346 public function templateName()
347 {
348 return 'include/plview.trombi.tpl';
349 }
350
351 public function apply(PlPage &$page)
352 {
353 if (!empty($GLOBALS['IS_XNET_SITE'])) {
354 global $globals;
355 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
356 }
357 return parent::apply($page);
358 }
359 }
360
361 class GeolocView implements PlView
362 {
363 private $set;
364 private $type;
365 private $params;
366
367 public function __construct(PlSet &$set, $data, array $params)
368 {
369 $this->params = $params;
370 $this->set =& $set;
371 $this->type = $data;
372 }
373
374 private function use_map()
375 {
376 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
377 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
378 }
379
380 public function args()
381 {
382 $args = $this->set->args();
383 unset($args['initfile']);
384 unset($args['mapid']);
385 return $args;
386 }
387
388 public function apply(PlPage &$page)
389 {
390 require_once 'geoloc.inc.php';
391 require_once '../modules/search/search.inc.php';
392
393 switch ($this->type) {
394 case 'icon.swf':
395 header("Content-type: application/x-shockwave-flash");
396 header("Pragma:");
397 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
398 exit;
399
400 case 'dynamap.swf':
401 header("Content-type: application/x-shockwave-flash");
402 header("Pragma:");
403 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
404 exit;
405
406 case 'init':
407 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
408 header('Content-Type: text/xml');
409 header('Pragma:');
410 if (!empty($GLOBALS['IS_XNET_SITE'])) {
411 $page->assign('background', 0xF2E9D0);
412 }
413 break;
414
415 case 'city':
416 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
417 header('Content-Type: text/xml');
418 header('Pragma:');
419 $only_current = Env::v('only_current', false)? ' AND FIND_IN_SET(\'active\', adrf.statut)' : '';
420 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, u.promo, al.alias',
421 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id $only_current)
422 LEFT JOIN aliases AS al ON (u.user_id = al.id
423 AND FIND_IN_SET('bestalias', al.flags))
424 INNER JOIN adresses AS avg ON (" . getadr_join('avg') . ")",
425 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
426 $page->assign('users', $it);
427 break;
428
429 case 'country':
430 if (Env::has('debug')) {
431 $page->changeTpl('geoloc/country.tpl', SIMPLE);
432 } else {
433 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
434 header('Content-Type: text/xml');
435 header('Pragma:');
436 }
437 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
438 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
439 $page->assign('countries', $countries);
440 $page->assign('cities', $cities);
441 break;
442
443 default:
444 global $globals;
445 if (!$this->use_map()) {
446 $page->assign('request_geodesix', true);
447 }
448 $page->assign('annu', @$this->params['with_annu']);
449 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
450 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
451 return 'include/plview.geoloc.tpl';
452 }
453 }
454 }
455
456 class GadgetView implements PlView
457 {
458 public function __construct(PlSet &$set, $data, array $params)
459 {
460 $this->set =& $set;
461 }
462
463 public function fields()
464 {
465 return "u.user_id AS id, u.*," .
466 "u.perms != 'pending' AS inscrit,
467 u.perms != 'pending' AS wasinscrit,
468 u.deces != 0 AS dcd, u.deces,
469 FIND_IN_SET('femme', u.flags) AS sexe,
470 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region" .
471 (S::logged() ? ", c.contact AS contact" : '');
472 }
473
474 public function joins()
475 {
476 return "LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : "
477 AND adr.pub = 'public'").")
478 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
479 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)" .
480 (S::logged() ?
481 "LEFT JOIN contacts AS c ON (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
482 : "");
483 }
484
485 public function apply(PlPage &$page)
486 {
487 $page->assign_by_ref('set',
488 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
489 }
490
491 public function args()
492 {
493 return null;
494 }
495 }
496
497 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
498 ?>