Merge commit 'origin/fusionax' into account
[platal.git] / include / userset.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 private $cond;
25
26 public function __construct($cond = null)
27 {
28 $this->cond = new UFC_And();
29 if (!is_null($cond)) {
30 $this->cond->addChild($cond);
31 }
32 }
33
34 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
35 {
36 $uf = new UserFilter($this->cond);
37 $users = $uf->getUsers($limitcount, $limitfrom);
38 $this->count = $uf->getTotalCount();
39 return $users;
40 }
41 }
42
43 class SearchSet extends UserSet
44 {
45 public $advanced = false;
46 private $score = null;
47 private $order = null;
48 private $quick = false;
49
50 public function __construct($quick = false, $no_search = false, $join = '', $where = '')
51 {
52 Platal::load('search', 'search.inc.php');
53 if ($no_search) {
54 return;
55 }
56
57 $this->quick = $quick;
58 if ($quick) {
59 $this->getQuick($join, $where);
60 } else {
61 $this->getAdvanced($join, $where);
62 }
63 }
64
65 private function getQuick($join, $where)
66 {
67 Platal::load('search', 'search.inc.php');
68 global $globals;
69 if (!S::logged()) {
70 Env::kill('with_soundex');
71 }
72 $qSearch = new QuickSearch('quick');
73 $fields = new SFieldGroup(true, array($qSearch));
74 if ($qSearch->isEmpty()) {
75 new ThrowError('Aucun critère de recherche n\'est spécifié.');
76 }
77 $this->score = $qSearch->get_score_statement();
78 $pwhere = $fields->get_where_statement();
79 if (trim($pwhere)) {
80 if (trim($where)) {
81 $where .= ' AND ';
82 }
83 $where .= $pwhere;
84 }
85 if (S::logged() && Env::has('nonins')) {
86 if (trim($where)) {
87 $where .= ' AND ';
88 }
89 $where .= 'u.perms="pending" AND u.deces=0';
90 }
91 parent::__construct($join . ' ' . $fields->get_select_statement(), $where);
92
93 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
94 'u.promo DESC, NomSortKey, prenom')));
95 }
96
97 private function getAdvanced($join, $where)
98 {
99 global $globals;
100 $this->advanced = true;
101 $fields = new SFieldGroup(true, advancedSearchFromInput());
102 if ($fields->too_large()) {
103 new ThrowError('Recherche trop générale.');
104 }
105 parent::__construct(@$join . ' ' . $fields->get_select_statement(),
106 @$where . ' ' . $fields->get_where_statement());
107 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
108 'promo DESC, NomSortKey, prenom')));
109 }
110
111 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
112 {
113 if ($this->score) {
114 $fields .= ', ' . $this->score;
115 }
116 return parent::get($fields, $joins, $where, $groupby, $order, $limitcount, $limitfrom);
117 }
118 }
119
120 class ArraySet extends UserSet
121 {
122 public function __construct(array $users)
123 {
124 $where = $this->getUids($users);
125 if ($where) {
126 $where = "u.hruid IN ($where)";
127 } else {
128 $where = " 0 ";
129 }
130 parent::__construct('', $where);
131 }
132
133 private function getUids(array $users)
134 {
135 $users = User::getBulkHruid($users, array('User', '_silent_user_callback'));
136 if (is_null($users)) {
137 return '';
138 }
139 return '\'' . implode('\', \'', $users) . '\'';
140 }
141 }
142
143 class MinificheView extends MultipageView
144 {
145 public function __construct(PlSet &$set, $data, array $params)
146 {
147 require_once 'education.func.inc.php';
148 global $globals;
149 $this->entriesPerPage = $globals->search->per_page;
150 if (@$params['with_score']) {
151 $this->addSortKey('score', array('-score', '-date', '-d.promo', 'sort_name'), 'pertinence');
152 }
153 $this->addSortKey('name', array('sort_name'), 'nom');
154 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
155 $this->addSortKey('date_mod', array('-date', '-d.promo', 'sort_name'), 'dernière modification');
156 parent::__construct($set, $data, $params);
157 }
158
159 public function bounds()
160 {
161 return null;
162 }
163
164 public function fields()
165 {
166 return null;
167 }
168
169 public function templateName()
170 {
171 return 'include/plview.minifiche.tpl';
172 }
173 }
174
175 class MentorView extends MultipageView
176 {
177 public function __construct(PlSet &$set, $data, array $params)
178 {
179 $this->entriesPerPage = 10;
180 $this->addSortKey('rand', array('RAND(' . S::i('uid') . ')'), 'aléatoirement');
181 $this->addSortKey('name', array('sort_name'), 'nom');
182 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
183 $this->addSortKey('date_mod', array('-date', '-d.promo', 'sort_name'), 'dernière modification');
184 parent::__construct($set, $data, $params);
185 }
186
187 public function fields()
188 {
189 return "m.uid, d.promo, u.hruid,
190 m.expertise, mp.country, ms.sectorid, ms.subsectorid,
191 d.directory_name, d.sort_name";
192 }
193
194 public function joins()
195 {
196 return "INNER JOIN profile_display AS d ON (d.pid = u.user_id)";
197 }
198
199 public function bounds()
200 {
201 $order = Env::v('order', $this->defaultkey);
202 $show_bounds = 0;
203 if (($order == "name") || ($order == "-name")) {
204 $this->bound_field = "nom";
205 $show_bounds = 1;
206 } elseif (($order == "promo") || ($order == "-promo")) {
207 $this->bound_field = "promo";
208 $show_bounds = -1;
209 }
210 if ($order{0} == '-') {
211 $show_bounds = -$show_bounds;
212 }
213 return $show_bounds;
214 }
215
216 public function templateName()
217 {
218 return 'include/plview.referent.tpl';
219 }
220 }
221
222 class TrombiView extends MultipageView
223 {
224 public function __construct(PlSet &$set, $data, array $params)
225 {
226 $this->entriesPerPage = 24;
227 $this->order = explode(',', Env::v('order', 'sort_name'));
228 if (@$params['with_score']) {
229 $this->addSortKey('score', array('-score', '-watch_last', '-d.promo', 'sort_name'), 'pertinence');
230 }
231 $this->addSortKey('name', array('sort_name'), 'nom');
232 $this->addSortKey('promo', array('-d.promo', 'sort_name'), 'promotion');
233 parent::__construct($set, $data, $params);
234 }
235
236 public function fields()
237 {
238 return "u.user_id, d.directory_name, d.sort_name, u.promo, d.promo, u.hruid ";
239 }
240
241 public function joins()
242 {
243 return "INNER JOIN photo AS p ON (p.uid = u.user_id)
244 INNER JOIN profile_display AS d ON (d.pid = u.user_id)";
245 }
246
247 public function bounds()
248 {
249 $order = Env::v('order', $this->defaultkey);
250 $show_bounds = 0;
251 if (($order == "name") || ($order == "-name")) {
252 $this->bound_field = "nom";
253 $show_bounds = 1;
254 } elseif (($order == "promo") || ($order == "-promo")) {
255 $this->bound_field = "promo";
256 $show_bounds = -1;
257 }
258 if ($order{0} == '-') {
259 $show_bounds = -$show_bounds;
260 }
261 return $show_bounds;
262 }
263
264 public function templateName()
265 {
266 return 'include/plview.trombi.tpl';
267 }
268
269 public function apply(PlPage &$page)
270 {
271 if (!empty($GLOBALS['IS_XNET_SITE'])) {
272 global $globals;
273 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
274 }
275 return parent::apply($page);
276 }
277 }
278
279 class GadgetView implements PlView
280 {
281 public function __construct(PlSet &$set, $data, array $params)
282 {
283 $this->set =& $set;
284 }
285
286 public function fields()
287 {
288 return "u.user_id AS id, u.*," .
289 "u.perms != 'pending' AS inscrit,
290 u.perms != 'pending' AS wasinscrit,
291 u.deces != 0 AS dcd, u.deces,
292 FIND_IN_SET('femme', u.flags) AS sexe,
293 " // adr.city, gr.name AS region
294 . "gc.iso_3166_1_a2, gc.countryFR AS countrytxt" .
295 (S::logged() ? ", c.contact AS contact" : '');
296 }
297
298 public function joins()
299 {
300 return "LEFT JOIN profile_addresses AS adr ON (u.user_id = adr.pid AND
301 FIND_IN_SET('current', adr.flags)"
302 . (S::logged() ? "" : "AND adr.pub = 'public'") . ")
303 LEFT JOIN geoloc_countries AS gc ON (adr.countryId = gc.iso_3166_1_a2)
304 LEFT JOIN geoloc_administrativeareas AS gr ON (adr.countryId = gr.country
305 AND adr.administrativeAreaId = gr.id)
306 " . (S::logged() ?
307 "LEFT JOIN contacts AS c ON (c.contact = u.user_id
308 AND c.uid = " . S::v('uid') . ")" : "");
309 }
310
311 public function apply(PlPage &$page)
312 {
313 $page->assign_by_ref('set',
314 $this->set->get($this->fields(), $this->joins(), null, null, null, 5, 0));
315 }
316
317 public function args()
318 {
319 return null;
320 }
321 }
322
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
324 ?>