Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 58966151

Von Thomas Heck vor etwa 12 Jahren hinzugefügt

  • ID 5896615180704b3dddba46f4064a47ff1f094c3c
  • Vorgänger 261cfce2
  • Nachfolger 51d0274d

SL::Template::Plugin::L::select_tag und SL::Template::Plugin::L::options_for_select fusionieren.

Unterschiede anzeigen:

SL/Template/Plugin/L.pm
86 86
sub select_tag {
87 87
  my $self            = shift;
88 88
  my $name            = shift;
89
  my $options_str     = shift;
89
  my $collection      = shift;
90 90
  my %attributes      = _hashify(@_);
91 91

  
92 92
  $attributes{id}   ||= $self->name_to_id($name);
93
  $options_str        = $self->options_for_select($options_str) if ref $options_str;
94 93

  
95
  return $self->html_tag('select', $options_str, %attributes, name => $name);
94
  my $value_key       = delete($attributes{value_key}) || 'id';
95
  my $title_key       = delete($attributes{title_key}) || $value_key;
96
  my $default_key     = delete($attributes{default_key}) || 'selected';
97

  
98

  
99
  my $value_title_sub = delete($attributes{value_title_sub});
100

  
101
  my $value_sub       = delete($attributes{value_sub});
102
  my $title_sub       = delete($attributes{title_sub});
103
  my $default_sub     = delete($attributes{default_sub});
104

  
105

  
106
  my %selected;
107

  
108
  if ( ref($attributes{default}) eq 'ARRAY' ) {
109

  
110
    foreach my $entry (@{$attributes{default}}) {
111
      $selected{$entry} = 1;
112
    }
113
  } elsif ( defined($attributes{default}) ) {
114
    $selected{$attributes{default}} = 1;
115
  }
116

  
117
  delete($attributes{default});
118

  
119

  
120
  my @options;
121

  
122
  if ( delete($attributes{with_empty}) ) {
123
    push(@options, [undef, $attributes{empty_title} || '']);
124
  }
125

  
126
  my $normalize_entry = sub {
127

  
128
    my ($type, $entry, $sub, $key) = @_;
129

  
130
    if ( $sub ) {
131
      return $sub->($entry);
132
    }
133

  
134
    my $ref = ref($entry);
135

  
136
    if ( !$ref ) {
137

  
138
      if ( $type eq 'value' || $type eq 'title' ) {
139
        return $entry;
140
      }
141

  
142
      return 0;
143
    }
144

  
145
    if ( $ref eq 'ARRAY' ) {
146

  
147
      if ( $type eq 'value' ) {
148
        return $entry->[0];
149
      }
150

  
151
      if ( $type eq 'title' ) {
152
        return $entry->[1];
153
      }
154

  
155
      return $entry->[2];
156
    }
157

  
158
    if ( $ref eq 'HASH' ) {
159
      return $entry->{$key};
160
    }
161

  
162
    if ( $type ne 'default' || $entry->can($key) ) {
163
      return $entry->$key;
164
    }
165

  
166
    return undef;
167
  };
168

  
169
  foreach my $entry ( @{ $collection } ) {
170
    my $value;
171
    my $title;
172

  
173
    if ( $value_title_sub ) {
174
      ($value, $title) = $value_title_sub->($entry);
175
    } else {
176

  
177
      $value = $normalize_entry->('value', $entry, $value_sub, $value_key);
178
      $title = $normalize_entry->('title', $entry, $title_sub, $title_key);
179
    }
180

  
181
    my $default = $normalize_entry->('default', $entry, $default_sub, $default_key);
182

  
183
    push(@options, [$value, $title, $default]);
184
  }
185

  
186
  foreach my $entry (@options) {
187
    if ( exists($selected{$entry->[0]}) ) {
188
      $entry->[2] = 1;
189
    }
190
  }
191

  
192
  my $code = '';
193

  
194
  foreach my $entry (@options) {
195
    my %args = (value => $entry->[0]);
196

  
197
    $args{selected} = $entry->[2];
198

  
199
    $code .= $self->html_tag('option', _H($entry->[1]), %args);
200
  }
201

  
202
  $code = $self->html_tag('select', $code, %attributes, name => $name);
203

  
204
  return $code;
96 205
}
97 206

  
98 207
sub textarea_tag {
......
207 316
  return $self->html_tag('input', undef, %attributes, value => $value, onclick => $onclick);
208 317
}
209 318

  
210
sub options_for_select {
211
  my $self            = shift;
212
  my $collection      = shift;
213
  my %options         = _hashify(@_);
214

  
215
  my $value_key       = $options{value} || 'id';
216
  my $title_key       = $options{title} || $value_key;
217

  
218
  my $value_sub       = $options{value_sub};
219
  my $title_sub       = $options{title_sub};
220

  
221
  my $value_title_sub = $options{value_title_sub};
222

  
223
  my %selected        = map { ( $_ => 1 ) } @{ ref($options{default}) eq 'ARRAY' ? $options{default} : defined($options{default}) ? [ $options{default} ] : [] };
224

  
225
  my $access = sub {
226
    my ($element, $index, $key, $sub) = @_;
227
    my $ref = ref $element;
228
    return  $sub            ? $sub->($element)
229
         : !$ref            ? $element
230
         :  $ref eq 'ARRAY' ? $element->[$index]
231
         :  $ref eq 'HASH'  ? $element->{$key}
232
         :                    $element->$key;
233
  };
234

  
235
  my @elements = ();
236
  push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
237
  push @elements, map [
238
    $value_title_sub ? @{ $value_title_sub->($_) } : (
239
      $access->($_, 0, $value_key, $value_sub),
240
      $access->($_, 1, $title_key, $title_sub),
241
    )
242
  ], @{ $collection } if $collection && ref $collection eq 'ARRAY';
243

  
244
  my $code = '';
245
  foreach my $result (@elements) {
246
    my %attributes = ( value => $result->[0] );
247
    $attributes{selected} = 'selected' if $selected{ defined($result->[0]) ? $result->[0] : '' };
248

  
249
    $code .= $self->html_tag('option', _H($result->[1]), %attributes);
250
  }
251

  
252
  return $code;
253
}
254

  
255 319
sub yes_no_tag {
256 320
  my ($self, $name, $value) = splice @_, 0, 3;
257 321
  my %attributes            = _hashify(@_);
258 322

  
259
  my $options               = $self->options_for_select([ [ 1, $::locale->text('Yes') ], [ 0, $::locale->text('No') ] ], default => $value ? 1 : 0);
260
  return $self->select_tag($name, $options, %attributes);
323
  return $self->select_tag($name, [ [ 1 => $::locale->text('Yes') ], [ 0 => $::locale->text('No') ] ], default => $value ? 1 : 0, %attributes);
261 324
}
262 325

  
263 326
sub javascript {
......
356 419

  
357 420
  my $actual_vendor_id = (defined $::form->{"$name"})? ((ref $::form->{"$name"}) ? $::form->{"$name"}->id : $::form->{"$name"}) :
358 421
                         (ref $value && $value->can('id')) ? $value->id : '';
359
  my $options_str = $self->options_for_select(SL::DB::Manager::Vendor->get_all(),
360
                                              default      => $actual_vendor_id,
361
                                              title_sub    => sub { $_[0]->vendornumber . " : " . $_[0]->name },
362
                                              'with_empty' => 1);
363 422

  
364
  return $self->select_tag($name, $options_str, %params);
423
  return $self->select_tag($name, SL::DB::Manager::Vendor->get_all(),
424
                                  default      => $actual_vendor_id,
425
                                  title_sub    => sub { $_[0]->vendornumber . " : " . $_[0]->name },
426
                                  'with_empty' => 1,
427
                                  %params);
365 428
}
366 429

  
367 430

  
......
371 434

  
372 435
  my $actual_part_id = (defined $::form->{"$name"})? ((ref $::form->{"$name"})? $::form->{"$name"}->id : $::form->{"$name"}) :
