summaryrefslogtreecommitdiff
path: root/LedgerSMB/Inifile.pm
blob: 97c1a7ffe432c58039abc3b142906233ae890275 (plain)
  1. =head1 NAME
  2. Inifile Routines to load LedgerSMB menu files.
  3. =head1 STATUS
  4. API I<deprecated>, removed in 1.3.
  5. =head1 SYNOPSIS
  6. Routines to load LedgerSMB menu files. LedgerSMB menu files are a specific
  7. form of ini 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. =back
  49. =head1 Copyright (C) 2006, The LedgerSMB core team.
  50. #====================================================================
  51. # LedgerSMB
  52. # Small Medium Business Accounting software
  53. # http://www.ledgersmb.org/
  54. #
  55. # Copyright (C) 2006
  56. # This work contains copyrighted information from a number of sources
  57. # all used with permission.
  58. #
  59. # This file contains source code included with or based on SQL-Ledger
  60. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  61. # and licensed under the GNU General Public License version 2 or, at
  62. # your option, any later version. For a full list including contact
  63. # information of contributors, maintainers, and copyright holders,
  64. # see the CONTRIBUTORS file.
  65. #
  66. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  67. # Copyright (C) 2002
  68. #
  69. # Author: DWS Systems Inc.
  70. # Web: http://www.sql-ledger.org
  71. #
  72. # Contributors:
  73. # Tony Fraser <tony@sybaspace.com>
  74. #
  75. #=====================================================================
  76. =cut
  77. package Inifile;
  78. sub new {
  79. my ( $type, $file ) = @_;
  80. warn "$type has no copy constructor! creating a new object."
  81. if ref($type);
  82. $type = ref($type) || $type;
  83. my $self = bless {}, $type;
  84. $self->add_file($file) if defined $file;
  85. return $self;
  86. }
  87. sub add_file {
  88. my ( $self, $file ) = @_;
  89. my $id = "";
  90. my %menuorder = ();
  91. for ( @{ $self->{ORDER} } ) { $menuorder{$_} = 1 }
  92. open FH, '<', "$file" or Form->error("$file : $!");
  93. while (<FH>) {
  94. next if /^(#|;|\s)/;
  95. last if /^\./;
  96. chop;
  97. # strip comments
  98. s/\s*(#|;).*//g;
  99. # remove any trailing whitespace
  100. s/^\s*(.*?)\s*$/$1/;
  101. if (/^\[/) {
  102. s/(\[|\])//g;
  103. $id = $_;
  104. push @{ $self->{ORDER} }, $_ if !$menuorder{$_};
  105. $menuorder{$_} = 1;
  106. next;
  107. }
  108. # add key=value to $id
  109. my ( $key, $value ) = split /=/, $_, 2;
  110. $self->{$id}{$key} = $value;
  111. }
  112. close FH;
  113. }
  114. 1;