Exemplo n.º 1
0
 public function testGroupProperty()
 {
     $arr = array('Home', 'work', 'Friends, Family');
     $vcard = new \OCA\Contacts\VObject\VCard();
     $property = $vcard->createProperty('CATEGORIES');
     $property->setParts($arr);
     // Test parsing and serializing
     $this->assertEquals('Home,work,Friends\\, Family', $property->getValue());
     $this->assertEquals('CATEGORIES:Home,work,Friends\\, Family' . "\r\n", $property->serialize());
     $this->assertEquals(3, count($property->getParts()));
     // Test add
     $property->addGroup('Coworkers');
     $this->assertTrue($property->hasGroup('coworkers'));
     $this->assertEquals(4, count($property->getParts()));
     $this->assertEquals('Home,work,Friends\\, Family,Coworkers', $property->getValue());
     // Test remove
     $this->assertTrue($property->hasGroup('Friends, fAmIlY'));
     $property->removeGroup('Friends, fAmIlY');
     $this->assertEquals(3, count($property->getParts()));
     $parts = $property->getParts();
     $this->assertEquals('Coworkers', $parts[2]);
     // Test rename
     $property->renameGroup('work', 'Work');
     $parts = $property->getParts();
     $this->assertEquals('Work', $parts[1]);
     //$this->assertEquals(true, false);
 }
 /**
  * @brief converts a VCard into a owncloud VCard
  * @param $element the VCard element to convert
  * @return VCard|false
  */
 public function convertElementToVCard($element)
 {
     try {
         $source = VObject\Reader::read($element, VObject\Reader::OPTION_FORGIVING);
     } catch (VObject\ParseException $error) {
         return false;
     }
     $dest = new \OCA\Contacts\VObject\VCard();
     foreach ($source->children() as $sourceProperty) {
         $importEntry = $this->getImportEntry($sourceProperty, $source);
         if ($importEntry) {
             $value = $sourceProperty->getValue();
             if (isset($importEntry['remove'])) {
                 $value = str_replace($importEntry['remove'], '', $sourceProperty->getValue());
             }
             $values = array($value);
             if (isset($importEntry['separator'])) {
                 $values = explode($importEntry['separator'], $value);
             }
             foreach ($values as $oneValue) {
                 if (isset($importEntry->vcard_favourites)) {
                     foreach ($importEntry->vcard_favourites as $vcardFavourite) {
                         if (strcasecmp((string) $vcardFavourite, trim($oneValue)) == 0) {
                             $property = $dest->createProperty("X-FAVOURITES", 'yes');
                             $dest->add($property);
                         } else {
                             $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
                             $this->updateProperty($property, $importEntry, trim($oneValue));
                         }
                     }
                 } else {
                     $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
                     $this->updateProperty($property, $importEntry, $sourceProperty->getValue());
                 }
             }
         } else {
             $property = clone $sourceProperty;
             $dest->add($property);
         }
     }
     $dest->validate(\Sabre\VObject\Component\VCard::REPAIR);
     return $dest;
 }
 /**
  * @brief converts a unique element into a owncloud VCard
  * @param $element the element to convert
  * @return VCard, all unconverted elements are stored in X-Unknown-Element parameters
  */
 public function convertElementToVCard($element, $title = null)
 {
     $vcard = new \OCA\Contacts\VObject\VCard();
     $nbElt = count($element);
     for ($i = 0; $i < $nbElt; $i++) {
         if ($element[$i] != '') {
             //$importEntry = false;
             // Look for the right import_entry
             if (isset($this->configContent->import_core->base_parsing)) {
                 if (strcasecmp((string) $this->configContent->import_core->base_parsing, 'position') == 0) {
                     $importEntry = $this->getImportEntryFromPosition((string) $i);
                 } else {
                     if (strcasecmp((string) $this->configContent->import_core->base_parsing, 'name') == 0 && isset($title[$i])) {
                         $importEntry = $this->getImportEntryFromName($title[$i]);
                     }
                 }
             }
             if ($importEntry) {
                 // Create a new property and attach it to the vcard
                 $value = $element[$i];
                 if (isset($importEntry['remove'])) {
                     $value = str_replace($importEntry['remove'], '', $element[$i]);
                 }
                 $values = array($value);
                 if (isset($importEntry['separator'])) {
                     $values = explode($importEntry['separator'], $value);
                 }
                 foreach ($values as $oneValue) {
                     if (isset($importEntry->vcard_favourites)) {
                         foreach ($importEntry->vcard_favourites as $vcardFavourite) {
                             if (strcasecmp((string) $vcardFavourite, trim($oneValue)) == 0) {
                                 $property = $vcard->createProperty("X-FAVOURITES", 'yes');
                                 $vcard->add($property);
                             } else {
                                 $property = $this->getOrCreateVCardProperty($vcard, $importEntry->vcard_entry);
                                 $this->updateProperty($property, $importEntry, trim($oneValue));
                             }
                         }
                     } else {
                         $property = $this->getOrCreateVCardProperty($vcard, $importEntry->vcard_entry);
                         $this->updateProperty($property, $importEntry, trim($oneValue));
                     }
                 }
             } else {
                 if (isset($element[$i]) && isset($title[$i])) {
                     $property = $vcard->createProperty("X-Unknown-Element", StringUtil::convertToUTF8($element[$i]));
                     $property->add('TYPE', StringUtil::convertToUTF8($title[$i]));
                     $vcard->add($property);
                 }
             }
         }
     }
     $vcard->validate(\Sabre\VObject\Component\VCard::REPAIR);
     return $vcard;
 }