Adds Outlook CSV format for birthdays and contacts (Closes #670)
[platal.git] / modules / carnet / outlook.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 Outlook {
23
24 static $contact_fields = array(
25 'fr' => array("Nom","Titre","Prénom","Deuxième prénom","Nom","Suffixe","Surnom","Société ","Service ","Titre","Rue (bureau)","Rue (bureau) 2","Rue (bureau) 3","Ville (bureau)","Dép/Région (bureau)","Code postal (bureau)","Pays (bureau)","Rue (domicile)","Rue (domicile) 2","Rue (domicile) 3","Ville (domicile)","Dép/Région (domicile)","Code postal (domicile)","Pays (domicile)","Rue (autre)","Rue (autre) 2","Rue (autre) 3","Ville (autre)","Dép/Région (autre)","Code postal (autre)","Pays (autre)","Téléphone de l'assistant(e)","Télécopie (bureau)","Téléphone (bureau)","Téléphone 2 (bureau)","Rappel","Téléphone (voiture)","Téléphone société","Télécopie (domicile)","Téléphone (domicile)","Téléphone 2 (domicile)","RNIS","Tél. mobile","Télécopie (autre)","Téléphone (autre)","Récepteur de radiomessagerie","Téléphone principal","Radio téléphone","Téléphone TDD/TTY","Télex","Adresse de messagerie","Type de messagerie","Nom complet de l'adresse de messagerie","Adresse de messagerie 2","Type de messagerie 2","Nom complet de l'adresse de messagerie 2","Adresse de messagerie 3","Type de messagerie 3","Nom complet de l'adresse de messagerie 3","Anniversaire","Anniversaire de mariage ou fête","Autre boîte postale","B.P. professionnelle","Boîte postale du domicile","Bureau","Catégories","Code gouvernement","Compte","Conjoint(e)","Critère de diffusion","Disponibilité Internet","Emplacement","Enfants","Informations facturation","Initiales","Kilométrage","Langue","Mots clés","Nom de l'assistant(e)","Notes","Numéro d'identification de l'organisation","Page Web","Passe-temps","Priorité","Privé","Profession","Recommandé par","Responsable","Serveur d'annuaire","Sexe","Utilisateur 1","Utilisateur 2","Utilisateur 3","Utilisateur 4"),
26 );
27
28 private static function add_address(&$adr, &$contact, $adr_type = 'autre') {
29 $contact['Rue ('.$adr_type.')'] = $adr->text;
30 $contact['Code postal ('.$adr_type.')'] = $adr->postalCode;
31 $contact['Ville ('.$adr_type.')'] = $adr->locality;
32 $contact['Dép/Région ('.$adr_type.')'] = $adr->administrativeArea;
33 $contact['Pays ('.$adr_type.')'] = $adr->country;
34 $contact['Téléphone ('.$adr_type.')'] = $adr->fixed_tel;
35 $contact['Télécopie ('.$adr_type.')'] = $adr->fax_tel;
36 }
37
38 private static function profile_to_contact(&$p) {
39 $contact = array(
40 'Prénom' => $p->firstName(),
41 'Nom' => $p->lastName(),
42 'Notes' => '('.$p->promo.')',
43 'Tél. mobile' => $p->mobile,
44 'Anniversaire' => $p->birthdate,
45 'Surnom' => $p->nickname,
46 );
47 // Homes
48 $adrs = $p->iterAddresses(Profile::ADDRESS_PERSO);
49 if ($adr = $adrs->next()) {
50 Outlook::add_address(&$adr, &$contact, 'domicile');
51 }
52 if ($adr = $adrs->next()) {
53 Outlook::add_address(&$adr, &$contact, 'autre');
54 }
55 // Pro
56 $adrs = $p->iterAddresses(Profile::ADDRESS_PRO);
57 if ($adr = $adrs->next()) {
58 Outlook::add_address(&$adr, &$contact, 'bureau');
59 }
60 $mainjob = $p->getMainJob();
61 if ($mainjob && $mainjob->company) {
62 $contact['Société '] = $mainjob->company->name;
63 }
64 if (!empty($p->section)) {
65 $contact['Utilisateur 2'] = 'Section : '. $p->section;
66 }
67 if ($p->isFemale()) {
68 $contact['Sexe'] = 'Féminin';
69 } else {
70 $contact['Sexe'] = 'Masculin';
71 }
72 $binets = $p->getBinets();
73 if (count($binets)) {
74 $bn = DirEnum::getOptions(DirEnum::BINETS);
75 $bns = array();
76 foreach (array_keys($binets) as $bid) if (!empty($bn[$bid])) {
77 $bns[$bid] = $bn[$bid];
78 }
79 if (count($bns) > 0) {
80 $contact['Utilisateur 3'] = 'Binets : '.join(', ', $bns);
81 }
82 }
83 $user = $p->owner();
84 if ($user) {
85 $contact['Adresse de messagerie'] = $user->bestalias;
86 $contact['Nom complet de l\'adresse de messagerie'] = $p->fullName().' <'.$user->bestalias.'>';
87 $contact['Adresse de messagerie 2'] = $user->bestalias_alternate;
88 $contact['Nom complet de l\'adresse de messagerie 2'] = $p->fullName().' <'.$user->bestalias_alternate.'>';
89 if ($user->bestalias != $user->forlife) {
90 $contact['Adresse de messagerie 3'] = $user->forlife;
91 $contact['Nom complet de l\'adresse de messagerie 3'] = $p->fullName().' <'.$user->forlife.'>';
92 }
93 $groups = $user->groups();
94 if (count($groups)) {
95 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
96 $gns = array();
97 foreach (array_keys($groups) as $gid) if (!empty($gn[$gid])) {
98 $gns[$gid] = $gn[$gid];
99 }
100 if (count($gns) > 0) {
101 $contact['Utilisateur 1'] = 'Groupes X : '. join(', ', $gns);
102 }
103 }
104 }
105 return $contact;
106 }
107
108 private static function protect($t) {
109 if (empty($t)) {
110 return '""';
111 }
112 $t = preg_replace("/\r?\n/", ", ", $t);
113 return '"'.strtr(utf8_decode($t),'"', '\\"').'"';
114 }
115
116 public static function output_profiles(&$profiles, $lang) {
117 header("Content-Type: text/plain;charset=iso8859-15");
118 $fields =& Outlook::$contact_fields[$lang];
119 foreach ($fields as $i => $k) {
120 if ($i != 0) {
121 echo ',';
122 }
123 echo Outlook::protect($k);
124 }
125 echo "\r\n";
126 foreach ($profiles as &$p) {
127 $values = Outlook::profile_to_contact(&$p);
128 foreach ($fields as $i => $k) {
129 if ($i != 0) {
130 echo ',';
131 echo Outlook::protect($values[$k]);
132 } else {
133 // HACK to fix fullname
134 echo Outlook::protect($p->firstName()." ".$p->lastName());
135 }
136 }
137 echo "\r\n";
138 }
139 }
140 }
141 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
142 ?>