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