From 237ba65a20edfbd346405e03583a96808602a2ca Mon Sep 17 00:00:00 2001
From: Thomas Tanghus <thomas@tanghus.net>
Date: Wed, 11 Jan 2012 20:07:15 +0100
Subject: [PATCH] Localizin strings and adding error checking.

---
 apps/contacts/ajax/activation.php             |  9 +++++++-
 apps/contacts/ajax/addcard.php                |  7 ++++++
 apps/contacts/ajax/addproperty.php            |  6 ++++-
 apps/contacts/ajax/createaddressbook.php      | 14 ++++++++++--
 apps/contacts/ajax/deleteproperty.php         |  8 ++++++-
 apps/contacts/ajax/setproperty.php            |  7 +++++-
 apps/contacts/ajax/updateaddressbook.php      | 17 +++++++++++---
 apps/contacts/js/interface.js                 | 22 +++++++++----------
 apps/contacts/l10n/xgettextfiles              | 21 ++++++++++++++++++
 apps/contacts/lib/addressbook.php             |  1 +
 apps/contacts/lib/vcard.php                   |  2 ++
 apps/contacts/templates/index.php             |  2 +-
 apps/contacts/templates/part.addcardform.php  | 10 ++-------
 .../part.chooseaddressbook.rowfields.php      |  2 +-
 .../templates/part.editaddressbook.php        |  2 +-
 apps/contacts/templates/settings.php          |  4 ++--
 16 files changed, 101 insertions(+), 33 deletions(-)
 create mode 100644 apps/contacts/l10n/xgettextfiles

diff --git a/apps/contacts/ajax/activation.php b/apps/contacts/ajax/activation.php
index f4a2c94a14..fda63a528a 100644
--- a/apps/contacts/ajax/activation.php
+++ b/apps/contacts/ajax/activation.php
@@ -10,10 +10,17 @@
 require_once ("../../../lib/base.php");
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
+$l=new OC_L10N('contacts');
+
 $bookid = $_POST['bookid'];
-OC_Contacts_Addressbook::setActive($bookid, $_POST['active']);
+if(!OC_Contacts_Addressbook::setActive($bookid, $_POST['active'])) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error (de)activating addressbook.'))));
+	OC_Log::write('contacts','ajax/activation.php: Error activating addressbook: '.$bookid, OC_Log::ERROR);
+	exit();
+}
 $book = OC_Contacts_App::getAddressbook($bookid);
 
+
 /* is there an OC_JSON::error() ? */
 OC_JSON::success(array(
 	'active' => OC_Contacts_Addressbook::isActive($bookid),
diff --git a/apps/contacts/ajax/addcard.php b/apps/contacts/ajax/addcard.php
index 9d9a99de33..7e47659d23 100644
--- a/apps/contacts/ajax/addcard.php
+++ b/apps/contacts/ajax/addcard.php
@@ -26,6 +26,7 @@ require_once('../../../lib/base.php');
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
+$l=new OC_L10N('contacts');
 
 $aid = $_POST['id'];
 $addressbook = OC_Contacts_App::getAddressbook( $aid );
@@ -74,5 +75,11 @@ foreach( $add as $propname){
 	}
 }
 $id = OC_Contacts_VCard::add($aid,$vcard->serialize());
+if(!$id) {
+	OC_JSON::error(array('data' => array('message' => $l->t('There was an error adding the contact.'))));
+	OC_Log::write('contacts','ajax/addcard.php: Recieved non-positive ID on adding card: '.$name, OC_Log::ERROR);
+	exit();
+}
 
+// NOTE: Why is this in OC_Contacts_App?
 OC_Contacts_App::renderDetails($id, $vcard);
diff --git a/apps/contacts/ajax/addproperty.php b/apps/contacts/ajax/addproperty.php
index 74f1c3d0e9..6e3ba3566c 100644
--- a/apps/contacts/ajax/addproperty.php
+++ b/apps/contacts/ajax/addproperty.php
@@ -61,7 +61,11 @@ foreach ($parameters as $key=>$element) {
 	}
 }
 
-OC_Contacts_VCard::edit($id,$vcard->serialize());
+if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error adding contact property.'))));
+	OC_Log::write('contacts','ajax/addproperty.php: Error updating contact property: '.$name, OC_Log::ERROR);
+	exit();
+}
 
 $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
 $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
diff --git a/apps/contacts/ajax/createaddressbook.php b/apps/contacts/ajax/createaddressbook.php
index f94ad34e8d..edcf794f49 100644
--- a/apps/contacts/ajax/createaddressbook.php
+++ b/apps/contacts/ajax/createaddressbook.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
+ * Copyright (c) 2011-2012 Thomas Tanghus <thomas@tanghus.net>
  * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
  * This file is licensed under the Affero General Public License version 3 or
  * later.
