theme configurator: number of navbar links

Skinning and designing Serendipity (CSS, HTML, Smarty)
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

theme configurator: number of navbar links

Post by yellowled »

Hey guys,

sorry if this has already been answered somewhere, but I found it pretty difficult to come up with search terms for this.

Here's what I'd like to do: A lot of templates now have navbars, mostly in the header, which in v1.1 can be configured using the fabulous theme configurator. Usually, the template author sets a fixed number of those links, but sometimes users need more or less navbar links. So it would be nice (forget nice, it would be awesome!) if users could actually set the number of navbar links in the admin frontend and then enter text and URL for each link.

Is that possible? Does anybody have a working code example for this?

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!

This could already be done using more advanced PHP code.

The idea is this: The config.inc.php file calls up the serendipity_loadThemeOptions() function to pick the current configuration. Then depending on the output of that, you can check the resultset array on which number of elements the user wanted.

And then you use foreach() to expand the $template_options array with the number of required items.

An example (untested) would be this:

Code: Select all

<?php
$template_config = array(
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => '',
        'type'          => 'string',
        'default'       => '5',
    )
);


$vars = serendipity_loadThemeOptions($template_config);

for ($i = 0; $i <= $vars['amount']; $i++) {
    $template_config[] = array(
        'var'           => 'navlink' . $i,
        'name'          => 'Navlink ' . $i,
        'description'   => '',
        'type'          => 'string',
        'default'       => 'http://',
    );
}
HTH,
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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:An example (untested) would be this:
Now it's tested, and it works! However, a little fix for your code (Gosh, I can't believe I just typed that ... :wink:): I've set the initial value for $i to 1 instead of 0 (see code block below), since if it's set to 0, the foreach creates i+1 navlinks.

Also, there's a little problem which probably isn't very user-friendly: If the number of navlinks is changed, the Theme/Style options have to be reloaded to show the new setting. I'm no coder, but I guess it would need some AJAX stuff like in the new plugin configuration to do this dynamically ..?

Oh, and a follow-up question: How do I integrate other stuff into this like a) the text for the navlink and b) stuff for a 'single' configuration option (i.e. one that needs to be put out just once, I hope you understand what I mean ...)?

I tried to figure out a) by myself and came up with this code:

Code: Select all

$template_config = array(
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => '',
        'type'          => 'string',
        'default'       => '5',
    )
);

$vars = serendipity_loadThemeOptions($template_config);

for ($i = 1; $i <= $vars['amount']; $i++) {
    $template_config[] = array(
    array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . $i,
        'description'   => NAV_LINK_DESC,
        'type'          => 'string',
        'default'       => NAV_DEFAULT_ . $i,
    ),
    array(
        'var'           => 'navlink' . $i,
        'name'          => 'Navlink ' . $i,
        'description'   => ' ',
        'type'          => 'string',
        'default'       => '#',
    );
}
but it doesn't work and I don't understand why not :cry: :wink:
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!
Now it's tested, and it works! However, a little fix for your code (Gosh, I can't believe I just typed that ... :wink:): I've set the initial value for $i to 1 instead of 0 (see code block below), since if it's set to 0, the foreach creates i+1 navlinks.
Yeah, that should be fine. You could have also used "<" instead of "<=" in the lop. :)
Also, there's a little problem which probably isn't very user-friendly: If the number of navlinks is changed, the Theme/Style options have to be reloaded to show the new setting. I'm no coder, but I guess it would need some AJAX stuff like in the new plugin configuration to do this dynamically ..?
Yes, this definitely requires Ajax. You can emit custom HTML using your config.inc.php for the panel, but the ajax stuff creates more overhead and things to do than the whole stuff is really worth. So IMHO it's not so bad to make the user hit the save button if he changes the amount of navlinks...
Oh, and a follow-up question: How do I integrate other stuff into this like a) the text for the navlink and b) stuff for a 'single' configuration option (i.e. one that needs to be put out just once, I hope you understand what I mean ...)?
Try this:

Code: Select all

$template_config = array(
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => '',
        'type'          => 'string',
        'default'       => '5',
    ),
    array(
        'var'           => 'anyothervar',
        'name'          => 'Any other var that will not be looped',
        'description'   => 'blabla',
        'type'          => 'string',
        'default'       => '5',
    )
);

$vars = serendipity_loadThemeOptions($template_config);

