Shout: begin work on admin screen to manage numbers
authorBen Klang <ben@alkaloid.net>
Fri, 2 Apr 2010 23:34:02 +0000 (19:34 -0400)
committerBen Klang <ben@alkaloid.net>
Fri, 2 Apr 2010 23:34:02 +0000 (19:34 -0400)
shout/admin.php
shout/admin/accounts.php [new file with mode: 0644]
shout/admin/numbers.php [new file with mode: 0644]
shout/lib/Driver/Sql.php
shout/lib/Forms/NumberForm.php [new file with mode: 0644]
shout/lib/Shout.php
shout/templates/admin/numbers/edit.inc.php [new file with mode: 0644]
shout/templates/admin/numbers/list.inc.php [new file with mode: 0644]

index 023f36e..9e59bde 100644 (file)
@@ -14,89 +14,13 @@ $shout = Horde_Registry::appInit('shout');
 
 require_once SHOUT_BASE . '/lib/Forms/AccountForm.php';
 
-$action = Horde_Util::getFormData('action');
+
 $curaccount = $_SESSION['shout']['curaccount'];
 
 $RENDERER = new Horde_Form_Renderer();
 
 $title = _("Accounts: ");
 
-switch ($action) {
-case 'add':
-case 'edit':
-    $vars = Horde_Variables::getDefaultVariables();
-    $vars->set('account', $curaccount['code']);
-    $Form = new AccountDetailsForm($vars);
-
-    if ($Form->isSubmitted() && $Form->validate($vars, true)) {
-        // Form is Valid and Submitted
-        try {
-            $Form->execute();
-            $notification->push(_("Account information saved."),
-                                  'horde.success');
-            $action = 'list';
-        } catch (Exception $e) {
-            $notification->push($e);
-        }
-        break;
-    } elseif ($Form->isSubmitted()) {
-        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
-    }
-
-    // Create a new add/edit form
-    $account = Horde_Util::getFormData('extension');
-    $accounts = $shout->storage->getAccounts();
-    $vars = new Horde_Variables(array('code' => $account, 'name' => $accounts[$account]['name']));
-    $vars->set('action', $action);
-
-    // Make sure we get the right template below.
-    $action = 'edit';
-    break;
-
-case 'delete':
-    $title .= sprintf(_("Delete Extension %s"), $extension);
-    $extension = Horde_Util::getFormData('extension');
-
-    $vars = Horde_Variables::getDefaultVariables();
-    $vars->set('account', $curaccount['code']);
-    $Form = new ExtensionDeleteForm($vars);
-
-    $FormValid = $Form->validate($vars, true);
-
-    if ($Form->isSubmitted() && $FormValid) {
-        try {
-            $Form->execute();
-            $notification->push(_("Extension Deleted."));
-            $action = 'list';
-        } catch (Exception $e) {
-            $notification->push($e);
-        }
-    } elseif ($Form->isSubmitted()) {
-        // Submitted but not valid
-        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
-    }
-
-    $vars = Horde_Variables::getDefaultVariables(array());
-    $vars->set('account', $curaccount['code']);
-    $Form = new ExtensionDeleteForm($vars);
-
-    break;
-
-case 'list':
-default:
-    $action = 'list';
-    $title .= _("List Accounts");
-}
-
-
-// Fetch the (possibly updated) list of extensions
-try {
-    $accounts = $shout->storage->getAccounts();
-} catch (Exception $e) {
-    $notification->push($e);
-    $extensions = array();
-}
-
 Horde::addScriptFile('stripe.js', 'horde');
 Horde::addScriptFile('prototype.js', 'horde');
 
@@ -105,6 +29,7 @@ require SHOUT_TEMPLATES . '/menu.inc';
 
 $notification->notify();
 
-require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc';
+Shout::getAdminTabs();
 
 require $registry->get('templates', 'horde') . '/common-footer.inc';