@@ -16,7 +16,17 @@ OC_JSON::checkAppEnabled('contacts');
 
 $userid = OC_User::getUser();
 $bookid = OC_Contacts_Addressbook::add($userid, $_POST['name'], null);
-OC_Contacts_Addressbook::setActive($bookid, 1);
+if(!$bookid) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error adding addressbook.'))));
+	OC_Log::write('contacts','ajax/createaddressbook.php: Error adding addressbook: '.$_POST['name'], OC_Log::ERROR);
+	exit();
+}
+
+if(!OC_Contacts_Addressbook::setActive($bookid, 1)) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error activating addressbook.'))));
+	OC_Log::write('contacts','ajax/createaddressbook.php: Error activating addressbook: '.$bookid, OC_Log::ERROR);
+	//exit();
+}
 $addressbook = OC_Contacts_App::getAddressbook($bookid);
 $tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
 $tmpl->assign('addressbook', $addressbook);
diff --git a/apps/contacts/ajax/deleteproperty.php b/apps/contacts/ajax/deleteproperty.php
index f69735e61c..89cf292f4f 100644
--- a/apps/contacts/ajax/deleteproperty.php
+++ b/apps/contacts/ajax/deleteproperty.php
@@ -26,6 +26,7 @@ require_once('../../../lib/base.php');
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
+$l10n = new OC_L10N('contacts');
 
 $id = $_GET['id'];
 $checksum = $_GET['checksum'];
@@ -35,5 +36,10 @@ $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
 
 unset($vcard->children[$line]);
 
-OC_Contacts_VCard::edit($id,$vcard->serialize());
+if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error deleting contact property.'))));
+	OC_Log::write('contacts','ajax/deleteproperty.php: Error deleting contact property', OC_Log::ERROR);
+	exit();
+}
+
 OC_JSON::success(array('data' => array( 'id' => $id )));
diff --git a/apps/contacts/ajax/setproperty.php b/apps/contacts/ajax/setproperty.php
index bcc4c161cc..e0cd70236c 100644
--- a/apps/contacts/ajax/setproperty.php
+++ b/apps/contacts/ajax/setproperty.php
@@ -72,9 +72,14 @@ foreach($missingparameters as $i){
 }
 
 // Do checksum and be happy
+// NOTE: This checksum is not used..?
 $checksum = md5($vcard->children[$line]->serialize());
 
-OC_Contacts_VCard::edit($id,$vcard->serialize());
+if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
+	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();
+}
 
 $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
 $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
diff --git a/apps/contacts/ajax/updateaddressbook.php b/apps/contacts/ajax/updateaddressbook.php
index 516736cc50..7d9e2aea91 100644
--- a/apps/contacts/ajax/updateaddressbook.php
+++ b/apps/contacts/ajax/updateaddressbook.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * Copyright (c) 2011-2012 Thomas Tanghus <thomas@tanghus.net>
  * This file is licensed under the Affero General Public License version 3 or
  * later.
  * See the COPYING-README file.
@@ -15,8 +15,19 @@ OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
 $bookid = $_POST['id'];
-OC_Contacts_Addressbook::edit($bookid, $_POST['name'], null);
-OC_Contacts_Addressbook::setActive($bookid, $_POST['active']);
+
+if(!OC_Contacts_Addressbook::edit($bookid, $_POST['name'], null)) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error updating addressbook.'))));
+	OC_Log::write('contacts','ajax/updateaddressbook.php: Error adding addressbook: ', OC_Log::ERROR);
+	//exit();
+}
+
+if(!OC_Contacts_Addressbook::setActive($bookid, $_POST['active'])) {
+	OC_JSON::error(array('data' => array('message' => $l->t('Error (de)activating addressbook.'))));
+	OC_Log::write('contacts','ajax/updateaddressbook.php: Error (de)activating addressbook: '.$bookid, OC_Log::ERROR);
+	//exit();
+}
+
 $addressbook = OC_Contacts_App::getAddressbook($bookid);
 $tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
 $tmpl->assign('addressbook', $addressbook);
diff --git a/apps/contacts/js/interface.js b/apps/contacts/js/interface.js
index 741483633d..8a1de3051a 100644
--- a/apps/contacts/js/interface.js
+++ b/apps/contacts/js/interface.js
@@ -106,7 +106,7 @@ Contacts={
 							Contacts.UI.Contacts.update();
 							Contacts.UI.Addressbooks.overview();
 						} else {
-							Contacts.UI.messageBox('Error', data.message);
+							Contacts.UI.messageBox(t('contacts', 'Error'), data.message);
 							//alert('Error: ' + data.message);
 						}
 					  });
