Clean up activesync admin/prefs scripts, particularly javascript
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 4 Aug 2010 22:30:11 +0000 (16:30 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 6 Aug 2010 18:10:00 +0000 (12:10 -0600)
horde/admin/activesync.php
horde/js/activesyncadmin.js
horde/js/activesyncprefs.js
horde/lib/Prefs/Ui.php
horde/templates/prefs/activesync.html

index e1a93c5..220091f 100644 (file)
@@ -1,27 +1,31 @@
 <?php
 /**
- * Administrative management of activesync devices.
+ * Administrative management of ActiveSync devices.
  *
  * Copyright 1999-2010 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @author   Michael J. Rubinsky <mrubinsk@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Horde
  */
 
 require_once dirname(__FILE__) . '/../lib/Application.php';
 Horde_Registry::appInit('horde', array('admin' => true));
 
-if (!empty($conf['activesync']['enabled'])) {
-    $state_params = $conf['activesync']['state']['params'];
-    $state_params['db'] = $injector->getInstance('Horde_Db_Adapter_Base');
-    $stateMachine = new Horde_ActiveSync_State_History($state_params);
-    $stateMachine->setLogger($injector->getInstance('Horde_Log_Logger'));
-} else {
+if (empty($conf['activesync']['enabled'])) {
     throw new Horde_Exception_PermissionDenied(_("ActiveSync not activated."));
 }
 
+$state_params = array_merge($conf['activesync']['state']['params'], array(
+    'db' => $injector->getInstance('Horde_Db_Adapter_Base')
+));
+$stateMachine = new Horde_ActiveSync_State_History($state_params);
+$stateMachine->setLogger($injector->getInstance('Horde_Log_Logger'));
+
 /** Check for any actions **/
 if ($actionID = Horde_Util::getPost('actionID')) {
     $deviceID = Horde_Util::getPost('deviceID');
@@ -50,18 +54,31 @@ if ($actionID = Horde_Util::getPost('actionID')) {
     Horde::selfUrl()->redirect();
 }
 
-Horde::addScriptFile('activesyncadmin.js');
 $devices = $stateMachine->listDevices();
+$js = array();
+foreach ($devices as $key => $val) {
+    $js[$key] = array(
+        'id' => $val['device_id'],
+        'user' => $val['device_user']
+    );
+}
+
+Horde::addScriptFile('activesyncadmin.js');
+Horde::addInlineScript(array(
+    'HordeActiveSyncAdmin.devices = ' . Horde_Serialize::serialize($js, Horde_Serialize::JSON, $registry->getCharset())
+));
 
 $title = _("ActiveSync Device Administration");
 require HORDE_TEMPLATES . '/common-header.inc';
 require HORDE_TEMPLATES . '/admin/menu.inc';
+
 ?>
-<form name="activesyncadmin" action="<?php echo Horde::selfUrl()?>" method="post">
+<form id="activesyncadmin" name="activesyncadmin" action="<?php echo Horde::selfUrl()?>" method="post">
 <input type="hidden" name="actionID" id="actionID" />
 <input type="hidden" name="deviceID" id="deviceID" />
 <input type="hidden" name="uid" id="uid" />
 <?php
+
 $spacer = '&nbsp;&nbsp;&nbsp;&nbsp;';
 $icondir = array('icondir' => Horde_Themes::img());
 $base_node_params = $icondir + array('icon' => 'administration.png');
@@ -93,15 +110,8 @@ $tree->addNode('root',
                $base_node_params,
                array('--', $spacer, '--', $spacer, '--', $spacer, '--', $spacer, '<input class="button" type="button" value="' . _("Reprovision All Devices") . '" id="reset" />' ));
 
-/* To hold the inline javascript */
-$js = array();
-$i = 0;
-
-/* Observe the reprovision button */
-$js[] = '$("reset").observe("click", function() {HordeActiveSyncAdmin.reprovision();});';
-
 /* Build the device entry */
-foreach ($devices as $device) {
+foreach ($devices as $key => $device) {
     $node_params = array();
     if (array_search($device['device_user'], $users) === false) {
         $users[] = $device['device_user'];
@@ -117,11 +127,14 @@ foreach ($devices as $device) {
         $status = '<span class="notice">' . _("Wipe is pending") . '</span>';
         $device['ispending'] = true;
         break;
+
     case Horde_ActiveSync::RWSTATUS_WIPED:
         $status = '<span class="notice">' . _("Device is wiped") . '</span>';
         break;
+
     default:
         $status = $device['device_policykey'] ?_("Provisioned") : _("Not Provisioned");
+        break;
     }
 
     /* Last sync time */
@@ -130,15 +143,11 @@ foreach ($devices as $device) {
     /* Build the action links */
     $actions = '';
     if ($device['device_policykey']) {
-        $actions .= '<input class="button" type="button" value="' . _("Wipe") . '" id="wipe' . $i . '" />';
-        $js[] = '$("wipe' . $i . '").observe("click", function() {HordeActiveSyncAdmin.requestRemoteWipe("' . $device['device_id'] . '");});';
+        $actions .= '<input class="button" type="button" value="' . _("Wipe") . '" id="wipe_' . $key . '" . />';
     } elseif ($device['device_rwstatus'] == Horde_ActiveSync::RWSTATUS_PENDING) {
-        $actions .= '<input class="button" type="button" value="' . _("Cancel Wipe") . '" id="cancel' . $i . '" />';
-        $js[] = '$("cancel' . $i . '").observe("click", function() {HordeActiveSyncAdmin.cancelRemoteWipe("' . $device['device_id'] . '");});';
+        $actions .= '<input class="button" type="button" value="' . _("Cancel Wipe") . '" id="cancel_' . $key . '" />';
     }
-    $i++;
-    $actions .= '&nbsp;<input class="button" type="button" value="' . _("Remove") . '" id="delete' . $i . '" />';
-    $js[] = '$("delete' . $i . '").observe("click", function() {HordeActiveSyncAdmin.removeDevice("' . $device['device_id'] . '", "' . $device['device_user'] . '");});';
+    $actions .= '&nbsp;<input class="button removeDevice" type="button" value="' . _("Remove") . '" id="remove_' . $key . '" />';
 
     /* Add it */
     $tree->addNode($device['device_id'],
@@ -153,5 +162,4 @@ foreach ($devices as $device) {
 echo '<h1 class="header">' . Horde::img('group.png') . ' ' . _("ActiveSync Devices") . '</h1>';
 $tree->renderTree();
 echo '</form>';
-Horde::addInlineScript($js, 'load');
 require HORDE_TEMPLATES . '/common-footer.inc';
index 8102617..b82eee2 100644 (file)
@@ -1,33 +1,51 @@
 /**
- * Provides the javascript for managing activesync partner devices.
+ * Provides the javascript for administering ActiveSync partner devices.
  *
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  */
 var HordeActiveSyncAdmin = {
 
-    requestRemoteWipe: function(device) {
-        document.forms.activesyncadmin.deviceID.value = device;
-        document.forms.activesyncadmin.actionID.value = 'wipe';
-        document.forms.activesyncadmin.submit();
-    },
+    // Set in admin/activesync.php: devices
 
-    cancelRemoteWipe: function(device) {
-        document.forms.activesyncadmin.deviceID.value = device;
-        document.forms.activesyncadmin.actionID.value = 'cancelwipe';
-        document.forms.activesyncadmin.submit();
-    },
+    clickHandler: function(e)
+    {
+        var elt = e.element(),
+            id = elt.readAttribute('id') || '';
+
+        switch (id) {
+        case 'reset':
+            $('actionID').setValue('reset');
+            $('activesyncadmin').submit();
+            e.stop();
+            break;
 
-    removeDevice: function(device, user) {
-        document.forms.activesyncadmin.deviceID.value = device;
-        document.forms.activesyncadmin.uid.value = user;
-        document.forms.activesyncadmin.actionID.value = 'delete';
-        document.forms.activesyncadmin.submit();
+        default:
+            if (id.startsWith('wipe_')) {
+                $('deviceID').setValue(this.devices[id.substr(5)].id);
+                $('actionID').setValue('wipe');
+                $('activesyncadmin').submit();
+                e.stop();
+            } else if (id.startsWith('cancel_')) {
+                $('deviceID').setValue(this.devices[id.substr(7)].id);
+                $('actionID').setValue('cancelwipe');
+                $('activesyncadmin').submit();
+                e.stop();
+            } else if (id.startsWith('remove_')) {
+                $('deviceID').setValue(this.devices[id.substr(7)].id);
+                $('actionID').setValue('delete');
+                $('uid').setValue(this.devices[id.substr(7)].user);
+                $('activesyncadmin').submit();
+                e.stop();
+            }
+            break;
+        }
     },
 
-    reprovision: function() {
-        document.forms.activesyncadmin.actionID.value = 'reset';
-        document.forms.activesyncadmin.submit();
+    onDomLoad: function()
+    {
+        $('activesyncadmin').observe('click', this.clickHandler.bindAsEventListener(this));
     }
+}
 
-}
\ No newline at end of file
+document.observe('dom:loaded', HordeActiveSyncAdmin.onDomLoad.bind(HordeActiveSyncAdmin));
index 0b06328..97e048c 100644 (file)
@@ -1,26 +1,40 @@
 /**
- * Provides the javascript for managing activesync partner devices.
+ * Provides the javascript for managing ActiveSync partner devices.
  *
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  */
 var HordeActiveSyncPrefs = {
 
-    requestRemoteWipe: function(device) {
-        $('wipeid').setValue(device);
-        document.forms.prefs.actionID = 'update_special';
-        document.forms.prefs.submit();
-    },
+    // Set in lib/Prefs/Ui.php: devices
+
+    clickHandler: function(e)
+    {
+        var elt = e.element(),
+            id = elt.readAttribute('id') || '';
 
-    cancelRemoteWipe: function(device) {
-        $('cancelwipe').setValue(device);
-        document.forms.prefs.actionID = 'update_special';
-        document.forms.prefs.submit();
+        if (id.startsWith('wipe_')) {
+            $('wipeid').setValue(this.devices[id.substr(5)].id);
+            $('actionID').setValue('update_special');
+            $('prefs').submit();
+            e.stop();
+        } else if (id.startsWith('cancel_')) {
+            $('cancelwipe').setValue(this.devices[id.substr(7)].id);
+            $('actionID').setValue('update_special');
+            $('prefs').submit();
+            e.stop();
+        } else if (id.startsWith('remove_')) {
+            $('removedevice').setValue(this.devices[id.substr(7)].id);
+            $('actionID').setValue('update_special');
+            $('prefs').submit();
+            e.stop();
+        }
     },
 
-    removeDevice: function(device) {
-        $('removedevice').setValue(device);
-        document.forms.prefs.actionID = 'update_special';
-        document.forms.prefs.submit();
+    onDomLoad: function()
+    {
+        $('prefs').observe('click', this.clickHandler.bindAsEventListener(this));
     }
-}
\ No newline at end of file
+}
+
+document.observe('dom:loaded', HordeActiveSyncPrefs.onDomLoad.bind(HordeActiveSyncPrefs));
index 0e2585a..56fcd79 100644 (file)
@@ -410,40 +410,54 @@ class Horde_Prefs_Ui
      */
     protected function _activesyncManagement($ui)
     {
-        if (!empty($GLOBALS['conf']['activesync']['enabled'])) {
-            $state_params = $GLOBALS['conf']['activesync']['state']['params'];
-            $state_params['db'] = $GLOBALS['injector']->getInstance('Horde_Db_Adapter_Base');
-            $stateMachine = new Horde_ActiveSync_State_History($state_params);
-        } else {
+        if (empty($GLOBALS['conf']['activesync']['enabled'])) {
             return _("ActiveSync not activated.");
         }
+
+        $state_params = array_merge($GLOBALS['conf']['activesync']['state']['params'], array(
+            'db' => $GLOBALS['injector']->getInstance('Horde_Db_Adapter_Base')
+        ));
+        $stateMachine = new Horde_ActiveSync_State_History($state_params);
+        $devices = $stateMachine->listDevices($GLOBALS['registry']->getAuth());
+
         Horde::addScriptFile('activesyncprefs.js', 'horde');
+
         $t = $GLOBALS['injector']->createInstance('Horde_Template');
         $t->setOption('gettext', true);
+
         $selfurl = $ui->selfUrl();
         $t->set('reset', $selfurl->copy()->add('reset', 1));
-        $devices = $stateMachine->listDevices($GLOBALS['registry']->getAuth());
         $devs = array();
-        $i = 1;
-        foreach ($devices as $device) {
-            $device['class'] = fmod($i++, 2) ? 'rowOdd' : 'rowEven';
+
+        foreach ($devices as $key => $device) {
+            $device['class'] = fmod($key, 2) ? 'rowOdd' : 'rowEven';
+            $device['key'] = $key;
+
             $stateMachine->loadDeviceInfo($device['device_id'], $GLOBALS['registry']->getAuth());
             $ts = $stateMachine->getLastSyncTimestamp();
             $device['ts'] = empty($ts) ? _("None") : strftime($GLOBALS['prefs']->getValue('date_format') . ' %H:%M', $ts);
+
             switch ($device['device_rwstatus']) {
             case Horde_ActiveSync::RWSTATUS_PENDING:
                 $status = '<span class="notice">' . _("Wipe is pending") . '</span>';
                 $device['ispending'] = true;
                 break;
+
             case Horde_ActiveSync::RWSTATUS_WIPED:
                 $status = '<span class="notice">' . _("Device is wiped") . '</span>';
                 break;
+
             default:
-                $status = $device['device_policykey'] ?_("Provisioned") : _("Not Provisioned");
+                $status = $device['device_policykey']
+                    ? _("Provisioned")
+                    : _("Not Provisioned");
+                break;
             }
-            $device['wipe'] = $selfurl->copy()->add(array('wipe' => $device['device_id']));
-            $device['remove'] = $selfurl->copy()->add(array('remove' => $device['device_id']));
+
+            $device['wipe'] = $selfurl->copy()->add('wipe', $device['device_id']);
+            $device['remove'] = $selfurl->copy()->add('remove', $device['device_id']);
             $device['status'] = $status . '<br />' . _("Device id:") . $device['device_id'] . '<br />' . _("Policy Key:") . $device['device_policykey'] . '<br />' . _("User Agent:") . $device['device_agent'];
+
             $devs[] = $device;
         }
 
index 9457767..e5b9ec6 100644 (file)
@@ -1,7 +1,12 @@
 <div class="smallheader">
  <gettext>State Management</gettext>
 </div>
-<p><gettext>Reset all device state. This will cause your devices to resyncronize all items.</gettext><input class="button" type="submit" value="Reset" name="reset" />
+
+<p>
+ <gettext>Reset all device state. This will cause your devices to resyncronize all items.</gettext>
+ <input class="button" type="submit" value="Reset" name="reset" />
+</p>
+
 <div class="smallheader">
  <gettext>Device Management</gettext>
 </div>
  <tr class="<tag:devices.class />">
   <td>
    <if:devices.device_policykey>
-    <input class="button" type="button" value="<gettext>Wipe</gettext>" onclick="HordeActiveSyncPrefs.requestRemoteWipe('<tag:devices.device_id />');" />
+    <input class="button" type="button" value="<gettext>Wipe</gettext>" id="wipe_<tag:devices.key />" />
    </if:devices.device_policykey>
    <if:devices.ispending>
-    <input class="button" type="button" value="<gettext>Cancel Wipe</gettext>" onclick="HordeActiveSyncPrefs.cancelRemoteWipe('<tag:devices.device_id />');" />
+    <input class="button" type="button" value="<gettext>Cancel Wipe</gettext>" id="cancel_<tag:devices.key />" />
    </if:devices.ispending>
-   <input class="button" type="button" value="<gettext>Remove</gettext>" onclick="HordeActiveSyncPrefs.removeDevice('<tag:devices.device_id />', '<tag:devices.device_user />');" />
+   <input class="button" type="button" value="<gettext>Remove</gettext>" id="remove_<tag:devices.key />" />
   </td>
   <td><tag:devices.device_type /></td>
   <td><tag:devices.ts /></td>
@@ -38,4 +43,7 @@
  <em><gettext>None</gettext></em>
 </p>
 </else:devices></if:devices>
-<p><strong>NOTE: WIPING A DEVICE MAY RESET IT TO FACTORY DEFAULTS. PLEASE MAKE SURE YOU REALLY WANT TO DO THIS BEFORE REQUESTING A WIPE</strong></p>
+
+<p>
+ <strong>NOTE: WIPING A DEVICE MAY RESET IT TO FACTORY DEFAULTS. PLEASE MAKE SURE YOU REALLY WANT TO DO THIS BEFORE REQUESTING A WIPE</strong>
+</p>