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