summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/Elements.pm
blob: e111b646a144c7dec395f0c44e0c62f6c958816f (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::Elements: Template Utility Functions
  3. =head1 SYNOPSIS
  4. Provides utility functions for generating elements for the user interface
  5. templates
  6. =head1 METHODS
  7. =over
  8. =item LedgerSMB::Template::Elements->new()
  9. Returns a blessed hashref from this namespace.
  10. =back
  11. =cut
  12. package LedgerSMB::Template::Elements;
  13. use strict;
  14. sub new {
  15. my ($class) = @_;
  16. my $self = {};
  17. bless $self, $class;
  18. return $self;
  19. }
  20. =over
  21. =item $object->generate_hidden_elements([...]);
  22. Builds data structure for hidden form fields. Values from the
  23. $form object are run through $form->quote.
  24. Sample data structure added to $form->hidden_elements():
  25. $self->{form_elements}{hidden_elements} = [{ type => 'hidden', name => 'foo', value => 'bar'... } ...]
  26. A reference to this structure is returned as well.
  27. =back
  28. =cut
  29. sub generate_hidden_elements {
  30. my $self = shift;
  31. if (! $self->{form_elements}{hidden_elements} ) {
  32. $self->{form_elements}{hidden_elements} = [];
  33. }
  34. for (@_) {
  35. my $value = defined($self->{$_}) ? $self->quote( $self->{$_} ) : '';
  36. push @{$self->{form_elements}{hidden_elements}}, { type => 'hidden', name => $_, value => $value };
  37. }
  38. return $self->{form_elements}{hidden_elements};
  39. }
  40. =over
  41. =item $form->generate_radio_elements($radios);
  42. Roll out a single radios hash to an array of radio elements,
  43. using the values array as index.
  44. Sample data structure added to $form->generate_radio_elements($radios):
  45. my $radios = {
  46. name => 'radio_name',
  47. class => 'radio',
  48. attributes => { foo => 'bar' },
  49. values => [ '1', '2', '3'],
  50. labels => [ 'Label one', '', 'Label three'],
  51. default_value => '2',
  52. };
  53. =back
  54. =cut
  55. sub generate_radio_elements {
  56. my $self = shift;
  57. my $radios = shift;
  58. my $elements = [];
  59. my $i = 0;
  60. # One new radio element for each listed value.
  61. foreach my $radio_value ( @{$radios->{values}} ) {
  62. my $element = {};
  63. # copy all additional attributes
  64. while ( my ($key, $value) = each(%$radios) ) {
  65. if ( $key !~ /^(values|labels|id|(default_)?value)$/ ) {
  66. $element->{$key} = $value;
  67. }
  68. }
  69. $element->{id} = $radios->{name} .'-'. $radio_value;
  70. # id tags with only numbers, letters, and dashes -- nicer CSS.
  71. $element->{id} =~ s/[^\p{IsAlnum}]/-/g;
  72. $element->{value} = $radio_value;
  73. $element->{type} = 'radio';
  74. # Add label key if present for this element.
  75. if ( $radios->{labels}[$i] ) {
  76. $element->{label} = $radios->{labels}[$i];
  77. }
  78. # Add checked attribute if the default value applies to this element.
  79. if ( defined($radios->{default_value}) && $radios->{default_value} eq $radio_value) {
  80. $element->{checked} = 'checked';
  81. }
  82. push @$elements, $element;
  83. $i++;
  84. }
  85. return $elements;
  86. }
  87. =over
  88. =item $form->generate_checkbox_elements($checkboxes);
  89. Roll out a single checkboxes hash to an array of checkbox elements,
  90. using the names array as index. Note that if no 'values' array
  91. is passed, value for all checkboxes default to 1.
  92. Sample data structure added to $form->generate_checkbox_elements($checkboxes):
  93. my $checkboxes = {
  94. names => [
  95. 'checkbox_name1',
  96. 'checkbox_name2',
  97. 'checkbox_name3',
  98. ],
  99. class => 'checkbox',
  100. attributes => { foo => 'bar' },
  101. values => [ '4', '', '3'],
  102. labels => [ 'Label one', '', 'Label three'],
  103. default_values => [ 'checkbox_name1'],
  104. };
  105. =back
  106. =cut
  107. sub generate_checkbox_elements {
  108. my $self = shift;
  109. my $checkboxes = shift;
  110. my $elements = [];
  111. my $i = 0;
  112. # One new checkbox element for each listed name.
  113. foreach my $checkbox_name ( @{$checkboxes->{names}} ) {
  114. my $element = {};
  115. # Additional attributes
  116. while ( my ($key, $value) = each(%$checkboxes) ) {
  117. if ( $key !~ /^(names|(default_)?values|labels|id|value|name)$/ ) {
  118. $element->{$key} = $value;
  119. }
  120. }
  121. # Value defaults to 1 if not passed for this element.
  122. $element->{value} = defined($checkboxes->{values}[$i])
  123. ? $checkboxes->{values}[$i]
  124. : '1';
  125. $element->{name} = $checkbox_name;
  126. $element->{id} = $element->{name};
  127. # id tags with only numbers, letters, and dashes -- nicer CSS.
  128. $element->{id} =~ s/[^\p{IsAlnum}]/-/g;
  129. $element->{type} = 'checkbox';
  130. # Add label key if present for this element.
  131. if ( $checkboxes->{labels}[$i] ) {
  132. $element->{label} = $checkboxes->{labels}[$i];
  133. }
  134. # Add checked attribute if the default value applies to this element.
  135. if ( defined($checkboxes->{default_values}) &&
  136. grep {$_ eq $checkbox_name} @{$checkboxes->{default_values}}) {
  137. $element->{checked} = 'checked';
  138. }
  139. push @$elements, $element;
  140. $i++;
  141. }
  142. return $elements;
  143. }
  144. 1;