Proof of concept:
[platal.git] / htdocs / carnet / mescontacts.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 require_once("xorg.inc.php");
23 new_skinned_page("carnet/mescontacts.tpl",AUTH_COOKIE);
24 require_once("applis.func.inc.php");
25 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
26
27 $uid = Session::getInt('uid');
28 $user = Env::get('user');
29
30 switch (Env::get('action')) {
31 case 'retirer':
32 if (preg_match('/^\d+$/', $user)) {
33 if ($globals->xdb->execute('DELETE FROM contacts WHERE uid = {?} AND contact = {?}', $uid, $user))
34 {
35 $page->trig("Contact retiré !");
36 }
37 } else {
38 if ($globals->xdb->execute(
39 'DELETE FROM contacts
40 USING contacts AS c
41 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
42 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
43 {
44 $page->trig("Contact retiré !");
45 }
46 }
47 break;
48
49 case 'ajouter':
50 require_once('user.func.inc.php');
51 if (($login = get_user_login($user)) !== false) {
52 if ($globals->xdb->execute(
53 'INSERT INTO contacts (uid, contact)
54 SELECT {?}, id
55 FROM aliases
56 WHERE alias = {?}', $uid, $login))
57 {
58 $page->trig('Contact ajouté !');
59 } else {
60 $page->trig('Contact déjà dans la liste !');
61 }
62 }
63 }
64
65 if(Get::get('trombi')) {
66 require_once('trombi.inc.php');
67 function getList($offset,$limit) {
68 global $globals;
69 $uid = Session::getInt('uid');
70 $res = $globals->xdb->query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
71 $total = $res->fetchOneCell();
72
73 $order = Get::get('order');
74 $orders = Array(
75 'nom' => 'nom DESC, u.prenom, u.promo',
76 'promo' => 'promo DESC, nom, u.prenom',
77 'last' => 'u.date DESC, nom, u.prenom, promo');
78 if ($order != 'promo' && $order != 'last')
79 $order = 'nom';
80 $order = $orders[$order];
81 if (Get::get('inv') == '')
82 $order = str_replace(" DESC,", ",", $order);
83
84 $res = $globals->xdb->query("
85 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
86 FROM contacts AS c
87 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
88 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
89 WHERE c.uid = {?}
90 ORDER BY $order
91 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
92 $list = $res->fetchAllAssoc();
93
94 return Array($total, $list);
95 }
96
97 $trombi = new Trombi('getList');
98 $trombi->setNbRows(4);
99 $page->assign_by_ref('trombi',$trombi);
100
101 $order = Get::get('order');
102 if ($order != 'promo' && $order != 'last')
103 $order = 'nom';
104 $page->assign('order', $order);
105 $page->assign('inv', Get::get('inv'));
106 } else {
107
108 $order = Get::get('order');
109 $orders = Array(
110 'nom' => 'sortkey DESC, a.prenom, a.promo',
111 'promo' => 'promo DESC, sortkey, a.prenom',
112 'last' => 'a.date DESC, sortkey, a.prenom, promo');
113 if ($order != 'promo' && $order != 'last')
114 $order = 'nom';
115 $page->assign('order', $order);
116 $page->assign('inv', Get::get('inv'));
117 $order = $orders[$order];
118 if (Get::get('inv') == '')
119 $order = str_replace(" DESC,", ",", $order);
120
121 $sql = "SELECT contact AS id,
122 a.*, l.alias AS forlife,
123 1 AS inscrit,
124 a.perms != 'pending' AS wasinscrit,
125 a.deces != 0 AS dcd, a.deces, a.matricule_ax, FIND_IN_SET('femme', a.flags) AS sexe,
126 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
127 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
128 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
129 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
130 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
131 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey
132 FROM contacts AS c
133 INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact)
134 INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie')
135 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id)
136 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
137 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
138 LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2)
139 LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0)
140 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
141 LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1)
142 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
143 LEFT JOIN adresses AS adr ON (a.user_id = adr.uid AND FIND_IN_SET('active', adr.statut))
144 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
145 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
146 WHERE c.uid = $uid
147 ORDER BY ".$order;
148
149 $page->assign_by_ref('citer', $globals->xdb->iterator($sql));
150 }
151
152 $page->run();
153
154 // vim:set et sw=4 sts=4 sws=4:
155 ?>