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