Initial work on #448: unified user-set interface
[platal.git] / include / userset.inc.php
CommitLineData
8c4a0c30 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 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
22require_once('plset.php');
23require_once('xorg.misc.inc.php');
24require_once('user.func.inc.php');
25
26global $globals;
27
28$globals->search->result_where_statement = '
29 LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0)
30 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
31 LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1)
32 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
33 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id)
34 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
35 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
36 LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2)
37 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut))
38 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
39 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
40 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = \'active\')';
41
42class UserSet extends PlSet
43{
44 public function __construct($joins = '', $where = '')
45 {
46 global $globals;
47 parent::__construct('auth_user_md5 AS u',
48 (!empty($GLOBALS['IS_XNET_SITE']) ?
49 'INNER JOIN groupex.membres AS gxm ON (u.user_id = gxm.uid
50 AND gxm.asso_id = ' . $globals->asso('id') . ') ' : '')
51 . 'LEFT JOIN auth_user_quick AS q USING (user_id)
52 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type = \'a_vie\')
53 ' . $joins,
54 $where,
55 'u.user_id');
56 }
57}
58
59class SearchSet extends UserSet
60{
61 private $order = null;
62 private $quick = false;
63
64 public function __construct($quick = false, $no_search = false)
65 {
66 require_once dirname(__FILE__).'/../modules/search/search.inc.php';
67
68 if ($no_search) {
69 return;
70 }
71
72 $this->quick = $quick;
73 if ($quick) {
74 $this->getQuick();
75 } else {
76 $this->getAdvanced();
77 }
78 }
79
80 private function getQuick()
81 {
82 require_once dirname(__FILE__).'/../modules/search/search.inc.php';
83 global $globals;
84 if (!S::logged()) {
85 Env::kill('with_soundex');
86 }
87 $qSearch = new QuickSearch('quick');
88 $fields = new SFieldGroup(true, array($qSearch));
89 if ($qSearch->isEmpty()) {
90 new ThrowError('Recherche trop générale.');
91 }
92 parent::__construct($fields->get_select_statement() . ' ' . $globals->search->result_where_statement,
93 $fields->get_where_statement() .
94 (S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : ''));
95
96 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
97 'u.promo DESC, NomSortKey, prenom')));
98 }
99
100 private function getAdvanced()
101 {
102 global $globals;
103 $fields = new SFieldGroup(true, advancedSearchFromInput());
104 if ($fields->too_large()) {
105 new ThrowError('Recherche trop générale.');
106 }
107 parent::__construct($fields->get_select_statement() . ' ' . $globals->search->result_where_statement,
108 $fields->get_where_statement());
109 $this->order = implode(',',array_filter(array($fields->get_order_statement(),
110 'promo DESC, NomSortKey, prenom')));
111 }
112}
113
114class MinificheView extends MultipageView
115{
116 public function __construct(PlSet &$set, $data, array $params)
117 {
118 require_once 'applis.func.inc.php';
119 global $globals;
120 $this->entriesPerPage = $globals->search->per_page;
121 $this->order = explode(',', Env::v('order', 'nom,prenom,promo'));
122 parent::__construct($set, $data, $params);
123 }
124
125 public function fields()
126 {
127 return "u.user_id AS id,
128 u.*, a.alias AS forlife,
129 u.perms != 'pending' AS inscrit,
130 u.perms != 'pending' AS wasinscrit,
131 u.deces != 0 AS dcd, u.deces, u.matricule_ax,
132 FIND_IN_SET('femme', u.flags) AS sexe,
133 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
134 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
135 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
136 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
137 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
138 IF(u.nom_usage<>'',u.nom_usage,u.nom) AS sortkey,
139 COUNT(em.email) > 0 AS actif" . (S::logged() ? ", c.contact AS contact" : '');
140 }
141
142 public function joins()
143 {
144 return ($this->set instanceof SearchSet ? "" :
145 "LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id)
146 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
147 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
148 LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2)
149 LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0)
150 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
151 LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1)
152 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
153 LEFT JOIN adresses AS adr ON (u.user_id = adr.uid
154 AND FIND_IN_SET('active', adr.statut))
155 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
156 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
157 LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = 'active')
158 ") . (S::logged() ?
159 "LEFT JOIN contacts AS c On (c.contact = u.user_id AND c.uid = " . S::v('uid') . ")"
160 : "");
161 }
162
163 public function templateName()
164 {
165 return 'include/plview.minifiche.tpl';
166 }
167}
168
169class TrombiView extends MultipageView
170{
171 public function __construct(PlSet &$set, $data, array $params)
172 {
173 $this->entriesPerPage = 24;
174 $this->order = explode(',', Env::v('order', 'nom,prenom,promo'));
175 parent::__construct($set, $data, $params);
176 }
177
178 public function fields()
179 {
180 return "u.user_id, IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, u.prenom, u.promo, a.alias AS forlife ";
181 }
182
183 public function joins()
184 {
185 return "INNER JOIN photo AS p ON (p.uid = u.user_id) ";
186 }
187
188 public function templateName()
189 {
190 return 'include/plview.trombi.tpl';
191 }
192
193 public function apply(PlatalPage &$page)
194 {
195 if (!empty($GLOBALS['IS_XNET_SITE'])) {
196 global $globals;
197 $page->assign('mainsiteurl', 'https://' . $globals->core->secure_domain . '/');
198 }
199 return parent::apply($page);
200 }
201}
202
203class GeolocView implements PlView
204{
205 private $set;
206 private $type;
207 private $params;
208
209 public function __construct(PlSet &$set, $data, array $params)
210 {
211 $this->params = $params;
212 $this->set =& $set;
213 $this->type = $data;
214 }
215
216 private function use_map()
217 {
218 return is_file(dirname(__FILE__) . '/../modules/geoloc/dynamap.swf') &&
219 is_file(dirname(__FILE__) . '/../modules/geoloc/icon.swf');
220 }
221
222 private function make_qs($urlencode = true)
223 {
224 $qs = "";
225 foreach ($_GET as $v=>$a) {
226 if ($v != 'initfile' && $v != 'n' && $v != 'mapid') {
227 if (!$urlencode) {
228 $qs .= $v . '=' . $a . '&';
229 } else {
230 $qs .= urlencode($v) . '=' . urlencode($a) . '&amp;';
231 }
232 }
233 }
234 return $qs;
235 }
236
237 public function apply(PlatalPage &$page)
238 {
239 require_once 'geoloc.inc.php';
240 require_once '../modules/search/search.inc.php';
241
242 switch ($this->type) {
243 case 'icon.swf':
244 header("Content-type: application/x-shockwave-flash");
245 header("Pragma:");
246 readfile(dirname(__FILE__).'/../modules/geoloc/icon.swf');
247 exit;
248
249 case 'dynamap.swf':
250 header("Content-type: application/x-shockwave-flash");
251 header("Pragma:");
252 readfile(dirname(__FILE__).'/../modules/geoloc/dynamap.swf');
253 exit;
254
255 case 'init':
256 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
257 header('Content-Type: text/xml');
258 header('Pragma:');
259 if (!empty($GLOBALS['IS_XNET_SITE'])) {
260 $page->assign('background', 0xF2E9D0);
261 }
262 $page->assign('querystring', $this->make_qs());
263 break;
264
265 case 'city':
266 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
267 header('Content-Type: text/xml');
268 header('Pragma:');
269 $it =& $this->set->get('u.user_id AS id, u.prenom, u.nom, u.promo, al.alias',
270 "INNER JOIN adresses AS adrf ON (adrf.uid = u.user_id)
271 LEFT JOIN aliases AS al ON (u.user_id = al.id
272 AND FIND_IN_SET(al.flags, 'bestalias'))
273 INNER JOIN adresses AS av ON (" . getadr_join('av') . ")",
274 'adrf.cityid = ' . Env::i('cityid'), null, null, 11);
275 $page->assign('users', $it);
276 break;
277
278 case 'country':
279 if (Env::has('debug')) {
280 $page->changeTpl('geoloc/country.tpl', SIMPLE);
281 } else {
282 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
283 header('Content-Type: text/xml');
284 header('Pragma:');
285 }
286 $querystring = $this->make_qs();
287 $page->assign('searchvars', $querystring);
288 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
289 list($countries, $cities) = geoloc_getData_subcountries($mapid, $this->set, 10);
290 $page->assign('countries', $countries);
291 $page->assign('cities', $cities);
292 break;
293
294 default:
295 global $globals;
296 if (!$this->use_map()) {
297 $page->assign('request_geodesix', true);
298 }
299 if (!empty($GLOBALS['IS_XNET_SITE'])) {
300 $page->assign('no_annu', true);
301 }
302 $page->assign('protocole', @$_SERVER['HTTPS'] ? 'https' : 'http');
303 $this->set->get('u.user_id', null, "u.perms != 'pending' AND u.deces = 0", "u.user_id", null);
304 $url = '?' . $this->make_qs(false);
305 $page->assign('search_nourlencode', $url);
306 $page->assign('search', urlencode($url));
307 return 'include/plview.geoloc.tpl';
308 }
309 }
310}
311
312// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
313?>