merge with master
[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 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
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 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) {
74 $this->getQuick($join, $where);
75 } else {
76 $this->getAdvanced($join, $where);
77 }
78 }
79
80 private function getQuick($join, $where)
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()) {
90 new ThrowError('Aucun critère de recherche n\'est spécifié.');
91 }
92 $this->score = $qSearch->get_score_statement();
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);
107
108 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
109 'u.promo DESC, NomSortKey, prenom')));
110 }
111
112 private function getAdvanced($join, $where)
113 {
114 global $globals;
115 $this->advanced = true;
116 $fields = new SFieldGroup(true, advancedSearchFromInput());
117 if ($fields->too_large()) {
118 new ThrowError('Recherche trop générale.');
119 }
120 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
121 @$where . ' ' . $fields->get_where_statement());
122 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
123 'promo DESC, NomSortKey, prenom')));
124 }
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 }
133 }
134
135 class 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
158 class 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;
165 if (@$params['with_score']) {
166 $this->addSortKey('score', array('-score', '-date', '-promo', 'name_sort'), 'pertinence');
167 }
168 $this->addSortKey('name', array('name_sort'), 'nom');
169 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
170 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
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 (COUNT(em.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif,
188 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort" .
189 (S::logged() ? ", c.contact AS contact" : '');
190 }
191
192 public function joins()
193 {
194 return "LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id".(S::logged() ? "" : " AND e.pub = 'public'").")
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
203 AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : " AND adr.pub = 'public'").")
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)
206 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = 'active')
207 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)" .
208 (S::logged() ?
209 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
210 : "");
211 }
212
213 public function bounds()
214 {
215 $order = Env::v('order', $this->defaultkey);
216 $show_bounds = 0;
217 if (($order == "name") || ($order == "-name")) {
218 $this->bound_field = "nom";
219 $show_bounds = 1;
220 } elseif (($order == "promo") || ($order == "-promo")) {
221 $this->bound_field = "promo";
222 $show_bounds = -1;
223 }
224 if ($order{0} == '-') {
225 $show_bounds = -$show_bounds;
226 }
227 return $show_bounds;
228 }
229
230 public function templateName()
231 {
232 return 'include/plview.minifiche.tpl';
233 }
234 }
235
236 class MentorView extends MultipageView
237 {
238 public function __construct(PlSet &$set, $data, array $params)
239 {
240 $this->entriesPerPage = 10;
241 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
242 $this->addSortKey('name', array('name_sort'), 'nom');
243 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
244 $this->addSortKey('date_mod', array('-date', '-promo', 'name_sort'), 'dernière modification');
245 parent::__construct($set, $data, $params);
246 }
247
248 public function fields()
249 {
250 return "m.uid, u.promo,
251 a.alias AS bestalias, m.expertise, mp.pid,
252 ms.secteur, ms.ss_secteur,
253 nd.display AS name_display, nd.tooltip AS name_tooltip, nd.sort AS name_sort";
254 }
255
256 public function joins()
257 {
258 return "INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
259 }
260
261 public function bounds()
262 {
263 $order = Env::v('order', $this->defaultkey);
264 $show_bounds = 0;
265 if (($order == "name") || ($order == "-name")) {
266 $this->bound_field = "nom";
267 $show_bounds = 1;
268 } elseif (($order == "promo") || ($order == "-promo")) {
269 $this->bound_field = "promo";
270 $show_bounds = -1;
271 }
272 if ($order{0} == '-') {
273 $show_bounds = -$show_bounds;
274 }
275 return $show_bounds;
276 }
277
278 public function templateName()
279 {
280 return 'include/plview.referent.tpl';
281 }
282 }
283
284 class TrombiView extends MultipageView
285 {
286 public function __construct(PlSet &$set, $data, array $params)
287 {
288 $this->entriesPerPage = 24;
289 $this->order = explode(',', Env::v('order', 'name_sort'));
290 if (@$params['with_score']) {
291 $this->addSortKey('score', array('-score', '-watch_last', '-promo', 'name_sort'), 'pertinence');
292 }
293 $this->addSortKey('name', array('name_sort'), 'nom');
294 $this->addSortKey('promo', array('-promo', 'name_sort'), 'promotion');
295 parent::__construct($set, $data, $params);
296 }
297
298 public function fields()
299 {
300 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 ";
301 }
302
303 public function joins()
304 {
305 return "INNER JOIN photo AS p ON (p.uid = u.user_id)
306 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)";
307 }
308
309 public function bounds()
310 {
311 $order = Env::v('order', $this->defaultkey);
312 $show_bounds = 0;
313 if (($order == "name") || ($order == "-name")) {
314 $this->bound_field = "nom";
315 $show_bounds = 1;
316 } elseif (($order == "promo") || ($order == "-promo")) {
317 $this->bound_field = "promo";
318 $show_bounds = -1;
319 }
320 if ($order{0} == '-') {
321 $show_bounds = -$show_bounds;
322 }
323 return $show_bounds;
324 }
325
326 public function templateName()
327 {
328 return 'include/plview.trombi.tpl';
329 }
330
331 public function apply(PlPage &$page)
332 {
333 if (!empty($GLOBALS['IS_XNET_SITE'])) {
334 global $globals;
335 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
336 }
337 return parent::apply($page);
338 }
339 }
340
341 class GeolocView implements PlView
342 {
343 private $set;
344 private $type;
345 private $params;
346
347 public function __construct(PlSet &$set, $data, array $params)
348 {
349 $this->params = $params;
350 $this->set =& $set;
351 $this->type = $data;
352 }
353
354 private function use_map()
355 {
356 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
357 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
358 }
359
360 public function args()
361 {
362 $args = $this->set->args();
363 unset($args['initfile']);
364 unset($args['mapid']);
365 return $args;
366 }
367
368 public function apply(PlPage &$page)
369 {
370 require_once 'geoloc.inc.php';
371 require_once '../modules/search/search.inc.php';
372
373 switch ($this->type) {
374 case 'icon.swf':
375 header("Content-type: application/x-shockwave-flash");
376 header("Pragma:");
377 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
378 exit;
379
380 case 'dynamap.swf':
381 header("Content-type: application/x-shockwave-flash");
382 header("Pragma:");
383 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
384 exit;
385
386 case 'init':
387 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
388 header('Content-Type: text/xml');
389 header('Pragma:');
390 if (!empty($GLOBALS['IS_XNET_SITE'])) {
391 $page->assign('background', 0xF2E9D0);
392 }
393 break;
394
395 case 'city':
396 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
397 header('Content-Type: text/xml');
398 header('Pragma:');
399 $only_current = Env::v('only_current', false)? ' AND FIND_IN_SET(\'active\', adrf.statut)' : '';
400 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, u.promo, al.alias',
401 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id $only_current)
402 LEFT JOIN aliases AS al ON (u.user_id = al.id
403 AND FIND_IN_SET('bestalias', al.flags))
404 INNER JOIN adresses AS avg ON (" . getadr_join('avg') . ")",
405 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
406 $page->assign('users', $it);
407 break;
408
409 case 'country':
410 if (Env::has('debug')) {
411 $page->changeTpl('geoloc/country.tpl', SIMPLE);
412 } else {
413 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
414 header('Content-Type: text/xml');
415 header('Pragma:');
416 }
417 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
418 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
419 $page->assign('countries', $countries);
420 $page->assign('cities', $cities);
421 break;
422
423 default:
424 global $globals;
425 if (!$this->use_map()) {
426 $page->assign('request_geodesix', true);
427 }
428 $page->assign('annu', @$this->params['with_annu']);
429 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
430 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
431 return 'include/plview.geoloc.tpl';
432 }
433 }
434 }
435
436 class GadgetView implements PlView
437 {
438 public function __construct(PlSet &$set, $data, array $params)
439 {
440 $this->set =& $set;
441 }
442
443 public function fields()
444 {
445 return "u.user_id AS id,
446 u.*, a.alias AS forlife," .
447 "u.perms != 'pending' AS inscrit,
448 u.perms != 'pending' AS wasinscrit,
449 u.deces != 0 AS dcd, u.deces,
450 FIND_IN_SET('femme', u.flags) AS sexe,
451 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region" .
452 (S::logged() ? ", c.contact AS contact" : '');
453 }
454
455 public function joins()
456 {
457 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'").")
458 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
459 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)" .
460 (S::logged() ?
461 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
462 : "");
463 }
464
465 public function apply(PlPage &$page)
466 {
467 $page->assign_by_ref('set',
468 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
469 }
470
471 public function args()
472 {
473 return null;
474 }
475 }
476
477 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
478 ?>