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