summaryrefslogtreecommitdiff
path: root/LedgerSMB/Menufile.pm
blob: 85674efdfc126fbc9c62cc4fdfbe18ef671df080 (plain)
  1. =head1 NAME
  2. LedgerSMB::Menufile Routines to handle LedgerSMB menu files and format entries
  3. for display.
  4. =head1 SYNOPSIS
  5. Routines to handle LedgerSMB menu files and conversion of menu entries into a
  6. form usable by a web browser. LedgerSMB menu files are a specific form of ini
  7. file.
  8. Files use both ';' and '#' to denote line comments. Any text after a line that
  9. starts with '.' (i.e. ".foo") is ignored. Menu items are denoted as [section],
  10. with the sections containing key=value pairs. The keys 'module', 'action',
  11. 'target', 'href', and 'submenu' are specially treated, while other keys are
  12. output as arguments to the destination link. Blank lines are ignored.
  13. =head2 Special key treatment
  14. =over
  15. =item action
  16. This key is deleted on menuitem calls if there is no href value.
  17. =item module
  18. This is the Perl script that the menu item will call if the href attribute is
  19. not set. This key is always deleted on a menuitem call.
  20. =item target
  21. The value given for target will be passed as the target attribute for the tag.
  22. This key is always deleted on a menuitem call.
  23. =item href
  24. When set, this key's value becomes the base URL for the menu item. This key is
  25. always deleted on a menuitem call.
  26. =item submenu
  27. This key is not displayed in output, but is deleted from the Menufile object
  28. when menuitem is called on the item.
  29. =back
  30. =head2 Value Interpolation
  31. If a value for a regular key includes an equals sign (=), values from the user's
  32. configuration are substituted into the place of the string preceding and the
  33. first encountered equals sign in the value. So a menu entry of 'apples=login='
  34. would have the substition of 'apples=$myconfig->{login}' on generation of the
  35. menu link.
  36. =head1 METHODS
  37. =over
  38. =item new ([$filename])
  39. Create a new Menufile object. If a filename is specified, load the file with
  40. add_file.
  41. =item add_file ($filename)
  42. Load the contents of the specified file into the Menufile object. If the file
  43. cannot be read, Form->error will be called with the failure message. Attempts
  44. to load already loaded items will result in the newer item merging with and
  45. overwriting stored data from the previous load.
  46. Menu item titles are stored as keys in the Menufile object, and a special key,
  47. ORDER maintains a list of the order in which menu items were first seen.
  48. =item menuitem ($myconfig, $form, $item)
  49. Formats the menu item for the given key $item as an HTML <a href=""> open tag.
  50. Returns the tag and deletes the module, target, href, action, and submenu
  51. attributes for the item from the Menufile object.
  52. If the menubar attribute of the passed in Form attribute is set, no style will
  53. be set for the tag, otherwise the style is set to "display:block".
  54. =item access_control ($myconfig, [$menulevel])
  55. Returns the list of menu items that can be displayed with $myconfig->{acs} at
  56. the selected menu level. $menulevel is the string corresponding to a displayed
  57. menu, such as 'AR' or 'AR--Reports'. A blank level corresponds to the top
  58. level. Merely excluding a top-level element does not exclude corresponding
  59. lower level elements, i.e. excluding 'AR' will not block 'AR--Reports'.
  60. $myconfig->{acs} is a semicolon seperated list of menu items to exclude.
  61. This is only a cosmetic form of access_control. Users can still access
  62. "disallowed" sections of the application by manually entering in the URL.
  63. =back
  64. =head1 Copyright (C) 2006, The LedgerSMB core team.
  65. #====================================================================
  66. # LedgerSMB
  67. # Small Medium Business Accounting software
  68. # http://www.ledgersmb.org/
  69. #
  70. # Copyright (C) 2006
  71. # This work contains copyrighted information from a number of sources
  72. # all used with permission.
  73. #
  74. # This file contains source code included with or based on SQL-Ledger
  75. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  76. # and licensed under the GNU General Public License version 2 or, at
  77. # your option, any later version. For a full list including contact
  78. # information of contributors, maintainers, and copyright holders,
  79. # see the CONTRIBUTORS file.
  80. #
  81. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  82. # Copyright (C) 2002
  83. #
  84. # Author: DWS Systems Inc.
  85. # Web: http://www.sql-ledger.org
  86. #
  87. # Contributors:
  88. # Tony Fraser <tony@sybaspace.com>
  89. #
  90. #=====================================================================
  91. =cut
  92. package LedgerSMB::Menufile;
  93. use LedgerSMB::Form;
  94. sub new {
  95. my ( $type, $file ) = @_;
  96. warn "$type has no copy constructor! creating a new object."
  97. if ref($type);
  98. $type = ref($type) || $type;
  99. my $self = bless {}, $type;
  100. $self->add_file($file) if defined $file;
  101. return $self;
  102. }
  103. sub add_file {
  104. my ( $self, $file ) = @_;
  105. my $id = "";
  106. my %menuorder = ();
  107. for ( @{ $self->{ORDER} } ) { $menuorder{$_} = 1 }
  108. open FH, '<', "$file" or Form->error("$file : $!");
  109. while (<FH>) {
  110. next if /^(#|;|\s)/;
  111. last if /^\./;
  112. chop;
  113. # strip comments
  114. s/\s*(#|;).*//g;
  115. # remove any trailing whitespace
  116. s/^\s*(.*?)\s*$/$1/;
  117. if (/^\[/) {
  118. s/(\[|\])//g;
  119. $id = $_;
  120. push @{ $self->{ORDER} }, $_ if !$menuorder{$_};
  121. $menuorder{$_} = 1;
  122. next;
  123. }
  124. # add key=value to $id
  125. my ( $key, $value ) = split /=/, $_, 2;
  126. $self->{$id}{$key} = $value;
  127. }
  128. close FH;
  129. }
  130. sub menuitem {
  131. my ( $self, $myconfig, $form, $item ) = @_;
  132. my $module =
  133. ( $self->{$item}{module} ) ? $self->{$item}{module} : $form->{script};
  134. my $action =
  135. ( $self->{$item}{action} ) ? $self->{$item}{action} : "section_menu";
  136. my $target = ( $self->{$item}{target} ) ? $self->{$item}{target} : "";
  137. my $level = $form->escape($item);
  138. my $style;
  139. if ( $form->{menubar} ) {
  140. $style = "";
  141. }
  142. else {
  143. $style = "display:block;";
  144. }
  145. my $str =
  146. qq|<a style="$style"|
  147. . qq|href="$module?path=$form->{path}&amp;action=$action&amp;|
  148. . qq|level=$level&amp;login=$form->{login}&amp;|
  149. . qq|timeout=$form->{timeout}&amp;sessionid=$form->{sessionid}|
  150. . qq|&amp;js=$form->{js}|;
  151. my @vars = qw(module action target href);
  152. if ( $self->{$item}{href} ) {
  153. $str = qq|<a href="$self->{$item}{href}|;
  154. @vars = qw(module target href);
  155. }
  156. for (@vars) { delete $self->{$item}{$_} }
  157. delete $self->{$item}{submenu};
  158. # add other params
  159. foreach my $key ( keys %{ $self->{$item} } ) {
  160. $str .= "&amp;" . $form->escape($key) . "=";
  161. ( $value, $conf ) = split /=/, $self->{$item}{$key}, 2;
  162. $value = "$myconfig->{$value}$conf"
  163. if $self->{$item}{$key} =~ /=/;
  164. $str .= $form->escape($value);
  165. }
  166. $str .= qq|#id$form->{tag}| if $target eq 'acc_menu';
  167. if ($target) {
  168. $str .= qq|" target="$target"|;
  169. }
  170. else {
  171. $str .= '"';
  172. }
  173. $str .= qq|>|;
  174. }
  175. sub access_control {
  176. my ( $self, $myconfig, $menulevel ) = @_;
  177. my @menu = ();
  178. if ( $menulevel eq "" ) {
  179. @menu = grep { !/--/ } @{ $self->{ORDER} };
  180. }
  181. else {
  182. @menu = grep { /^${menulevel}--/; } @{ $self->{ORDER} };
  183. }
  184. my @a = split /;/, $myconfig->{acs};
  185. my %excl = ();
  186. # remove --AR, --AP from array
  187. grep { ( $a, $b ) = split /--/; s/--$a$//; } @a;
  188. for (@a) { $excl{$_} = 1 }
  189. @a = ();
  190. for (@menu) { push @a, $_ unless $excl{$_} }
  191. @a;
  192. }
  193. 1;