コード例 #1
0
 /**
  * @param $properties
  * @return mixed
  */
 public function createOrUpdate($properties)
 {
     $id = null;
     /**
      * @var \OCA\Contacts\VObject\VCard
      */
     $vcard = null;
     if (array_key_exists('id', $properties)) {
         // TODO: test if $id belongs to this addressbook
         $id = $properties['id'];
         // TODO: Test $vcard
         $vcard = $this->addressBook->getChild($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 = $this->addressBook->addChild($vcard);
         } catch (\Exception $e) {
             \OCP\Util::writeLog('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) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
         return false;
     }
     $asarray = VCard::structureContact($vcard);
     $asarray['id'] = $id;
     return $asarray;
 }