Contacts: change card parameter type of VCard::edit and VCard::add

changed to OC_VObject
This commit is contained in:
Bart Visscher 2012-03-07 21:27:03 +01:00
parent ab760578f8
commit 9599548203
10 changed files with 51 additions and 88 deletions

View 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);

View 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);

View 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();

View 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();

View 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);

View 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();

View 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();

View 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();
OC_JSON::success();

View 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;;;');
}

View 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