Shout: Add ability to create new dialplan menus
authorBen Klang <ben@alkaloid.net>
Tue, 9 Mar 2010 01:24:54 +0000 (20:24 -0500)
committerBen Klang <ben@alkaloid.net>
Tue, 9 Mar 2010 01:25:13 +0000 (20:25 -0500)
shout/dialplan.php
shout/lib/Driver/Sql.php
shout/lib/Forms/MenuForm.php [new file with mode: 0644]
shout/templates/dialplan/add.inc [new file with mode: 0644]
shout/templates/dialplan/list.inc

index f3af4c8..3350215 100644 (file)
@@ -12,7 +12,7 @@
 require_once dirname(__FILE__) . '/lib/Application.php';
 $shout = Horde_Registry::appInit('shout');
 
-require_once SHOUT_BASE . '/lib/Forms/ExtensionForm.php';
+require_once SHOUT_BASE . '/lib/Forms/MenuForm.php';
 
 $action = Horde_Util::getFormData('action');
 $menu = Horde_Util::getFormData('menu');
@@ -21,6 +21,33 @@ $curaccount = $_SESSION['shout']['curaccount'];
 $menus = $shout->storage->getMenus($curaccount);
 
 switch($action) {
+case 'add':
+    $vars = Horde_Variables::getDefaultVariables();
+    $vars->set('account', $curaccount);
+    $Form = new MenuForm($vars);
+
+    if ($Form->isSubmitted() && $Form->validate($vars, true)) {
+        // Form is Valid and Submitted
+        try {
+            $Form->execute();
+            $notification->push(_("Menu added."),
+                                  'horde.success');
+            $menus = $shout->storage->getMenus($curaccount);
+            $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 form
+    $vars = new Horde_Variables();
+    $vars->set('action', $action);
+    //$Form = new MenuForm($vars);
+
+    break;
 case 'edit':
     if (!isset($menus[$menu])) {
         $notification->push(_("That menu does not exist."), 'horde.error');
index 303e442..2ac4123 100644 (file)
@@ -75,22 +75,17 @@ class Shout_Driver_Sql extends Shout_Driver
 
     public function getMenus($account)
     {
-        static $menus;
-        if (isset($menus[$account])) {
-            return $menus[$account];
-        }
-
         $this->_connect();
 
         $sql = 'SELECT accounts.code AS account, menus.name AS name, ' .
                'menus.description AS description, menus.soundfile AS soundfile ' .
                'FROM menus INNER JOIN accounts ON menus.account_id = accounts.id ' .
                'WHERE accounts.code = ?';
-        $vars = array($account);
+        $values = array($account);
 
         $msg = 'SQL query in Shout_Driver_Sql#getMenus(): ' . $sql;
         Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
-        $result = $this->_db->query($sql, $vars);
+        $result = $this->_db->query($sql, $values);
         if ($result instanceof PEAR_Error) {
             throw new Shout_Exception($result);
         }
@@ -100,10 +95,10 @@ class Shout_Driver_Sql extends Shout_Driver
             throw new Shout_Exception($row);
         }
 
-        $menus[$account] = array();
+        $menus = array();
         while ($row && !($row instanceof PEAR_Error)) {
             $menu = $row['name'];
-            $menus[$account][$menu] = array(
+            $menus[$menu] = array(
                 'name' => $menu,
                 'description' => $row['description'],
                 'soundfile' => $row['soundfile']
@@ -111,7 +106,38 @@ class Shout_Driver_Sql extends Shout_Driver
             $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
         }
         $result->free();
-        return $menus[$account];
+        return $menus;
+    }
+
+    function saveMenu($account, $details)
+    {
+        $menus = $this->getMenus($account);
+        if (isset($details['oldname'])) {
+            if (!isset($menus[$details['oldname']])) {
+                throw new Shout_Exception(_("Old menu not found.  Edit aborted."));
+            } else {
+                $sql = 'UPDATE menus SET name = ?, description = ?, ' .
+                       'soundfile = ? WHERE account_id = (SELECT id FROM ' .
+                       'WHERE code = ?) AND name = ?';
+                $values = array($details['name'], $details['description'],
+                                $details['soundfile'], $account,
+                                $details['oldname']);
+            }
+        } else {
+            $sql = "INSERT INTO menus (account_id, name, description, soundfile) " .
+                   "VALUES ((SELECT id FROM accounts WHERE code = ?), ?, ?, ?)";
+            $values = array($account, $details['name'],
+                            $details['description'], $details['soundfile']);
+        }
+
+        $msg = 'SQL query in Shout_Driver_Sql#saveMenu(): ' . $sql;
+        Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
+        $result = $this->_db->query($sql, $values);
+        if ($result instanceof PEAR_Error) {
+            throw new Shout_Exception($result);
+        }
+
+        return true;
     }
 
     function getMenuActions($account, $menu)
diff --git a/shout/lib/Forms/MenuForm.php b/shout/lib/Forms/MenuForm.php
new file mode 100644 (file)
index 0000000..cf61346
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/**
+ * $Id: ExtensionForm.php 502 2009-12-21 04:01:12Z bklang $
+ *
+ * Copyright 2005-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.
+ *
+ * @package Shout
+ */
+
+class MenuForm extends Horde_Form {
+
+    function __construct(&$vars)
+    {
+        global $shout_extensions;
+
+        if ($vars->exists('menu')) {
+            $formtitle = _("Edit Menu");
+            $menu = $vars->get('menu');
+            $edit = true;
+        } else {
+            $formtitle = _("Add Device");
+            $edit = false;
+        }
+
+        $curaccount = $_SESSION['shout']['curaccount'];
+        $accountname = $_SESSION['shout']['accounts'][$curaccount];
+        $title = sprintf(_("%s - Account: %s"), $formtitle, $accountname);
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'action', 'text', true);
+        
+        if ($edit) {
+            $this->addHidden('', 'oldname', 'text', true);
+            $vars->set('oldname', $menu);
+        }
+        $this->addVariable(_("Menu Name"), 'name', 'text', true);
+        $this->addVariable(_("Description"), 'description', 'text', false);
+        $this->addVariable(_("Sound File"), 'soundfile', 'text', true);
+
+        return true;
+    }
+
+    public function execute()
+    {
+        global $shout;
+        $account = $_SESSION['shout']['curaccount'];
+
+        $details = array(
+            'name' => $this->_vars->get('name'),
+            'description' => $this->_vars->get('description'),
+            'soundfile' => $this->_vars->get('description')
+        );
+
+        // FIXME: Validate soundfile
+
+        if ($action == 'edit') {
+            $details['oldname'] = $this->_vars->get('oldname');
+        }
+
+        $shout->devices->saveMenu($account, $details);
+    }
+
+}
+
+class DeviceMenuForm extends Horde_Form
+{
+    function __construct(&$vars)
+    {
+        $menu = $vars->get('$menu');
+        $account = $vars->get('account');
+
+        $title = _("Delete Menu %s - Account: %s");
+        $title = sprintf($title, $menu, $_SESSION['shout']['accounts'][$account]);
+        parent::__construct($vars, $title);
+
+        $this->setButtons(array(_("Delete"), _("Cancel")));
+    }
+
+    function execute()
+    {
+        global $shout;
+        $account = $this->_vars->get('account');
+        $menu = $this->_vars->get('menu');
+        $shout->devices->deleteMenu($account, $menu);
+    }
+}
\ No newline at end of file
diff --git a/shout/templates/dialplan/add.inc b/shout/templates/dialplan/add.inc
new file mode 100644 (file)
index 0000000..9763d36
--- /dev/null
@@ -0,0 +1,2 @@
+<?php
+$Form->renderActive($RENDERER, $vars, Horde::applicationUrl('dialplan.php'), 'post');
index d154a45..431215a 100644 (file)
@@ -1,7 +1,7 @@
 <div class="header">
     <ul id="controls">
         <?php
-        $addurl = Horde::applicationUrl('extensions.php');
+        $addurl = Horde::applicationUrl('dialplan.php');
         $addurl = Horde_Util::addParameter($addurl, 'action', 'add');
         $editurl = Horde::applicationUrl('dialplan.php');
         $editurl = Horde_Util::addParameter($editlink, 'action', 'edit');