Improve the PlView code, adding comments.
[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 protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
130 {
131 $profiles = $pf->getProfiles($limit, Profile::FETCH_MINIFICHES);
132 return $profiles;
133 }
134 }
135
136 /** Simple set based on an array of User objects
137 */
138 class ArraySet extends ProfileSet
139 {
140 public function __construct(array $users)
141 {
142 $hruids = User::getBulkHruid($users, array('User', '_silent_user_callback'));
143 if (is_null($hruids) || count($hruids) == 0) {
144 $cond = new PFC_False();
145 } else {
146 $cond = new UFC_Hruid($hruids);
147 }
148 parent::__construct($cond);
149 }
150 }
151
152 /** A multipage view for profiles
153 * Allows the display of bounds when sorting by name or promo.
154 */
155 abstract class ProfileView extends MultipageView
156 {
157 protected function getBoundValue($obj)
158 {
159 if ($obj instanceof Profile) {
160 switch ($this->bound_field) {
161 case 'name':
162 $name = $obj->name('%l');
163 return strtoupper($name);
164 case 'promo':
165 return $obj->promo();
166 default:
167 return null;
168 }
169 }
170 return null;
171 }
172
173 public function bounds()
174 {
175 $order = Env::v('order', $this->defaultkey);
176 $show_bounds = 0;
177 if (($order == "name") || ($order == "-name")) {
178 $this->bound_field = "name";
179 $show_bounds = 1;
180 } elseif (($order == "promo") || ($order == "-promo")) {
181 $this->bound_field = "promo";
182 $show_bounds = -1;
183 }
184 if ($order{0} == '-') {
185 $show_bounds = -$show_bounds;
186 }
187 return $show_bounds;
188 }
189 }
190
191 /** An extended multipage view for profiles, as minifiches.
192 * Allows to sort by:
193 * - score (for a search query)
194 * - name
195 * - promo
196 * - latest modification
197 *
198 * Paramaters for this view are:
199 * - with_score: whether to allow ordering by score (set only for a quick search)
200 * - starts_with: show only names beginning with the given letter
201 */
202 class MinificheView extends ProfileView
203 {
204 public function __construct(PlSet &$set, array $params)
205 {
206 global $globals;
207 $this->entriesPerPage = $globals->search->per_page;
208 if (@$params['with_score']) {
209 $this->addSort(new PlViewOrder('score', array(
210 new UFO_Score(true),
211 new UFO_ProfileUpdate(true),
212 new UFO_Promo(UserFilter::DISPLAY, true),
213 new UFO_Name(Profile::DN_SORT),
214 ), 'pertinence'));
215 }
216 $this->addSort(new PlViewOrder(
217 'name',
218 array(new UFO_Name(Profile::DN_SORT)),
219 'nom'));
220 $this->addSort(new PlViewOrder('promo', array(
221 new UFO_Promo(UserFilter::DISPLAY, true),
222 new UFO_Name(Profile::DN_SORT),
223 ), 'promotion'));
224 $this->addSort(new PlViewOrder('date_mod', array(
225 new UFO_ProfileUpdate(true),
226 new UFO_Promo(UserFilter::DISPLAY, true),
227 new UFO_Name(Profile::DN_SORT),
228 ), 'dernière modification'));
229 parent::__construct($set, $params);
230 }
231
232 public function apply(PlPage &$page)
233 {
234 if (array_key_exists('starts_with', $this->params)
235 && $this->params['starts_with'] != ""
236 && $this->params['starts_with'] != null) {
237
238 $this->set->addCond(
239 new UFC_Name(Profile::LASTNAME,
240 $this->params['starts_with'], UFC_Name::PREFIX)
241 );
242 }
243 return parent::apply($page);
244 }
245
246 public function templateName()
247 {
248 return 'include/plview.minifiche.tpl';
249 }
250 }
251
252 class MentorView extends ProfileView
253 {
254 public function __construct(PlSet &$set, array $params)
255 {
256 $this->entriesPerPage = 10;
257 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
258 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
259 $this->addSort(new PlViewOrder('promo', array(
260 new UFO_Promo(UserFilter::DISPLAY, true),
261 new UFO_Name(Profile::DN_SORT),
262 ), 'promotion'));
263 $this->addSort(new PlViewOrder('date_mod', array(
264 new UFO_ProfileUpdate(true),
265 new UFO_Promo(UserFilter::DISPLAY, true),
266 new UFO_Name(Profile::DN_SORT),
267 ), 'dernière modification'));
268 parent::__construct($set, $params);
269 }
270
271 public function templateName()
272 {
273 return 'include/plview.referent.tpl';
274 }
275 }
276
277 class TrombiView extends ProfileView
278 {
279 public function __construct(PlSet &$set, array $params)
280 {
281 $this->entriesPerPage = 24;
282 $this->defaultkey = 'name';
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 $set->addCond(new UFC_Photo());
292 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
293 $this->addSort(new PlViewOrder('promo', array(
294 new UFO_Promo(UserFilter::DISPLAY, true),
295 new UFO_Name(Profile::DN_SORT),
296 ), 'promotion'));
297 parent::__construct($set, $params);
298 }
299
300 public function templateName()
301 {
302 return 'include/plview.trombi.tpl';
303 }
304
305 public function apply(PlPage &$page)
306 {
307 if (!empty($GLOBALS['IS_XNET_SITE'])) {
308 global $globals;
309 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
310 }
311 return parent::apply($page);
312 }
313 }
314
315 class GadgetView implements PlView
316 {
317 public function __construct(PlSet &$set, array $params)
318 {
319 $this->set =& $set;
320 }
321
322 public function apply(PlPage &$page)
323 {
324 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
325 }
326
327 public function args()
328 {
329 return null;
330 }
331 }
332
333 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
334 ?>