From e5840e1fe27bbeec8ce4894c70c50d5c9b787982 Mon Sep 17 00:00:00 2001
From: Thomas Tanghus <thomas@tanghus.net>
Date: Fri, 11 May 2012 01:13:19 +0200
Subject: [PATCH] Contacts: Hack to add structured name from formatted name.

---
 apps/contacts/js/contacts.js | 20 +++++++++++++++++---
 apps/contacts/lib/vcard.php  | 19 ++++++++++---------
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js
index 2c9cb68b00..b37383a12f 100644
--- a/apps/contacts/js/contacts.js
+++ b/apps/contacts/js/contacts.js
@@ -443,7 +443,6 @@ Contacts={
 					}
 				});
 				this.fn = ''; this.fullname = ''; this.givname = ''; this.famname = ''; this.addname = ''; this.honpre = ''; this.honsuf = '';
-				var full = '';
 				var narray = undefined;
 				if(this.data.FN) {
 					this.fn = this.data.FN[0]['value'];
@@ -453,7 +452,6 @@ Contacts={
 				}
 				if(this.data.N == undefined) {
 					narray = [this.fn,'','','','']; // Checking for non-existing 'N' property :-P
-					full = this.fn;
 				} else {
 					narray = this.data.N[0]['value'];
 				}
@@ -479,7 +477,6 @@ Contacts={
 				}
 				$('#n').html(this.fullname);
 				$('#fn_select option').remove();
-				$('#fn_select').combobox('value', this.fn);
 				var names = [this.fullname, this.givname + ' ' + this.famname, this.famname + ' ' + this.givname, this.famname + ', ' + this.givname];
 				if(this.data.ORG) {
 					names[names.length]=this.data.ORG[0].value;
@@ -489,6 +486,7 @@ Contacts={
 						.append($('<option></option>')
 						.text(value)); 
 				});
+				$('#fn_select').combobox('value', this.fn);
 				$('#contact_identity').find('*[data-element="N"]').data('checksum', this.data.N[0]['checksum']);
 				if(this.data.FN) {
 					$('#contact_identity').find('*[data-element="FN"]').data('checksum', this.data.FN[0]['checksum']);
@@ -564,6 +562,22 @@ Contacts={
 				var checksum = container.data('checksum');
 				var name = container.data('element');
 				var fields = container.find('input.contacts_property,select.contacts_property').serializeArray();
+				switch(name) {
+					case 'FN':
+						var nempty = true;
+						for(var i in Contacts.UI.Card.data.N[0]['value']) {
+							if(Contacts.UI.Card.data.N[0]['value'][i] != '') {
+								nempty = false;
+								break;
+							}
+						}
+						if(nempty) {
+							$('#n').val(fields[0].value + ';;;;');
+							Contacts.UI.Card.data.N[0]['value'] = Array(fields[0].value, '', '', '', '');
+							setTimeout(function() {Contacts.UI.Card.saveProperty($('#n'))}, 500);
+						}
+						break;
+				}
 				var q = container.find('input.contacts_property,select.contacts_property,textarea.contacts_property').serialize();
 				if(q == '' || q == undefined) {
 					OC.dialogs.alert(t('contacts', 'Couldn\'t serialize elements.'), t('contacts', 'Error'));
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index c2ab0e07e9..91ae3a7514 100755
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -241,15 +241,6 @@ class OC_Contacts_VCard{
 		if(self::trueUID($aid, $uid)) {
 			$vcard->setString('UID', $uid);
 		}
-		$vcard->setString('VERSION','3.0');
-		// Add product ID is missing.
-		$prodid = trim($vcard->getAsString('PRODID'));
-		if(!$prodid) {
-			$appinfo = OCP\App::getAppInfo('contacts');
-			$appversion = OCP\App::getAppVersion('contacts');
-			$prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN';
-			$vcard->setString('PRODID', $prodid);
-		}
 		$now = new DateTime;
 		$vcard->setString('REV', $now->format(DateTime::W3C));
 	}
@@ -272,6 +263,16 @@ class OC_Contacts_VCard{
 			self::updateValuesFromAdd($aid, $card);
 		}
 
+		$card->setString('VERSION','3.0');
+		// Add product ID is missing.
+		$prodid = trim($card->getAsString('PRODID'));
+		if(!$prodid) {
+			$appinfo = OCP\App::getAppInfo('contacts');
+			$appversion = OCP\App::getAppVersion('contacts');
+			$prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN';
+			$card->setString('PRODID', $prodid);
+		}
+
 		$fn = $card->getAsString('FN');
 		if (empty($fn)) {
 			$fn = '';
-- 
GitLab