Shout: fix adding and editing conference rooms
authorBen Klang <ben@alkaloid.net>
Mon, 29 Mar 2010 01:44:32 +0000 (21:44 -0400)
committerBen Klang <ben@alkaloid.net>
Mon, 29 Mar 2010 01:44:32 +0000 (21:44 -0400)
shout/conferences.php
shout/lib/Driver/Sql.php
shout/lib/Forms/ConferenceForm.php
shout/templates/conferences/edit.inc [new file with mode: 0644]
shout/templates/conferences/list.inc

index afa57b1..16152bc 100644 (file)
@@ -33,8 +33,6 @@ case 'edit':
     if ($Form->isSubmitted() && $Form->isValid()) {
         // Form is Valid and Submitted
         try {
-            $devid = Horde_Util::getFormData('devid');
-
             $Form->execute();
             $notification->push(_("Conference information saved."),
                                   'horde.success');
@@ -50,17 +48,18 @@ case 'edit':
     }
 
     // Create a new add/edit form
-    $devid = Horde_Util::getFormData('devid');
-    $devices = $shout->devices->getDevices($curaccount);
-    $vars = new Horde_Variables($devices[$devid]);
+    $roomno = Horde_Util::getFormData('roomno');
+    $conferences = $shout->storage->getConferences($curaccount);
+    $vars = new Horde_Variables($conferences[$roomno]);
 
     $vars->set('action', $action);
+
     $Form = new ConferenceDetailsForm($vars);
 
     // Make sure we get the right template below.
     $action = 'edit';
-
     break;
+
 case 'delete':
     $title .= sprintf(_("Delete Devices %s"), $extension);
     $devid = Horde_Util::getFormData('devid');
@@ -86,7 +85,6 @@ case 'delete':
     $vars = Horde_Variables::getDefaultVariables(array());
     $vars->set('account', $curaccount);
     $Form = new DeviceDeleteForm($vars);
-
     break;
 
 case 'list':
index c7af303..4319e30 100644 (file)
@@ -427,14 +427,13 @@ class Shout_Driver_Sql extends Shout_Driver
 
     public function getConferences($account)
     {
-        $sql = 'SELECT id, room_number, name, pin, options ' .
+        $sql = 'SELECT id, room_number AS roomno, name, pin, options ' .
                'FROM conferences ' .
                'WHERE account_id = (SELECT id FROM accounts WHERE code = ?);';
         $args = array($account);
         $msg = 'SQL query in Shout_Driver_Sql#getConferences(): ' . $sql;
         Horde::logMessage($msg, 'DEBUG');
-        $sth = $this->_db->prepare($sql);
-        $result = $this->_db->execute($sth, $args);
+        $result = $this->_db->query($sql, $args);
         if ($result instanceof PEAR_Error) {
             throw new Shout_Exception($result);
         }
@@ -446,7 +445,7 @@ class Shout_Driver_Sql extends Shout_Driver
 
         $conferences = array();
         while ($row && !($row instanceof PEAR_Error)) {
-            $roomno = $row['room_number'];
+            $roomno = $row['roomno'];
             $conferences[$roomno] = $row;
 
             /* Advance to the new row in the result set. */
@@ -457,6 +456,33 @@ class Shout_Driver_Sql extends Shout_Driver
         return $conferences;
     }
 
+    public function saveConference($account, $roomno, $details)
+    {
+        if (isset($details['oldroomno'])) {
+            // This is an edit
+            $sql = 'UPDATE conferences ' .
+                   'SET room_number = ?, name = ?, pin = ? ' .
+                   'WHERE room_number = ? AND account_id = ' .
+                   '(SELECT id FROM accounts WHERE code = ?)';
+            $args = array($roomno, $details['name'], $details['pin'],
+                          $details['oldroomno'], $account);
+        } else {
+            $sql = 'INSERT INTO conferences ' .
+                   '(room_number, name, pin, account_id) ' .
+                   'VALUES (?, ?, ?, (SELECT id FROM accounts WHERE code = ?))';
+            $args = array($roomno, $details['name'], $details['pin'], $account);
+        }
+
+        $msg = 'SQL query in Shout_Driver_Sql#saveConference(): ' . $sql;
+        Horde::logMessage($msg, 'DEBUG');
+        $result = $this->_db->query($sql, $args);
+        if ($result instanceof PEAR_Error) {
+            throw new Shout_Exception($result);
+        }
+
+        return true;
+    }
+
     /**
      * Attempts to open a persistent connection to the SQL server.
      *
index e69de29..9e9eb2e 100644 (file)
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Copyright 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 ConferenceDetailsForm extends Horde_Form {
+
+    function __construct(&$vars)
+    {
+        if ($vars->exists('roomno')) {
+            $formtitle = "Edit Conference Room";
+            $roomno = $vars->get('roomno');
+            $this->addHidden('', 'oldroomno', 'text', true);
+            $vars->set('oldroomno', $roomno);
+            $edit = true;
+        } else {
+            $formtitle = "Create Conference Room";
+            $edit = false;
+        }
+
+        $curaccount = $_SESSION['shout']['curaccount'];
+        $accountname = $vars->account;
+        $title = sprintf(_("$formtitle - Account: %s"), $accountname);
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'action', 'text', true);
+        $this->addVariable(_("Room Name"), 'name', 'text', true);
+        $this->addVariable(_("Room Number"), 'roomno', 'number', true);
+        $this->addVariable(_("PIN"), 'pin', 'number', false);
+        return true;
+    }
+
+    public function execute()
+    {
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+
+        $action = $this->_vars->get('action');
+        $account = $this->_vars->get('account');
+        $roomno = $this->_vars->get('roomno');
+        $details = array(
+            'name' => $this->_vars->get('name'),
+            'pin' => $this->_vars->get('pin')
+        );
+
+
+        // For safety, we force the device ID and password rather than rely
+        // on the form to pass them around.
+        if ($action != 'add') { // $action must be 'edit'
+            $oldroomno = $this->_vars->get('oldroomno');
+            $conferences = $shout->storage->getConferences($account);
+            if (!isset($conferences[$roomno])) {
+                // The device requested doesn't already exist.  This can't
+                // be a valid edit.
+                throw new Shout_Exception(_("That conference room does not exist."),
+                                            'horde.error');
+            }
+            $details['oldroomno'] = $oldroomno;
+        }
+
+        $shout->storage->saveConference($account, $roomno, $details);
+    }
+
+}
+
+class ConferenceDeleteForm extends Horde_Form
+{
+    function __construct(&$vars)
+    {
+        $devid = $vars->get('devid');
+        $account = $vars->get('account');
+
+        $title = _("FIXME Delete Device %s - Account: %s");
+        $title = sprintf($title, $devid, $_SESSION['shout']['accounts'][$account]);
+        parent::__construct($vars, $title);
+
+        $this->addHidden('', 'account', 'text', true);
+        $this->addHidden('', 'devid', 'text', true);
+        $this->addHidden('', 'action', 'text', true);
+        $this->setButtons(array(_("Delete"), _("Cancel")));
+    }
+
+    function execute()
+    {
+        $shout = $GLOBALS['registry']->getApiInstance('shout', 'application');
+        $account = $this->_vars->get('account');
+        $devid = $this->_vars->get('devid');
+        $shout->devices->deleteDevice($account, $devid);
+    }
+}
\ No newline at end of file
diff --git a/shout/templates/conferences/edit.inc b/shout/templates/conferences/edit.inc
new file mode 100644 (file)
index 0000000..28410e8
--- /dev/null
@@ -0,0 +1,2 @@
+<?php
+$Form->renderActive($RENDERER, $vars, Horde::applicationUrl('conferences.php'), 'post');
index 151a228..6d02315 100644 (file)
             <td class="uheader">Options</td>
         </tr>
         <?php
+            $url = Horde::applicationUrl("conferences.php");
             $editurl = Horde_Util::addParameter($url, 'action', 'edit');
             $deleteurl = Horde_Util::addParameter($url, 'action', 'delete');
             foreach ($conferences as $roomno => $info) {
-
-                $url = Horde::applicationUrl("conferences.php");
-                $url = Horde_Util::addParameter($url,
+                $editurl = Horde_Util::addParameter($editurl,
                     array(
                         'roomno' => $roomno,
                     )
                 );
-
-        ?>
-        <tr class="item">
-            <td>
-                <?php echo Horde::link($editurl); echo $info['name']; ?></a>
-            </td>
-            <td>
-                <?php echo $roomno; ?>
-            </td>
-            <td>
-                <?php echo $info['pin']; ?>
-            </td>
-            <td>
-                <?php echo Horde::link($editurl); echo $info['options']; ?></a>
-            </td>
-        </tr>
-        <?php
+                ?>
+                <tr class="item">
+                    <td>
+                        <?php echo Horde::link($editurl); echo $info['name']; echo '</a>'; ?>
+                    </td>
+                    <td>
+                        <?php echo Horde::link($editurl); echo $roomno; echo '</a>'; ?>
+                    </td>
+                    <td>
+                        <?php echo $info['pin']; ?>
+                    </td>
+                    <td>
+                        <?php echo $info['options']; ?>
+                    </td>
+                </tr>
+                <?php
             }
         ?>
     </table>