+
diff --git a/shout/admin/accounts.php b/shout/admin/accounts.php
new file mode 100644 (file)
index 0000000..6969872
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+/**
+ * Copyright 2005-2010 Alkaloid Networks LLC (http://projects.alkaloid.net)
+ *
+ * See the enclosed file COPYING for license information (BSD). If you
+ * did not receive this file, see
+ * http://www.opensource.org/licenses/bsd-license.php.
+ *
+ * @author  Ben Klang <ben@alkaloid.net>
+ */
+
+require_once dirname(__FILE__) . '/../lib/Application.php';
+$shout = Horde_Registry::appInit('shout');
+
+require_once SHOUT_BASE . '/lib/Forms/AccountForm.php';
+
+$action = Horde_Util::getFormData('action');
+$curaccount = $_SESSION['shout']['curaccount'];
+
+$RENDERER = new Horde_Form_Renderer();
+
+$title = _("Accounts: ");
+
+switch ($action) {
+case 'add':
+case 'edit':
+    $vars = Horde_Variables::getDefaultVariables();
+    $vars->set('account', $curaccount['code']);
+    $Form = new AccountDetailsForm($vars);
+
+    if ($Form->isSubmitted() && $Form->validate($vars, true)) {
+        // Form is Valid and Submitted
+        try {
+            $Form->execute();
+            $notification->push(_("Account information saved."),
+                                  'horde.success');
+            $action = 'list';
+        } catch (Exception $e) {
+            $notification->push($e);
+        }
+        break;
+    } elseif ($Form->isSubmitted()) {
+        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
+    }
+
+    // Create a new add/edit form
+    $account = Horde_Util::getFormData('extension');
+    $accounts = $shout->storage->getAccounts();
+    $vars = new Horde_Variables(array('code' => $account, 'name' => $accounts[$account]['name']));
+    $vars->set('action', $action);
+
+    // Make sure we get the right template below.
+    $action = 'edit';
+    break;
+
+case 'delete':
+    $title .= sprintf(_("Delete Extension %s"), $extension);
+    $extension = Horde_Util::getFormData('extension');
+
+    $vars = Horde_Variables::getDefaultVariables();
+    $vars->set('account', $curaccount['code']);
+    $Form = new ExtensionDeleteForm($vars);
+
+    $FormValid = $Form->validate($vars, true);
+
+    if ($Form->isSubmitted() && $FormValid) {
+        try {
+            $Form->execute();
+            $notification->push(_("Extension Deleted."));
+            $action = 'list';
+        } catch (Exception $e) {
+            $notification->push($e);
+        }
+    } elseif ($Form->isSubmitted()) {
+        // Submitted but not valid
+        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
+    }
+
+    $vars = Horde_Variables::getDefaultVariables(array());
+    $vars->set('account', $curaccount['code']);
+    $Form = new ExtensionDeleteForm($vars);
+
+    break;
+
+case 'list':
+default:
+    $action = 'list';
+    $title .= _("List Accounts");
+}
+
+
+// Fetch the (possibly updated) list of extensions
+try {
+    $accounts = $shout->storage->getAccounts();
+} catch (Exception $e) {
+    $notification->push($e);
+    $extensions = array();
+}
+
+Horde::addScriptFile('stripe.js', 'horde');
+Horde::addScriptFile('prototype.js', 'horde');
+
+require SHOUT_TEMPLATES . '/common-header.inc';
+require SHOUT_TEMPLATES . '/menu.inc';
+
+$notification->notify();
+
+Shout::getAdminTabs();
+
+require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc';
+
+require $registry->get('templates', 'horde') . '/common-footer.inc';
diff --git a/shout/admin/numbers.php b/shout/admin/numbers.php
new file mode 100644 (file)
index 0000000..3187eea
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Copyright 2010 Alkaloid Networks LLC (http://projects.alkaloid.net)
+ *
+ * See the enclosed file COPYING for license information (BSD). If you
+ * did not receive this file, see
+ * http://www.opensource.org/licenses/bsd-license.php.
+ *
+ * @author  Ben Klang <ben@alkaloid.net>
+ */
+
+require_once dirname(__FILE__) . '/../lib/Application.php';
+$shout = Horde_Registry::appInit('shout');
+
+require_once SHOUT_BASE . '/lib/Forms/NumberForm.php';
+
+$action = Horde_Util::getFormData('action');
+$curaccount = $_SESSION['shout']['curaccount'];
+
+$RENDERER = new Horde_Form_Renderer();
+
+$title = _("Numbers: ");
+
+switch ($action) {
+case 'add':
+case 'edit':
+    $vars = Horde_Variables::getDefaultVariables();
+    $Form = new NumberDetailsForm($vars);
+
+    if ($Form->isSubmitted() && $Form->validate($vars, true)) {
+        // Form is Valid and Submitted
+        try {
+            $Form->execute();
+            $notification->push(_("Account information saved."),
+                                  'horde.success');
+            $action = 'list';
+        } catch (Exception $e) {
+            $notification->push($e);
+        }
+        break;
+    } elseif ($Form->isSubmitted()) {
+        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
+    }
+
+    // FIXME: Preserve vars on edit
+
+    // Make sure we get the right template below.
+    $action = 'edit';
+    break;
+
+case 'delete':
+    $title .= sprintf(_("Delete Extension %s"), $extension);
+    $extension = Horde_Util::getFormData('extension');
+
+    $vars = Horde_Variables::getDefaultVariables();
+    $vars->set('account', $curaccount['code']);
+    $Form = new ExtensionDeleteForm($vars);
+
+    $FormValid = $Form->validate($vars, true);
+
+    if ($Form->isSubmitted() && $FormValid) {
+        try {
+            $Form->execute();
+            $notification->push(_("Extension Deleted."));
+            $action = 'list';
+        } catch (Exception $e) {
+            $notification->push($e);
+        }
+    } elseif ($Form->isSubmitted()) {
+        // Submitted but not valid
+        $notification->push(_("Problem processing the form.  Please check below and try again."), 'horde.warning');
+    }
+
+    $vars = Horde_Variables::getDefaultVariables(array());
+    $vars->set('account', $curaccount['code']);
+    $Form = new ExtensionDeleteForm($vars);
+
+    break;
+
+case 'list':
+default:
+    $action = 'list';
+    $title .= _("List Numbers");
+}
+
+try {
+    $numbers = $shout->storage->getNumbers();
+} catch (Exception $e) {
+    $notification->push($e);
+}
+
+
+Horde::addScriptFile('stripe.js', 'horde');
+Horde::addScriptFile('prototype.js', 'horde');
+
+require SHOUT_TEMPLATES . '/common-header.inc';
+require SHOUT_TEMPLATES . '/menu.inc';
+
+$notification->notify();
+
+Shout::getAdminTabs();
+
+require SHOUT_TEMPLATES . '/admin/numbers/' . $action . '.inc.php';
+
+require $registry->get('templates', 'horde') . '/common-footer.inc';
index ddd7dc2..24d3a95 100644 (file)
@@ -120,7 +120,6 @@ class Shout_Driver_Sql extends Shout_Driver
             throw new Shout_Exception($row);
         }
 
