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