@@ -145,7 +145,7 @@ Contacts={
 						$('#contacts').html(jsondata.data.page);
 					}
 					else{
-						Contacts.UI.messageBox('Error',jsondata.data.message);
+						Contacts.UI.messageBox(t('contacts', 'Error'),jsondata.data.message);
 						//alert(jsondata.data.message);
 					}
 				});
@@ -186,7 +186,7 @@ $(document).ready(function(){
 				$('#leftcontent li[data-id="'+jsondata.data.id+'"]').addClass('active');
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		});
@@ -206,7 +206,7 @@ $(document).ready(function(){
 				$('#rightcontent').empty();
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		});
@@ -225,7 +225,7 @@ $(document).ready(function(){
 				$('#contacts_addproperty').hide();
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				alert('From handler: '+jsondata.data.message);
 			}
 		});
@@ -258,7 +258,7 @@ $(document).ready(function(){
 				$('#contacts_addpropertyform').before(jsondata.data.page);
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 			}
 		}, 'json');
 		return false;
@@ -283,7 +283,7 @@ $(document).ready(function(){
 					.find('select').chosen();
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		});
@@ -313,7 +313,7 @@ $(document).ready(function(){
 				}
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		}, 'json');
@@ -332,7 +332,7 @@ $(document).ready(function(){
 					.find('select').chosen();
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		});
@@ -348,7 +348,7 @@ $(document).ready(function(){
 				$('.contacts_property[data-checksum="'+jsondata.data.oldchecksum+'"]').replaceWith(jsondata.data.page);
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		},'json');
@@ -363,7 +363,7 @@ $(document).ready(function(){
 				$('.contacts_property[data-checksum="'+checksum+'"]').remove();
 			}
 			else{
-				Contacts.UI.messageBox('Error', jsondata.data.message);
+				Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
 				//alert(jsondata.data.message);
 			}
 		});
