Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Global for ENUM-field (checkboxes)

Quote Reply
Global for ENUM-field (checkboxes)
Hello,

I would like to define different globals for an ENUM - field (typ: checkbox) that will used in different languages.

Jack has sent me some code from Aki for displaying a list of countries:

sub {
my $tags = GT::Template->tags;
my $e = qq|<select name="Location">|;
my %countries = (
DE_Germany => 'DE: Germany',
UK_United_Kingdom => 'UK: United Kingdom'
);
my $l = $tags->{Location} || '';
foreach my $k ( sort keys %countries ) {
my $is_selected = $l eq $k ? "selected" : "";
$e .= qq{<option value="$k" $is_selected>$countries{$k}</option>};
}

$e .= "</select>";
return $e;
}


How this needs to be changed for working with the checkboxes???

Lars
Quote Reply
Re: [calliope] Global for ENUM-field (checkboxes) In reply to
Code:
sub {
my $tags = GT::Template->tags;
my $e;
my %countries = (
DE_Germany => 'DE: Germany',
UK_United_Kingdom => 'UK: United Kingdom'
);
my $l = $tags->{Location} || '';
foreach my $k ( sort keys %countries ) {
my $is_selected = $l eq $k ?
qq|$countries{$k} <input type=checkbox name="Location" value="$k" checked>| :
qq|$countries{$k} <input type=checkbox name="Location">|;
$e .= $is_selected;
}
return $e;
}

Well, not tested, but theoritically this should display the country list, with checkboxes near names.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Quote:
sort keys %countries

You'd want the following to sort properly irrespective of case ...

Code:
sort { lc($a) cmp lc($b) }keys %countries
Quote Reply
Re: [Paul] Global for ENUM-field (checkboxes) In reply to
Aki wrote the original code. I did change only the checkbox related parts, and I don't mind about sorting...

BTW: country keys are starting all CAPITALS (i.e. DE_Germany), so your proposal isn't valid.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Quote:
your proposal isn't valid.

Sure it is. My proposal was to sort alphabrtically irrespective of case, which is what my example does. The fact that the countries happen to already start with uppercase letters doesn't mean the code is less valid.
Quote Reply
Re: [Paul] Global for ENUM-field (checkboxes) In reply to
Well, you can't guess thoughts of Calliope.
Calliope will decide what values will want to have in the country list, what letter case will use, and how he want to sort them.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Post deleted by calliope In reply to
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Thank you both!

I hope I understood how to change this code for a different field.
I will post anyhow.

Lars
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Code:
Calliope will decide what values will want to have in the country list, what letter case will use, and how he want to sort them.

Indeed, and in that situation then it's best to sort irrespective of case allowing him to pick what case he wants without worrying how it will affect the sorting =)
Quote Reply
Re: [Paul] Global for ENUM-field (checkboxes) In reply to
For the following it doesn´t make a difference - but I thank Paul for the lesson. In other cases it might be useful!!!

The boxes show up properly but I get an error when submitting the entry:

• kind kann nicht den Wert 'on on' speichern.

in english:
• kind cannot get the value 'on on'


Any idea what is wrong in the following code?

sub {
my $tags = GT::Template->tags;
my $e;
my %values = (
Produzent => '&nbsp;&nbsp;Produzent:',
Handel => '&nbsp;&nbsp;Handel:',
sonstige => '&nbsp;&nbsp;sonstige:'
);
my $l = $tags->{kind} || '';
foreach my $k ( sort { lc($a) cmp lc($b) }keys %values ) {
my $is_selected = $l eq $k ?
qq|$values{$k} <input type=checkbox name="kind" value="$k" checked>| :
qq|$values{$k} <input type=checkbox name="kind">|;
qq|$values{$k} <input type=checkbox name="kind">|;
$e .= $is_selected;
}
return $e;
}



Lars
Quote Reply
Re: [calliope] Global for ENUM-field (checkboxes) In reply to
At first sight, I see the following code duplicated:
qq|$values{$k} <input type=checkbox name="kind">|;

Also may need a space before 'keys'.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [calliope] Global for ENUM-field (checkboxes) In reply to
"on" means the radio is checked. You must have something in the def file disallowing that value.
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Enum fields are good only for one item, set fields are good for multiple. Links SQL uses a different system, using a standard char field with a multi select interface to it. If you wanted to have a enum/set based checkbox system, it would be a custom job, just send me an email since I'll have to ask you some specifics of how you want it to work.

However, if you're willing to go along the route of changing the field in question into a char + multiselect field this global should do the trick:

