Setting multiple config values from one select menu

Creating and modifying plugins.
Post Reply
Jabrwock
Regular
Posts: 34
Joined: Fri Feb 24, 2006 5:42 pm
Location: Saskatoon, Saskatchewan, Canada
Contact:

Setting multiple config values from one select menu

Post by Jabrwock »

Right now I set one value (type) from a select menu. Another item (id_type) used in the plug-in when it's displayed depends on the 'type' value, but doesn't use the same value (but they do map to each other). Is there any way to set two different things from the same select box?

Ex:

Menu - Type - ID
Scroll - 'scroll' - 's'
Calendar - 'cal' - 'c'
IM Status - 'im' - 'i'

So when I choose "Scroll" from the menu, it puts 'scroll' into type, and 's' into id_type.

Otherwise I have to put a case statement in the "generate content" section.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Setting multiple config values from one select menu

Post by garvinhicking »

Your problem is hard to abstract, so that's not really doable "out of the box".

But the serendipity Plugin API saves configuration values by the "set_config" method defined in the serendipity_plugin class. So your custom plugin can override this set_config method by specifying the method.

By default this method looks like:

Code: Select all

    function set_config($name, $value, $implodekey = '^')
    {
        $name = $this->instance . '/' . $name;

        if (is_array($value)) {
            $dbvalue = implode($implodekey, $value);
            $_POST['serendipity']['plugin'][$name] = $dbvalue;
        } else {
            $dbvalue = $value;
        }

        return serendipity_set_config_var($name, $dbvalue);
    }
and you could implement it in your plugin like this:

Code: Select all

    function set_config($name, $value, $implodekey = '^')
    {
        $name = $this->instance . '/' . $name;

        if (is_array($value)) {
            $dbvalue = implode($implodekey, $value);
            $_POST['serendipity']['plugin'][$name] = $dbvalue;
        } else {
            $dbvalue = $value;
        }

        if ($name == 'type') {
            switch($value) {
                case 'scroll': serendipity_set_config_var('id', 's'); break;
                case 'cal': serendipity_set_config_var('id', 'c'; break;
                case 'im': serendipity_set_config_var('id', 'i'); break;            
            }        
        } 

        return serendipity_set_config_var($name, $dbvalue);
    }
This would then save two config values. As you can see of the way frmo the code, this would be hard to do generically.

However I suggest you to think about if it's really necessary to save two config options.

Instead, a switch in your generate_content() method that sets ID depending on the $type would be better and more generic.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply