Page 1 of 1

Setting multiple config values from one select menu

Posted: Fri Mar 03, 2006 5:06 pm
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.

Re: Setting multiple config values from one select menu

Posted: Sun Mar 05, 2006 8:07 pm
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