Fix advanced search 'form cleanup' JS
[platal.git] / include / userset.inc.php
CommitLineData
8c4a0c30 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
8c4a0c30 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
8c4a0c30 22class UserSet extends PlSet
23{
a7d9ab89 24 public function __construct(PlFilterCondition &$cond, $orders = null)
8c4a0c30 25 {
8e720253 26 parent::__construct($cond, $orders);
76cbe885
FB
27 }
28
8e720253 29 protected function buildFilter(PlFilterCondition &$cond, $orders)
76cbe885 30 {
8e720253 31 return new UserFilter($cond, $orders);
8c4a0c30 32 }
33}
34
a7d9ab89
RB
35class 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
48class SearchSet extends ProfileSet
8c4a0c30 49{
86b5c8f0 50 public $advanced = false;
8e720253 51 private $score = null;
8e720253 52 private $quick = false;
8c4a0c30 53
8eb6931f 54 public function __construct($quick = false, PlFilterCondition $cond = null)
8c4a0c30 55 {
8c4a0c30 56 if ($no_search) {
57 return;
58 }
59
60 $this->quick = $quick;
8e720253
RB
61
62 if (is_null($cond)) {
8eb6931f 63 $conds = new PFC_And();
8e720253 64 } else if ($cond instanceof PFC_And) {
8eb6931f 65 $conds = $cond;
8e720253 66 } else {
8eb6931f 67 $conds = new PFC_And($cond);
8e720253
RB
68 }
69
8c4a0c30 70 if ($quick) {
8eb6931f 71 $this->getQuick($conds);
8c4a0c30 72 } else {
8eb6931f 73 $this->getAdvanced($conds);
8c4a0c30 74 }
75 }
76
8eb6931f
RB
77 /** Sets up the conditions for a Quick Search
78 * @param $conds Additional conds (as a PFC_And)
79 */
80 private function getQuick($conds)
8c4a0c30 81 {
8c4a0c30 82 if (!S::logged()) {
83 Env::kill('with_soundex');
84 }
8e720253
RB
85
86 require_once 'ufbuilder.inc.php';
87 $ufb = new UFB_QuickSearch();
88
89 if (!$ufb->isValid()) {
90 return;
3b2f9d11 91 }
8e720253 92
3314838e 93 $ufc = $ufb->getUFC();
8eb6931f 94 $conds->addChild($ufc);
8e720253
RB
95
96 $orders = $ufb->getOrders();
8e720253 97
3b2f9d11 98 if (S::logged() && Env::has('nonins')) {
8eb6931f 99 $conds = new PFC_And($conds,
669db4e9
RB
100 new PFC_Not(new UFC_Dead()),
101 new PFC_Not(new UFC_Registered())
8e720253 102 );
3b2f9d11 103 }
8c4a0c30 104
8eb6931f 105 parent::__construct($conds, $orders);
8c4a0c30 106 }
107
8eb6931f
RB
108 /** Sets up the conditions for an Advanced Search
109 * @param $conds Additional conds (as a PFC_And)
110 */
111 private function getAdvanced($conds)
8c4a0c30 112 {
86b5c8f0 113 $this->advanced = true;
8e720253
RB
114 require_once 'ufbuilder.inc.php';
115 $ufb = new UFB_AdvancedSearch();
116
117 if (!$ufb->isValid()) {
118 return;
8c4a0c30 119 }
8e720253 120
8eb6931f
RB
121 $ufc = $ufb->getUFC();
122 $conds->addChild($ufc);
123
124 $orders = $ufb->getOrders();
125
126 parent::__construct($conds, $orders);
8c4a0c30 127 }
35fa92e8 128
5c4b28bb
RB
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
8467def7 141 protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
35fa92e8 142 {
d667c548
SJ
143 $profiles = $pf->getProfiles($limit, Profile::FETCH_MINIFICHES);
144 return $profiles;
35fa92e8 145 }
8c4a0c30 146}
147
8eb6931f
RB
148/** Simple set based on an array of User objects
149 */
470d14f6 150class ArraySet extends ProfileSet
1cc0afe7 151{
152 public function __construct(array $users)
153 {
8e720253
RB
154 $hruids = User::getBulkHruid($users, array('User', '_silent_user_callback'));
155 if (is_null($hruids) || count($hruids) == 0) {
156 $cond = new PFC_False();
1cc0afe7 157 } else {
8e720253 158 $cond = new UFC_Hruid($hruids);
1cc0afe7 159 }
8e720253 160 parent::__construct($cond);
1cc0afe7 161 }
162}
163
8eb6931f
RB
164/** A multipage view for profiles
165 * Allows the display of bounds when sorting by name or promo.
166 */
169edfac
RB
167abstract 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 }
316120eb
FB
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 }
169edfac
RB
201}
202
8eb6931f
RB
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 */
169edfac 214class MinificheView extends ProfileView
8c4a0c30 215{
8eb6931f 216 public function __construct(PlSet &$set, array $params)
8c4a0c30 217 {
8c4a0c30 218 global $globals;
219 $this->entriesPerPage = $globals->search->per_page;
35fa92e8 220 if (@$params['with_score']) {
8e720253
RB
221 $this->addSort(new PlViewOrder('score', array(
222 new UFO_Score(true),
223 new UFO_ProfileUpdate(true),
224 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 225 new UFO_Name(Profile::DN_SORT),
8e720253 226 ), 'pertinence'));
35fa92e8 227 }
8e720253
RB
228 $this->addSort(new PlViewOrder(
229 'name',
913a4e90 230 array(new UFO_Name(Profile::DN_SORT)),
8e720253
RB
231 'nom'));
232 $this->addSort(new PlViewOrder('promo', array(
233 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 234 new UFO_Name(Profile::DN_SORT),
8e720253
RB
235 ), 'promotion'));
236 $this->addSort(new PlViewOrder('date_mod', array(
237 new UFO_ProfileUpdate(true),
238 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 239 new UFO_Name(Profile::DN_SORT),
8e720253 240 ), 'dernière modification'));
8eb6931f
RB
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);
8c4a0c30 256 }
257
8c4a0c30 258 public function templateName()
259 {
260 return 'include/plview.minifiche.tpl';
261 }
262}
263
169edfac 264class MentorView extends ProfileView
ff3eb9b7 265{
8eb6931f 266 public function __construct(PlSet &$set, array $params)
ff3eb9b7 267 {
eaf30d86 268 $this->entriesPerPage = 10;
8e720253 269 $this->addSort(new PlViewOrder('rand', array(new PFO_Random(S::i('uid'))), 'aléatoirement'));
913a4e90 270 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
8e720253
RB
271 $this->addSort(new PlViewOrder('promo', array(
272 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 273 new UFO_Name(Profile::DN_SORT),
8e720253
RB
274 ), 'promotion'));
275 $this->addSort(new PlViewOrder('date_mod', array(
276 new UFO_ProfileUpdate(true),
277 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 278 new UFO_Name(Profile::DN_SORT),
8e720253 279 ), 'dernière modification'));
8eb6931f 280 parent::__construct($set, $params);
ff3eb9b7 281 }
282
ff3eb9b7 283 public function templateName()
284 {
285 return 'include/plview.referent.tpl';
286 }
287}
288
169edfac 289class TrombiView extends ProfileView
8c4a0c30 290{
8eb6931f 291 public function __construct(PlSet &$set, array $params)
8c4a0c30 292 {
293 $this->entriesPerPage = 24;
316120eb 294 $this->defaultkey = 'name';
35fa92e8 295 if (@$params['with_score']) {
8e720253
RB
296 $this->addSort(new PlViewOrder('score', array(
297 new UFO_Score(true),
298 new UFO_ProfileUpdate(true),
299 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 300 new UFO_Name(Profile::DN_SORT),
8e720253 301 ), 'pertinence'));
35fa92e8 302 }
316120eb 303 $set->addCond(new UFC_Photo());
913a4e90 304 $this->addSort(new PlViewOrder('name', array(new UFO_Name(Profile::DN_SORT)), 'nom'));
8e720253
RB
305 $this->addSort(new PlViewOrder('promo', array(
306 new UFO_Promo(UserFilter::DISPLAY, true),
913a4e90 307 new UFO_Name(Profile::DN_SORT),
8e720253 308 ), 'promotion'));
8eb6931f 309 parent::__construct($set, $params);
8c4a0c30 310 }
311
8c4a0c30 312 public function templateName()
313 {
314 return 'include/plview.trombi.tpl';
315 }
316
04334c61 317 public function apply(PlPage &$page)
8c4a0c30 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
92e80560
VZ
327class GadgetView implements PlView
328{
8eb6931f 329 public function __construct(PlSet &$set, array $params)
92e80560
VZ
330 {
331 $this->set =& $set;
332 }
333
04334c61 334 public function apply(PlPage &$page)
92e80560 335 {
8e720253 336 $page->assign_by_ref('set', $this->set->get(new PlLimit(5, 0)));
92e80560
VZ
337 }
338
339 public function args()
340 {
341 return null;
342 }
343}
344
8c4a0c30 345// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
346?>