/[cvs]/nfo/patches/horde/turba/csv-mapping/data.php.orig
ViewVC logotype

Annotation of /nfo/patches/horde/turba/csv-mapping/data.php.orig

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Mon Oct 21 11:20:18 2002 UTC (21 years, 10 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
initial checkin

1 joko 1.1 <?php
2     /*
3     * $Horde: turba/data.php,v 1.12.2.10 2002/05/27 17:05:20 jan Exp $
4     *
5     * Copyright 2001-2002 Jan Schneider <jan@horde.org>
6     *
7     * See the enclosed file COPYING for license information (GPL). If you
8     * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
9     */
10    
11     define('TURBA_BASE', dirname(__FILE__));
12     require_once TURBA_BASE . '/lib/base.php';
13     require_once TURBA_BASE . '/lib/Source.php';
14     require_once HORDE_BASE . '/lib/Data.php';
15     require TURBA_BASE . '/config/attributes.php';
16    
17     if (!$conf['menu']['import_export']) {
18     header('Location: ' . Horde::applicationUrl('index.php', true));
19     exit;
20     }
21    
22     $error = false;
23     $actionID = Horde::getFormData('actionID', NO_ACTION);
24    
25     if ($actionID == HORDE_IMPORT) {
26    
27     $importID = Horde::getFormData('importID');
28     $dates = array();
29    
30     if ($importID == IMPORT_MAPPED) {
31    
32     include_once HORDE_BASE . '/lib/SessionCache.php';
33     $cache = new SessionCache();
34     $cacheID = Horde::getFormData('cacheID');
35    
36     if (!isset($cacheID) || !(list($importData, $dest) = $cache->getObject($cacheID))) {
37     Horde::raiseMessage(_("The data got lost."), HORDE_ERROR);
38     $error = true;
39     } else {
40     $import_type = Horde::getFormData('import_type');
41     $dataKeys = Horde::getFormData('dataKeys', '');
42     $appKeys = Horde::getFormData('appKeys', '');
43     if (empty($dataKeys) || empty($appKeys)) {
44     Horde::raiseMessage(_("You didn't map any fields from the imported file to the corresponding fields in Turba."), HORDE_ERROR);
45     $error = true;
46     } else {
47     $dataKeys = explode("\t", $dataKeys);
48     $appKeys = explode("\t", $appKeys);
49     $map = array();
50     foreach ($appKeys as $key => $app) {
51     $map[$app] = $dataKeys[$key];
52     if ($attributes[$app]['type'] == 'date' ||
53     $attributes[$app]['type'] == 'time' ||
54     $attributes[$app]['type'] == 'datetime') {
55     $dates[$dataKeys[$key]]['type'] = $attributes[$app]['type'];
56     for ($i = 0; $i < min(count($importData), 10); $i++) {
57     if ($importData[$i][$dataKeys[$key]] != '') {
58     $dates[$dataKeys[$key]]['values'][] = $importData[$i][$dataKeys[$key]];
59     }
60     }
61     }
62     }
63    
64     if (count($dates) > 0) {
65     $cacheID = $cache->putObject(array($importData, $dest, $map));
66     }
67     }
68     }
69    
70     } elseif ($importID == IMPORT_DATETIME) {
71    
72     include_once HORDE_BASE . '/lib/SessionCache.php';
73     $cache = new SessionCache();
74     $cacheID = Horde::getFormData('cacheID');
75    
76     if (!isset($cacheID) || !($tmp = $cache->getObject($cacheID))) {
77     Horde::raiseMessage(_("The data got lost"), HORDE_ERROR);
78     $error = true;
79     } else {
80     $importData = $tmp[0];
81     $dest = $tmp[1];
82     $map = $tmp[2];
83     $delimiter = Horde::getFormData('delimiter');
84     $format = Horde::getFormData('format');
85     }
86    
87     }
88    
89    
90     if ($importID == IMPORT_MAPPED || $importID == IMPORT_DATETIME) {
91    
92     if (!$error && count($dates) == 0) {
93    
94     $driver = &Turba_Source::singleton($dest, $cfgSources[$dest]);
95     if (PEAR::isError($driver)) {
96     Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR);
97     $error = true;
98     } else {
99     foreach ($importData as $row) {
100     $hash = array();
101     reset($cfgSources[$dest]['map']);
102     while (list($key, ) = each($cfgSources[$dest]['map'])) {
103     if (isset($map[$key]) && !empty($row[$map[$key]])) {
104     if ($attributes[$key]['type'] == 'date' ||
105     $attributes[$key]['type'] == 'time' ||
106     $attributes[$key]['type'] == 'datetime') {
107     $dt_arr = Data::mapDate($row[$map[$key]], $attributes[$key]['type'], $delimiter[$map[$key]], $format[$map[$key]]);
108     }
109     if ($attributes[$key]['type'] == 'date' &&
110     $dt_arr['mday'] != 0 &&
111     $dt_arr['month'] != 0 &&
112     $dt_arr['year'] != 0) {
113     $hash[$key] = mktime(0, 0, 0, $dt_arr['month'], $dt_arr['mday'], $dt_arr['year']);
114     } elseif ($attributes[$key]['type'] == 'time') {
115     $hash[$key] = mktime($dt_arr['hour'], $dt_arr['min'], $dt_arr['sec'], 0, 0, 0);
116     } elseif ($attributes[$key]['type'] == 'datetime') {
117     $hash[$key] = mktime($dt_arr['hour'], $dt_arr['min'], $dt_arr['sec'], $dt_arr['month'], $dt_arr['mday'], $dt_arr['year']);
118     } else {
119     $hash[$key] = $row[$map[$key]];
120     }
121     }
122     }
123     $hash['__owner'] = Auth::getAuth();
124     if (!$driver->addObject($hash)) {
125     $error = true;
126     }
127     }
128     }
129    
130     if ($error) {
131     Horde::raiseMessage(_("There was an error importing the data."), HORDE_ERROR);
132     } else {
133     $import_type = Horde::getFormData('import_type');
134     switch ($import_type) {
135     case IMPORT_CSV:
136     $type = "CSV";
137     break;
138     case IMPORT_OUTLOOK:
139     $type = "Outlook";
140     break;
141     }
142     Horde::raiseMessage(sprintf(_("%s file successfully imported"), $type), HORDE_SUCCESS);
143     }
144    
145     }
146    
147     } else {
148    
149     if (!isset($HTTP_POST_FILES['import_file']['size']) ||
150     !isset($HTTP_POST_FILES['import_file']['tmp_name'])) {
151     Horde::raiseMessage(_("There was a problem with the file upload. The file may have been larger than the maximum allowed size."), HORDE_ERROR);
152     $error = true;
153     } elseif (!is_uploaded_file($HTTP_POST_FILES['import_file']['tmp_name'])) {
154     Horde::raiseMessage(_("Illegal or malformed form data."), HORDE_ERROR);
155     $error = true;
156     } elseif ($HTTP_POST_FILES['import_file']['size'] > 0) {
157     _fileCleanup($HTTP_POST_FILES['import_file']['tmp_name']);
158    
159     $header = null;
160     switch ($importID) {
161    
162     case IMPORT_OUTLOOK:
163     $header = true;
164    
165     case IMPORT_CSV:
166     $csv = new Data_csv();
167     if (!isset($header)) {
168     $header = Horde::getFormData('header');
169     }
170     $importData = $csv->importFile($HTTP_POST_FILES['import_file']['tmp_name'], $header, ',');
171    
172     if (!isset($importData) || !is_array($importData)) {
173     Horde::raiseMessage(_("There was an error importing the uploaded file"), HORDE_ERROR);
174     $error = true;
175     } else {
176     $dest = Horde::getFormData('dest');
177     include_once HORDE_BASE . '/lib/SessionCache.php';
178     $cache = new SessionCache();
179     $cacheID = $cache->putObject(array($importData, $dest));
180     $actionUrl = Horde::applicationUrl('data.php');
181    
182     $appFields = array();
183     reset($cfgSources[$dest]['map']);
184     while (list($key, ) = each($cfgSources[$dest]['map'])) {
185     if (substr($key, 0, 2) != '__') {
186     $appFields[$key] = $attributes[$key]['desc'];
187     }
188     }
189     }
190    
191     break;
192    
193     case IMPORT_VCARD:
194     $vcf = new Data_rfc2425();
195     $data = $vcf->importFile($HTTP_POST_FILES['import_file']['tmp_name']);
196     $dest = Horde::getFormData('dest');
197     $driver = &Turba_Source::singleton($dest, $cfgSources[$dest]);
198     if (PEAR::isError($driver)) {
199     Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR);
200     $error = true;
201     } else {
202     $dest = $cfgSources[$dest]['map'];
203     foreach ($data as $object) {
204     if ($object['type'] == 'VCARD') {
205     $hash = array();
206     foreach ($object['params'] as $item) {
207    
208     switch ($item['name']) {
209    
210     case 'FN':
211     $hash['name'] = $vcf->read($item);
212     break;
213    
214     case 'NICKNAME':
215     $hash['alias'] = $vcf->read($item);
216     break;
217    
218     // We use LABEL but should probably use ADR.
219     case 'LABEL':
220     if (isset($item['params']['HOME'])) {
221     $hash['homeAddress'] = $vcf->read($item);
222     } elseif (isset($item['params']['WORK'])) {
223     $hash['workAddress'] = $vcf->read($item);
224     } else {
225     $hash['workAddress'] = $vcf->read($item);
226     }
227     break;
228    
229     case 'TEL':
230     if (isset($item['params']['VOICE'])) {
231     if (isset($item['params']['HOME'])) {
232     $hash['homePhone'] = $vcf->read($item);
233     } elseif (isset($item['params']['WORK'])) {
234     $hash['workPhone'] = $vcf->read($item);
235     } elseif (isset($item['params']['CELL'])) {
236     $hash['cellPhone'] = $vcf->read($item);
237     }
238     } elseif (isset($item['params']['FAX'])) {
239     $hash['fax'] = $vcf->read($item);
240     }
241     break;
242    
243     case 'EMAIL':
244     if (isset($item['params']['PREF']) || !isset($hash['email'])) {
245     $hash['email'] = $vcf->read($item);
246     }
247     break;
248    
249     case 'TITLE':
250     $hash['title'] = $vcf->read($item);
251     break;
252    
253     case 'ORG':
254     $units = array();
255     for ($i = 0; $i < count($item['values']); $i++) {
256     $units[] = $vcf->read($item, $i);
257     }
258     $hash['company'] = implode(', ', $units);
259     break;
260    
261     case 'NOTE':
262     $hash['notes'] = $vcf->read($item);
263     break;
264    
265     }
266    
267     }
268     $hash['__owner'] = Auth::getAuth();
269     if (!$driver->addObject($hash)) {
270     $error = true;
271     }
272     }
273     }
274     }
275    
276     if ($error) {
277     Horde::raiseMessage(_("There was an error importing the data."), HORDE_ERROR);
278     } else {
279     Horde::raiseMessage(sprintf(_("%s file successfully imported"), 'vCard'), HORDE_SUCCESS);
280     }
281    
282     break;
283    
284     }
285    
286     } else {
287     Horde::raiseMessage(_("The file contained no data."), HORDE_ERROR);
288     $error = true;
289     }
290     }
291    
292     } elseif ($actionID == HORDE_EXPORT) {
293     $exportID = Horde::getFormData('exportID');
294    
295     switch ($exportID) {
296    
297     case EXPORT_CSV:
298     $source = Horde::getFormData('source');
299     if (!isset($source) && isset($cfgSources) && is_array($cfgSources) && count($cfgSources) > 0) {
300     reset($cfgSources);
301     $source = key($cfgSources);
302     }
303    
304     $driver = &Turba_Source::singleton($source, $cfgSources[$source]);
305     if (PEAR::isError($driver)) {
306     Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR);
307     $error = true;
308     } else {
309     if (get_class($addresses = $driver->search(array())) != 'turba_list') {
310     Horde::raiseMessage(sprintf(_("Failed to search the directory: %s"), $driver->error()), HORDE_ERROR);
311     $error = true;
312     } else {
313     $data = array();
314     while ($ob = $addresses->next()) {
315     $tmp = $ob->getAttributes();
316     $row = array();
317     foreach ($tmp as $key => $attribute) {
318     if (substr($key, 0, 2) != '__') {
319     $row[$key] = $attribute;
320     }
321     }
322     $data[] = $row;
323     }
324    
325     $csv = new Data_csv();
326     $csv->exportFile('turba.csv', $data, true);
327     exit;
328     }
329     }
330     break;
331    
332     }
333     }
334    
335     $title = _("Import/Export Addressbooks");
336     $js_onLoad = null;
337     require TURBA_TEMPLATES . '/common-header.inc';
338     require TURBA_BASE . '/menu.php';
339     require TURBA_BASE . '/status.php';
340    
341     if ($actionID == NO_ACTION || $error) {
342     /* Build the directory sources select widget. */
343     $source_options = '';
344     foreach ($cfgSources as $key => $entry) {
345     if (!empty($entry['export'])) {
346     $source_options .= "<option value=\"$key\">";
347     $source_options .= $entry['title'] . "</option>\n";
348     }
349     }
350    
351     /* Build the directory destination select widget. */
352     $dest_options = '';
353     $hasWriteable = false;
354     foreach ($cfgSources as $key => $entry) {
355     if (empty($entry['readonly']) || (isset($entry['admin']) && in_array(Auth::getAuth(), $entry['admin']))) {
356     $dest_options .= "<option value=\"$key\">";
357     $dest_options .= $entry['title'] . "</option>\n";
358     $hasWriteable = true;
359     }
360     }
361    
362     if ($hasWriteable) {
363     include TURBA_TEMPLATES . '/data/import.inc';
364     }
365     include TURBA_TEMPLATES . '/data/export.inc';
366     } else {
367     switch ($importID) {
368    
369     case IMPORT_OUTLOOK:
370     case IMPORT_CSV:
371     include $registry->getParam('templates', 'horde') . '/data/csvmap.inc';
372     break;
373    
374     case IMPORT_MAPPED:
375     if (count($dates) > 0) {
376     $actionUrl = Horde::applicationUrl('data.php');
377     include $registry->getParam('templates', 'horde') . '/data/datemap.inc';
378     }
379     break;
380    
381     }
382     }
383    
384     $registry->shutdown();
385     require TURBA_TEMPLATES . '/common-footer.inc';
386    
387     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed