1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
<?php
require_once 'bedita_base.php';
class AddressbookShell extends BeditaBaseShell {
public function import() {
if(!isset($this->params['f'])) {
$this->out('Input file is mandatory');
return;
}
$options = array('mailGroups' => array());
$mail_group_id = null;
$mailgroup = ClassRegistry::init('MailGroup');
if (isset($this->params['m'])) {
$mail_group_id = $mailgroup->field('id', array('group_name' => $this->params['m']));
if (empty($mail_group_id)) {
$this->out('Mail group ' . $this->params['m'] . ' not found: import aborted');
return false;
}
$options['joinGroup'][0]['mail_group_id'] = $mail_group_id;
$options['joinGroup'][0]['status'] = 'confirmed';
} else {
$allMailGroups = $mailgroup->find('all');
if (!empty($allMailGroups)) {
foreach ($allMailGroups as $mg) {
$options['mailGroups'][$mg['MailGroup']['group_name']] = $mg['MailGroup']['id'];
}
}
}
$cardFile = $this->params['f'];
if(!file_exists($cardFile)) {
$this->out("$cardFile not found, bye");
return;
}
if (!isset($this->params['c'])) {
$this->out('No categories set');
} else {
$categories = trim($this->params['c']);
$catTmp = split(',', $categories);
$categoryModel = ClassRegistry::init('Category');
$cardTypeId = Configure::read('objectTypes.card.id');
$options['Category'] = $categoryModel->findCreateCategories($catTmp, $cardTypeId);
}
$ext = strtolower(substr($cardFile, strrpos($cardFile, ".")+1));
$isCsv = ($ext == 'csv');
$this->out("Importing file $cardFile using " . (($isCsv) ? 'CSV' : 'VCard') . ' format');
if (isset($this->params['delimiter'])) {
$options['delimiter'] = $this->params['delimiter'];
$this->out('Using delimiter: ' . $options['delimiter']);
}
if (isset($this->params['overwritePolicy'])) {
$options['overwritePolicy'] = $this->params['overwritePolicy'];
$this->out('Using overwritePolicy: ' . $options['overwritePolicy']);
}
App::import('Component', 'Transaction');
$transaction = new TransactionComponent();
$cardModel = ClassRegistry::init('Card');
if($isCsv) {
$result = $cardModel->importCSVFile($cardFile, $options);
} else {
$result = $cardModel->importVCardFile($cardFile, $options);
}
$this->out('Done');
$this->out('Result: ' . print_r($result, true));
$transaction->commit();
}
public function export() {
if (!isset($this->params['f'])) {
$this->out('Output file is mandatory');
return;
}
$cardFile = $this->params['f'];
$this->checkExportFile($cardFile);
$options = array();
$type = 'vcard';
if (isset($this->params['t'])) {
$type = strtolower($this->params['t']);
if (!in_array($type, array('vcard', 'csv', 'custom'))) {
$this->out("Unknown type $type");
return;
}
if ($type == 'custom') {
$options['custom'] = true;
}
} else {
$ext = strtolower(substr($cardFile, strrpos($cardFile, '.')+1));
if ($ext == 'csv') {
$type = 'csv';
}
}
$this->out("Exporting to $cardFile using '$type' format");
if (isset($this->params['delimiter'])) {
$options['delimiter'] = $this->params['delimiter'];
$this->out('Using delimiter: ' . $options['delimiter']);
}
$cardModel = ClassRegistry::init('Card');
$cardModel->contain();
$res = $cardModel->find('all', array('fields' => array('id')));
$handle = fopen($cardFile, 'w');
if($type !== 'vcard') {
fwrite($handle, $cardModel->headerCSV($options) . "\n");
}
foreach ($res as $r) {
$cardModel->id = $r['id'];
if($type !== 'vcard') {
$str = $cardModel->exportCSV($options);
} else {
$str = $cardModel->exportVCard();
}
fwrite($handle, $str . "\n");
}
fclose($handle);
$this->out("$cardFile created.");
}
function help() {
$this->out('Available functions:');
$this->out(' ');
$this->out('1. import: import vcf/vcard or microsoft outlook csv file, or custom csv file');
$this->out(' ');
$this->out(' Usage: import -f <csv-cardfile> [-c <categories>] [-m <mail-group-name>] [-delimiter <delimiter>] [-overwritePolicy <policy>]' );
$this->out(' ');
$this->out(" -f <csv-cardfile>\t vcf/vcard or csv file to import");
$this->out(" -c <categories> \t comma separated <categories> to use on import (created if not exist)");
$this->out(" -m <mail-group-name> \t name of mail group to associate with imported cards");
$this->out(" -delimiter <delimiter> \t CSV delimiter, default is ','");
$this->out(" -overwritePolicy <policy> \t 'skip'(default), 'overwrite', 'new'");
$this->out(' ');
$this->out('2. export: export to vcf/vcard or microsoft outlook csv file');
$this->out(' ');
$this->out(' Usage: export -f <csv-cardfile> [-t <type>] [-delimiter <delimiter>]');
$this->out(' ');
$this->out(" -f <csv-cardfile>\t vcf/vcard or csv file to export");
$this->out(" -t <type> \t 'vcard' (default), 'csv' or 'custom'");
$this->out(" -delimiter <delimiter> \t CSV delimiter, default is ','");
$this->out(' ');
}
}
?>