Export the members of a list as a CSV file (Closes #927)
[platal.git] / modules / lists / lists.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 // {{{ function list_sort_owners
23
24 function list_sort_owners(&$members, $tri_promo = true) {
25 global $globals;
26
27 $membres = Array();
28
29 foreach($members as $mem) {
30 list($m, $dom) = explode('@', $mem);
31 $info = list_fetch_name($mem);
32 if (is_null($info['uid'])) {
33 $membres[0][] = array('l' => $mem, 'p' => (!$tri_promo ? 'inconnue' : null));
34 } else {
35 $uid = $info['uid'];
36 $nom = $info['nom'];
37 $prenom = $info['prenom'];
38 $promo = $info['promo'];
39 $broken = $info['lost'];
40 $key = $tri_promo ? ($promo != 'non-X' ? $promo : 0) : strtoupper(@$nom{0});
41 if ($tri_promo) {
42 $promo = null;
43 }
44 $membres[$key][$nom.$m] = Array('n' => "$prenom $nom", 'l' => $m, 'p' => $promo, 'x' => $uid, 'b' => $broken);
45 }
46 }
47
48 ksort($membres);
49 foreach($membres as $key=>$val) ksort($membres[$key]);
50 return $membres;
51 }
52
53 // }}}
54 // {{{ list_extract_member
55
56 function _list_extract_member($member)
57 {
58 return $member[1];
59 }
60
61 function list_extract_members($members)
62 {
63 return array_map('_list_extract_member', $members);
64 }
65
66 // }}}
67 // {{{ function list_sort_members
68
69 function list_sort_members(&$members, $tri_promo = true)
70 {
71 return list_sort_owners(list_extract_members($members), $tri_promo);
72 }
73
74 // }}}
75 // {{{ function list_fetch_names
76
77 function list_fetch_name($member)
78 {
79 global $globals;
80 list($m, $dom) = explode('@', $member);
81 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
82 $res = XDB::query('SELECT u.user_id AS uid, prenom AS prenom, IF(nom_usage="", nom, nom_usage) AS nom,
83 promo AS promo,
84 (e.uid IS NULL AND FIND_IN_SET("googleapps", u.mail_storage) = 0) AS lost
85 FROM auth_user_md5 AS u
86 INNER JOIN aliases AS a ON u.user_id = a.id
87 LEFT JOIN emails AS e ON (e.flags = "active" AND e.uid = u.user_id)
88 WHERE a.alias = {?}
89 GROUP BY u.user_id', $m);
90 } else {
91 $res = XDB::query('SELECT m2.uid AS uid,
92 IF(m2.origine="X", u.prenom, m1.prenom) AS prenom,
93 IF(m2.origine="X", u.nom, m1.nom) AS nom,
94 IF(m2.origine="X", u.promo, "non-X") AS promo,
95 0 AS lost
96 FROM groupex.membres AS m1
97 LEFT JOIN groupex.membres AS m2 ON(m1.email=m2.email AND m2.asso_id={?})
98 LEFT JOIN auth_user_md5 AS u ON(m2.origine = "X" AND m2.uid = u.user_id)
99 WHERE m1.email={?}', $globals->asso('id'), $member);
100 }
101 if ($res->numRows() == 0) {
102 return array('email' => $member);
103 } else {
104 return array_merge(array('email' => $member), $res->fetchOneAssoc());
105 }
106 }
107
108 function list_fetch_names($members)
109 {
110 $res = array();
111 foreach ($members as $member) {
112 $res[] = list_fetch_name($member);
113 }
114 return $res;
115 }
116
117 // }}}
118 // {{{ function list_header_decode
119
120 function _list_header_decode($charset, $c, $str) {
121 $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str);
122 $s = iconv($charset, 'UTF-8', $s);
123 return str_replace('_', ' ', $s);
124 }
125
126 function list_header_decode($value) {
127 if (!$value) {
128 return "[pas de sujet]";
129 }
130 $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value);
131 return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_list_header_decode("\1", "\2", "\3")', $val);
132 }
133
134 // }}}
135 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
136 ?>