Make use of a drop-down box to select the context instead of a text box
authorBen Klang <ben@alkaloid.net>
Tue, 1 Jul 2008 22:25:00 +0000 (22:25 +0000)
committerBen Klang <ben@alkaloid.net>
Sun, 10 Jan 2010 04:05:27 +0000 (23:05 -0500)
lib/Driver/asterisksql.php
lib/Form/SearchCDR.php
lib/Operator.php
search.php
viewgraph.php

index 60cb27f..799b70b 100644 (file)
@@ -20,7 +20,7 @@
  * The table structure can be created by the scripts/sql/operator_foo.sql
  * script.
  *
- * $Horde: incubator/operator/lib/Driver/asterisksql.php,v 1.6 2008/07/01 20:29:11 bklang Exp $
+ * $Horde: incubator/operator/lib/Driver/asterisksql.php,v 1.7 2008/07/01 22:25:01 bklang Exp $
  *
  * Copyright 2007-2008 The Horde Project (http://www.horde.org/)
  *
@@ -155,7 +155,7 @@ class Operator_Driver_asterisksql extends Operator_Driver {
      *                              method will additionall return PEAR_Error
      *                              on failure.
      */
-    function getCallStatsByMonth($start, $end, $accountcode = null,
+    function getMonthlyCallStats($start, $end, $accountcode = null,
                                  $dcontext = null)
     {
         if (!is_a($start, 'Horde_Date') || !is_a($end, 'Horde_Date')) {
@@ -294,6 +294,23 @@ class Operator_Driver_asterisksql extends Operator_Driver {
         return $stats;
     }
 
+    function getAccountCodes()
+    {
+        /* Make sure we have a valid database connection. */
+        $this->_connect();
+
+        $sql = 'SELECT DISTINCT(accountcode) FROM ' . $this->_params['table'] .
+               ' ORDER BY accountcode';
+        Horde::logMessage(sprintf('Operator_Driver_asterisksql::getAccountCodes(): %s', $sql), __FILE__, __LINE__, PEAR_LOG_DEBUG);
+        $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."));
+        }
+
+        return $res;
+    }
+
     /**
      * Attempts to open a connection to the SQL server.
      *
index fb76826..9e7153d 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * SearchCDRForm Class
  *
- * $Horde: incubator/operator/lib/Form/SearchCDR.php,v 1.2 2008/06/27 17:17:11 bklang Exp $
+ * $Horde: incubator/operator/lib/Form/SearchCDR.php,v 1.3 2008/07/01 22:25:01 bklang Exp $
  *
  * Copyright 2008 Alkaloid Networks LLC <http://projects.alkaloid.net>
  *
@@ -61,7 +61,8 @@ class SearchCDRForm extends Horde_Form {
         $params = array($start_year, $end_year, $picker, $format_in,
                         $format_out, $show_seconds);
 
-        $this->addVariable(_("Account Code"), 'accountcode', 'text', false, false, _("An empty account code will select records with an empty account code.  To search for all records account codes use %"));
+        $this->addVariable(_("Account Code"), 'accountcode', 'enum', false, false, null, array(Operator::getAccountCodes()));
+        //$this->addVariable(_("Account Code"), 'accountcode', 'text', false, false, _("An empty account code will select records with an empty account code.  To search for all records account codes use %"));
         $this->addVariable(_("Destination Context"), 'dcontext', 'text', false, false, _("An empty destination context will select records with an empty destination context.  To search for all destination contexts use %"));
         $this->addVariable(_("Start Date/Time"), 'startdate', 'datetime', true, false, null, $params);
         $this->addVariable(_("End Date/Time"), 'enddate', 'datetime', true, false, null, $params);
index eb493b2..b73b2e2 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Operator Base Class.
  *
- * $Horde: incubator/operator/lib/Operator.php,v 1.2 2008/07/01 20:29:29 bklang Exp $
+ * $Horde: incubator/operator/lib/Operator.php,v 1.3 2008/07/01 22:25:00 bklang Exp $
  *
  * Copyright 2008 Alkaloid Networks LLC <http://projects.alkaloid.net>
  *
@@ -82,4 +82,46 @@ class Operator {
             break;
         }
     }
+
+    /**
+     * Get a list of valid account codes from the database
+     *
+     * @return array  List of valid account codes.
+     */
+    function getAccountCodes()
+    {
+        global $cache, $operator_driver;
+
+        // Use 0 lifetime to allow cache lifetime to be set when storing the
+        // object.
+        $accountcodes = $cache->get('operator-accountcodes', 0);
+        if ($accountcodes === false) {
+            $accountcodes = $operator_driver->getAccountCodes();
+
+            // Add an option to select all accounts
+            $keys = $accountcodes;
+            array_unshift($keys, '%');
+            $values = $accountcodes;
+            array_unshift($values, _("-- All Accounts --"));
+
+            if ($index = array_search('', $values)) {
+               $values[$index] = _("-- Empty Accountcode --");
+            }
+
+            // Make the index of each array entry the same as its value
+            $accountcodes = array_combine($keys, $values);
+
+            $res = $cache->set('operator-accountcodes',
+                               serialize($accountcodes), 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."));
+                $accountcodes = array();
+            }
+        } else {
+            $accountcodes = unserialize($accountcodes);
+        }
+
+        return $accountcodes;
+    }
 }
index 66d5609..e14966f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * $Horde: incubator/operator/search.php,v 1.2 2008/06/26 17:31:27 bklang Exp $
+ * $Horde: incubator/operator/search.php,v 1.3 2008/07/01 22:25:00 bklang Exp $
  *
  * Copyright 2008 Alkaloid Networks LLC <http://projects.alkaloid.net>
  *
@@ -24,17 +24,14 @@ $vars = Variables::getDefaultVariables();
 
 $form = new SearchCDRForm($vars);
 if ($form->isSubmitted() && $form->validate($vars, true)) {
-    if ($vars->exists('accountcode')) {
-        $accountcode = $vars->get('accountcode');
-    } else {
-        // Search all accounts.
-        $accountcode = null;
-    }
+    $accountcode = $vars->get('accountcode');
+    $dcontext = $vars->get('dcontext');
     $start = new Horde_Date($vars->get('startdate'));
     $end = new Horde_Date($vars->get('enddate'));
-    $data = $operator_driver->getData($start, $end, $accountcode);
+    $data = $operator_driver->getData($start, $end, $accountcode, $dcontext);
     $_SESSION['operator']['lastsearch']['params'] = array(
         'accountcode' => $vars->get('accountcode'),
+        'dcontext' => $vars->get('dcontext'),
         'startdate' => $vars->get('startdate'),
         'enddate' => $vars->get('enddate'));
     $_SESSION['operator']['lastsearch']['data'] = $data;
index 007720a..1a4bea0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * $Horde: incubator/operator/viewgraph.php,v 1.3 2008/06/27 17:17:10 bklang Exp $
+ * $Horde: incubator/operator/viewgraph.php,v 1.4 2008/07/01 22:25:00 bklang Exp $
  *
  * Copyright 2008 Alkaloid Networks LLC <http://projects.alkaloid.net>
  *
@@ -24,28 +24,18 @@ $vars = Variables::getDefaultVariables();
 
 $form = new SearchCDRForm($vars);
 if ($form->isSubmitted() && $form->validate($vars, true)) {
-    if ($vars->exists('accountcode')) {
-        $accountcode = $vars->get('accountcode');
-    } else {
-        // Search all accounts.
-        $accountcode = null;
-    }
-    if ($vars->exists('dcontext')) {
-        $dcontext = $vars->get('dcontext');
-    } else {
-        // Search all contexts.
-        $dcontext = null;
-    }
+    $accountcode = $vars->get('accountcode');
+    $dcontext = $vars->get('dcontext');
     $start = new Horde_Date($vars->get('startdate'));
     $end = new Horde_Date($vars->get('enddate'));
 
     // See if we have cached data
-    $cachekey = md5(serialize(array('getCallStatsByMonth', $start, $end,
+    $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->getCallStatsByMonth($start, $end,
+        $stats = $operator_driver->getMonthlyCallStats($start, $end,
                                                        $accountcode, $dcontext);
         $res = $cache->set($cachekey, serialize($stats), 600);
         if ($res === false) {
@@ -59,6 +49,7 @@ if ($form->isSubmitted() && $form->validate($vars, true)) {
     }
     $_SESSION['operator']['lastsearch']['params'] = array(
         'accountcode' => $vars->get('accountcode'),
+        'dcontext' => $vars->get('dcontext'),
         'startdate' => $vars->get('startdate'),
         'enddate' => $vars->get('enddate'));
 } else {