0eb0033dbd40f58a42fd8d2298ed750ba5e2617e
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 class UserSet extends PlSet
23 {
24 private $cond;
25
26 public function __construct($cond = null)
27 {
28 $this->cond = new UFC_And();
29 if (!is_null($cond)) {
30 $this->cond->addChild($cond);
31 }
32 }
33
34 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
35 {
36 $uf = new UserFilter($this->cond);
37 $users = $uf->getUsers($limitcount, $limitfrom);
38 $this->count = $uf->getTotalCount();
39 return $users;
40 }
41 }
42
43 class SearchSet extends UserSet
44 {
45 public $advanced = false;
46 private $score = null;
47 private $order = null;
48 private $quick = false;
49
50 public function __construct($quick = false, $no_search = false, $join = '', $where = '')
51 {
52 Platal::load('search', 'search.inc.php');
53 if ($no_search) {
54 return;
55 }
56
57 $this->quick = $quick;
58 if ($quick) {
59 $this->getQuick($join, $where);
60 } else {
61 $this->getAdvanced($join, $where);
62 }
63 }
64
65 private function getQuick($join, $where)
66 {
67 Platal::load('search', 'search.inc.php');
68 global $globals;
69 if (!S::logged()) {
70 Env::kill('with_soundex');
71 }
72 $qSearch = new QuickSearch('quick');
73 $fields = new SFieldGroup(true, array($qSearch));
74 if ($qSearch->isEmpty()) {
75 new ThrowError('Aucun critère de recherche n\'est spécifié.');
76 }
77 $this->score = $qSearch->get_score_statement();
78 $pwhere = $fields->get_where_statement();
79 if (trim($pwhere)) {
80 if (trim($where)) {
81 $where .= ' AND ';
82 }
83 $where .= $pwhere;
84 }
85 if (S::logged() && Env::has('nonins')) {
86 if (trim($where)) {
87 $where .= ' AND ';
88 }
89 $where .= 'u.perms="pending" AND u.deces=0';
90 }
91 parent::__construct($join . ' ' . $fields->get_select_statement(), $where);
92
93 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
94 'u.promo DESC, NomSortKey, prenom')));
95 }
96
97 private function getAdvanced($join, $where)
98 {
99 global $globals;
100 $this->advanced = true;
101 $fields = new SFieldGroup(true, advancedSearchFromInput());
102 if ($fields->too_large()) {
103 new ThrowError('Recherche trop générale.');
104 }
105 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
106 @$where . ' ' . $fields->get_where_statement());
107 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
108 'promo DESC, NomSortKey, prenom')));
109 }
110
111 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
112 {
113 if ($this->score) {
114 $fields .= ', ' . $this->score;
115 }
116 return parent::get($fields, $joins, $where, $groupby, $order, $limitcount, $limitfrom);
117 }
118 }
119
120 class ArraySet extends UserSet
121 {
122 public function __construct(array $users)
123 {
124 $where = $this->getUids($users);
125 if ($where) {
126 $where = "u.hruid IN ($where)";
127 } else {
128 $where = " 0 ";
129 }
130 parent::__construct('', $where);
131 }
132
133 private function getUids(array $users)
134 {
135 $users = User::getBulkHruid($users, array('User', '_silent_user_callback'));
136 if (is_null($users)) {
137 return '';
138 }
139 return '\'' . implode('\', \'', $users) . '\'';
140 }
141 }
142
143 class MinificheView extends MultipageView
144 {
145 public function __construct(PlSet &$set, $data, array $params)
146 {
147 require_once 'education.func.inc.php';
148 global $globals;
149 $this->entriesPerPage = $globals->search->per_page;
150 if (@$params['with_score']) {
151 $this->addSortKey('score', array('-score', '-date', '-d.promo', 'sort_name'), 'pertinence');
152 }
153 $this->addSortKey('name', array('sort_name'), 'nom');
154 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
155 $this->addSortKey('date_mod', array('-date', '-d.promo', 'sort_name'), 'dernière modification');
156 parent::__construct($set, $data, $params);
157 }
158
159 public function bounds()
160 {
161 return null;
162 }
163
164 public function fields()
165 {
166 return null;
167 }
168
169 public function templateName()
170 {
171 return 'include/plview.minifiche.tpl';
172 }
173 }
174
175 class MentorView extends MultipageView
176 {
177 public function __construct(PlSet &$set, $data, array $params)
178 {
179 $this->entriesPerPage = 10;
180 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
181 $this->addSortKey('name', array('sort_name'), 'nom');
182 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
183 $this->addSortKey('date_mod', array('-date', '-d.promo', 'sort_name'), 'dernière modification');
184 parent::__construct($set, $data, $params);
185 }
186
187 public function fields()
188 {
189 return "m.uid, d.promo, u.hruid,
190 m.expertise, mp.country, ms.sectorid, ms.subsectorid,
191 d.directory_name, d.sort_name";
192 }
193
194 public function joins()
195 {
196 return "INNER JOIN profile_display AS d ON (d.pid = u.user_id)";
197 }
198
199 public function bounds()
200 {
201 $order = Env::v('order', $this->defaultkey);
202 $show_bounds = 0;
203 if (($order == "name") || ($order == "-name")) {
204 $this->bound_field = "nom";
205 $show_bounds = 1;
206 } elseif (($order == "promo") || ($order == "-promo")) {
207 $this->bound_field = "promo";
208 $show_bounds = -1;
209 }
210 if ($order{0} == '-') {
211 $show_bounds = -$show_bounds;
212 }
213 return $show_bounds;
214 }
215
216 public function templateName()
217 {
218 return 'include/plview.referent.tpl';
219 }
220 }
221
222 class TrombiView extends MultipageView
223 {
224 public function __construct(PlSet &$set, $data, array $params)
225 {
226 $this->entriesPerPage = 24;
227 $this->order = explode(',', Env::v('order', 'sort_name'));
228 if (@$params['with_score']) {
229 $this->addSortKey('score', array('-score', '-watch_last', '-d.promo', 'sort_name'), 'pertinence');
230 }
231 $this->addSortKey('name', array('sort_name'), 'nom');
232 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
233 parent::__construct($set, $data, $params);
234 }
235
236 public function fields()
237 {
238 return "u.user_id, d.directory_name, d.sort_name, u.promo, d.promo, u.hruid ";
239 }
240
241 public function joins()
242 {
243 return "INNER JOIN photo AS p ON (p.uid = u.user_id)
244 INNER JOIN profile_display AS d ON (d.pid = u.user_id)";
245 }
246
247 public function bounds()
248 {
249 $order = Env::v('order', $this->defaultkey);
250 $show_bounds = 0;
251 if (($order == "name") || ($order == "-name")) {
252 $this->bound_field = "nom";
253 $show_bounds = 1;
254 } elseif (($order == "promo") || ($order == "-promo")) {
255 $this->bound_field = "promo";
256 $show_bounds = -1;
257 }
258 if ($order{0} == '-') {
259 $show_bounds = -$show_bounds;
260 }
261 return $show_bounds;
262 }
263
264 public function templateName()
265 {
266 return 'include/plview.trombi.tpl';
267 }
268
269 public function apply(PlPage &$page)
270 {
271 if (!empty($GLOBALS['IS_XNET_SITE'])) {
272 global $globals;
273 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
274 }
275 return parent::apply($page);
276 }
277 }
278
279 class GeolocView implements PlView
280 {
281 private $set;
282 private $type;
283 private $params;
284
285 public function __construct(PlSet &$set, $data, array $params)
286 {
287 $this->params = $params;
288 $this->set =& $set;
289 $this->type = $data;
290 }
291
292 private function use_map()
293 {
294 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
295 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
296 }
297
298 public function args()
299 {
300 $args = $this->set->args();
301 unset($args['initfile']);
302 unset($args['mapid']);
303 return $args;
304 }
305
306 public function apply(PlPage &$page)
307 {
308 require_once 'geoloc.inc.php';
309 require_once '../modules/search/search.inc.php';
310
311 switch ($this->type) {
312 case 'icon.swf':
313 header("Content-type: application/x-shockwave-flash");
314 header("Pragma:");
315 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
316 exit;
317
318 case 'dynamap.swf':
319 header("Content-type: application/x-shockwave-flash");
320 header("Pragma:");
321 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
322 exit;
323
324 case 'init':
325 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
326 header('Content-Type: text/xml');
327 header('Pragma:');
328 if (!empty($GLOBALS['IS_XNET_SITE'])) {
329 $page->assign('background', 0xF2E9D0);
330 }
331 break;
332
333 case 'city':
334 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
335 header('Content-Type: text/xml');
336 header('Pragma:');
337 $only_current = Env::v('only_current', false)? ' AND FIND_IN_SET(\'active\', adrf.statut)' : '';
338 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, d.promo, al.alias',
339 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id $only_current)
340 INNER JOIN profile_display AS d ON (d.pid = u.user_id)
341 LEFT JOIN aliases AS al ON (u.user_id = al.id
342 AND FIND_IN_SET('bestalias', al.flags))
343 INNER JOIN adresses AS avg ON (" . getadr_join('avg') . ")",
344 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
345 $page->assign('users', $it);
346 break;
347
348 case 'country':
349 if (Env::has('debug')) {
350 $page->changeTpl('geoloc/country.tpl', SIMPLE);
351 } else {
352 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
353 header('Content-Type: text/xml');
354 header('Pragma:');
355 }
356 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
357 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
358 $page->assign('countries', $countries);
359 $page->assign('cities', $cities);
360 break;
361
362 default:
363 global $globals;
364 if (!$this->use_map()) {
365 $page->assign('request_geodesix', true);
366 }
367 $page->assign('annu', @$this->params['with_annu']);
368 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
369 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
370 return 'include/plview.geoloc.tpl';
371 }
372 }
373 }
374
375 class GadgetView implements PlView
376 {
377 public function __construct(PlSet &$set, $data, array $params)
378 {
379 $this->set =& $set;
380 }
381
382 public function fields()
383 {
384 return "u.user_id AS id, u.*," .
385 "u.perms != 'pending' AS inscrit,
386 u.perms != 'pending' AS wasinscrit,
387 u.deces != 0 AS dcd, u.deces,
388 FIND_IN_SET('femme', u.flags) AS sexe,
389 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region" .
390 (S::logged() ? ", c.contact AS contact" : '');
391 }
392
393 public function joins()
394 {
395 return "LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)".(S::logged() ? "" : "
396 AND adr.pub = 'public'").")
397 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
398 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)" .
399 (S::logged() ?
400 "LEFT JOIN contacts AS c ON (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
401 : "");
402 }
403
404 public function apply(PlPage &$page)
405 {
406 $page->assign_by_ref('set',
407 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
408 }
409
410 public function args()
411 {
412 return null;
413 }
414 }
415
416 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
417 ?>