Ooops
[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
54 public function __construct($quick = false, $no_search = false, PlFilterCondition $cond = null)
55 {
56 Platal::load('search', 'search.inc.php');
57
58 if ($no_search) {
59 return;
60 }
61
62 $this->quick = $quick;
63
64 if (is_null($cond)) {
65 $this->conds = new PFC_And();
66 } else if ($cond instanceof PFC_And) {
67 $this->conds = $cond;
68 } else {
69 $this->conds = new PFC_And($cond);
70 }
71
72 if ($quick) {
73 $this->getQuick();
74 } else {
75 $this->getAdvanced();
76 }
77 }
78
79 private function getQuick()
80 {
81 if (!S::logged()) {
82 Env::kill('with_soundex');
83 }
84
85 require_once 'ufbuilder.inc.php';
86 $ufb = new UFB_QuickSearch();
87
88 if (!$ufb->isValid()) {
89 return;
90 }
91
92 $ufc = $ufb->getUFC();
93 $this->conds->addChild($ufc);
94
95 $orders = $ufb->getOrders();
96 $orders[] = new UFO_Promo(UserFilter::DISPLAY, true);
97 $orders[] = new UFO_Name(Profile::DN_SORT);
98
99 if (S::logged() && Env::has('nonins')) {
100 $this->conds = new PFC_And($this->conds,
101 new PFC_Not(new UFC_Dead()),
102 new PFC_Not(new UFC_Registered())
103 );
104 }
105
106 parent::__construct($this->conds, $orders);
107 }
108
109 private function getAdvanced()
110 {
111 $this->advanced = true;
112 require_once 'ufbuilder.inc.php';
113 $ufb = new UFB_AdvancedSearch();
114
115 if (!$ufb->isValid()) {
116 return;
117 }
118
119 $this->conds->addChild($ufb->getUFC());
120 }
121
122 public function &get(PlLimit $limit = null, $orders = array())
123 {
124 $orders = array_merge($orders, $this->orders);
125
126 $uf = $this->buildFilter($this->conds, $orders);
127 if (is_null($limit)) {
128 $limit = new PlLimit(20, 0);
129 }
130 $it = $uf->getProfiles($limit);
131 $this->count = $uf->getTotalCount();
132 return $it;
133 }
134 }
135
136 class ArraySet extends UserSet
137 {
138 public function __construct(array $users)
139 {
140 $hruids = User::getBulkHruid($users, array('User', '_silent_user_callback'));
141 if (is_null($hruids) || count($hruids) == 0) {
142 $cond = new PFC_False();
143 } else {
144 $cond = new UFC_Hruid($hruids);
145 }
146 parent::__construct($cond);
147 }
148 }
149
150 class MinificheView extends MultipageView
151 {
152 public function __construct(PlSet &$set, $data, array $params)
153 {
154 require_once 'education.func.inc.php';
155 global $globals;
156 $this->entriesPerPage = $globals->search->per_page;
157 if (@$params['with_score']) {
158 $this->addSort(new PlViewOrder('score', array(
159 new UFO_Score(true),
160 new UFO_ProfileUpdate(true),
161 new UFO_Promo(UserFilter::DISPLAY, true),
162 new UFO_Name(Profile::DN_SORT),
163 ), 'pertinence'));
164 }
165 $this->addSort(new PlViewOrder(
166 'name',
167 array(new UFO_Name(Profile::DN_SORT)),
168 'nom'));
169 $this->addSort(new PlViewOrder('promo', array(
170 new UFO_Promo(UserFilter::DISPLAY, true),
171 new UFO_Name(Profile::DN_SORT),
172 ), 'promotion'));
173 $this->addSort(new PlViewOrder('date_mod', array(
174 new UFO_ProfileUpdate(true),
175 new UFO_Promo(UserFilter::DISPLAY, true),
176 new UFO_Name(Profile::DN_SORT),
177 ), 'dernière modification'));
178 parent::__construct($set, $data, $params);
179 }
180
181 protected function getBoundValue($obj)
182 {
183 if ($obj instanceof Profile) {
184 switch ($this->bound_field) {
185 case 'name':
186 $name = $obj->name('%l');
187 return strtoupper($name[0]);
188 case 'promo':
189 return $obj->promo();
190 default:
191 return null;
192 }
193 }
194 return null;
195 }
196
197 public function bounds()
198 {
199 $order = Env::v('order', $this->defaultkey);
200 $show_bounds = 0;
201 if (($order == "name") || ($order == "-name")) {
202 $this->bound_field = "name";
203 $show_bounds = 1;
204 } elseif (($order == "promo") || ($order == "-promo")) {
205 $this->bound_field = "promo";
206 $show_bounds = -1;
207 }
208 if ($order{0} == '-') {
209 $show_bounds = -$show_bounds;
210 }
211 return $show_bounds;
212 }
213
214 public function templateName()
215 {
216 return 'include/plview.minifiche.tpl';
217 }
218 }
219
220 class MentorView extends MultipageView
221 {
222 public function __construct(PlSet &$set, $data, array $params)
223 {
224 $this->entriesPerPage = 10;
225 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
226 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
227 $this->addSort(new PlViewOrder('promo', array(
228 new UFO_Promo(UserFilter::DISPLAY, true),
229 new UFO_Name(Profile::DN_SORT),
230 ), 'promotion'));
231 $this->addSort(new PlViewOrder('date_mod', array(
232 new UFO_ProfileUpdate(true),
233 new UFO_Promo(UserFilter::DISPLAY, true),
234 new UFO_Name(Profile::DN_SORT),
235 ), 'dernière modification'));
236 parent::__construct($set, $data, $params);
237 }
238
239 protected function getBoundValue($obj)
240 {
241 if ($obj instanceof Profile) {
242 switch ($this->bound_field) {
243 case 'name':
244 $name = $obj->name('%l');
245 return strtoupper($name[0]);
246 case 'promo':
247 return $obj->promo();
248 default:
249 return null;
250 }
251 }
252 return null;
253 }
254
255 public function bounds()
256 {
257 $order = Env::v('order', $this->defaultkey);
258 $show_bounds = 0;
259 if (($order == "name") || ($order == "-name")) {
260 $this->bound_field = "nom";
261 $show_bounds = 1;
262 } elseif (($order == "promo") || ($order == "-promo")) {
263 $this->bound_field = "promo";
264 $show_bounds = -1;
265 }
266 if ($order{0} == '-') {
267 $show_bounds = -$show_bounds;
268 }
269 return $show_bounds;
270 }
271
272 public function templateName()
273 {
274 return 'include/plview.referent.tpl';
275 }
276 }
277
278 class TrombiView extends MultipageView
279 {
280 public function __construct(PlSet &$set, $data, array $params)
281 {
282 $this->entriesPerPage = 24;
283 if (@$params['with_score']) {
284 $this->addSort(new PlViewOrder('score', array(
285 new UFO_Score(true),
286 new UFO_ProfileUpdate(true),
287 new UFO_Promo(UserFilter::DISPLAY, true),
288 new UFO_Name(Profile::DN_SORT),
289 ), 'pertinence'));
290 }
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, $data, $params);
297 }
298
299 protected function getBoundValue($obj)
300 {
301 if ($obj instanceof Profile) {
302 switch ($this->bound_field) {
303 case 'name':
304 $name = $obj->name('%l');
305 return strtoupper($name[0]);
306 case 'promo':
307 return $obj->promo();
308 default:
309 return null;
310 }
311 }
312 return null;
313 }
314
315 public function bounds()
316 {
317 $order = Env::v('order', $this->defaultkey);
318 $show_bounds = 0;
319 if (($order == "name") || ($order == "-name")) {
320 $this->bound_field = "nom";
321 $show_bounds = 1;
322 } elseif (($order == "promo") || ($order == "-promo")) {
323 $this->bound_field = "promo";
324 $show_bounds = -1;
325 }
326 if ($order{0} == '-') {
327 $show_bounds = -$show_bounds;
328 }
329 return $show_bounds;
330 }
331
332 public function templateName()
333 {
334 return 'include/plview.trombi.tpl';
335 }
336
337 public function apply(PlPage &$page)
338 {
339 if (!empty($GLOBALS['IS_XNET_SITE'])) {
340 global $globals;
341 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
342 }
343 return parent::apply($page);
344 }
345 }
346
347 class GadgetView implements PlView
348 {
349 public function __construct(PlSet &$set, $data, array $params)
350 {
351 $this->set =& $set;
352 }
353
354 public function apply(PlPage &$page)
355 {
356 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
357 }
358
359 public function args()
360 {
361 return null;
362 }
363 }
364
365 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
366 ?>