[Tickets #1395] Add preference data type support for floating point numbers

bugs at bugs.horde.org bugs at bugs.horde.org
Thu Feb 17 18:15:37 PST 2005


DO NOT REPLY TO THIS MESSAGE. THIS EMAIL ADDRESS IS NOT MONITORED.

Ticket URL: http://bugs.horde.org/ticket/?id=1395
-----------------------------------------------------------------------
 Ticket             | 1395
 Updated By         | scott at realorganized.com
 Summary            | Add preference data type support for floating point numbers
 Queue              | Horde Base
 Version            | 3.0.3
 State              | Assigned
 Priority           | 1. Low
 Type               | Enhancement
 Owners             | Horde Developers
-----------------------------------------------------------------------


scott at realorganized.com (2005-02-17 18:15) wrote:

I have a second related feature request.  Currently, you can create UI
gadgets for specific properties within your application. While useful, this
has the disadvantage that the UI gadgets are one-of-a-kind and therefore do
not lend themselves to multiple use of the preerences UI gadget that you
create.  For example, if I created an RGB control, it would be great if I
could simply define a type 'rgbcolor' in such a way that I could use this
gadget and handler for any properties that I like.  I think this promotes
reusability and have a particular need for this in the application that I am
developing as I have several dozen preference settings with some which are
similar to one another.  Instructions for use are in a comment in the code.

Towards this end, I propose the following patch.  It requires two quite
small changes to the horde/lib/horde/prefs/ui.php file.  I have marked the
changes with SDS.

    function generateUI($group = null)
    {
        global $browser, $conf, $prefs, $prefGroups, $_prefs, $registry,
$app;

        /* Check if any options are actually available. */
        if (is_null($prefGroups)) {
            $GLOBALS['notification']->push(_("There are no options
available."), 'horde.message');
        }

        /* Show the header. */
        Prefs_UI::generateHeader($group);

        /* Assign variables to hold select lists. */
        if (!$prefs->isLocked('language')) {
            $GLOBALS['language_options'] = &$GLOBALS['nls']['languages'];
        }

        if (!empty($group) && Prefs_UI::groupIsEditable($group)) {
            foreach ($prefGroups[$group]['members'] as $pref) {
                if (!$prefs->isLocked($pref)) {
                    /* Get the help link. */
                    if (!empty($_prefs[$pref]['help'])) {
                        $helplink =
Help::link(!empty($_prefs[$pref]['shared']) ? 'horde' : $registry->getApp(),
$_prefs[$pref]['help']);
                    } else {
                        $helplink = null;
                    }

                    switch ($_prefs[$pref]['type']) {
                    case 'implicit':
                        break;

                    case 'special':
                        require $registry->get('templates',
!empty($_prefs[$pref]['shared']) ? 'horde' : $registry->getApp()) .
"/prefs/$pref.inc";
                        break;

                    case 'link':	// SDS 2/17/2005
                    case 'select':
                    case 'text':
                    case 'textarea':
                    case 'password':
                    case 'enum':
                    case 'multienum':
                    case 'number':
                    case 'floatnumber':
                    case 'checkbox':
                        require $registry->get('templates', 'horde') .
'/prefs/' . $_prefs[$pref]['type'] . '.inc';
                        break;
                        
                    default:	// SDS 2/17/2005
                    	/* Individual applications can create their own class
of UI handler
                         * just invent your own type.  Then place a file
inside your application
                         * at templates/prefs/yourcustomtype.inc which
contains your html for the control.
                         * Inside of the lib/prefs.php in your application,
add a function
                         * handle_yourcustotype and you are done.  If you
have a shared
                         * preference, this UI gadget will not be available
to other applications.
                         * Differs from 'special' in that you can re-use
same UI widget
                         * in your application's preferences. */
                        $custType = $_prefs[$pref]['type'];
                        require $registry->get('templates',
$registry->getApp()) . "/prefs/$custType.inc";
                        break;
                    }
                }
            }
            require $registry->get('templates', 'horde') .
'/prefs/end.inc';
        } else {
            $columns = array();
            if (is_array($prefGroups)) {
                foreach ($prefGroups as $group => $gvals) {
                    if (Prefs_UI::groupIsEditable($group)) {
                        $col = $gvals['column'];
                        unset($gvals['column']);
                        $columns[$col][$group] = $gvals;
                    }
                }
                $span = round(100 / count($columns));
            } else {
                $span = 100;
            }

            require $registry->get('templates', 'horde') .
'/prefs/overview.inc';
        }
    }

    function handleForm(&$group, &$save)
    {
        global $prefs, $prefGroups, $_prefs, $notification, $registry;

        $updated = false;

        /* Run through the action handlers */
        if (Util::getPost('actionID') == 'update_prefs') {
            if (isset($group) && Prefs_UI::groupIsEditable($group)) {
                $updated = false;

                foreach ($prefGroups[$group]['members'] as $pref) {
                    if (!$prefs->isLocked($pref) ||
                        ($_prefs[$pref]['type'] == 'special')) {
                        switch ($_prefs[$pref]['type']) {

                        /* These either aren't set or are set in other
                         * parts of the UI. */
                        case 'implicit':
                        case 'link':
                            break;

                        case 'select':
                        case 'text':
                        case 'textarea':
                        case 'password':
                            $updated = $updated | $save->setValue($pref,
Util::getPost($pref));
                            break;

                        case 'enum':
                            $val = Util::getPost($pref);
                            if (isset($_prefs[$pref]['enum'][$val])) {
                                $updated = $updated | $save->setValue($pref,
$val);
                            } else {
                                $notification->push(_("An illegal value was
specified."), 'horde.error');
                            }
                            break;

                        case 'multienum':
                            $vals = Util::getPost($pref);
                            $set = array();
                            $invalid = false;
                            if (is_array($vals)) {
                                foreach ($vals as $val) {
                                    if (isset($_prefs[$pref]['enum'][$val]))
{
                                        $set[] = $val;
                                    } else {
                                        $invalid = true;
                                        continue;
                                    }
                                }
                            }

                            if ($invalid) {
                                $notification->push(_("An illegal value was
specified."), 'horde.error');
                            } else {
                                $updated = $updated | $save->setValue($pref,
@serialize($set));
                            }
                            break;

                        case 'number':
                            $num = Util::getPost($pref);
                            if (intval($num) != $num) {
                                $notification->push(_("This value must be a
number."), 'horde.error');
                            } elseif ($num == 0) {
                                $notification->push(_("This number must be
at least one."), 'horde.error');
                            } else {
                                $updated = $updated | $save->setValue($pref,
$num);
                            }
                            break;

                       case 'floatnumber':	// SDS 2/16/2005
                            $num = Util::getPost($pref);
                            if (floatval($num) != $num) {
                                $notification->push(_("This value must be a
number."), 'horde.error');
                            } else {
                                $updated = $updated | $save->setValue($pref,
$num);
                            }
                            break;

                        case 'checkbox':
                            $val = Util::getPost($pref);
                            $updated = $updated | $save->setValue($pref,
isset($val) ? 1 : 0);
                            break;

                        case 'special':
                            /* Code for special elements must be
                             * written specifically for each
                             * application. */
                            if (function_exists('handle_' . $pref)) {
                                $updated = $updated |
call_user_func('handle_' . $pref, $updated);
                            }
                            break;
                            
                        default :	// SDS 2/17/2004
                            /* Individual applications can create their own
class of UI handler
                             * just use the special 'type' that you create
                             * differs from 'special' in that you can use
same UI widget
                             * in several places in your application's
preferences . */
                            if (function_exists('handle_' .
$_prefs[$pref]['type'])) {
                                $updated = $updated |
call_user_func('handle_' . $_prefs[$pref]['type'], $updated, $pref);
                            }
                            break;
                        
                        }
                    }
                }

                if ($updated) {
                    if (function_exists('prefs_callback')) {
                        prefs_callback();
                    }
                    $notification->push(_("Your options have been
updated."), 'horde.message');
                    $group = null;
                }
            }
        }

        return $updated;
    }

    /**
     * Generate the UI for the preferences interface, either for a
     * specific group, or the group selection interface.
     *
     * @access public
     *
     * @param optional string $group  The group to generate the UI for.
     */
    function generateUI($group = null)
    {
        global $browser, $conf, $prefs, $prefGroups, $_prefs, $registry,
$app;

        /* Check if any options are actually available. */
        if (is_null($prefGroups)) {
            $GLOBALS['notification']->push(_("There are no options
available."), 'horde.message');
        }

        /* Show the header. */
        Prefs_UI::generateHeader($group);

        /* Assign variables to hold select lists. */
        if (!$prefs->isLocked('language')) {
            $GLOBALS['language_options'] = &$GLOBALS['nls']['languages'];
        }

        if (!empty($group) && Prefs_UI::groupIsEditable($group)) {
            foreach ($prefGroups[$group]['members'] as $pref) {
                if (!$prefs->isLocked($pref)) {
                    /* Get the help link. */
                    if (!empty($_prefs[$pref]['help'])) {
                        $helplink =
Help::link(!empty($_prefs[$pref]['shared']) ? 'horde' : $registry->getApp(),
$_prefs[$pref]['help']);
                    } else {
                        $helplink = null;
                    }

                    switch ($_prefs[$pref]['type']) {
                    case 'implicit':
                        break;

                    case 'special':
                        require $registry->get('templates',
!empty($_prefs[$pref]['shared']) ? 'horde' : $registry->getApp()) .
"/prefs/$pref.inc";
                        break;

                    case 'link':	// SDS 2/17/2005
                    case 'select':
                    case 'text':
                    case 'textarea':
                    case 'password':
                    case 'enum':
                    case 'multienum':
                    case 'number':
                    case 'floatnumber':
                    case 'checkbox':
                        require $registry->get('templates', 'horde') .
'/prefs/' . $_prefs[$pref]['type'] . '.inc';
                        break;
                        
                    default:	// SDS 2/17/2005
                    	/* Individual applications can create their own class
of UI handler
                         * just invent your own type.  Then place a file
inside your application
                         * at templates/prefs/yourcustotype.inc which
contains your html for the control
                         * inside of the lib/prefs.php in your application,
add a function
                         * handle_yourcustotype an you are done.  Remember -
if you have a shared
                         * preference, your UI will not be available to
other applications.
                         * Differs from 'special' in that you can re-use
same UI widget
                         * in your application's preferences. */
                        $custType = $_prefs[$pref]['type'];
                        require $registry->get('templates',
$registry->getApp()) . "/prefs/$custType.inc";
                        break;
                    }
                }
            }
            require $registry->get('templates', 'horde') .
'/prefs/end.inc';
        } else {
            $columns = array();
            if (is_array($prefGroups)) {
                foreach ($prefGroups as $group => $gvals) {
                    if (Prefs_UI::groupIsEditable($group)) {
                        $col = $gvals['column'];
                        unset($gvals['column']);
                        $columns[$col][$group] = $gvals;
                    }
                }
                $span = round(100 / count($columns));
            } else {
                $span = 100;
            }

            require $registry->get('templates', 'horde') .
'/prefs/overview.inc';
        }
    }







More information about the bugs mailing list