373 436
                       (ref $value && $value->can('id')) ? $value->id : '';
374
  my $options_str = $self->options_for_select(SL::DB::Manager::Part->get_all(),
375
                                              default      => $actual_part_id,
376
                                              title_sub    => sub { $_[0]->partnumber . " : " . $_[0]->description },
377
                                              'with_empty' => 1);
378 437

  
379
  return $self->select_tag($name, $options_str, %params);
438
  return $self->select_tag($name, SL::DB::Manager::Part->get_all(),
439
                           default      => $actual_part_id,
440
                           title_sub    => sub { $_[0]->partnumber . " : " . $_[0]->description },
441
                           with_empty   => 1,
442
                           %params);
380 443
}
381 444

  
382 445

  
......
619 682

  
620 683
  [% USE L %]
621 684

  
622
  [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
685
  [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right', 1 ] ]) %]
623 686

  
624
  [% L.select_tag('direction', L.options_for_select([ { direction => 'left',  display => 'To the left'  },
625
                                                      { direction => 'right', display => 'To the right' } ],
626
                                                    value => 'direction', title => 'display', default => 'right')) %]
687
  [% L.select_tag('direction', [ { direction => 'left',  display => 'To the left'  },
688
                                 { direction => 'right', display => 'To the right' } ],
689
                               value_key => 'direction', title_key => 'display', default => 'right')) %]
690

  
691
  [% L.select_tag('direction', [ { direction => 'left',  display => 'To the left'  },
692
                                 { direction => 'right', display => 'To the right', selected => 1 } ],
693
                               value_key => 'direction', title_key => 'display')) %]
