From: Ben Klang Date: Mon, 15 Mar 2010 03:11:26 +0000 (-0400) Subject: Shout: Implement saveMenuAction AJAX method X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=4109083a1eb16a700ba0460ba1be494afd8fbc78;p=horde.git Shout: Implement saveMenuAction AJAX method --- diff --git a/shout/lib/Ajax/Application.php b/shout/lib/Ajax/Application.php index 1a6584287..3ba94eb7d 100644 --- a/shout/lib/Ajax/Application.php +++ b/shout/lib/Ajax/Application.php @@ -175,7 +175,30 @@ class Shout_Ajax_Application extends Horde_Ajax_Application_Base public function saveAction() { - return null; + try { + $shout = $_GLOBALS['shout'] = Horde_Registry::appInit('shout'); + $vars = $this->_vars; + if (!($action = $vars->get('action'))) { + throw new Shout_Exception("Invalid action requested."); + } + $account = $_SESSION['shout']['curaccount']; + $digit = $vars->get('digit'); + $menu = $vars->get('menu'); + $action = $vars->get('action'); + $actions = Shout::getMenuActions(); + if (!isset($actions[$action])) { + throw new Shout_Exception('Invalid action requested.'); + } + $args = array(); + foreach ($action[$action]['args'] as $name => $info) { + $args[$name] = $vars->get($name); + } + $shout->dialplan->saveMenuAction($account, $menu, $action, $args); + } catch (Exception $e) { + //FIXME: Create a way to notify the user of the failure. + Horde::logMessage($e->getMessage(), __FILE__, __LINE__, PEAR_LOG_ERR); + return false; + } } public function responseType() diff --git a/shout/lib/Driver/Sql.php b/shout/lib/Driver/Sql.php index 2ac41230d..7a40b0558 100644 --- a/shout/lib/Driver/Sql.php +++ b/shout/lib/Driver/Sql.php @@ -38,7 +38,7 @@ class Shout_Driver_Sql extends Shout_Driver * * @param array $params A hash containing connection parameters. */ - function __construct($params = array()) + public function __construct($params = array()) { parent::__construct($params); $this->_connect(); @@ -109,7 +109,7 @@ class Shout_Driver_Sql extends Shout_Driver return $menus; } - function saveMenu($account, $details) + public function saveMenu($account, $details) { $menus = $this->getMenus($account); if (isset($details['oldname'])) { @@ -132,7 +132,7 @@ class Shout_Driver_Sql extends Shout_Driver $msg = 'SQL query in Shout_Driver_Sql#saveMenu(): ' . $sql; Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_db->query($sql, $values); + $result = $this->_write_db->query($sql, $values); if ($result instanceof PEAR_Error) { throw new Shout_Exception($result); } @@ -140,7 +140,7 @@ class Shout_Driver_Sql extends Shout_Driver return true; } - function getMenuActions($account, $menu) + public function getMenuActions($account, $menu) { static $menuActions; if (!empty($menuActions[$menu])) { @@ -182,6 +182,34 @@ class Shout_Driver_Sql extends Shout_Driver return $menuActions[$menu]; } + public function saveMenuAction($account, $menu, $digit, $action, $args) + { + // Remove any existing action + $sql = 'DELETE FROM menu_entries WHERE digit = ?'; + $values = array($digit); + $msg = 'SQL query in Shout_Driver_Sql#saveMenuAction(): ' . $sql; + Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($sql, $values); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + + $sql = 'INSERT INTO menu_entries (menu_id, digit, action_id, args) ' . + 'VALUES((SELECT id FROM menus WHERE account_id = ' . + '(SELECT id FROM accounts WHERE code = ?) AND name = ?), ?, ' . + '(SELECT id FROM actions WHERE name = ?), ?)'; + $yamlargs = Horde_Yaml::dump($args); + $values = array($account, $menu, $digit, $action, $yamlargs); + $msg = 'SQL query in Shout_Driver_Sql#saveMenuAction(): ' . $sql; + Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($sql, $values); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + + return true; + } + /** * Get a list of devices for a given account *