2006 => 2007 Happy New Year\!
[platal.git] / modules / search / search.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 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
abc68084 22require_once dirname(__FILE__).'/classes.inc.php';
0337d704 23
56670b6a 24// {{{ function advancedSearchFromInput
2b105fb6 25function getadr_join($table) {
5e2307dc 26 return 'u.user_id='.$table.'.uid'.(Env::v('only_current',false)?' AND FIND_IN_SET(\'active\','.$table.'.statut)':'');
2b105fb6 27}
56670b6a 28function advancedSearchFromInput()
29{
30 if ($with_soundex = Env::has('with_soundex')) {
31 $nameField = new RefWithSoundexSField('name',array('rn.nom1_soundex','rn.nom2_soundex','rn.nom3_soundex'),'recherche_soundex','rn','u.matricule = rn.matricule');
32 $firstnameField = new RefWithSoundexSField('firstname',array('rp.prenom1_soundex','rp.prenom2_soundex'),'recherche_soundex','rp','u.matricule = rp.matricule');
33 } else {
34 $nameField = new NameSField('name',array('u.nom','u.nom_usage'),'');
35 $firstnameField = new StringSField('firstname',array('u.prenom'),'');
36 }
37 $nicknameField = new StringSField('nickname',array('q.profile_nick'),'');
38
39 $promo1Field = new PromoSField('promo1','egal1',array('u.promo'),'');
40 $promo2Field = new PromoSField('promo2','egal2',array('u.promo'),'');
41 $womanField = new RefSField('woman',array('FIND_IN_SET(u.flags,\'femme\')+1'),'','','');
42 $subscriberField = new RefSField('subscriber',array('!(u.perms IN (\'admin\',\'user\'))+1'),'','','');
43 $aliveField = new RefSField('alive',array('(u.deces!=0)+1'),'','','');
44
56670b6a 45 $townField = new RefSField('city',array('ac.city'),'adresses','ac',getadr_join('ac'),false);
46 $cityIdField = new RefSField('cityid',array('av.cityid'),'adresses','av',getadr_join('av'));
47 $countryField = new RefSField('country',array('ap.country'),'adresses','ap',getadr_join('ap'));
48 $regionField = new RefSField('region',array('ar.region'),'adresses','ar',getadr_join('ar'));
49 $mapField = new MapSField('mapid', array('gcim.map_id'), array('adresses','geoloc_city_in_maps'), array('am','gcim'), array(getadr_join('am'), 'am.cityid = gcim.city_id'));
50
51 $entrepriseField = new RefSField('entreprise',array('ee.entreprise'),'entreprises','ee','u.user_id=ee.uid',false);
52 $posteField = new RefSField('poste',array('ep.poste'),'entreprises','ep','u.user_id=ep.uid', false);
53 $fonctionField = new RefSField('fonction',array('en.fonction'),'entreprises','en','u.user_id=en.uid');
54 $secteurField = new RefSField('secteur',array('fm.secteur'),'entreprises','fm','u.user_id=fm.uid');
55 $cvField = new RefSField('cv',array('u.cv'),'','','',false);
56
57 $natField = new RefSField('nationalite',array('u.nationalite'),'','','');
58 $binetField = new RefSField('binet',array('b.binet_id'),'binets_ins','b','u.user_id=b.user_id');
59 $groupexField = new RefSField('groupex',array('g.gid'),'groupesx_ins','g','u.user_id=g.guid');
60 $sectionField = new RefSField('section',array('u.section'),'','','');
61 $schoolField = new RefSField('school',array('as.aid'),'applis_ins','`as`','u.user_id=as.uid');
62 $diplomaField = new RefSField('diploma',array('ad.type'),'applis_ins','ad','u.user_id=ad.uid');
63
64 $freeField = new RefSField('free',array('q.profile_freetext'),'','','',false);
65
66 return array(
67 $nameField, $firstnameField, $nicknameField, $promo1Field,
68 $promo2Field, $womanField, $subscriberField, $aliveField,
69 $townField, $countryField, $regionField, $entrepriseField,
70 $posteField, $secteurField, $cvField, $natField, $binetField,
71 $groupexField, $sectionField, $schoolField, $diplomaField,
72 $freeField, $fonctionField, $cityIdField, $mapField);
73}
74
75// }}}
76
0337d704 77// {{{ class XOrgSearch
78
79class XOrgSearch extends XOrgPlugin
80{
81 // {{{ properties
82
83 var $_get_vars = Array('offset', 'order', 'order_inv', 'rechercher');
84 var $limit = 20;
85 var $order_defaut = 'promo';
86 // type of orders : (field name, default ASC, text name, auth)
87 var $orders = array(
88 'nom' =>array('nom,prenom', true, 'nom', AUTH_PUBLIC),
89 'promo' =>array('promo,nom', false, 'promotion', AUTH_PUBLIC),
90 'date_mod' =>array('u.date,nom', false, 'dernière modification', AUTH_COOKIE)
91 );
92
93 // }}}
94 // {{{ function setNbLines()
95
96 function setNbLines($lines)
97 { $this->limit = $lines; }
98
99 // }}}
100 // {{{ function setAuth()
101
102 function setAuth()
103 {
104 foreach ($this->orders as $key=>$o) {
105 if ($o[3] == AUTH_COOKIE) {
cab08090 106 $this->orders[$key][3] = S::logged();
0337d704 107 } elseif ($o[3] == AUTH_PUBLIC) {
108 $this->orders[$key][3] = true;
109 } else {
cab08090 110 $this->orders[$key][3] = S::identified();
0337d704 111 }
112 }
113 }
114
115 // }}}
116 // {{{ function addOrder()
117
118 function addOrder($name, $field, $inv_order, $text, $auth, $defaut=false)
119 {
120 // reverse the array, to get the defaut order first
121 if ($defaut)
122 $this->orders = array_reverse($this->orders);
123 $this->orders[$name] = array($field, $inv_order, $text, $auth);
124 if ($defaut) {
125 $this->orders = array_reverse($this->orders);
126 $this->order_defaut = $name;
127 }
128 }
129
130 // }}}
131 // {{{ function show()
132
133 function show()
134 {
135 $this->setAuth();
136 global $page;
137
138 $offset = intval($this->get_value('offset'));
1a013db7 139 $tab = @$this->orders[$this->get_value('order')];
0337d704 140 if (!$tab || !$tab[3]) {
141 $tab = $this->orders[$this->order_defaut];
142 }
143 $order = $tab[0];
144 $order_inv = ($this->get_value('order_inv') != '') == $tab[1];
145
146 if ($order_inv && $order)
147 $sql_order = str_replace(",", " DESC,", $order)." DESC";
148 else $sql_order = $order;
149
150 list($list, $total) = call_user_func($this->_callback, $offset, $this->limit, $sql_order);
151
152 $page_max = intval(($total-1)/$this->limit);
cab08090 153 if(!S::logged() && $page_max > $globals->search->public_max)
21c445de 154 $page_max = $globals->search->public_max;
0337d704 155
156 $links = Array();
157 if ($offset) {
158 $links[] = Array('u'=> $this->make_url(Array('offset'=>$offset-1)), 'i' => $offset-1, 'text' => 'précédent');
159 }
160 for ($i = 0; $i <= $page_max ; $i++) {
161 $links[] = Array('u'=>$this->make_url(Array('offset'=>$i)), 'i' => $i, 'text' => $i+1);
162 }
163 if ($offset < $page_max) {
164 $links[] = Array ('u' => $this->make_url(Array('offset'=>$offset+1)), 'i' => $offset+1, 'text' => 'suivant');
165 }
166
167 $page->assign('search_results', $list);
168 $page->assign('search_results_nb', $total);
169 $page->assign('search_page', $offset);
170 $page->assign('search_pages_nb', $page_max+1);
171 $page->assign('search_pages_link', $links);
172
173 $order_links = Array();
174 foreach ($this->orders as $key=>$o) if ($o[3]) {
175 $order_links[] = Array(
176 "text" => $o[2],
177 "url" => $this->make_url(Array('order' => $key, 'order_inv' => ($o[0] == $order) && ($order_inv != $o[1]))),
178 "asc" => ($o[0] == $order) && $order_inv,
179 "desc" => ($o[0] == $order) && !$order_inv
180 );
181 }
182 $page->assign('search_order_link', $order_links);
183
184 return $total;
185 }
186
187 // }}}
188}
189
190// }}}
191
192// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
193?>