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