Operator: Change from PEAR_Error to Exception handling
authorBen Klang <ben@alkaloid.net>
Sun, 10 Jan 2010 21:43:31 +0000 (16:43 -0500)
committerBen Klang <ben@alkaloid.net>
Sun, 10 Jan 2010 21:43:31 +0000 (16:43 -0500)
operator/graphgen.php
operator/lib/Driver.php
operator/lib/Driver/asterisksql.php
operator/search.php
operator/viewgraph.php

index 9004bd5..fecd8a0 100644 (file)
@@ -14,8 +14,9 @@ $operator = new Operator_Application(array('init' => true));
 $cache = &$GLOBALS['cache'];
 
 // Work around warnings in Image_Graph
-error_reporting(E_NONE);
-ini_set("display_errors", 0);
+// Needed for Image_Graph <= 0.7.2 and Image_Canvas <= 0.3.2
+//error_reporting(E_NONE);
+//ini_set("display_errors", 0);
 
 #setlocale(LC_ALL, Horde_Nls::select());
 #setlocale(LC_ALL, 'en_US');
@@ -58,7 +59,7 @@ if (isset($graphinfo['orientation']) &&
 if (!empty($conf['ttf_font'])) {
     // add a TrueType font
     $Font =& $graph->addNew('ttf_font', $conf['ttf_font']);
-    // set the font size to 11 pixels
+    // Set the font size to 11 pixels.  Yes, 8 really does mean 11
     $Font->setSize(8);
     $graph->setFont($Font);
 }
@@ -180,6 +181,7 @@ exit;
 
 function _format($number)
 {
+    // Only show the decimal if the value has digits after the decimal
     if (($number - (int)$number) == 0) {
         return money_format('%!.0n', $number);
     } else {
@@ -192,6 +194,7 @@ function number2human($number, $showCurrency = true)
     $currency = '';
     $suffix = '';
     if ($showCurrency) {
+        // FIXME: Make currency configurable
         //$currency = 'ISK ';
         $currency = '';
     }
index 59b6d52..af819a3 100644 (file)
@@ -19,7 +19,8 @@ class Operator_Driver {
      * Search the database for call detail records, taking permissions into
      * consideration.
      *
-     * @return boolean|PEAR_Error  True on success, PEAR_Error on failure.
+     * @return boolean  True on success
+     * @throws Operator_Exception
      */
     function getRecords($start, $end, $accountcode = null, $dcontext = null,
                          $rowstart = 0, $rowlimit = 100)
@@ -38,7 +39,7 @@ class Operator_Driver {
             return $this->_getRecords($start, $end, $accountcode, $dcontext,
                                       $rowstart, $rowlimit);
         }
-        return PEAR::raiseError(_("You do not have permission to view call detail records for that account code."));
+        throw new Operator_Exception(_("You do not have permission to view call detail records for that account code."));
     }
 
     /**
@@ -52,12 +53,11 @@ class Operator_Driver {
      * @param string dcontext       Destination of calls.  Defaults to null.
      *
      *
-     * @return array|PEAR_Error     Array of call statistics.  The key of each
+     * @return array                Array of call statistics.  The key of each
      *                              element is the month name in date('Y-m')
      *                              format and the value being an array of
-     *                              statistics for calls placed that month. This
-     *                              method will additionall return PEAR_Error
-     *                              on failure.
+     *                              statistics for calls placed that month.
+     * @throws Operator_Exception|Horde_Date_Exception
      */
     function getMonthlyCallStats($start, $end, $accountcode = null,
                                  $dcontext = null){
@@ -76,7 +76,7 @@ class Operator_Driver {
                                                $dcontext);
         }
 
-        return PEAR::raiseError(_("You do not have permission to view call detail records for that account code."));
+        throw new Operator_Exception(_("You do not have permission to view call detail records for that account code."));
     }
 
     /**
index 48181d7..e990ca9 100644 (file)
@@ -74,7 +74,8 @@ class Operator_Driver_asterisksql extends Operator_Driver {
     /**
      * Get call detail records from the database
      *
-     * @return boolean|PEAR_Error  True on success, PEAR_Error on failure.
+     * @return boolean
+     * @throws Operator_Exception|Horde_Date_Exception
      */
     function _getRecords($start, $end, $accountcode = null, $dcontext = null,
                          $rowstart = 0, $rowlimit = 100)
@@ -91,20 +92,17 @@ class Operator_Driver_asterisksql extends Operator_Driver {
 
         if (!is_numeric($rowstart)) {
             Horde::logMessage('Invalid start row requested.', __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
         if (!is_numeric($rowlimit)) {
             Horde::logMessage('Invalid row limit requested.', __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
 
         // Start Date
         if (!is_a($start, 'Horde_Date')) {
             $start = new Horde_Date($start);
-            if (is_a($start, 'PEAR_Error')) {
-                return $start;
-            }
         }
         $filter[] = 'calldate >= ?';
         $values[] = $start->strftime('%Y-%m-%d %T');
@@ -112,9 +110,6 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         // End Date
         if (!is_a($end, 'Horde_Date')) {
             $end = new Horde_Date($end);
-            if (is_a($end, 'PEAR_Error')) {
-                return $end;
-            }
         }
         $filter[] = 'calldate < ?';
         $values[] =  $end->strftime('%Y-%m-%d %T');
@@ -147,7 +142,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $res = $this->_db->limitQuery($sql, $rowstart, $rowlimit, $values);
         if (is_a($res, 'PEAR_Error')) {
             Horde::logMessage($res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
         
         $data = array();
@@ -166,7 +161,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $res = $this->_db->getRow($sql, $values, DB_FETCHMODE_ASSOC);
         if (is_a($res, 'PEAR_Error')) {
             Horde::logMessage($res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         return array_merge($data, $res);
@@ -183,19 +178,18 @@ class Operator_Driver_asterisksql extends Operator_Driver {
      * @param string dcontext       Destination of calls.  Defaults to null.
      *
      *
-     * @return array|PEAR_Error     Array of call statistics.  The key of each
+     * @return array                Array of call statistics.  The key of each
      *                              element is the month name in date('Y-m')
      *                              format and the value being an array of
-     *                              statistics for calls placed that month. This
-     *                              method will additionall return PEAR_Error
-     *                              on failure.
+     *                              statistics for calls placed that month.
+     * @throws Operator_Exception|Horde_Date_Exception
      */
     function _getMonthlyCallStats($start, $end, $accountcode = null,
                                  $dcontext = null)
     {
         if (!is_a($start, 'Horde_Date') || !is_a($end, 'Horde_Date')) {
             Horde::logMessage('Start ane end date must be Horde_Date objects.', __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         /* Make sure we have a valid database connection. */
@@ -259,7 +253,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $numcalls_res = $this->_db->getAll($sql, $values, DB_FETCHMODE_ASSOC);
         if (is_a($numcalls_res, 'PEAR_Error')) {
             Horde::logMessage($numcalls_res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         $sql = sprintf($minutes_query, $filterstring);
@@ -267,7 +261,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $minutes_res = $this->_db->getAll($sql, $values, DB_FETCHMODE_ASSOC);
         if (is_a($minutes_res, 'PEAR_Error')) {
             Horde::logMessage($minutes_res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         $sql = sprintf($failed_query, $filterstring);
@@ -275,7 +269,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $failed_res = $this->_db->getAll($sql, $values, DB_FETCHMODE_ASSOC);
         if (is_a($failed_res, 'PEAR_Error')) {
             Horde::logMessage($failed_res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         // Normalize the results from the database.  This is done because
@@ -349,7 +343,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         $res = $this->_db->getCol($sql, 'accountcode');
         if (is_a($res, 'PEAR_Error')) {
             Horde::logMessage($res, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError(_("Internal error.  Details have been logged for the administrator."));
+            throw new Operator_Exception(_("Internal error.  Details have been logged for the administrator."));
         }
 
         return $res;
index c61ba98..645e836 100644 (file)
@@ -38,25 +38,24 @@ if ($form->isSubmitted() && $form->validate($vars, true)) {
     if (empty($dcontext)) {
         $dcontext = '%';
     }
-    $start = new Horde_Date($vars->get('startdate'));
 
-    $end = new Horde_Date($vars->get('enddate'));
-    if (is_a($start, 'PEAR_Error') || is_a($end, 'PEAR_Error')) {
-        $notification->push(_("Invalid date requested."));
-    } else {
+    try {
+        $start = new Horde_Date($vars->get('startdate'));
+        $end = new Horde_Date($vars->get('enddate'));
         $data = $operator->driver->getRecords($start, $end, $accountcode,
                                               $dcontext, $rowstart,
                                               $GLOBALS['conf']['storage']['searchlimit']);
-        if (is_a($data, 'PEAR_Error')) {
-            $notification->push($data);
-            $data = array();
-        }
+
         $_SESSION['operator']['lastsearch']['params'] = array(
             'accountcode' => $vars->get('accountcode'),
             'dcontext' => $vars->get('dcontext'),
             'startdate' => $vars->get('startdate'),
             'enddate' => $vars->get('enddate'));
         $_SESSION['operator']['lastdata'] = $data;
+    } catch (Exception $e) {
+        //$notification->push(_("Invalid date requested."));
+        $notification->push($e);
+        $data = array();
     }
 } else {
     if (isset($_SESSION['operator']['lastsearch']['params'])) {
index e9e1430..182b569 100644 (file)
@@ -27,46 +27,50 @@ if ($form->isSubmitted() && $form->validate($vars, true)) {
     if (empty($dcontext)) {
         $dcontext = '%';
     }
-    $start = new Horde_Date($vars->get('startdate'));
-    $end = new Horde_Date($vars->get('enddate'));
 
-    if (is_a($start, 'PEAR_Error') || is_a($end, 'PEAR_Error')) {
-        $notification->push(_("Invalid date requested."));
-    } elseif (($end->month - $start->month) == 0 &&
-        ($end->year - $start->year) == 0) {
-        $notification->push(_("You must select a range that includes more than one month to view these graphs."));
-    } else {
-        // See if we have cached data
-        $cachekey = md5(serialize(array('getMonthlyCallStats', $start, $end,
-                                        $accountcode, $dcontext)));
-        // Use 0 lifetime to allow cache lifetime to be set when storing
-        // the object.
-        $stats = $cache->get($cachekey, 0);
-        if ($stats === false) {
-            $stats = $operator->driver->getMonthlyCallStats($start,
-                                                           $end,
-                                                           $accountcode,
-                                                           $dcontext);
-            if (is_a($stats, 'PEAR_Error')) {
-                $notification->push($stats);
-                $stats = array();
-            } else {
+    try {
+        $start = new Horde_Date($vars->get('startdate'));
+        $end = new Horde_Date($vars->get('enddate'));
+    
+        if (($end->month - $start->month) == 0 &&
+            ($end->year - $start->year) == 0) {
+            // FIXME: This should not cause an error but is due to a bug in
+            // Image_Graph.
+            $notification->push(_("You must select a range that includes more than one month to view these graphs."));
+        } else {
+            // See if we have cached data
+            $cachekey = md5(serialize(array('getMonthlyCallStats', $start, $end,
+                                            $accountcode, $dcontext)));
+            // Use 0 lifetime to allow cache lifetime to be set when storing
+            // the object.
+            $stats = $cache->get($cachekey, 0);
+            if ($stats === false) {
+                $stats = $operator->driver->getMonthlyCallStats($start,
+                                                               $end,
+                                                               $accountcode,
+                                                               $dcontext);
+
                 $res = $cache->set($cachekey, serialize($stats), 600);
                 if ($res === false) {
                     Horde::logMessage('The cache system has experienced an error.  Unable to continue.', __FILE__, __LINE__, PEAR_LOG_ERR);
                     $notification->push(_("Internal error.  Details have been logged for the administrator."));
-                    unset($stats);
+                    $stats = array();
                 }
+
+            } else {
+                // Cached data is stored serialized
+                $stats = unserialize($stats);
             }
-        } else {
-            // Cached data is stored serialized
-            $stats = unserialize($stats);
+            $_SESSION['operator']['lastsearch']['params'] = array(
+                'accountcode' => $vars->get('accountcode'),
+                'dcontext' => $vars->get('dcontext'),
+                'startdate' => $vars->get('startdate'),
+                'enddate' => $vars->get('enddate'));
         }
-        $_SESSION['operator']['lastsearch']['params'] = array(
-            'accountcode' => $vars->get('accountcode'),
-            'dcontext' => $vars->get('dcontext'),
-            'startdate' => $vars->get('startdate'),
-            'enddate' => $vars->get('enddate'));
+    } catch (Horde_Exception $e) {
+        //$notification->push(_("Invalid dates requested."));
+        $notification->push($e);
+        $stats = array();
     }
 } else {
     if (isset($_SESSION['operator']['lastsearch']['params'])) {