示例#1
0
 protected function actionImportVCard($params)
 {
     $summaryLog = new \GO\Base\Component\SummaryLog();
     $readOnly = !empty($params['readOnly']);
     if (isset($_FILES['files']['tmp_name'][0])) {
         $params['file'] = $_FILES['files']['tmp_name'][0];
     }
     if (!empty($params['importBaseParams'])) {
         $importBaseParams = json_decode($params['importBaseParams'], true);
         $params['addressbook_id'] = $importBaseParams['addressbook_id'];
     }
     $file = new \GO\Base\Fs\File($params['file']);
     $file->convertToUtf8();
     $options = \Sabre\VObject\Reader::OPTION_FORGIVING + \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES;
     $vcards = new \Sabre\VObject\Splitter\VCard(fopen($file->path(), 'r+'), $options);
     unset($params['file']);
     $nr = 0;
     if ($readOnly) {
         $contactsAttr = array();
     }
     while ($vObject = $vcards->getNext()) {
         $nr++;
         \GO\Base\VObject\Reader::convertVCard21ToVCard30($vObject);
         $contact = new \GO\Addressbook\Model\Contact();
         try {
             if ($contact->importVObject($vObject, $params, !$readOnly)) {
                 $summaryLog->addSuccessful();
             }
             if ($readOnly) {
                 $contactsAttr[] = $contact->getAttributes('formatted');
             }
         } catch (\Exception $e) {
             $summaryLog->addError($nr, $e->getMessage());
         }
     }
     $response = $summaryLog->getErrorsJson();
     if ($readOnly) {
         $response['contacts'] = $contactsAttr;
     }
     $response['successCount'] = $summaryLog->getTotalSuccessful();
     $response['totalCount'] = $summaryLog->getTotal();
     $response['success'] = true;
     return $response;
 }