Code:
sub {
# --------------------------------------------------
# Small global for char fields with multiselect form fields
# turned on. Simply supply the Links SQL database column name
# and it will take care of the rest.
#
my $col_name = shift or return;
my $col_info = $Links::DB->table( 'Links' )->cols->{$col_name};

my $vl = $col_info->{form_names};
my $nl = $col_info->{form_values};

my $cs = {
map { $_ => 1 }
split /\r?\n/,
GT::Template->tags->{$col_name} || ''
};

my $out = '';
for my $i ( 0 .. $#$vl ) {
my $v = $vl->[$i];
my $selected = $cs->{$v} ? 'checked' : '';

# Edit the following line to what display format you need
$out .= qq{
$nl->[$i] : <input type="checkbox" name="$col_name" value="$v" $selected>
};
};

return $out;
}

All you'll have to do is to put the appropriate tag, say if this global was called, "checkbox_list", it would be call in your template as: <%checkbox_list("columnname")%> I just whipped it up though, so it may not work all the time (like how I assumed that the input will always be a scalar). Just let me know if something doesn't work and I'll make the proper corrections.
Quote Reply
Re: [Aki] Global for ENUM-field (checkboxes) In reply to
Well, your solution does almost the same as original suggested code, except that you use form_names & form_values from column definion info, and not defined within the global itself.

Personally I also prefer using form_names & form_values.
However in that special case, there was a working global, and Lars asked to change from select to checkbox usage, so I changed only the necessary parts.

I think Lars will be glad of your fine solution.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: May 8, 2003, 3:32 PM
Quote Reply
Re: [webmaster33] Global for ENUM-field (checkboxes) In reply to
Thank you Aki for working on it.

But because you use the definitions of the column I cannot make it multilingal.

That was one of the reasons to define a global for each language that includes the values.

Best Regards,
lars
Quote Reply
Re: [calliope] Global for ENUM-field (checkboxes) In reply to
Aki, for select pull down menus I use this code in a global:

Code:
sub {
my ($select_db, $select_field, $select_default) = @_;
my $html = $DB->html ( [$select_db], $IN );
return $html->select ( { name => $select_field, value => $select_default });
}



I call the global with Database I want to use, Field, and default values. something like:
<%global(a,b,$c)%>
$c is the default value for the select (form)

The select it's a method of the html class or object or DB class/object (not sure of object oriented terminology sorry) So I guess the checkbox method should be implemented if not already there right? the point is: if It is SO EASY to display a select based on values entered in the admin, why can not be the above global used for displaying radio and checkboxes as well?
Quote Reply
Re: [Aki] Global for ENUM-field (checkboxes) In reply to
In fact... the above example actually works if you replace select with checkbox... just checked and the checkbox method was in GT/SQL/Display/HTML.pm so I tried changing the global.
Quote Reply
Re: [jaltuve] Global for ENUM-field (checkboxes) In reply to
I tried to use this in GL 3.2.0 and it works.

Code:
sub {
my ($select_db, $select_field, $select_default) = @_;
my $html = $DB->html ( [$select_db], $IN );
return $html->radio ( { name => $select_field, value => $select_default });
}
My first question: Isn't there a build-in way in 3.2.0?

My second question: To use it in include_form (in error-case or modify-case), I need the selected value instead of the default value as third parameter. Then the default parameter will be the first in the def-file, I guess?

Regards,
Chris
Quote Reply
Re: [cwschroeder] Global for ENUM-field (checkboxes) In reply to
Hi
,
Is this for CHECKBOX or RADIO ?

If its for CHECKBOX, should be able to use something like this: (untested!)

Code:
sub {

my @current_val_split = split /\n/, $_[0];
my $vals;
foreach (@current_val_split) { $vals->{$_} = 1 }

for (my $i = 0; $i <= 250; $i++) {
my $tmp;
if (!$field_vals->{form_values}[$i]) { last }
$tmp->{value} = $field_vals->{form_values}[$i];
$tmp->{name} = $field_vals->{form_names}[$i];
if ($vals->{$tmp->{name}}) { $tmp->{selected} = 1; }
push @checkbox_loop, $tmp;

}
return { checkbox_loop => \@checkbox_loop }

}

Then, call with:

Code:
<%global_name($Checkbox_field_name)%>
<ul>
<%loop checkbox_loop%>
<li value="<%value%>" <%if selected%>selected="yes"<%endif%>><%name%></li>
<%endloop%>
</ul>

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!