Shout: Implement saveMenuAction AJAX method
authorBen Klang <ben@alkaloid.net>
Mon, 15 Mar 2010 03:11:26 +0000 (23:11 -0400)
committerBen Klang <ben@alkaloid.net>
Mon, 15 Mar 2010 03:11:26 +0000 (23:11 -0400)
shout/lib/Ajax/Application.php
shout/lib/Driver/Sql.php

index 1a65842..3ba94eb 100644 (file)
@@ -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()
index 2ac4123..7a40b05 100644 (file)
@@ -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
      *