Merge remote branch 'origin/xorg/maint' into xorg/1.0.2/master
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 class SearchSet extends ProfileSet
49 {
50 public $advanced = false;
51 private $score = null;
52 private $quick = false;
53 private $valid = true;
54
55 public function __construct($quick = false, PlFilterCondition $cond = null)
56 {
57 if (isset($no_search)) {
58 return;
59 }
60
61 $this->quick = $quick;
62
63 if (is_null($cond)) {
64 $conds = new PFC_And();
65 } else if ($cond instanceof PFC_And) {
66 $conds = $cond;
67 } else {
68 $conds = new PFC_And($cond);
69 }
70
71 if ($quick) {
72 $this->getQuick($conds);
73 } else {
74 $this->getAdvanced($conds);
75 }
76 }
77
78 public function isValid()
79 {
80 return $this->valid;
81 }
82
83 /** Sets up the conditions for a Quick Search
84 * @param $conds Additional conds (as a PFC_And)
85 */
86 private function getQuick($conds)
87 {
88 if (!S::logged()) {
89 Env::kill('with_soundex');
90 }
91
92 require_once 'ufbuilder.inc.php';
93 $ufb = new UFB_QuickSearch();
94
95 if (!$ufb->isValid()) {
96 $this->valid = false;
97 return;
98 }
99
100 $ufc = $ufb->getUFC();
101 $conds->addChild($ufc);
102
103 $orders = $ufb->getOrders();
104
105 if (S::logged() && Env::has('nonins')) {
106 $conds = new PFC_And($conds,
107 new PFC_Not(new UFC_Dead()),
108 new PFC_Not(new UFC_Registered())
109 );
110 }
111
112 parent::__construct($conds, $orders);
113 }
114
115 /** Sets up the conditions for an Advanced Search
116 * @param $conds Additional conds (as a PFC_And)
117 */
118 private function getAdvanced($conds)
119 {
120 $this->advanced = true;
121 require_once 'ufbuilder.inc.php';
122 $ufb = new UFB_AdvancedSearch();
123
124 if (!$ufb->isValid()) {
125 $this->valid = false;
126 return;
127 }
128
129 $ufc = $ufb->getUFC();
130 $conds->addChild($ufc);
131
132 $orders = $ufb->getOrders();
133
134 parent::__construct($conds, $orders);
135 }
136
137 /** Add a "rechercher=Chercher" field to the query to simulate the POST
138 * behaviour.
139 */
140 public function args()
141 {
142 $args = parent::args();
143 if (!isset($args['rechercher'])) {
144 $args['rechercher'] = 'Chercher';
145 }
146 return $args;
147 }
148
149 protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
150 {
151 $profiles = $pf->getProfiles($limit, Profile::FETCH_MINIFICHES);
152 return $profiles;
153 }
154 }
155
156 /** Simple set based on an array of User objects
157 */
158 class ArraySet extends ProfileSet
159 {
160 public function __construct(array $users)
161 {
162 $hruids = User::getBulkHruid($users, array('User', '_silent_user_callback'));
163 if (is_null($hruids) || count($hruids) == 0) {
164 $cond = new PFC_False();
165 } else {
166 $cond = new UFC_Hruid($hruids);
167 }
168 parent::__construct($cond);
169 }
170 }
171
172 /** A multipage view for profiles
173 * Allows the display of bounds when sorting by name or promo.
174 */
175 abstract class ProfileView extends MultipageView
176 {
177 protected function getBoundValue($obj)
178 {
179 if ($obj instanceof Profile) {
180 switch ($this->bound_field) {
181 case 'name':
182 $name = $obj->name('%l');
183 return strtoupper($name);
184 case 'promo':
185 return $obj->promo();
186 default:
187 return null;
188 }
189 }
190 return null;
191 }
192
193 public function bounds()
194 {
195 $order = Env::v('order', $this->defaultkey);
196 $show_bounds = 0;
197 if (($order == "name") || ($order == "-name")) {
198 $this->bound_field = "name";
199 $show_bounds = 1;
200 } elseif (($order == "promo") || ($order == "-promo")) {
201 $this->bound_field = "promo";
202 $show_bounds = -1;
203 }
204 if ($order{0} == '-') {
205 $show_bounds = -$show_bounds;
206 }
207 return $show_bounds;
208 }
209 }
210
211 /** An extended multipage view for profiles, as minifiches.
212 * Allows to sort by:
213 * - score (for a search query)
214 * - name
215 * - promo
216 * - latest modification
217 *
218 * Paramaters for this view are:
219 * - with_score: whether to allow ordering by score (set only for a quick search)
220 * - starts_with: show only names beginning with the given letter
221 */
222 class MinificheView extends ProfileView
223 {
224 public function __construct(PlSet &$set, array $params)
225 {
226 global $globals;
227 $this->entriesPerPage = $globals->search->per_page;
228 if (@$params['with_score']) {
229 $this->addSort(new PlViewOrder('score', array(
230 new UFO_Score(true),
231 new UFO_ProfileUpdate(true),
232 new UFO_Promo(UserFilter::DISPLAY, true),
233 new UFO_Name(Profile::DN_SORT),
234 ), 'pertinence'));
235 }
236 $this->addSort(new PlViewOrder(
237 'name',
238 array(new UFO_Name(Profile::DN_SORT)),
239 'nom'));
240 $this->addSort(new PlViewOrder('promo', array(
241 new UFO_Promo(UserFilter::DISPLAY, true),
242 new UFO_Name(Profile::DN_SORT),
243 ), 'promotion'));
244 $this->addSort(new PlViewOrder('date_mod', array(
245 new UFO_ProfileUpdate(true),
246 new UFO_Promo(UserFilter::DISPLAY, true),
247 new UFO_Name(Profile::DN_SORT),
248 ), 'dernière modification'));
249 parent::__construct($set, $params);
250 }
251
252 public function apply(PlPage &$page)
253 {
254 if (array_key_exists('starts_with', $this->params)
255 && $this->params['starts_with'] != ""
256 && $this->params['starts_with'] != null) {
257
258 $this->set->addCond(
259 new UFC_Name(Profile::LASTNAME,
260 $this->params['starts_with'], UFC_Name::PREFIX)
261 );
262 }
263 return parent::apply($page);
264 }
265
266 public function templateName()
267 {
268 return 'include/plview.minifiche.tpl';
269 }
270 }
271
272 class MentorView extends ProfileView
273 {
274 public function __construct(PlSet &$set, array $params)
275 {
276 $this->entriesPerPage = 10;
277 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
278 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
279 $this->addSort(new PlViewOrder('promo', array(
280 new UFO_Promo(UserFilter::DISPLAY, true),
281 new UFO_Name(Profile::DN_SORT),
282 ), 'promotion'));
283 $this->addSort(new PlViewOrder('date_mod', array(
284 new UFO_ProfileUpdate(true),
285 new UFO_Promo(UserFilter::DISPLAY, true),
286 new UFO_Name(Profile::DN_SORT),
287 ), 'dernière modification'));
288 parent::__construct($set, $params);
289 }
290
291 public function templateName()
292 {
293 return 'include/plview.referent.tpl';
294 }
295 }
296
297 class TrombiView extends ProfileView
298 {
299 public function __construct(PlSet &$set, array $params)
300 {
301 $this->entriesPerPage = 24;
302 $this->defaultkey = 'name';
303 if (@$params['with_score']) {
304 $this->addSort(new PlViewOrder('score', array(
305 new UFO_Score(true),
306 new UFO_ProfileUpdate(true),
307 new UFO_Promo(UserFilter::DISPLAY, true),
308 new UFO_Name(Profile::DN_SORT),
309 ), 'pertinence'));
310 }
311 $set->addCond(new UFC_Photo());
312 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
313 $this->addSort(new PlViewOrder('promo', array(
314 new UFO_Promo(UserFilter::DISPLAY, true),
315 new UFO_Name(Profile::DN_SORT),
316 ), 'promotion'));
317 parent::__construct($set, $params);
318 }
319
320 public function templateName()
321 {
322 return 'include/plview.trombi.tpl';
323 }
324
325 public function apply(PlPage &$page)
326 {
327 if (!empty($GLOBALS['IS_XNET_SITE'])) {
328 global $globals;
329 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
330 }
331 return parent::apply($page);
332 }
333 }
334
335 class GadgetView implements PlView
336 {
337 public function __construct(PlSet &$set, array $params)
338 {
339 $this->set =& $set;
340 }
341
342 public function apply(PlPage &$page)
343 {
344 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
345 }
346
347 public function args()
348 {
349 return null;
350 }
351 }
352
353 class AddressesView implements PlView
354 {
355 private $set;
356
357 public function __construct(PlSet &$set, array $params)
358 {
359 $this->set =& $set;
360 }
361
362 public function apply(PlPage &$page)
363 {
364 $pids = $this->set->getIds(new PlLimit());
365 $visibility = new ProfileVisibility(ProfileVisibility::VIS_AX);
366 pl_content_headers('text/x-csv');
367
368 $csv = fopen('php://output', 'w');
369 fputcsv($csv, array('adresses'), ';');
370 $res = XDB::query('SELECT pd.public_name, pa.postalText
371 FROM profile_addresses AS pa
372 INNER JOIN profile_display AS pd ON (pd.pid = pa.pid)
373 WHERE pa.type = \'home\' AND pa.pub IN (\'public\', \'ax\') AND FIND_IN_SET(\'mail\', pa.flags) AND pa.pid IN {?}
374 GROUP BY pa.pid', $pids);
375 foreach ($res->fetchAllAssoc() as $item) {
376 fputcsv($csv, $item, ';');
377 }
378 fclose($csv);
379 exit();
380 }
381
382 public function args()
383 {
384 return $this->set->args();
385 }
386 }
387
388 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
389 ?>