for ($i = 0; $i < $vars['amount']; $i++) {
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . $i,
        'description'   => NAV_LINK_DESC,
        'type'          => 'string',
        'default'       => NAV_DEFAULT_ . $i
    );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . $i,
        'description'   => NAV_LINK_DESC,
        'type'          => 'string',
        'default'       => NAV_DEFAULT_ . $i
    );
}
Your problem simply was that you created another level of arrays, which is not so good, because then the structure is no longer what s9y requires it to be. :-)

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

I'll skip the part in which I promise you my first-born etc. :wink:
garvinhicking wrote:So IMHO it's not so bad to make the user hit the save button if he changes the amount of navlinks...
You're right. Well, actually the user has to hit save and reload the 'Manage styles' page, but let's just assume that s9y users are smart enough to do that and not overload this :)
garvinhicking wrote:Try this:
I did. Didn't work right out of the box (I think you had some copy & paste error in there, but I'll forgive you :wink:), but I think I fixed it, except for one tiny little thing. Let's see the code first (this works), the single example is cocomment support, plus I switched the order to keep that navbar stuff together:

Code: Select all

$template_config = array(
    array(
        'var'           => 'cocommentactive',
        'name'          => COCOMMENT_ACTIVE,
        'description'   => COCOMMENT_ACTIVE_DESC,
        'type'          => 'select',
        'default'       => 'inactive',
        'select_values' => array('active' => USE_COCOMMENT,
                                 'inactive' => NO_COCOMMENT)
    ),
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => 'Enter the number of navlinks you want to use in the navbar.',
        'type'          => 'string',
        'default'       => '5',
    )
);

$vars = serendipity_loadThemeOptions($template_config);

for ($i = 0; $i < $vars['amount']; $i++) {
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . ' #' . $i,
        'description'   => NAV_LINK_DESC . ' #' .$i,
        'type'          => 'string',
        'default'       => NAV_DEFAULT_ . $i,
	);
    $template_config[] = array(
        'var'           => 'navlink' . $i,
        'name'          => NAV_LINK_URL . ' #' . $i,
        'description'   => NAV_LINK_URL_DESC . ' #' . $i,
        'type'          => 'string',
        'default'       => '#',
    );
}
The part that isn't working (yet, I hope): the default values for NAV_LINK_TEXT, which are NAV_DEFAULT_1 to NAV_DEFAULT_5 (all of them @define-d in lang_en.inc.php and lang_de.inc.php) are not, well, evaluated obviously. Which means the default value is set to NAV_DEFAULT_1 instead of the value of NAV_DEFAULT_1. I hope you understand what I mean ... also, I just found out that when I change this to i.e. 'Index', the navbar link in question is still labeled 'NAV_DEFAULT_1', so obviously there is something wrong here ...

Can we by the way use arrays in lang_XY.inc.php? Maybe that would make it easier ..? (Just a non-coders thought ...)

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!
YellowLed wrote:I'll skip the part in which I promise you my first-born etc. :wink:
A great name for a first-born! ;)
The part that isn't working (yet, I hope): the default values for NAV_LINK_TEXT, which are NAV_DEFAULT_1 to NAV_DEFAULT_5 (all of them @define-d in lang_en.inc.php and lang_de.inc.php) are not, well, evaluated obviously. Which means the default value is set to NAV_DEFAULT_1 instead of the value of NAV_DEFAULT_1. I hope you understand what I mean ... also, I just found out that when I change this to i.e. 'Index', the navbar link in question is still labeled 'NAV_DEFAULT_1', so obviously there is something wrong here ...
This is because constants cannot be concatenated. Instead of this:

Code: Select all

        'default'       => NAV_DEFAULT_ . $i,
you need to use:

Code: Select all

        'default'       => constant('NAV_DEFAULT_' . $i),
Can we by the way use arrays in lang_XY.inc.php? Maybe that would make it easier ..? (Just a non-coders thought ...)
Arrays actually are IMHO harder to read and initialize. Also they can be overwritten in user-scope which might lead to injection attacks by overwriting them...

So, constants are for the time being IMHO the better way.

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:A great name for a first-born! ;)
Yeah, but if this becomes common sense, we'll have a planet of Garvins within a few years ... :wink:
garvinhicking wrote:you need to use:

Code: Select all

        'default'       => constant('NAV_DEFAULT_' . $i),
I hate to be the bearer of bad news, but this doesn't work. Also, although I have set the navlink URL default to '#', it still reads 'http://' on the 'Manage Styles' page, so I'm guessing that part of the information entered in this is cached or stored somewhere and not reset or overwritten when it's changed ... yes, I already cleared templates_c.

