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');
$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');
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);
}
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']
$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)
--- /dev/null
+<?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
--- /dev/null
+<?php
+$Form->renderActive($RENDERER, $vars, Horde::applicationUrl('dialplan.php'), 'post');
<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');