-        $menus = array();
         while ($row && !($row instanceof PEAR_Error)) {
             $menu = $row['name'];
             $menus[$menu] = array(
@@ -592,6 +591,39 @@ class Shout_Driver_Sql extends Shout_Driver
         return true;
     }
 
+    public function getNumbers()
+    {
+        $sql = 'SELECT numbers.id AS id, numbers.did AS did, ' .
+               'accounts.code AS accountcode, menus.name AS menuName ' .
+               'FROM numbers ' .
+               'INNER JOIN accounts ON numbers.account_id = accounts.id ' .
+               'INNER JOIN menus ON numbers.menu_id = menus.id';
+        
+        $msg = 'SQL query in Shout_Driver_Sql#getNumbers(): ' . $sql;
+        Horde::logMessage($msg, 'DEBUG');
+        $result = $this->_db->query($sql, $args);
+        if ($result instanceof PEAR_Error) {
+            throw new Shout_Exception($result);
+        }
+
+        $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
+        if ($row instanceof PEAR_Error) {
+            throw new Shout_Exception($row);
+        }
+
+        $numbers = array();
+        while ($row && !($row instanceof PEAR_Error)) {
+            $id = $numbers['did'];
+            $numbers[$id] = $row;
+
+            /* Advance to the new row in the result set. */
+            $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
+        }
+
+        $result->free();
+        return $numbers;
+    }
+
     /**
      * Attempts to open a persistent connection to the SQL server.
      *
diff --git a/shout/lib/Forms/NumberForm.php b/shout/lib/Forms/NumberForm.php
new file mode 100644 (file)
index 0000000..8086cb6
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+/**
+ * $Id$
+ *
+ * Copyright 2010 Alkaloid Networks LLC (http://projects.alkaloid.net)
+ *
+ * See the enclosed file LICENSE for license information (BSD). If you
+ * did not receive this file, see
+ * http://www.opensource.org/licenses/bsd-license.php.
+ *
+ * @author Ben Klang <ben@alkaloid.net>
+ * @package Shout
+ */
+
+class NumberDetailsForm extends Horde_Form {
+
+    /**
+     * AccountDetailsForm constructor.
+     *
+     * @param mixed reference $vars
+     * @return boolean
+     */
+    function __construct(&$vars)
+    {
+        $action = $vars->get('action');
+        if ($action == 'edit') {
+            $title = _("Edit Number");
+        } else {
+            $title = _("Add Number");
+        }
+
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'action', 'text', true);
+        //$this->addHidden('', 'oldaccount', 'text', false);
+        $this->addVariable(_("Telephone Number"), 'number', 'phone', true);
+
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+        $accounts = $shout->storage->getAccounts();
+        foreach ($accounts as $id => $info) {
+            $list[$id] = $info['name'];
+        }
+        $select = $this->addVariable(_("Account Code"), 'accountcode',
+                                     'enum', false, false, null, array($list));
+        $action = &Horde_Form_Action::factory('reload');
+        $select->setAction($action);
+        $select->setOption('trackchange', true);
+
+        $accountcode = $vars->get('accountcode');
+        $menus = $shout->storage->getMenus($accountcode);
+        $list = array('' => _("-- none --"));
+        foreach ($menus as $id => $info) {
+            $list[$id] = $info['name'];
+        }
+        $this->addVariable(_("Menu"), 'menuName', 'enum', false,
+                                     false, null, array($list));
+        return true;
+    }
+
+    /**
+     * Process this form, saving its information to the backend.
+     */
+    function execute()
+    {
+        die("FIXME");
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+
+        $code = $this->_vars->get('code');
+        $name = $this->_vars->get('name');
+        $adminpin = $this->_vars->get('adminpin');
+        if (empty($adminpin)) {
+            $adminpin = rand(1000, 9999);
+        }
+
+        $shout->storage->saveAccount($code, $name, $adminpin);
+    }
+
+}
+
+class NumberDeleteForm extends Horde_Form
+{
+    function __construct(&$vars)
+    {
+        die("FIXME");
+        $extension = $vars->get('extension');
+        $account = $vars->get('account');
+
+        $title = _("Delete Extension %s - Account: %s");
+        $title = sprintf($title, $extension, $_SESSION['shout']['accounts'][$account]['name']);
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'account', 'text', true);
+        $this->addHidden('', 'extension', 'int', true);
+        $this->addHidden('', 'action', 'text', true);
+        $this->setButtons(array(_("Delete"), _("Cancel")));
+    }
+
+    function execute()
+    {
+        die("FIXME");
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+        $account = $this->_vars->get('account');
+        $shout->storage->deleteAccount($account);
+    }
+}
index 9518a00..27796cb 100644 (file)
@@ -240,4 +240,21 @@ class Shout
         return $res;
     }
 