diff --git a/apps/contacts/l10n/xgettextfiles b/apps/contacts/l10n/xgettextfiles
new file mode 100644
index 0000000000..91d5da46db
--- /dev/null
+++ b/apps/contacts/l10n/xgettextfiles
@@ -0,0 +1,21 @@
+../appinfo/app.php
+../ajax/activation.php
+../ajax/addbook.php
+../ajax/addcard.php
+../ajax/addproperty.php
+../ajax/createaddressbook.php
+../ajax/deletebook.php
+../ajax/deleteproperty.php
+../ajax/getdetails.php
+../ajax/setproperty.php
+../ajax/updateaddressbook.php
+../lib/app.php
+../templates/index.php
+../templates/part.addcardform.php
+../templates/part.chooseaddressbook.php
+../templates/part.chooseaddressbook.rowfields.php
+../templates/part.details.php
+../templates/part.editaddressbook.php
+../templates/part.property.php
+../templates/part.setpropertyform.php
+../templates/settings.php
diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php
index 41d488c09f..052c19e55f 100644
--- a/apps/contacts/lib/addressbook.php
+++ b/apps/contacts/lib/addressbook.php
@@ -256,6 +256,7 @@ class OC_Contacts_Addressbook{
 	 * @return boolean
 	 */
 	public static function delete($id){
+		// FIXME: There's no error checking at all.
 		self::setActive($id, false);
 		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
 		$stmt->execute(array($id));
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index 726927013c..c61f0dfc11 100644
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -240,6 +240,7 @@ class OC_Contacts_VCard{
 	 * @return boolean
 	 */
 	public static function delete($id){
+		// FIXME: Add error checking.
 		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE id = ?' );
 		$stmt->execute(array($id));
 
@@ -261,6 +262,7 @@ class OC_Contacts_VCard{
 	 * @return boolean
 	 */
 	public static function deleteFromDAVData($aid,$uri){
+		// FIXME: Add error checking. Deleting a card gives an Kontact/Akonadi error.
 		$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri=?' );
 		$stmt->execute(array($aid,$uri));
 
diff --git a/apps/contacts/templates/index.php b/apps/contacts/templates/index.php
index d548f17172..5d9c312712 100644
--- a/apps/contacts/templates/index.php
+++ b/apps/contacts/templates/index.php
@@ -4,7 +4,7 @@
 <div id="controls">
 	<form>
 		<input type="button" id="contacts_newcontact" value="<?php echo $l->t('Add Contact'); ?>">
-		<input type="button" id="chooseaddressbook" value="<?php echo $l->t('Address Books'); ?>">
+		<input type="button" id="chooseaddressbook" value="<?php echo $l->t('Addressbooks'); ?>">
 	</form>
 </div>
 <div id="leftcontent" class="leftcontent">
diff --git a/apps/contacts/templates/part.addcardform.php b/apps/contacts/templates/part.addcardform.php
index 510096a9e8..53b32188dd 100644
--- a/apps/contacts/templates/part.addcardform.php
+++ b/apps/contacts/templates/part.addcardform.php
@@ -5,7 +5,7 @@
 		<fieldset class="inputs">
 			<dl class="form">
 				<dt>
-					<label class="label" for="id"><?php echo $l->t('Group'); ?></label>
+					<label class="label" for="id"><?php echo $l->t('Addressbook'); ?></label>
 				</dt>
 				<dd>
 					<select name="id" size="1">
@@ -106,11 +106,5 @@
 			</dd>
 		</dl>
 	</fieldset>
-	<fieldset class="buttons">
-		<ol>
-			<li class="commit button">
-				<input class="create" type="submit" name="submit" value="<?php echo $l->t('Create Contact'); ?>">
-			</li>
-		</ol>
-	</fieldset>
+	<input class="create" type="submit" name="submit" value="<?php echo $l->t('Create Contact'); ?>">
 </form>
diff --git a/apps/contacts/templates/part.chooseaddressbook.rowfields.php b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
index f612e39eca..0cbfe2bf80 100644
--- a/apps/contacts/templates/part.chooseaddressbook.rowfields.php
+++ b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
@@ -1,5 +1,5 @@
 <?php
-
+	// FIXME: Make this readable.
 	echo "<td width=\"20px\"><input id=\"active_" . $_['addressbook']["id"] . "\" type=\"checkbox\" onClick=\"Contacts.UI.Addressbooks.activation(this, " . $_['addressbook']["id"] . ")\"" . (OC_Contacts_Addressbook::isActive($_['addressbook']["id"]) ? ' checked="checked"' : '') . "></td>";
 	echo "<td><label for=\"active_" . $_['addressbook']["id"] . "\">" . $_['addressbook']["displayname"] . "</label></td>";
 	echo "<td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.showCardDAVUrl('" . OC_User::getUser() . "', '" . $_['addressbook']["uri"] . "');\" title=\"" . $l->t("CardDav Link") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/public.svg\"></a></td><td width=\"20px\"><a href=\"export.php?bookid=" . $_['addressbook']["id"] . "\" title=\"" . $l->t("Download") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/download.svg\"></a></td><td width=\"20px\"><a  href=\"#\" title=\"" . $l->t("Edit") . "\" class=\"action\" onclick=\"Contacts.UI.Addressbooks.editAddressbook(this, " . $_['addressbook']["id"] . ");\"><img class=\"svg action\" src=\"../../core/img/actions/rename.svg\"></a></td><td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.Addressbooks.deleteAddressbook('" . $_['addressbook']["id"] . "');\" title=\"" . $l->t("Delete") . "\" class=\"action\"><img  class=\"svg action\" src=\"../../core/img/actions/delete.svg\"></a></td>";
diff --git a/apps/contacts/templates/part.editaddressbook.php b/apps/contacts/templates/part.editaddressbook.php
index cb1371731b..48fe5c3b37 100644
--- a/apps/contacts/templates/part.editaddressbook.php
+++ b/apps/contacts/templates/part.editaddressbook.php
@@ -6,7 +6,7 @@
  * See the COPYING-README file.
  */
 ?>
-<td id="<?php echo $_['new'] ? 'new' : 'edit' ?>addressbook_dialog" title="<?php echo $_['new'] ? $l->t("New Address Book") : $l->t("Edit Address Book"); ?>" colspan="6">
+<td id="<?php echo $_['new'] ? 'new' : 'edit' ?>addressbook_dialog" title="<?php echo $_['new'] ? $l->t("New Addressbook") : $l->t("Edit Addressbook"); ?>" colspan="6">
 <table width="100%" style="border: 0;">
 <tr>
 	<th><?php echo $l->t('Displayname') ?></th>
diff --git a/apps/contacts/templates/settings.php b/apps/contacts/templates/settings.php
index d913062520..c647e44c25 100644
--- a/apps/contacts/templates/settings.php
+++ b/apps/contacts/templates/settings.php
@@ -1,7 +1,7 @@
 <form id="mediaform">
 	<fieldset class="personalblock">
-		<strong>Contacts</strong><br />
-		CardDAV syncing address:
+		<strong><?php echo $l->t('Contacts'); ?></strong><br />
+		<?php echo $l->t('CardDAV syncing address:'); ?>
   		<?php echo OC_Helper::linkTo('apps/contacts', 'carddav.php', null, true); ?><br />
 	</fieldset>
 </form>
-- 
GitLab