From 7db2662ac196f10e00b895160b08eb260ccc7672 Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Mon, 8 Mar 2010 20:24:54 -0500 Subject: [PATCH] Shout: Add ability to create new dialplan menus --- shout/dialplan.php | 29 ++++++++++++- shout/lib/Driver/Sql.php | 46 +++++++++++++++----- shout/lib/Forms/MenuForm.php | 90 +++++++++++++++++++++++++++++++++++++++ shout/templates/dialplan/add.inc | 2 + shout/templates/dialplan/list.inc | 2 +- 5 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 shout/lib/Forms/MenuForm.php create mode 100644 shout/templates/dialplan/add.inc diff --git a/shout/dialplan.php b/shout/dialplan.php index f3af4c8bb..3350215b2 100644 --- a/shout/dialplan.php +++ b/shout/dialplan.php @@ -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'); diff --git a/shout/lib/Driver/Sql.php b/shout/lib/Driver/Sql.php index 303e44285..2ac41230d 100644 --- a/shout/lib/Driver/Sql.php +++ b/shout/lib/Driver/Sql.php @@ -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 index 000000000..cf61346b3 --- /dev/null +++ b/shout/lib/Forms/MenuForm.php @@ -0,0 +1,90 @@ +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 index 000000000..9763d3607 --- /dev/null +++ b/shout/templates/dialplan/add.inc @@ -0,0 +1,2 @@ +renderActive($RENDERER, $vars, Horde::applicationUrl('dialplan.php'), 'post'); diff --git a/shout/templates/dialplan/list.inc b/shout/templates/dialplan/list.inc index d154a4578..431215ab1 100644 --- a/shout/templates/dialplan/list.inc +++ b/shout/templates/dialplan/list.inc @@ -1,7 +1,7 @@