Shout: Create admin section for managing accounts
authorBen Klang <ben@alkaloid.net>
Wed, 24 Mar 2010 21:10:42 +0000 (17:10 -0400)
committerBen Klang <ben@alkaloid.net>
Wed, 24 Mar 2010 21:11:01 +0000 (17:11 -0400)
shout/admin.php [new file with mode: 0644]
shout/lib/Driver/Sql.php
shout/lib/Forms/AccountForm.php [new file with mode: 0644]
shout/lib/Shout.php
shout/templates/accounts/edit.inc [new file with mode: 0644]
shout/templates/accounts/list.inc [new file with mode: 0644]
shout/themes/graphics/admin.png [new file with mode: 0755]

diff --git a/shout/admin.php b/shout/admin.php
new file mode 100644 (file)
index 0000000..dde59bc
--- /dev/null
@@ -0,0 +1,110 @@
+<?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);
+    $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]));
+    $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);
+    $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);
+    $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();
+
+require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc';
+
+require $registry->get('templates', 'horde') . '/common-footer.inc';
index c683e90..11748a2 100644 (file)
@@ -73,6 +73,32 @@ class Shout_Driver_Sql extends Shout_Driver
          return $accounts;
     }
 
+    public function saveAccount($code, $name)
+    {
+        $this->_connect();
+
+        if (isset($details['oldname'])) {
+            if (!isset($menus[$details['oldname']])) {
+                throw new Shout_Exception(_("Old account not found.  Edit aborted."));
+            } else {
+                throw new Shout_Exception(_("Unsupported operation."));
+                $sql = 'UPDATE accounts SET code = ?, name =? WHERE code = ?';
+            }
+        } else {
+            $sql = 'INSERT INTO accounts (code, name) VALUES (?,?)';
+        }
+
+        $vars = array($code, $name);
+
+        $msg = 'SQL query in Shout_Driver_Sql#getAccounts(): ' . $sql;
+        Horde::logMessage($msg, 'DEBUG');
+        $result = $this->_db->query($sql, $vars);
+        if ($result instanceof PEAR_Error) {
+            throw new Shout_Exception($result);
+        }
+
+    }
+
     public function getMenus($account)
     {
         $this->_connect();
diff --git a/shout/lib/Forms/AccountForm.php b/shout/lib/Forms/AccountForm.php
new file mode 100644 (file)
index 0000000..f2779c8
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * $Id$
+ *
+ * Copyright 2005-2009 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 AccountDetailsForm extends Horde_Form {
+
+    /**
+     * AccountDetailsForm constructor.
+     *
+     * @param mixed reference $vars
+     * @return boolean
+     */
+    function __construct(&$vars)
+    {
+        $account = $_SESSION['shout']['curaccount'];
+        $action = $vars->get('action');
+        if ($action == 'edit') {
+            $formtitle = "Edit Account";
+            $vars->set('oldaccount', $account);
+        } else {
+            $formtitle = "Add Account";
+        }
+
+        $accountname = $_SESSION['shout']['accounts'][$curaccount];
+        $title = sprintf(_("$formtitle %s"), $accountname);
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'action', 'text', true);
+        //$this->addHidden('', 'oldaccount', 'text', false);
+        $this->addVariable(_("Account Name"), 'name', 'text', true);
+        $this->addVariable(_("Account Code"), 'code', 'text', true);
+
+        return true;
+    }
+
+    /**
+     * Process this form, saving its information to the backend.
+     */
+    function execute()
+    {
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+
+        $code = $this->_vars->get('code');
+        $name = $this->_vars->get('name');
+
+        $shout->storage->saveAccount($code, $name);;
+    }
+
+}
+
+class AccountDeleteForm extends Horde_Form
+{
+    function __construct(&$vars)
+    {
+        $extension = $vars->get('extension');
+        $account = $vars->get('account');
+
+        $title = _("Delete Extension %s - Account: %s");
+        $title = sprintf($title, $extension, $_SESSION['shout']['accounts'][$account]);
+        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()
+    {
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+        $account = $this->_vars->get('account');
+        $shout->storage->deleteAccount($account);
+    }
+}
index a83d471..176d5bf 100644 (file)
@@ -24,10 +24,6 @@ class Shout
      */
     static public function getMenu($returnType = 'object')
     {
-        global $conf, $curaccount, $section, $action;
-
-        require_once 'Horde/Menu.php';
-
         $menu = new Horde_Menu(Horde_Menu::MASK_ALL);
 
         $menu->add(Horde::applicationUrl('dialplan.php'), _("Incoming Calls"), "dialplan.png");
@@ -35,6 +31,11 @@ class Shout
         $menu->add(Horde::applicationUrl('devices.php'), _("Devices"), "shout.png");
         $menu->add(Horde::applicationUrl('recordings.php'), _("Recordings"), "recordings.png");
 
+        /* Administration. */
+        if (Horde_Auth::isAdmin('shout:admin')) {
+            $menu->add(Horde::applicationUrl('admin.php'), _("_Admin"), 'admin.png');
+        }
+
         if ($returnType == 'object') {
             return $menu;
         } else {
diff --git a/shout/templates/accounts/edit.inc b/shout/templates/accounts/edit.inc
new file mode 100644 (file)
index 0000000..e42e0b1
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+$Form->renderActive($RENDERER, $vars, Horde::applicationUrl('admin.php'), 'post');
+
+if ($vars->get('action') == 'edit') {
+    $deleteUrl = Horde::applicationUrl('admin.php');
+    $params = array ('action' => 'delete',
+                     'account' => $account);
+    $deleteUrl = Horde_Util::addParameter($deleteUrl, $params);
+    echo '<a href="' . $deleteUrl . '">Delete Account</a>';
+}
diff --git a/shout/templates/accounts/list.inc b/shout/templates/accounts/list.inc
new file mode 100644 (file)
index 0000000..2278c70
--- /dev/null
@@ -0,0 +1,45 @@
+<div class="header">
+    <ul id="controls">
+        <?php
+        $addurl = Horde::applicationUrl('admin.php');
+        $addurl = Horde_Util::addParameter($addurl, 'action', 'add');
+        ?>
+        <li><a href="<?php echo $addurl; ?>">
+            <?php echo Horde::img('add-extension.png'); ?>&nbsp;New Account
+            </a>
+        </li>
+    </ul>
+    Account: <?php echo $_SESSION['shout']['accounts'][$curaccount]; ?>
+</div>
+
+<div id="extensionList">
+    <table width="100%" cellspacing="0" class="striped">
+        <tr>
+            <td class="uheader">Account Code</td>
+            <td id ="destinationsCol" class="uheader">Account Name</td>
+        </tr>
+        <?php
+            foreach ($accounts as $code => $name) {
+
+                $url = Horde::applicationUrl("admin.php");
+                $url = Horde_Util::addParameter($url,
+                    array(
+                        'account' => $code,
+                    )
+                );
+                $editurl = Horde_Util::addParameter($url, 'action', 'edit');
+                $deleteurl = Horde_Util::addParameter($url, 'action', 'delete');
+        ?>
+        <tr class="item" style="vertical-align: top">
+            <td>
+                <?php echo Horde::link($editurl); echo $code; ?></a>
+            </td>
+            <td>
+                <?php echo $name; ?>
+            </td>
+        </tr>
+        <?php
+            }
+        ?>
+    </table>
+</div>
diff --git a/shout/themes/graphics/admin.png b/shout/themes/graphics/admin.png
new file mode 100755 (executable)
index 0000000..4ec1a92
Binary files /dev/null and b/shout/themes/graphics/admin.png differ