Move alarms handling into prefs ui widgets
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 14:51:54 +0000 (08:51 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Apr 2010 14:51:54 +0000 (08:51 -0600)
framework/Core/lib/Horde/Core/Prefs/Ui.php
framework/Core/lib/Horde/Core/Prefs/Ui/Widgets.php
horde/config/prefs.php.dist
kronolith/config/prefs.php.dist
kronolith/lib/Application.php
nag/config/prefs.php.dist
nag/lib/Application.php

index 82024c0..06d1a3c 100644 (file)
@@ -198,31 +198,6 @@ class Horde_Core_Prefs_Ui
         /* Run through the action handlers */
         foreach ($preflist as $pref) {
             switch ($this->prefs[$pref]['type']) {
-            case 'alarm':
-                $methods = Horde_Alarm::notificationMethods();
-                $val = (isset($this->vars->$pref) && is_array($this->vars->$pref))
-                    ? $this->vars->$pref
-                    : array();
-                $value = array();
-
-                foreach ($val as $method) {
-                    $value[$method] = array();
-                    if (!empty($methods[$method])) {
-                        foreach (array_keys($methods[$method]) as $param) {
-                            $value[$method][$param] = $this->vars->get($pref . '_' . $param, '');
-                            if (is_array($methods[$method][$param]) &&
-                                $methods[$method][$param]['required'] &&
-                                ($value[$method][$param] === '')) {
-                                $notification->push(sprintf(_("You must provide a setting for \"%s\"."), $methods[$method][$param]['desc']), 'horde.error');
-                                break 3;
-                            }
-                        }
-                    }
-                }
-
-                $updated |= $prefs->setValue($pref, serialize($value));
-                break;
-
             case 'checkbox':
                 $updated |= $prefs->setValue($pref, intval(isset($this->vars->$pref)));
                 break;
@@ -380,82 +355,6 @@ class Horde_Core_Prefs_Ui
 
                 $type = $this->prefs[$pref]['type'];
                 switch ($type) {
-                case 'alarm':
-                    Horde::addScriptFile('alarmprefs.js', 'horde');
-                    Horde::addInlineScript(array(
-                        'HordeAlarmPrefs.pref = ' . Horde_Serialize::serialize($pref, Horde_Serialize::JSON)
-                    ));
-
-                    $alarm_pref = unserialize($prefs->getValue($pref));
-                    $selected = array_keys($alarm_pref);
-
-                    $param_list = $select_list = array();
-                    foreach (Horde_Alarm::notificationMethods() as $method => $params) {
-                        $select_list[] = array(
-                            'l' => $params['__desc'],
-                            's' => in_array($method, $selected),
-                            'v' => $method
-                        );
-
-                        if (count($params > 1)) {
-                            $tmp = array(
-                                'method' => $method,
-                                'param' => array()
-                            );
-
-                            foreach ($params as $name => $param) {
-                                if (substr($name, 0, 2) == '__') {
-                                    continue;
-                                }
-
-                                switch ($param['type']) {
-                                case 'text':
-                                    $tmp['param'][] = array(
-                                        'label' => Horde::label($pref . '_' . $name, $param['desc']),
-                                        'name' => $pref . '_' . $name,
-                                        'text' => true,
-                                        'value' => empty($alarm_pref[$method][$name]) ? '' : htmlspecialchars($alarm_pref[$method][$name])
-                                    );
-                                    break;
-
-                                case 'bool':
-                                    $tmp['param'][] = array(
-                                        'bool' => true,
-                                        'checked' => !empty($alarm_pref[$method][$name]),
-                                        'label' => Horde::label($pref . '_' . $name, $param['desc']),
-                                        'name' => $pref . '_' . $name
-                                    );
-                                    break;
-
-                                case 'sound':
-                                    $current_sound = empty($alarm_pref[$method][$name])
-                                        ? ''
-                                        : $alarm_pref[$method][$name];
-                                    $sounds = array();
-                                    foreach (Horde_Themes::soundList() as $key => $val) {
-                                        $sounds[] = array(
-                                            'c' => ($current_sound == $key),
-                                            'uri' => htmlspecialchars($val->uri),
-                                            'val' => htmlspecialchars($key)
-                                        );
-                                    }
-
-                                    $tmp['param'][] = array(
-                                        'sound' => true,
-                                        'checked' => !$current_sound,
-                                        'name' => $pref . '_' . $name
-                                    );
-                                    break;
-                                }
-                            }
-
-                            $param_list[] = $tmp;
-                        }
-                    }
-                    $t->set('param_list', $param_list);
-                    $t->set('select_list', $select_list);
-                    break;
-
                 case 'checkbox':
                     $t->set('checked', $prefs->getValue($pref));
                     break;
index 2efd02a..e05f350 100644 (file)
@@ -220,4 +220,152 @@ class Horde_Core_Prefs_Ui_Widgets
         return $out . $t->fetch(HORDE_TEMPLATES . '/prefs/addressbooks.html');
     }
 
+    /* Alarms selection widget. */
+
+    /**
+     * Code to run on init for alarms selection.
+     */
+    static public function alarmInit()
+    {
+        Horde::addScriptFile('alarmprefs.js', 'horde');
+    }
+
+    /**
+     * Create code needed for alarm selection.
+     *
+     * @param array $data  Data items:
+     * <pre>
+     * 'helplink' - (string) [OPTIONAL] Help link.
+     * 'label' - (string) Label.
+     * 'pref' - (string) Preference name.
+     * </pre>
+     *
+     * @return string  HTML UI code.
+     */
+    static public function alarm($data)
+    {
+        $pref = $data['pref'];
+
+        Horde::addInlineScript(array(
+            'HordeAlarmPrefs.pref = ' . Horde_Serialize::serialize($pref, Horde_Serialize::JSON)
+        ));
+
+        $alarm_pref = unserialize($prefs->getValue($pref));
+        $selected = array_keys($alarm_pref);
+
+        $param_list = $select_list = array();
+        foreach (Horde_Alarm::notificationMethods() as $method => $params) {
+            $select_list[] = array(
+                'l' => $params['__desc'],
+                's' => in_array($method, $selected),
+                'v' => $method
+            );
+
+            if (count($params > 1)) {
+                $tmp = array(
+                    'method' => $method,
+                    'param' => array()
+                );
+
+                foreach ($params as $name => $param) {
+                    if (substr($name, 0, 2) == '__') {
+                        continue;
+                    }
+
+                    switch ($param['type']) {
+                    case 'text':
+                        $tmp['param'][] = array(
+                            'label' => Horde::label($pref . '_' . $name, $param['desc']),
+                            'name' => $pref . '_' . $name,
+                            'text' => true,
+                            'value' => empty($alarm_pref[$method][$name]) ? '' : htmlspecialchars($alarm_pref[$method][$name])
+                            );
+                        break;
+
+                    case 'bool':
+                        $tmp['param'][] = array(
+                            'bool' => true,
+                            'checked' => !empty($alarm_pref[$method][$name]),
+                            'label' => Horde::label($pref . '_' . $name, $param['desc']),
+                            'name' => $pref . '_' . $name
+                        );
+                        break;
+
+                    case 'sound':
+                        $current_sound = empty($alarm_pref[$method][$name])
+                            ? ''
+                            : $alarm_pref[$method][$name];
+                        $sounds = array();
+                        foreach (Horde_Themes::soundList() as $key => $val) {
+                            $sounds[] = array(
+                                'c' => ($current_sound == $key),
+                                'uri' => htmlspecialchars($val->uri),
+                                'val' => htmlspecialchars($key)
+                            );
+                        }
+
+                        $tmp['param'][] = array(
+                            'sound' => true,
+                            'checked' => !$current_sound,
+                            'name' => $pref . '_' . $name
+                        );
+                        break;
+                    }
+                }
+
+                $param_list[] = $tmp;
+            }
+        }
+
+        $base = $GLOBALS['injector']->createInstance('Horde_Template');
+        $base->setOption('gettext', true);
+
+        $t->set('desc', Horde::label($pref, $data['label']));
+        if (!empty($data['helplink'])) {
+            $t->set('helplink', $data['helplink']);
+        }
+        $t->set('pref', htmlspecialchars($pref));
+        $t->set('param_list', $param_list);
+        $t->set('select_list', $select_list);
+
+        return $t->fetch(HORDE_TEMPLATES . '/prefs/alarm.html');
+    }
+
+    /**
+     * Process form data.
+     *
+     * @param array $data  Data items:
+     * <pre>
+     * 'pref' - (string) Preference name.
+     * </pre>
+     *
+     * @return array  TODO
+     */
+    static public function alarmUpdate($ui, $data)
+    {
+        $pref = $data['pref'];
+        $methods = Horde_Alarm::notificationMethods();
+        $val = (isset($ui->vars->$pref) && is_array($ui->vars->$pref))
+            ? $ui->vars->$pref
+            : array();
+        $value = array();
+
+        foreach ($val as $method) {
+            $value[$method] = array();
+            if (!empty($methods[$method])) {
+                foreach (array_keys($methods[$method]) as $param) {
+                    $value[$method][$param] = $ui->vars->get($pref . '_' . $param, '');
+                    if (is_array($methods[$method][$param]) &&
+                        $methods[$method][$param]['required'] &&
+                        ($value[$method][$param] === '')) {
+                        $GLOBALS['notification']->push(sprintf(_("You must provide a setting for \"%s\"."), $methods[$method][$param]['desc']), 'horde.error');
+                        return null;
+                    }
+                }
+            }
+        }
+
+        return $value;
+    }
+
 }
index ace8408..60f1b3e 100644 (file)
  * following is the list of types, with a description of further keys used
  * for each type.
  *
- * 'alarm'
- * -------
- * TODO
- *
  * 'checkbox'
  * ----------
  * Provides a checkbox (yes/no) entry.
index 587e74c..1651c99 100644 (file)
@@ -47,7 +47,7 @@ $prefGroups['notification'] = array(
     'desc' => _("Choose how you want to be notified about event changes, event alarms and upcoming events."),
     'members' => array(
         'event_notification', 'event_notification_exclude_self',
-        'daily_agenda', 'event_reminder', 'event_alarms'
+        'daily_agenda', 'event_reminder', 'event_alarms_select'
     )
 );
 
@@ -288,10 +288,12 @@ $_prefs['event_reminder'] = array(
 );
 
 // alarm methods
+$_prefs['event_alarms_select'] = array(
+    'type' => 'special'
+);
+
 $_prefs['event_alarms'] = array(
-    'value' => 'a:1:{s:6:"notify";a:0:{}}',
-    'type' => 'alarm',
-    'desc' => _("Choose how you want to receive reminders for events with alarms:")
+    'value' => 'a:1:{s:6:"notify";a:0:{}}'
 );
 
 // number of days to generate Free/Busy information for:
index 15cde7d..e3b3303 100644 (file)
@@ -136,8 +136,12 @@ class Kronolith_Application extends Horde_Registry_Application
             break;
 
         case 'notification':
-            if (empty($conf['alarms']['driver'])) {
+            if (empty($conf['alarms']['driver']) ||
+                $prefs->isLocked('event_alarms') ||
+                $prefs->isLocked('event_alarms_select')) {
                 $ui->suppress[]= 'event_alarms';
+            } else {
+                Horde_Core_Prefs_Ui_Widgets::alarminit();
             }
             break;
 
@@ -201,6 +205,12 @@ class Kronolith_Application extends Horde_Registry_Application
         case 'default_alarm_management':
             return $this->_defaultAlarmManagement($ui);
 
+        case 'event_alarms_select':
+            return Horde_Core_Prefs_Ui_Widgets::alarm(array(
+                'label' => _("Choose how you want to receive reminders for events with alarms:"),
+                'pref' => 'event_alarms'
+            ));
+
         case 'sourceselect':
             $search = Kronolith::getAddressbookSearchParams();
             return Horde_Core_Prefs_Ui_Widgets::addressbooks(array(
@@ -227,6 +237,14 @@ class Kronolith_Application extends Horde_Registry_Application
             $GLOBALS['prefs']->setValue('default_alarm', (int)$ui->vars->alarm_value * (int)$ui->vars->alarm_unit);
             return true;
 
+        case 'event_alarms_select':
+            $data = Horde_Core_Prefs_Ui_Widgets::alarmUpdate($ui, array('pref' => 'event_alarms'));
+            if (!is_null($data)) {
+                $GLOBALS['prefs']->setValue('event_alarms', serialize($data));
+                return true;
+            }
+            break;
+
         case 'remote_cal_management':
             return $this->_prefsRemoteCalManagement($ui);
 
index 0f3f66d..36bfb07 100644 (file)
@@ -37,7 +37,7 @@ $prefGroups['notification'] = array(
     'column' => _("Task List and Share Options"),
     'label' => _("Notifications"),
     'desc' => _("Choose if you want to be notified of task changes and task alarms."),
-    'members' => array('task_notification', 'task_alarms'),
+    'members' => array('task_notification', 'task_alarms_select'),
 );
 
 $prefGroups['external'] = array(
@@ -155,10 +155,12 @@ $_prefs['task_notification'] = array(
 );
 
 // alarm methods
+$_prefs['task_alarms_select'] = array(
+    'type' => 'special'
+);
+
 $_prefs['task_alarms'] = array(
-    'value' => 'a:1:{s:6:"notify";a:0:{}}',
-    'type' => 'alarm',
-    'desc' => _("Choose how you want to receive reminders for tasks with alarms:"),
+    'value' => 'a:1:{s:6:"notify";a:0:{}}'
 );
 
 // show data from other applications that can be listed as tasks?
index 9a97618..8d26933 100644 (file)
@@ -102,7 +102,9 @@ class Nag_Application extends Horde_Registry_Application
 
         switch ($ui->group) {
         case 'notification':
-            if (empty($conf['alarms']['driver'])) {
+            if (empty($conf['alarms']['driver']) ||
+                $prefs->isLocked('task_alarms') ||
+                $prefs->isLocked('task_alarms_select')) {
                 $ui->suppress[] = 'task_alarms';
             }
             break;
@@ -159,6 +161,50 @@ class Nag_Application extends Horde_Registry_Application
     }
 
     /**
+     * Generate code used to display a special preference.
+     *
+     * @param Horde_Core_Prefs_Ui $ui  The UI object.
+     * @param string $item             The preference name.
+     *
+     * @return string  The HTML code to display on the options page.
+     */
+    public function prefsSpecial($ui, $item)
+    {
+        switch ($item) {
+        case 'task_alarms_select':
+            return Horde_Core_Prefs_Ui_Widgets::alarm(array(
+                'label' => _("Choose how you want to receive reminders for tasks with alarms:"),
+                'pref' => 'task_alarms'
+            ));
+        }
+
+        return '';
+    }
+
+    /**
+     * Special preferences handling on update.
+     *
+     * @param Horde_Core_Prefs_Ui $ui  The UI object.
+     * @param string $item             The preference name.
+     *
+     * @return boolean  True if preference was updated.
+     */
+    public function prefsSpecialUpdate($ui, $item)
+    {
+        switch ($item) {
+        case 'task_alarms_select':
+            $data = Horde_Core_Prefs_Ui_Widgets::alarmUpdate($ui, array('pref' => 'task_alarms'));
+            if (!is_null($data)) {
+                $GLOBALS['prefs']->setValue('task_alarms', serialize($data));
+                return true;
+            }
+            break;
+        }
+
+        return false;
+    }
+
+    /**
      * Generate the menu to use on the prefs page.
      *
      * @return Horde_Menu  A Horde_Menu object.