+    static public function getAdminTabs()
+    {
+        $tabname = Horde_Util::getFormData('tabname');
+        $tabs = new Horde_Ui_Tabs('view', Horde_Variables::getDefaultVariables());
+        $tabs->addTab(_("Telephone Numbers"),
+                      Horde::applicationUrl('admin/numbers.php'),
+                      array('tabname' => 'numbers', id => 'tabnumbers'));
+        $tabs->addTab(_("Accounts"),
+                      Horde::applicationUrl('admin/accounts.php'),
+                      array('tabname' => 'accounts', id => 'tabaccounts'));
+        if ($tabname === null) {
+            $tabname = 'numbers';
+        }
+
+        echo $tabs->render($tabname);
+    }
+
 }
diff --git a/shout/templates/admin/numbers/edit.inc.php b/shout/templates/admin/numbers/edit.inc.php
new file mode 100644 (file)
index 0000000..f65b322
--- /dev/null
@@ -0,0 +1 @@
+<?php $Form->renderActive();
\ No newline at end of file
diff --git a/shout/templates/admin/numbers/list.inc.php b/shout/templates/admin/numbers/list.inc.php
new file mode 100644 (file)
index 0000000..f615bf4
--- /dev/null
@@ -0,0 +1,40 @@
+<div id="adminNumberList">
+    <table width="100%" cellspacing="0" class="striped">
+        <tr>
+            <td class="uheader">Telephone Number</td>
+            <td class="uheader">Account Name</td>
+            <td class="uheader">Menu Name</td>
+        </tr>
+        <?php
+            $url = Horde::applicationUrl("admin/numbers.php");
+            $editurl = Horde_Util::addParameter($url, 'action', 'edit');
+            $deleteurl = Horde_Util::addParameter($url, 'action', 'delete');
+            foreach ($numbers as $numberinfo) {
+                $code = $acctinfo['code'];
+                ?>
+                <tr class="item" style="vertical-align: top">
+                    <td>
+                        <?php echo Horde::link(Horde_Util::addParameter($editurl,
+                            array('account' => $code))); echo $code; echo '</a>'; ?>
+                    </td>
+                    <td>
+                        <?php echo $acctinfo['name']; ?>
+                    </td>
+                    <td>
+                        <?php echo $acctinfo['adminpin']; ?>
+                    </td>
+                </tr>
+                <?php
+            }
+            ?>
+    </table>
+</div>
+<ul id="controls">
+    <?php
+    $addurl = Horde_Util::addParameter($url, 'action', 'add');
+    ?>
+    <li><a class="button" href="<?php echo $addurl; ?>">
+        <?php echo Horde::img('extension-add.png'); ?>&nbsp;New Number
+        </a>
+    </li>
+</ul>
\ No newline at end of file