]> source.dussan.org Git - nextcloud-server.git/commitdiff
Contacts: change card parameter type of VCard::edit and VCard::add
authorBart Visscher <bartv@thisnet.nl>
Wed, 7 Mar 2012 20:27:03 +0000 (21:27 +0100)
committerBart Visscher <bartv@thisnet.nl>
Wed, 7 Mar 2012 20:46:42 +0000 (21:46 +0100)
changed to OC_VObject

apps/contacts/ajax/addcard.php
apps/contacts/ajax/addcontact.php
apps/contacts/ajax/addproperty.php
apps/contacts/ajax/deleteproperty.php
apps/contacts/ajax/savecrop.php
apps/contacts/ajax/saveproperty.php
apps/contacts/ajax/setproperty.php
apps/contacts/import.php
apps/contacts/lib/app.php
apps/contacts/lib/vcard.php

index b1dc69a469105d56dfc56b3423dd40078ff6948f..49a4a16170b8a18c62ed482132522778a1419580 100644 (file)
@@ -92,7 +92,7 @@ foreach( $add as $propname){
                }
        }
 }
-$id = OC_Contacts_VCard::add($aid,$vcard->serialize());
+$id = OC_Contacts_VCard::add($aid,$vcard);
 if(!$id) {
        OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('There was an error adding the contact.'))));
        OC_Log::write('contacts','ajax/addcard.php: Recieved non-positive ID on adding card: '.$id, OC_Log::ERROR);
index 5d17631caa4deff8bc04386d3e2b3419121aa081..839a39199811dbbe451e0b3ed3991a357d038dcb 100644 (file)
@@ -52,7 +52,7 @@ $vcard->setUID();
 $vcard->setString('FN',$fn);
 $vcard->setString('N',$n);
 
-$id = OC_Contacts_VCard::add($aid,$vcard->serialize());
+$id = OC_Contacts_VCard::add($aid,$vcard);
 if(!$id) {
        OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('There was an error adding the contact.'))));
        OC_Log::write('contacts','ajax/addcontact.php: Recieved non-positive ID on adding card: '.$id, OC_Log::ERROR);
index 028974e1c66b1bc38dea45ec7512127a3a1e07aa..b6b5dc7c416446eb333bfc32329b461a78d5750b 100644 (file)
@@ -113,7 +113,7 @@ foreach ($parameters as $key=>$element) {
 }
 $checksum = md5($vcard->children[$line]->serialize());
 
