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