This can't be some kind of weird side effect because I'm still doing this on my local apache, can it? (Just guessing.)

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!
I hate to be the bearer of bad news, but this doesn't work. Also, although I have set the navlink URL default to '#', it still reads 'http://' on the 'Manage Styles' page, so I'm guessing that part of the information entered in this is cached or stored somewhere and not reset or overwritten when it's changed ... yes, I already cleared templates_c.
Are you sure that the default is displayed? Once you saved the theme configuration, the value that is shown in the input field is saved in the database! Have a look at the serendipity_options Database table...

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:Are you sure that the default is displayed? Once you saved the theme configuration, the value that is shown in the input field is saved in the database! Have a look at the serendipity_options Database table...
Gnagnagna. The false URL default was my fault, had the wrong $template_option variable in the index.tpl :oops:

However in the serendipity_options db table I have something like this:

navlink0text -> NAV_DEFAULT_0
navlink1text -> NAV_DEFAULT_1

and so on ...

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!

If you have NAV_DEFAULT_0 in your database, that means that you have not defined a constant "NAV_DEFAULT_0"...?!

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:If you have NAV_DEFAULT_0 in your database, that means that you have not defined a constant "NAV_DEFAULT_0"...?!
True, I guess. But I also have NAV_DEFAULT_1 and so on in the database - and those are definitely defined in the lang_*.inc.php files, i.e. lang_en.inc.php:

Code: Select all

<?php
[ ... some other stuff ... ]
@define('NAV_DEFAULT_1', 'Index');
@define('NAV_DEFAULT_2', 'Email');
@define('NAV_DEFAULT_3', 'RSS');
@define('NAV_DEFAULT_4', 'About this site');
@define('NAV_DEFAULT_5', 'About Me');
?>
This is why I was thinking that maybe the term:

Code: Select all

constant(NAV_DEFAULT_ . $i)
in the config.inc.php is not evaluated (I think that's what it's called?) correctly.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!

Are you aware that I did not post

Code: Select all

constant(NAV_DEFAULT_ . $i)
but instead

Code: Select all

constant('NAV_DEFAULT_' . $i)
?

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:Are you aware that I did not post

Code: Select all

constant(NAV_DEFAULT_ . $i)
but instead

Code: Select all

constant('NAV_DEFAULT_' . $i)
?
No, I wasn't :oops: Sorry about that. However, the second code doesn't work either :(

Also, I just saw that my local s9y installation (this is 1.1-beta5 with PHP 5.1.6 and MySQL 4.1.11 on an Apache 2.0.54 under Linux, if any of this is necessary) doesn't seem to switch the lang_*.inc.php files for the template options - I had switched this test blog to English earlier to be able to follow some thread in here better. I just now switched it back to German, but all the template options from config.inc.php are still displayed in English. I remember Carl mentioning a similar problem in a PM, by the way.

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: theme configurator: number of navbar links

Post by garvinhicking »

Hi!
No, I wasn't :oops: Sorry about that. However, the second code doesn't work either :(
Did you make sure to delete the serendipity_options table before you replaced the code? Otherwise, not a default will be shown, but the last stored value...
Also, I just saw that my local s9y installation (this is 1.1-beta5 with PHP 5.1.6 and MySQL 4.1.11 on an Apache 2.0.54 under Linux, if any of this is necessary) doesn't seem to switch the lang_*.inc.php files for the template options - I had switched this test blog to English earlier to be able to follow some thread in here better. I just now switched it back to German, but all the template options from config.inc.php are still displayed in English. I remember Carl mentioning a similar problem in a PM, by the way.
How does your config.inc.php file currently include the language files?

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/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: theme configurator: number of navbar links

Post by yellowled »

garvinhicking wrote:Did you make sure to delete the serendipity_options table before you replaced the code? Otherwise, not a default will be shown, but the last stored value...
No, but I also don't remember you mentioning this earlier :) Yeah, now it works!

Hm. But how will this possibly work in other environments, especially if users don't have access to their database tables? Since I copied most of the variables used from another theme, should I give them unique names like i.e. YAML_NAV_DEFAULT_0 and so on?
garvinhicking wrote:How does your config.inc.php file currently include the language files?
That's also code I borrowed somewhere else, I think from the new default theme I did with Carl ...

Code: Select all

<?php
// Be nice to the frontend users. They don't need the additional constants
// and file lookups. Only load them when in Admin mode.
if ($serendipity['GET']['adminModule'] == 'templates' || $serendipity['POST']['adminModule'] != 'templates') {
    // Probe for a language include with constants. Still include defines 
    // later on, if some constants were missing
    $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
    if (file_exists($probelang)) {
        include $probelang;
    } 
    
    include dirname(__FILE__) . '/lang_en.inc.php';
}
YL
Post Reply