saving multiple values from single select list

Discussion corner for Developers of Serendipity.
Post Reply
User avatar
Don Chambers
Regular
Posts: 3633
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

saving multiple values from single select list

Post by Don Chambers » Thu Jan 28, 2016 7:16 pm

This is within config.inc.php. Is it possible to save more than one value from a single select list?

For example, i want to provide a list of possible social icons. I want the theme configuration to save the name of the social networking service (Twitter, Facebook, etc) together with its corresponding font awesome icon.

Maybe something like this:

Code: Select all

$socialicons = array();
for ($i = 0; $i < $template_loaded_config['social_icons_amount']; $i++) {
    $template_config[] = array(
        'var'           => 'social_icon' . $i . 'icon',
        'name'          => 'SocialNameAndIcon ' .($i),
        'type'          => 'select',
        'default'       => 'Amazon',
        'select_values' => array('Amazon,fa-amazon'     => 'Amazon',
                                 'Facebook,fa-facebook'         =>'Facebook',
                                 'Flickr,fa-flickr'                    => 'Flickr',
                                 'Github,fa-github'                 => 'Github',
                                 'Google Plus,fa-google-plus'   => 'Google Plus',)
    );
    $template_config[] = array(
        'var'           => 'social_icon' . $i . 'url',
        'name'          => SOCIAL_ICON_URL,
        'type'          => 'string',
        'default'       => '#',
    );
    $socialicons[] = array(
        'icon'          => $template_loaded_config['social_icon' . $i . 'icon'],
        'url'           => $template_loaded_config['social_icon' . $i . 'url'],
    ); 
If the above works (untested), I then need to output it in the tpls. How would I do that? Some kind of 'explode" function?

User avatar
onli
Regular
Posts: 2230
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: saving multiple values from single select list

Post by onli » Fri Jan 29, 2016 5:38 pm

Why? Just let the user pick the name of a service, and have an array that maps the name of the service to the name of an icon. I don't see why this should be part of the configuration?

User avatar
Don Chambers
Regular
Posts: 3633
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: saving multiple values from single select list

Post by Don Chambers » Fri Jan 29, 2016 5:48 pm

onli wrote:Why? Just let the user pick the name of a service, and have an array that maps the name of the service to the name of an icon. I don't see why this should be part of the configuration?
Any chance you can provide an code example? :wink:

User avatar
onli
Regular
Posts: 2230
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: saving multiple values from single select list

Post by onli » Fri Jan 29, 2016 6:12 pm

I can try to code it down, but this is the raw form:

Code: Select all

function getIcon($service) {
   $icons = array('Github' => 'fagithub', 'Google' => 'fagoogle');
   return $icons[$service]; 
}
then you call that in your template. If you only need it in your config.inc.php there is not even a need to make it a function.

User avatar
Don Chambers
Regular
Posts: 3633
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: saving multiple values from single select list

Post by Don Chambers » Fri Jan 29, 2016 6:19 pm

onli wrote:I can try to code it down, but this is the raw form:

Code: Select all

function getIcon($service) {
   $icons = array('Github' => 'fagithub', 'Google' => 'fagoogle');
   return $icons[$service]; 
}
then you call that in your template. If you only need it in your config.inc.php there is not even a need to make it a function.
Thanks. What I want is for the configuration to allow a user to select a specific social networking service, then store the name of that service, and quite possibly the icon that corresponds to it.

Or, if I follow what you are saying, store just a single value, such as the name of the service ("Facebook"), then select the corresponding icon - either to store that icon in the database, or to display it in the tpl.

Seems to me that simply fetching the appropriate icon when needed would be the most efficient? Would I then need to register this with smarty?

User avatar
onli
Regular
Posts: 2230
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: saving multiple values from single select list

Post by onli » Fri Jan 29, 2016 6:21 pm

It depends where you call it. If you use it only in the config.inc.php, no – you could store the result in the settings then. If you want to calli it directly in the tpl, yes Both ways are valid.

User avatar
Don Chambers
Regular
Posts: 3633
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: saving multiple values from single select list

Post by Don Chambers » Sat Jan 30, 2016 1:56 am

This is what I have. Is it overkill for what I want?

Code: Select all

function getIcon($service) {
   $icons = array('Amazon' => 'fa-amazon', 'Facebook' => 'fa-facebook', 'Flickr' => 'fa-flickr', 'Github' => 'fa-github', 'Google' => 'fa-google');
   return $icons[$service];
}

$serendipity['smarty']->register_function('service_icon', 'getServiceIcon');

function getServiceIcon ($params, &$smarty) {
    return getIcon($params['from_service']);
}
In my tpl, I have something that looks like this:

Code: Select all

    <ul class="footer-socials list-inline">
        {foreach from=$socialicons item="socialicon" name="social_networking"}
            <li>
                <a href="{$socialicon.url}" title="{$socialicon.service}"><i class="fa {service_icon from_service=$socialicon.service}"></i></a>
            </li>        
        {/foreach}         
    </ul>
The list of services above are just an example - the real version will have many more. Anyone else have anything to add to this? I think this method offers good flexibility to the user while minimizing the need to edit index.tpl.

User avatar
Don Chambers
Regular
Posts: 3633
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: saving multiple values from single select list

Post by Don Chambers » Sat Jan 30, 2016 9:17 pm

The services I decided to include are as follows:
  • *Amazon
    *Digg
    *Dribbble
    *Dropbox
    *Facebook
    *Flickr
    *Github
    *Google
    *Google Plus
    *Instagram
    *LastFM
    *Linkedin
    *Paypal
    *Pinterest
    *RSS
    *Skype
    *Reddit
    *Stumbleupon
    *Tumblr
    *Twitter
    *Vimeo
    *Vine
    *Xing
    *YouTube
Not every one of these are truly "social media/networking", but I feel most might be used on someone's blog. I would appreciate any opinions on social media services that a) are included, and maybe should not be and b) services missing but should be included. The corresponding icons I will be using are all from Font Awesome Icons ver 4.5.0.

Thanks!

User avatar
onli
Regular
Posts: 2230
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: saving multiple values from single select list

Post by onli » Sun Jan 31, 2016 7:42 pm

Is it overkill for what I want?
It makes for a good readable tpl, I think it is fine.

Post Reply