-if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+if(!OC_Contacts_VCard::edit($id,$vcard)) {
        OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error adding contact property.'))));
        OC_Log::write('contacts','ajax/addproperty.php: Error updating contact property: '.$name, OC_Log::ERROR);
        exit();
index a9afffaad4c490f6902f416a4940c4be78ed456b..ab0958cac58ab0a6cdb5c9ff3d2da78871c9845b 100644 (file)
@@ -39,7 +39,7 @@ if(is_null($line)){
 
 unset($vcard->children[$line]);
 
-if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+if(!OC_Contacts_VCard::edit($id,$vcard)) {
        OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error deleting contact property.'))));
        OC_Log::write('contacts','ajax/deleteproperty.php: Error deleting contact property', OC_Log::ERROR);
        exit();
index 1a84f6fdfaea55c0d5b5711b0fff52aeef26f062..ffbfaeb6e384c1998c8e05b99f6a513e0101eeb0 100644 (file)
@@ -95,7 +95,7 @@ if(file_exists($tmp_path)) {
                                                OC_Log::write('contacts','savecrop.php: files: Adding PHOTO property.', OC_Log::DEBUG);
                                                $card->addProperty('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $image->mimeType()));
                                        }
-                                       if(!OC_Contacts_VCard::edit($id,$card->serialize())) {
+                                       if(!OC_Contacts_VCard::edit($id,$card)) {
                                                bailOut('Error saving contact.');
                                        }
                                        unlink($tmpfname);
index 6f8366243fe59377fe7dfce0be076102ee9b8f1c..db209fedfc702b94050736341dc8895855fdf5a6 100644 (file)
@@ -122,7 +122,7 @@ switch($element) {
 $checksum = md5($vcard->children[$line]->serialize());
 debug('New checksum: '.$checksum);
 
-if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+if(!OC_Contacts_VCard::edit($id,$vcard)) {
        OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error updating contact property.'))));
        OC_Log::write('contacts','ajax/setproperty.php: Error updating contact property: '.$value, OC_Log::ERROR);
        exit();
index f9e2a8e864733dfec8652cb15f63c382a3cb2c07..8e07b4a8f1cd383498f4683b91e32e2e163b1fcb 100644 (file)
@@ -80,7 +80,7 @@ foreach($missingparameters as $i){
 // NOTE: This checksum is not used..?
 $checksum = md5($vcard->children[$line]->serialize());
 
-if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+if(!OC_Contacts_VCard::edit($id,$vcard)) {
        OC_JSON::error(array('data' => array('message' => $l->t('Error updating contact property.'))));
        OC_Log::write('contacts','ajax/setproperty.php: Error updating contact property: '.$value, OC_Log::ERROR);
        exit();
index 4638bf0d73cd609ff6ac4e9e4d6efc6ede26954e..04cfc397d56a7322dadbaae7ad90f8e551092f3b 100644 (file)
@@ -97,11 +97,15 @@ if(is_writable('import_tmp/')){
        fclose($progressfopen);
 }
 if(count($parts) == 1){
-       OC_Contacts_VCard::add($id, $file);
-}else{
-       foreach($importready as $import){
-               OC_Contacts_VCard::add($id, $import);
+       $importready = array($file);
+}
+foreach($importready as $import){
+       $card = OC_VObject::parse($import);
+       if (!$card) {
+               OC_Log::write('contacts','Import: skipping card. Error parsing VCard: '.$import, OC_Log::ERROR);
+               continue; // Ditch cards that can't be parsed by Sabre.
        }
+       OC_Contacts_VCard::add($id, $card);
 }
 //done the import
 if(is_writable('import_tmp/')){
@@ -113,4 +117,4 @@ sleep(3);
 if(is_writable('import_tmp/')){
        unlink($progressfile);
 }
-OC_JSON::success();
\ No newline at end of file
+OC_JSON::success();
index ce52df4b75ef89f224fcc85ffc9b87d54e19690e..e8c3087c8a2f97fdec13fffb6dde4a0b290603d5 100644 (file)
@@ -92,7 +92,7 @@ class OC_Contacts_App {
                                OC_Log::write('contacts','getContactVCard, found FN field: '.$vcard->__get('FN'), OC_Log::DEBUG);
                                $n = implode(';', array_reverse(array_slice(explode(' ', $vcard->__get('FN')), 0, 2))).';;;';
                                $vcard->setString('N', $n);
-                               OC_Contacts_VCard::edit( $id, $vcard->serialize());
+                               OC_Contacts_VCard::edit( $id, $vcard);
                        } else { // Else just add an empty 'N' field :-P
                                $vcard->setString('N', 'Unknown;Name;;;');
                        }
index 0b8d95a2d97e09784057d2b8b4c833e6934e375e..9324f158cc77322f86ca7f5dbeca5bffcf5c4d99 100644 (file)
@@ -217,31 +217,35 @@ class OC_Contacts_VCard{
 
        /**
         * @brief Adds a card
-        * @param integer $id Addressbook id
-        * @param string $data  vCard file
-        * @return insertid on success or null if card is not parseable.
+        * @param integer $aid Addressbook id
+        * @param OC_VObject $card  vCard file
+        * @param string $uri the uri of the card, default based on the UID
+        * @return insertid on success or null if no card.
         */
-       public static function add($id,$data){
-               $fn = null;
-
-               $card = OC_VObject::parse($data);
-               if(!is_null($card)){
-                       self::updateValuesFromAdd($card);
-                       $data = $card->serialize();
-               }
-               else{
-                       OC_Log::write('contacts','OC_Contacts_VCard::add. Error parsing VCard: '.$data,OC_Log::ERROR);
-                       return null; // Ditch cards that can't be parsed by Sabre.
+       public static function add($aid, $card, $uri=null){
+               if(is_null($card)){
+                       OC_Log::write('contacts','OC_Contacts_VCard::add. No vCard supplied', OC_Log::ERROR);
+                       return null;
                };
 
+               self::updateValuesFromAdd($card);
+
                $fn = $card->getAsString('FN');
-               $uid = $card->getAsString('UID');
-               $uri = $uid.'.vcf';
+               if (empty($fn)) {
+                       $fn = null;
+               }
+
+               if (!$uri) {
+                       $uid = $card->getAsString('UID');
+                       $uri = $uid.'.vcf';
+               }
+
+               $data = $card->serialize();
                $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
-               $result = $stmt->execute(array($id,$fn,$data,$uri,time()));
+               $result = $stmt->execute(array($aid,$fn,$data,$uri,time()));
                $newid = OC_DB::insertid('*PREFIX*contacts_cards');
 
-               OC_Contacts_Addressbook::touch($id);
+               OC_Contacts_Addressbook::touch($aid);
 
                return $newid;
        }
@@ -255,49 +259,31 @@ class OC_Contacts_VCard{
         */
        public static function addFromDAVData($id,$uri,$data){
                $card = OC_VObject::parse($data);
-               if(!is_null($card)){
-                       self::updateValuesFromAdd($card);
-                       $data = $card->serialize();
-               } else {
-                       OC_Log::write('contacts','OC_Contacts_VCard::addFromDAVData. Error parsing VCard: '.$data, OC_Log::ERROR);
-                       return null; // Ditch cards that can't be parsed by Sabre.
-               };
-               $fn = $card->getAsString('FN');
-
-               $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
-               $result = $stmt->execute(array($id,$fn,$data,$uri,time()));
-               $newid = OC_DB::insertid('*PREFIX*contacts_cards');
-
-               OC_Contacts_Addressbook::touch($id);
-
-               return $newid;
+               return self::add($id, $data, $uri);
        }
 
        /**
         * @brief edits a card
         * @param integer $id id of card
-        * @param string $data  vCard file
+        * @param OC_VObject $card  vCard file
         * @return boolean
         */
-       public static function edit($id, $data){
+       public static function edit($id, OC_VObject $card){
                $oldcard = self::find($id);
-               $fn = null;
 
-               $card = OC_VObject::parse($data);
-               if(!is_null($card)){
-                       foreach($card->children as $property){
-                               if($property->name == 'FN'){
-                                       $fn = $property->value;
-                                       break;
-                               }
-                       }
-               } else {
+               if(is_null($card)) {
                        return false;
                }
+
+               $fn = $card->getAsString('FN');
+               if (empty($fn)) {
+                       $fn = null;
+               }
+
                $now = new DateTime;
                $card->setString('REV', $now->format(DateTime::W3C));
-               $data = $card->serialize();
 
+               $data = $card->serialize();
                $stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
                $result = $stmt->execute(array($fn,$data,time(),$id));
 
@@ -315,27 +301,8 @@ class OC_Contacts_VCard{
         */
        public static function editFromDAVData($aid,$uri,$data){
                $oldcard = self::findWhereDAVDataIs($aid,$uri);
-
-               $fn = null;
                $card = OC_VObject::parse($data);
-               if(!is_null($card)){
-                       foreach($card->children as $property){
-                               if($property->name == 'FN'){
-                                       $fn = $property->value;
-                                       break;
-                               }
-                       }
-               }
-               $now = new DateTime;
-               $card->setString('REV', $now->format(DateTime::W3C));
-               $data = $card->serialize();
-
-               $stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
-               $result = $stmt->execute(array($fn,$data,time(),$oldcard['id']));
-
-               OC_Contacts_Addressbook::touch($oldcard['addressbookid']);
-
-               return true;
+               return self::edit($oldcard['id'], $card);
        }
 
        /**
@@ -351,14 +318,6 @@ class OC_Contacts_VCard{
                return true;
        }
 
-       /**
-        * @brief Creates a UID
-        * @return string
-        */
-       public static function createUID(){
-               return substr(md5(rand().time()),0,10);
-       }
-
        /**
         * @brief deletes a card with the data provided by sabredav
         * @param integer $aid Addressbook id