Use Get::set() and Post::set() instead of direct assignations of
[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
8eb6931f
RB
126/** Simple set based on an array of User objects
127 */
470d14f6 128class ArraySet extends ProfileSet
1cc0afe7 129{
130 public function __construct(array $users)
131 {
8e720253
RB
132 $hruids = User::getBulkHruid($users, array('User', '_silent_user_callback'));
133 if (is_null($hruids) || count($hruids) == 0) {
134 $cond = new PFC_False();
1cc0afe7 135 } else {
8e720253 136 $cond = new UFC_Hruid($hruids);
1cc0afe7 137 }
8e720253 138 parent::__construct($cond);
1cc0afe7 139 }
140}
141
8eb6931f
RB
142/** A multipage view for profiles
143 * Allows the display of bounds when sorting by name or promo.
144 */
169edfac
RB
145abstract class ProfileView extends MultipageView
146{
147 protected function getBoundValue($obj)
148 {
149 if ($obj instanceof Profile) {
150 switch ($this->bound_field) {
151 case 'name':
152 $name = $obj->name('%l');
153 return strtoupper($name);
154 case 'promo':
155 return $obj->promo();
156 default:
157 return null;
158 }
159 }
160 return null;
161 }
316120eb
FB
162
163 public function bounds()
164 {
165 $order = Env::v('order', $this->defaultkey);
166 $show_bounds = 0;
167 if (($order == "name") || ($order == "-name")) {
168 $this->bound_field = "name";
169 $show_bounds = 1;
170 } elseif (($order == "promo") || ($order == "-promo")) {
171 $this->bound_field = "promo";
172 $show_bounds = -1;
173 }
174 if ($order{0} == '-') {
175 $show_bounds = -$show_bounds;
176 }
177 return $show_bounds;
178 }
169edfac
RB
179}
180
8eb6931f
RB
181/** An extended multipage view for profiles, as minifiches.
182 * Allows to sort by:
183 * - score (for a search query)
184 * - name
185 * - promo
186 * - latest modification
187 *
188 * Paramaters for this view are:
189 * - with_score: whether to allow ordering by score (set only for a quick search)
190 * - starts_with: show only names beginning with the given letter
191 */
169edfac 192class MinificheView extends ProfileView
8c4a0c30 193{
26ba053e 194 public function __construct(PlSet $set, array $params)
8c4a0c30 195 {
8c4a0c30 196 global $globals;
197 $this->entriesPerPage = $globals->search->per_page;
35fa92e8 198 if (@$params['with_score']) {
8e720253
RB
199 $this->addSort(new PlViewOrder('score', array(
200 new UFO_Score(true),
201 new UFO_ProfileUpdate(true),
202 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 203 new UFO_Name(Profile::DN_SORT),
8e720253 204 ), 'pertinence'));
35fa92e8 205 }
8e720253
RB
206 $this->addSort(new PlViewOrder(
207 'name',
913a4e90 208 array(new UFO_Name(Profile::DN_SORT)),
8e720253
RB
209 'nom'));
210 $this->addSort(new PlViewOrder('promo', array(
211 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 212 new UFO_Name(Profile::DN_SORT),
8e720253
RB
213 ), 'promotion'));
214 $this->addSort(new PlViewOrder('date_mod', array(
215 new UFO_ProfileUpdate(true),
216 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 217 new UFO_Name(Profile::DN_SORT),
8e720253 218 ), 'dernière modification'));
8eb6931f
RB
219 parent::__construct($set, $params);
220 }
221
26ba053e 222 public function apply(PlPage $page)
8eb6931f
RB
223 {
224 if (array_key_exists('starts_with', $this->params)
225 && $this->params['starts_with'] != ""
226 && $this->params['starts_with'] != null) {
227
228 $this->set->addCond(
229 new UFC_Name(Profile::LASTNAME,
230 $this->params['starts_with'], UFC_Name::PREFIX)
231 );
232 }
233 return parent::apply($page);
8c4a0c30 234 }
235
8c4a0c30 236 public function templateName()
237 {
238 return 'include/plview.minifiche.tpl';
239 }
240}
241
169edfac 242class MentorView extends ProfileView
ff3eb9b7 243{
26ba053e 244 public function __construct(PlSet $set, array $params)
ff3eb9b7 245 {
eaf30d86 246 $this->entriesPerPage = 10;
8e720253 247 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
913a4e90 248 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
8e720253
RB
249 $this->addSort(new PlViewOrder('promo', array(
250 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 251 new UFO_Name(Profile::DN_SORT),
8e720253
RB
252 ), 'promotion'));
253 $this->addSort(new PlViewOrder('date_mod', array(
254 new UFO_ProfileUpdate(true),
255 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 256 new UFO_Name(Profile::DN_SORT),
8e720253 257 ), 'dernière modification'));
8eb6931f 258 parent::__construct($set, $params);
ff3eb9b7 259 }
260
ff3eb9b7 261 public function templateName()
262 {
263 return 'include/plview.referent.tpl';
264 }
265}
266
169edfac 267class TrombiView extends ProfileView
8c4a0c30 268{
26ba053e 269 public function __construct(PlSet $set, array $params)
8c4a0c30 270 {
271 $this->entriesPerPage = 24;
316120eb 272 $this->defaultkey = 'name';
35fa92e8 273 if (@$params['with_score']) {
8e720253
RB
274 $this->addSort(new PlViewOrder('score', array(
275 new UFO_Score(true),
276 new UFO_ProfileUpdate(true),
277 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 278 new UFO_Name(Profile::DN_SORT),
8e720253 279 ), 'pertinence'));
35fa92e8 280 }
316120eb 281 $set->addCond(new UFC_Photo());
913a4e90 282 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
8e720253
RB
283 $this->addSort(new PlViewOrder('promo', array(
284 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 285 new UFO_Name(Profile::DN_SORT),
8e720253 286 ), 'promotion'));
8eb6931f 287 parent::__construct($set, $params);
8c4a0c30 288 }
289
8c4a0c30 290 public function templateName()
291 {
292 return 'include/plview.trombi.tpl';
293 }
294
26ba053e 295 public function apply(PlPage $page)
8c4a0c30 296 {
297 if (!empty($GLOBALS['IS_XNET_SITE'])) {
298 global $globals;
299 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
300 }
301 return parent::apply($page);
302 }
303}
304
92e80560
VZ
305class GadgetView implements PlView
306{
26ba053e 307 public function __construct(PlSet $set, array $params)
92e80560
VZ
308 {
309 $this->set =& $set;
310 }
311
26ba053e 312 public function apply(PlPage $page)
92e80560 313 {
8e720253 314 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
92e80560
VZ
315 }
316
317 public function args()
318 {
319 return null;
320 }
321}
322
39394cbf
SJ
323class AddressesView implements PlView
324{
325 private $set;
326
26ba053e 327 public function __construct(PlSet $set, array $params)
39394cbf
SJ
328 {
329 $this->set =& $set;
330 }
331
26ba053e 332 public function apply(PlPage $page)
39394cbf 333 {
1319b09e 334 $pids = $this->set->getIds(new PlLimit());
39394cbf 335 $visibility = new ProfileVisibility(ProfileVisibility::VIS_AX);
023c46fb 336 pl_cached_content_headers('text/x-csv', 1);
39394cbf
SJ
337
338 $csv = fopen('php://output', 'w');
339 fputcsv($csv, array('adresses'), ';');
1319b09e
SJ
340 $res = XDB::query('SELECT pd.public_name, pa.postalText
341 FROM profile_addresses AS pa
342 INNER JOIN profile_display AS pd ON (pd.pid = pa.pid)
343 WHERE pa.type = \'home\' AND pa.pub IN (\'public\', \'ax\') AND FIND_IN_SET(\'mail\', pa.flags) AND pa.pid IN {?}
344 GROUP BY pa.pid', $pids);
345 foreach ($res->fetchAllAssoc() as $item) {
346 fputcsv($csv, $item, ';');
39394cbf
SJ
347 }
348 fclose($csv);
349 exit();
350 }
351
352 public function args()
353 {
354 return $this->set->args();
355 }
356}
357
8c4a0c30 358// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
359?>