Page 1 of 1

saving multiple values from single select list

Posted: Thu Jan 28, 2016 6:16 pm
by Don Chambers
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?

Re: saving multiple values from single select list

Posted: Fri Jan 29, 2016 4:38 pm
by onli
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?

Re: saving multiple values from single select list

Posted: Fri Jan 29, 2016 4:48 pm
by Don Chambers
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:

Re: saving multiple values from single select list

Posted: Fri Jan 29, 2016 5:12 pm
by onli
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.

Re: saving multiple values from single select list

Posted: Fri Jan 29, 2016 5:19 pm
by Don Chambers
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?

Re: saving multiple values from single select list

Posted: Fri Jan 29, 2016 5:21 pm
by onli
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.

Re: saving multiple values from single select list

Posted: Sat Jan 30, 2016 12:56 am
by Don Chambers
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.

Re: saving multiple values from single select list

Posted: Sat Jan 30, 2016 8:17 pm
by Don Chambers
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!

Re: saving multiple values from single select list

Posted: Sun Jan 31, 2016 6:42 pm
by onli
Is it overkill for what I want?
It makes for a good readable tpl, I think it is fine.