Adds MapView.
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 public function __construct(PlFilterCondition $cond, $orders = null)
25 {
26 parent::__construct($cond, $orders);
27 }
28
29 protected function buildFilter(PlFilterCondition $cond, $orders)
30 {
31 return new UserFilter($cond, $orders);
32 }
33 }
34
35 class ProfileSet extends PlSet
36 {
37 public function __construct(PlFilterCondition $cond, $orders = null)
38 {
39 parent::__construct($cond, $orders);
40 }
41
42 protected function buildFilter(PlFilterCondition $cond, $orders)
43 {
44 return new ProfileFilter($cond, $orders);
45 }
46 }
47
48 require_once "ufbuilder.inc.php";
49
50 class SearchSet extends ProfileSet
51 {
52 protected $score = null;
53 protected $valid = true;
54
55 public function __construct(UserFilterBuilder $ufb, PlFilterCondition $cond = null)
56 {
57 if (is_null($cond)) {
58 $conds = new PFC_And();
59 } else if ($cond instanceof PFC_And) {
60 $conds = $cond;
61 } else {
62 $conds = new PFC_And($cond);
63 }
64
65 if (!$ufb->isValid()) {
66 $this->valid = false;
67 return;
68 }
69
70 $ufc = $ufb->getUFC();
71 $conds->addChild($ufc);
72
73 $orders = $ufb->getOrders();
74
75 parent::__construct($conds, $orders);
76 }
77
78 public function isValid()
79 {
80 return $this->valid;
81 }
82
83 /** Add a "rechercher=Chercher" field to the query to simulate the POST
84 * behaviour.
85 */
86 public function args()
87 {
88 $args = parent::args();
89 if (!isset($args['rechercher'])) {
90 $args['rechercher'] = 'Chercher';
91 }
92 return $args;
93 }
94
95 protected function &getFilterResults(PlFilter $pf, PlLimit $limit)
96 {
97 $profiles = $pf->getProfiles($limit, Profile::FETCH_MINIFICHES);
98 return $profiles;
99 }
100 }
101
102 // Specialized SearchSet for quick search.
103 class QuickSearchSet extends SearchSet
104 {
105 public function __construct(PlFilterCondition $cond = null)
106 {
107 if (!S::logged()) {
108 Env::kill('with_soundex');
109 }
110
111 parent::__construct(new UFB_QuickSearch(), $cond);
112 }
113 }
114
115 // Specialized SearchSet for advanced search.
116 class AdvancedSearchSet extends SearchSet
117 {
118 public function __construct($xorg_admin_fields, $ax_admin_fields,
119 PlFilterCondition $cond = null)
120 {
121 parent::__construct(new UFB_AdvancedSearch($xorg_admin_fields, $ax_admin_fields),
122 $cond);
123 }
124 }
125
126 /** Simple set based on an array of User emails
127 */
128 class UserArraySet extends UserSet
129 {
130 public function __construct(array $emails)
131 {
132 parent::__construct(new UFC_Email($emails));
133 }
134 }
135
136 /** Simple set based on an array of Profile emails
137 */
138 class ProfileArraySet extends ProfileSet
139 {
140 public function __construct(array $emails)
141 {
142 parent::__construct(new UFC_Email($emails));
143 }
144 }
145
146
147 /** A multipage view for profiles or users
148 * Allows the display of bounds when sorting by name or promo.
149 */
150 abstract class MixedView extends MultipageView
151 {
152 protected function getBoundValue($obj)
153 {
154 if ($obj instanceof Profile) {
155 switch ($this->bound_field) {
156 case 'name':
157 $name = $obj->name('%l');
158 return strtoupper($name);
159 case 'promo':
160 return $obj->promo();
161 default:
162 return null;
163 }
164 } elseif ($obj instanceof User) {
165 switch ($this->bound_field) {
166 case 'name':
167 $name = $obj->lastName();
168 return strtoupper($name);
169 case 'promo':
170 if ($obj->hasProfile()) {
171 return $obj->profile()->promo();
172 } else {
173 return 'ext';
174 }
175 default:
176 return null;
177 }
178 }
179 return null;
180 }
181
182 public function bounds()
183 {
184 $order = Env::v('order', $this->defaultkey);
185 $show_bounds = 0;
186 if (($order == "name") || ($order == "-name")) {
187 $this->bound_field = "name";
188 $show_bounds = 1;
189 } elseif (($order == "promo") || ($order == "-promo")) {
190 $this->bound_field = "promo";
191 $show_bounds = -1;
192 }
193 if ($order{0} == '-') {
194 $show_bounds = -$show_bounds;
195 }
196 return $show_bounds;
197 }
198 }
199
200 /** An extended multipage view for profiles, as minifiches.
201 * Allows to sort by:
202 * - score (for a search query)
203 * - name
204 * - promo
205 * - latest modification
206 *
207 * Paramaters for this view are:
208 * - with_score: whether to allow ordering by score (set only for a quick search)
209 * - starts_with: show only names beginning with the given letter
210 */
211 class MinificheView extends MixedView
212 {
213 public function __construct(PlSet $set, array $params)
214 {
215 global $globals;
216 $this->entriesPerPage = $globals->search->per_page;
217 if (@$params['with_score']) {
218 $this->addSort(new PlViewOrder('score', array(
219 new UFO_Score(true),
220 new UFO_ProfileUpdate(true),
221 new UFO_Promo(UserFilter::DISPLAY, true),
222 new UFO_Name(Profile::DN_SORT),
223 ), 'pertinence'));
224 }
225 $this->addSort(new PlViewOrder(
226 'name',
227 array(new UFO_Name(Profile::DN_SORT)),
228 'nom'));
229 $this->addSort(new PlViewOrder('promo', array(
230 new UFO_Promo(UserFilter::DISPLAY, true),
231 new UFO_Name(Profile::DN_SORT),
232 ), 'promotion'));
233 $this->addSort(new PlViewOrder('date_mod', array(
234 new UFO_ProfileUpdate(true),
235 new UFO_Promo(UserFilter::DISPLAY, true),
236 new UFO_Name(Profile::DN_SORT),
237 ), 'dernière modification'));
238 parent::__construct($set, $params);
239 }
240
241 public function apply(PlPage $page)
242 {
243 if (array_key_exists('starts_with', $this->params)
244 && $this->params['starts_with'] != ""
245 && $this->params['starts_with'] != null) {
246
247 $this->set->addCond(
248 new UFC_Name(Profile::LASTNAME,
249 $this->params['starts_with'], UFC_Name::PREFIX)
250 );
251 }
252 return parent::apply($page);
253 }
254
255 public function templateName()
256 {
257 return 'include/plview.minifiche.tpl';
258 }
259 }
260
261 class MentorView extends MixedView
262 {
263 public function __construct(PlSet $set, array $params)
264 {
265 $this->entriesPerPage = 10;
266 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
267 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
268 $this->addSort(new PlViewOrder('promo', array(
269 new UFO_Promo(UserFilter::DISPLAY, true),
270 new UFO_Name(Profile::DN_SORT),
271 ), 'promotion'));
272 $this->addSort(new PlViewOrder('date_mod', array(
273 new UFO_ProfileUpdate(true),
274 new UFO_Promo(UserFilter::DISPLAY, true),
275 new UFO_Name(Profile::DN_SORT),
276 ), 'dernière modification'));
277 parent::__construct($set, $params);
278 }
279
280 public function templateName()
281 {
282 return 'include/plview.referent.tpl';
283 }
284 }
285
286 class GroupMemberView extends MixedView
287 {
288 public function __construct(PlSet $set, array $params)
289 {
290 $this->entriesPerPage = 20;
291 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
292 $this->addSort(new PlViewOrder('promo', array(
293 new UFO_Promo(UserFilter::DISPLAY, true),
294 new UFO_Name(Profile::DN_SORT),
295 ), 'promotion'));
296 parent::__construct($set, $params);
297 }
298
299 public function templateName()
300 {
301 return 'include/plview.groupmember.tpl';
302 }
303 }
304
305 class ListMemberView extends MixedView
306 {
307 public function __construct(PlSet $set, array $params)
308 {
309 $this->entriesPerPage = 100;
310 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
311 $this->addSort(new PlViewOrder('promo', array(
312 new UFO_Promo(UserFilter::DISPLAY, true),
313 new UFO_Name(Profile::DN_SORT),
314 ), 'promotion'));
315 parent::__construct($set, $params);
316 }
317
318 public function templateName()
319 {
320 return 'include/plview.listmember.tpl';
321 }
322 }
323
324 class TrombiView extends MixedView
325 {
326 public function __construct(PlSet $set, array $params)
327 {
328 $this->entriesPerPage = 24;
329 $this->defaultkey = 'name';
330 if (@$params['with_score']) {
331 $this->addSort(new PlViewOrder('score', array(
332 new UFO_Score(true),
333 new UFO_ProfileUpdate(true),
334 new UFO_Promo(UserFilter::DISPLAY, true),
335 new UFO_Name(Profile::DN_SORT),
336 ), 'pertinence'));
337 }
338 $set->addCond(new UFC_Photo());
339 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
340 $this->addSort(new PlViewOrder('promo', array(
341 new UFO_Promo(UserFilter::DISPLAY, true),
342 new UFO_Name(Profile::DN_SORT),
343 ), 'promotion'));
344 parent::__construct($set, $params);
345 }
346
347 public function templateName()
348 {
349 return 'include/plview.trombi.tpl';
350 }
351
352 public function apply(PlPage $page)
353 {
354 if (!empty($GLOBALS['IS_XNET_SITE'])) {
355 global $globals;
356 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
357 }
358 return parent::apply($page);
359 }
360 }
361
362 class MapView implements PlView
363 {
364 private $set;
365
366 public function __construct(PlSet $set, array $params)
367 {
368 $this->set = $set;
369 }
370
371 public function apply(PlPage $page)
372 {
373 Platal::load('geoloc');
374
375 if (Get::b('ajax')) {
376 $pids = $this->set->getIds(new PlLimit());
377 GeolocModule::assign_json_to_map($page, $pids);
378 $page->runJSON();
379 exit;
380 } else {
381 $this->set->getIds(new PlLimit());
382 GeolocModule::prepare_map($page);
383 return 'geoloc/index.tpl';
384 }
385 }
386
387 public function args()
388 {
389 return $this->set->args();
390 }
391 }
392
393 class GadgetView implements PlView
394 {
395 public function __construct(PlSet $set, array $params)
396 {
397 $this->set =& $set;
398 }
399
400 public function apply(PlPage $page)
401 {
402 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
403 }
404
405 public function args()
406 {
407 return null;
408 }
409 }
410
411 class AddressesView implements PlView
412 {
413 private $set;
414
415 public function __construct(PlSet $set, array $params)
416 {
417 $this->set =& $set;
418 }
419
420 public function apply(PlPage $page)
421 {
422 $pids = $this->set->getIds(new PlLimit());
423 $visibility = new ProfileVisibility(ProfileVisibility::VIS_AX);
424 pl_cached_content_headers('text/x-csv', 1);
425
426 $csv = fopen('php://output', 'w');
427 fputcsv($csv, array('adresses'), ';');
428 $res = XDB::query('SELECT pd.public_name, pa.postalText
429 FROM profile_addresses AS pa
430 INNER JOIN profile_display AS pd ON (pd.pid = pa.pid)
431 WHERE pa.type = \'home\' AND pa.pub IN (\'public\', \'ax\') AND FIND_IN_SET(\'mail\', pa.flags) AND pa.pid IN {?}
432 GROUP BY pa.pid', $pids);
433 foreach ($res->fetchAllAssoc() as $item) {
434 fputcsv($csv, $item, ';');
435 }
436 fclose($csv);
437 exit();
438 }
439
440 public function args()
441 {
442 return $this->set->args();
443 }
444 }
445
446 class JSonView implements PlView
447 {
448 private $set;
449 private $params;
450
451 public function __construct(PlSet $set, array $params)
452 {
453 $this->set = $set;
454 $this->params = $params;
455 }
456
457 public function apply(PlPage $page)
458 {
459 $export = array();
460 $start = isset($this->params['offset']) ? $this->params['offset'] : 0;
461 $count = isset($this->params['count']) ? $this->params['count'] : 10;
462 $profiles = $this->set->get(new PlLimit($start, $count));
463 foreach ($profiles as $profile) {
464 $export[] = $profile->export();
465 }
466 $page->jsonAssign('profile_count', $this->set->count());
467 $page->jsonAssign('profiles', $export);
468 }
469
470 public function args()
471 {
472 return $this->set->args();
473 }
474 }
475
476 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
477 ?>