627 694

  
628 695
=head1 DESCRIPTION
629 696

  
......
661 728

  
662 729
=over 4
663 730

  
664
=item C<select_tag $name, $options_string, %attributes>
731
=item C<select_tag $name, \@collection, %attributes>
732

  
733
Creates a HTML 'select' tag named C<$name> with the contents of one
734
'E<lt>optionE<gt>' tag for each element in C<\@collection> and with arbitrary
735
HTML attributes from C<%attributes>. The value
736
to use and the title to display are extracted from the elements in
737
C<\@collection>. Each element can be one of four things:
738

  
739
=over 12
740

  
741
=item 1. An array reference with at least two elements. The first element is
742
the value, the second element is its title. The third element is optional and and should contain a boolean.
743
If it is true, than the element will be used as default.
744

  
745
=item 2. A scalar. The scalar is both the value and the title.
746

  
747
=item 3. A hash reference. In this case C<%attributes> must contain
748
I<value_key>, I<title_key> and may contain I<default_key> keys that name the keys in the element to use
749
for the value, title and default respectively.
750

  
751
=item 4. A blessed reference. In this case C<%attributes> must contain
752
I<value_key>, I<title_key> and may contain I<default_key> keys that name functions called on the blessed
753
reference whose return values are used as the value, title and default
754
respectively.
755

  
756
=back
757

  
758
For cases 3 and 4 C<$attributes{value_key}> defaults to C<id>,
759
C<$attributes{title_key}> defaults to C<$attributes{value_key}>
760
and C<$attributes{default_key}> defaults to C<selected>.
761

  
762
In addition to pure keys/method you can also provide coderefs as I<value_sub>
763
and/or I<title_sub> and/or I<default_sub>. If present, these take precedence over keys or methods,
764
and are called with the element as first argument. It must return the value, title or default.
765

  
766
Lastly a joint coderef I<value_title_sub> may be provided, which in turn takes
767
precedence over the C<value_sub> and C<title_sub> subs. It will only be called once for each
768
element and must return a list of value and title.
769

  
770
If the option C<with_empty> is set then an empty element (value
771
C<undef>) will be used as the first element. The title to display for
772
this element can be set with the option C<empty_title> and defaults to
773
an empty string.
665 774

  
666
Creates a HTML 'select' tag named C<$name> with the contents
667
C<$options_string> and with arbitrary HTML attributes from
668
C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
775
The option C<default> can be either a scalar or an array reference
776
containing the values of the options which should be set to be
777
selected.
669 778

  
670
The C<$options_string> is usually created by the
671
L</options_for_select> function. If C<$options_string> is an array
672
reference then it will be passed to L</options_for_select>
673
automatically.
779
The tag's C<id> defaults to C<name_to_id($name)>.
674 780

  
675 781
=item C<yes_no_tag $name, $value, %attributes>
676 782

  
677 783
Creates a HTML 'select' tag with the two entries C<yes> and C<no> by
678
calling L<select_tag> and L<options_for_select>. C<$value> determines
784
calling L<select_tag>. C<$value> determines
679 785
which entry is selected. The C<%attributes> are passed through to
680 786
L<select_tag>.
681 787

  
......
909 1015

  
910 1016
=over 4
911 1017

  
912
=item C<options_for_select \@collection, %options>
913

  
914
Creates a string suitable for a HTML 'select' tag consisting of one
915
'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
916
to use and the title to display are extracted from the elements in
917
C<\@collection>. Each element can be one of four things:
918

  
919
=over 12
920

  
921
=item 1. An array reference with at least two elements. The first element is
922
the value, the second element is its title.
923

  
924
=item 2. A scalar. The scalar is both the value and the title.
925

  
926
=item 3. A hash reference. In this case C<%options> must contain
927
I<value> and I<title> keys that name the keys in the element to use
928
for the value and title respectively.
929

  
930
=item 4. A blessed reference. In this case C<%options> must contain
931
I<value> and I<title> keys that name functions called on the blessed
932
reference whose return values are used as the value and title
933
respectively.
934

  
935
=back
936

  
937
For cases 3 and 4 C<$options{value}> defaults to C<id> and
938
C<$options{title}> defaults to C<$options{value}>.
939

  
940
In addition to pure keys/method you can also provide coderefs as I<value_sub>
941
and/or I<title_sub>. If present, these take precedence over keys or methods,
942
and are called with the element as first argument. It must return the value or
943
title.
944

  
945
Lastly a joint coderef I<value_title_sub> may be provided, which in turn takes
946
precedence over each individual sub. It will only be called once for each
947
element and must return a list of value and title.
948

  
949
If the option C<with_empty> is set then an empty element (value
950
C<undef>) will be used as the first element. The title to display for
951
this element can be set with the option C<empty_title> and defaults to
952
an empty string.
953

  
954
The option C<default> can be either a scalar or an array reference
955
containing the values of the options which should be set to be
956
selected.
957

  
958 1018
=item C<tab, description, target, %PARAMS>
959 1019

  
960 1020
Creates a tab for C<tabbed>. The description will be used as displayed name.

Auch abrufbar als: Unified diff