From 6fa8b14b533243f9bb58cd94564fe8c439613edb Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Sun, 28 Mar 2010 23:41:11 -0400 Subject: [PATCH] Shout: enable deleting of dialplan menus --- shout/dialplan.php | 13 +++++++--- shout/lib/Ajax/Application.php | 18 +++++++++++++ shout/lib/Driver/Sql.php | 26 ++++++++++++++++++- shout/templates/dialplan/edit.inc | 54 ++++++++++++++++++++++++++++++--------- 4 files changed, 94 insertions(+), 17 deletions(-) diff --git a/shout/dialplan.php b/shout/dialplan.php index 9524a2dce..564ef5573 100644 --- a/shout/dialplan.php +++ b/shout/dialplan.php @@ -33,7 +33,8 @@ case 'add': $notification->push(_("Menu added."), 'horde.success'); $menus = $shout->storage->getMenus($curaccount); - $action = 'list'; + $action = 'edit'; + } catch (Exception $e) { $notification->push($e); } @@ -46,11 +47,17 @@ case 'add': $vars = new Horde_Variables(); $vars->set('action', $action); //$Form = new MenuForm($vars); - break; + case 'edit': default: $action = 'edit'; + break; +} + +// Check again explicitly for edit as we may have converted to an edit +// after a successful add. +if ($action == 'edit') { try { $destinations = $shout->extensions->getExtensions($curaccount); $conferences = $shout->storage->getConferences($curaccount); @@ -59,8 +66,6 @@ default: Horde::logMessage($e, 'ERR'); $notification->push(_("Problem getting menu information.")); } - - break; } Horde::addScriptFile('stripe.js', 'horde'); diff --git a/shout/lib/Ajax/Application.php b/shout/lib/Ajax/Application.php index b1ef8ba87..f8d762dfd 100644 --- a/shout/lib/Ajax/Application.php +++ b/shout/lib/Ajax/Application.php @@ -123,6 +123,24 @@ class Shout_Ajax_Application extends Horde_Ajax_Application_Base } } + public function deleteMenu() + { + try { + $shout = $GLOBALS['registry']->getApiInstance('shout', 'application'); + $account = $_SESSION['shout']['curaccount']; + $menu = $this->_vars->get('menu'); + if (empty($menu)) { + throw new Shout_Exception('Must specify a menu to delete.'); + } + $shout->dialplan->deleteMenu($account, $menu); + return true; + } catch (Exception $e) { + //FIXME: Create a way to notify the user of the failure. + Horde::logMessage($e, 'ERR'); + return false; + } + } + public function getConferences() { try { diff --git a/shout/lib/Driver/Sql.php b/shout/lib/Driver/Sql.php index 4319e3037..1166c27b8 100644 --- a/shout/lib/Driver/Sql.php +++ b/shout/lib/Driver/Sql.php @@ -166,6 +166,30 @@ class Shout_Driver_Sql extends Shout_Driver return true; } + public function deleteMenu($account, $menu) + { + $sql = 'DELETE FROM menu_entries WHERE menu_id = ' . + '(SELECT id FROM menus WHERE name = ? AND account_id = ' . + '(SELECT id FROM accounts WHERE code = ?))'; + $values = array($menu, $account); + $msg = 'SQL query in Shout_Driver_Sql#deleteMenu(): ' . $sql; + Horde::logMessage($msg, 'DEBUG'); + $result = $this->_write_db->query($sql, $values); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + + $sql = 'DELETE FROM menus WHERE name = ? AND account_id = ' . + '(SELECT id FROM accounts WHERE code = ?)'; + $values = array($menu, $account); + $msg = 'SQL query in Shout_Driver_Sql#deleteMenu(): ' . $sql; + Horde::logMessage($msg, 'DEBUG'); + $result = $this->_write_db->query($sql, $values); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + } + public function getMenuActions($account, $menu) { static $menuActions; @@ -475,7 +499,7 @@ class Shout_Driver_Sql extends Shout_Driver $msg = 'SQL query in Shout_Driver_Sql#saveConference(): ' . $sql; Horde::logMessage($msg, 'DEBUG'); - $result = $this->_db->query($sql, $args); + $result = $this->_write_db->query($sql, $args); if ($result instanceof PEAR_Error) { throw new Shout_Exception($result); } diff --git a/shout/templates/dialplan/edit.inc b/shout/templates/dialplan/edit.inc index d41551b27..f4498d6a7 100644 --- a/shout/templates/dialplan/edit.inc +++ b/shout/templates/dialplan/edit.inc @@ -336,6 +336,16 @@ function refreshMenu() { $('menuWorking').show(); $('dialpadWorking').show(); + curmenu = $('menu.select').value; + + empty('menu.select'); + menuInfo.each(function (item) { + var option = document.createElement('option'); + option.value = item.key; + var text = document.createTextNode(item.value.name); + option.appendChild(text); + $('menu.select').appendChild(option); + }) $A(['1','2','3','4','5','6','7','8','9','0','star','octo']).each(function (digit){ empty('digit_' + digit); @@ -358,12 +368,16 @@ function refreshMenu() }) if (!(menuInfo.size())) { + curmenu = null; $('menuWorking').hide(); $('dialpadWorking').hide(); return true; } - curmenu = $('menu.select').value; + if (!menuInfo.get(curmenu)) { + curmenu = menuInfo.keys().first(); + } + $('menu.select').value = curmenu; var text = document.createTextNode(menuInfo.get(curmenu).description); empty('menu.description'); @@ -456,6 +470,33 @@ function editMenu() $('editMenu').show(); } +function deleteMenu() +{ + $('menuWorking').show(); + var msg = ''; + msg = msg.replace('%menu%', menuInfo.get(curmenu).name); + if (confirm(msg)) { + var params = {'menu': curmenu}; + new Ajax.Request(ajax_url + 'deleteMenu', + { + method: 'post', + parameters: params, + onSuccess: function(r) { + new Ajax.Request(ajax_url + 'getMenus', + { + method: 'get', + onSuccess: function(r) { + menuInfo = $H(r.responseJSON.response); + refreshMenu(); + } + }); + } + }); + } else { + $('menuWorking').hide(); + } +} + function saveMenu() { $('menuWorking').show(); @@ -470,13 +511,9 @@ function saveMenu() new Ajax.Request(ajax_url + 'getMenus', { method: 'get', - parameters: $H({ - 'menu': curmenu - }), onSuccess: function(r) { menuInfo = $H(r.responseJSON.response); refreshMenu(); - $('menuWorking').hide(); } }); } @@ -491,13 +528,6 @@ new Ajax.Request(ajax_url + 'getMenus', method: 'get', onSuccess: function(r) { menuInfo = $H(r.responseJSON.response); - menuInfo.each(function (item) { - var option = document.createElement('option'); - option.value = item.key; - var text = document.createTextNode(item.value.name); - option.appendChild(text); - $('menu.select').appendChild(option); - }) refreshMenu(); } }); -- 2.11.0