public function import() { if (is_null($this->userid)) { throw new \Exception('No user id set'); } if (!$this->isValid()) { return false; } $this->numofcomponents = count($this->vcardobject); if ($this->overwrite) { foreach (VCard::all($this->id) as $obj) { VCard::delete($obj['id']); } } foreach ($this->vcardobject as $object) { if (!$object instanceof \Sabre\VObject\Component\VCard) { //continue; } $vcard = \Sabre\VObject\Reader::read($object, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); $importVCard = VCard::structureContact($vcard); if ($importVCard !== false) { $newVcard = $this->parseVCard($importVCard, $vcard); $insertid = VCard::add($this->id, $newVcard); $this->abscount++; } else { $this->errorCount++; } if ($this->isDuplicate($insertid)) { VCard::delete($insertid); $this->abscount--; } $this->updateProgress(intval($this->abscount / $this->numofcomponents * 100)); } \OC::$server->getCache()->remove($this->progresskey); return true; }
/** * @param $properties * @return mixed */ public function createOrUpdate($properties) { $id = null; $vcard = null; if (array_key_exists('id', $properties)) { // TODO: test if $id belongs to this addressbook $id = $properties['id']; // TODO: Test $vcard $vcard = App::getContactVCard($properties['id']); foreach (array_keys($properties) as $name) { if (isset($vcard->{$name})) { unset($vcard->{$name}); } } } else { $vcard = \Sabre\VObject\Component::create('VCARD'); $uid = substr(md5(rand() . time()), 0, 10); $vcard->add('UID', $uid); try { $id = VCard::add($this->id, $vcard, null, true); } catch (Exception $e) { \OC_Log::write('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR); return false; } } foreach ($properties as $name => $value) { switch ($name) { case 'ADR': case 'N': if (is_array($value)) { $property = \Sabre\VObject\Property::create($name); $property->setParts($value); $vcard->add($property); } else { $vcard->{$name} = $value; } break; case 'BDAY': // TODO: try/catch $date = new \DateTime($value); $vcard->BDAY = $date->format('Y-m-d'); $vcard->BDAY->VALUE = 'DATE'; break; case 'EMAIL': case 'TEL': case 'IMPP': // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol case 'URL': if (is_array($value)) { foreach ($value as $val) { $vcard->add($name, strip_tags($val)); } } else { $vcard->add($name, strip_tags($value)); } default: $vcard->{$name} = $value; break; } } try { VCard::edit($id, $vcard); } catch (Exception $e) { \OC_Log::write('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR); return false; } $asarray = VCard::structureContact($vcard); $asarray['id'] = $id; return $asarray; }