From 004361274352b7000a81eb8c2a722727552ce52c Mon Sep 17 00:00:00 2001
From: Nigel Kukard <nkukard@lbsd.net>
Date: Wed, 12 Sep 2012 21:42:21 +0000
Subject: [PATCH] Added much better error handling for widget

---
 modules/widgets/cozaepp_balance.php | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/modules/widgets/cozaepp_balance.php b/modules/widgets/cozaepp_balance.php
index 844fd8d..e64c4c9 100644
--- a/modules/widgets/cozaepp_balance.php
+++ b/modules/widgets/cozaepp_balance.php
@@ -36,27 +36,32 @@
 # http://allworldit.com
 
 
-## VERSION: 0.1.0 ##
-
 
 # Function to implement the cozaepp balance widget
 function widget_cozaepp_balance($vars) {
 	# Setup include dir
 	$include_path = ROOTDIR . '/modules/registrars/cozaepp';
-	set_include_path(get_include_path() . PATH_SEPARATOR . $include_path);
-
+	set_include_path($include_path . PATH_SEPARATOR . get_include_path());
+	# Include EPP stuff we need
 	require_once 'cozaepp.php';
 	# Include registrar functions aswell
-	require ROOTDIR . '/includes/registrarfunctions.php';
+	require_once ROOTDIR . '/includes/registrarfunctions.php';
+	# We need pear for the error handling
+	require_once "PEAR.php";
 
 
 	# Grab module parameters
 	$params = getregistrarconfigoptions('cozaepp');
 
-	$title = "COZA Balance";
+	# Set widget contents
+	$title = "COZA EPP Balance";
+	$template = '<p align = "center" class="textblack"><strong>%s</strong></p>';
 
 	# Request balance from registrar
-	$client = _cozaepp_Client();
+	if (PEAR::isError($client = _cozaepp_Client())) {
+		return array('title'=>$title,'content'=>sprintf($template,"ERROR: ".$client->getMessage()));
+	}
+
 	$output = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:contact="urn:ietf:params:xml:ns:contact-1.0" 
 		xmlns:cozacontact="http://co.za/epp/extensions/cozacontact-1-0">
@@ -80,14 +85,12 @@ function widget_cozaepp_balance($vars) {
 	$doc->loadXML($output);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
 	if ($coderes == '1000') {
-		$balance = $doc->getElementsByTagName('balance')->item(0)->nodeValue;
+		$balancestr = "Current registrar balance is R ".$doc->getElementsByTagName('balance')->item(0)->nodeValue;
 	} else {
-		$balance = ' - ERROR -';
+		$balancestr = 'ERROR: Parsing';
 	}
 
-	$content = '<p align = "center" class="textblack"><strong>Current registrar balance is R '.$balance.'</strong></p>';
-
-	return array('title'=>$title,'content'=>$content);
+	return array('title'=>$title,'content'=>sprintf($template,$balancestr));
 }
 
 add_hook("AdminHomeWidgets",1,"widget_cozaepp_balance");
-- 
GitLab