summaryrefslogtreecommitdiff
path: root/LedgerSMB.pm
blob: 3fe186b9928f8b81b23ba6a6ad8621d92dc76c34 (plain)
  1. =head1 NAME
  2. LedgerSMB The Base class for many LedgerSMB objects, including DBObject.
  3. =head1 SYNOPSIS
  4. This module creates a basic request handler with utility functions available
  5. in database objects (LedgerSMB::DBObject)
  6. =head1 METHODS
  7. =over
  8. =item new ()
  9. This method creates a new base request instance. It also validates the
  10. session/user credentials, as appropriate for the run mode. Finally, it sets up
  11. the database connections for the user.
  12. =item date_to_number (user => $LedgerSMB::User, date => $string);
  13. This function takes the date in the format provided and returns a numeric
  14. string in YYMMDD format. This may be moved to User in the future.
  15. =item debug (file => $path);
  16. This dumps the current object to the file if that is defined and otherwise to
  17. standard output.
  18. =item escape (string => $string);
  19. This function returns the current string escaped using %hexhex notation.
  20. =item unescape (string => $string);
  21. This function returns the $string encoded using %hexhex using ordinary notation.
  22. =item format_amount (user => $LedgerSMB::User::hash, amount => $string, precision => $integer, neg_format => (-|DRCR));
  23. The function takes a monetary amount and formats it according to the user
  24. preferences, the negative format (- or DR/CR). Note that it may move to
  25. LedgerSMB::User at some point in the future.
  26. =item parse_amount (user => $LedgerSMB::User::hash, amount => $variable);
  27. If $amount is a Bigfloat, it is returned as is. If it is a string, it is
  28. parsed according to the user preferences stored in the LedgerSMB::User object.
  29. =item is_blank (name => $string)
  30. This function returns true if $self->{$string} only consists of whitespace
  31. characters or is an empty string.
  32. =item is_run_mode ('(cli|cgi|mod_perl)')
  33. This function returns 1 if the run mode is what is specified. Otherwise
  34. returns 0.
  35. =item is_allowed_role(allowed_roles => @role_names)
  36. This function returns 1 if the user's roles include any of the roles in
  37. @role_names. Currently it returns 1 when this is not found as well but when
  38. role permissions are introduced, this will change to 0.
  39. =item num_text_rows (string => $string, cols => $number, max => $number);
  40. This function determines the likely number of rows needed to hold text in a
  41. textbox. It returns either that number or max, which ever is lower.
  42. =item merge ($hashref, keys => @list, index => $number);
  43. This command merges the $hashref into the current object. If keys are
  44. specified, only those keys are used. Otherwise all keys are merged.
  45. If an index is specified, the merged keys are given a form of
  46. "$key" . "_$index", otherwise the key is used on both sides.
  47. =item redirect (msg => $string)
  48. This function redirects to the script and argument set determined by
  49. $self->{callback}, and if this is not set, goes to an info screen and prints
  50. $msg.
  51. =item redo_rows (fields => \@list, count => $integer, [index => $string);
  52. This function is undergoing serious redesign at the moment. If index is
  53. defined, that field is used for ordering the rows. If not, runningnumber is
  54. used. Behavior is not defined when index points to a field containing
  55. non-numbers.
  56. =item set (@attrs)
  57. Copies the given key=>vars to $self. Allows for finer control of
  58. merging hashes into self.
  59. =item remove_cgi_globals()
  60. Removes all elements starting with a . because these elements conflict with the
  61. ability to hide the entire structure for things like CSV lookups.
  62. =back
  63. =head1 Copyright (C) 2006, The LedgerSMB core team.
  64. # This work contains copyrighted information from a number of sources
  65. # all used with permission.
  66. #
  67. # This file contains source code included with or based on SQL-Ledger
  68. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  69. # and licensed under the GNU General Public License version 2 or, at
  70. # your option, any later version. For a full list including contact
  71. # information of contributors, maintainers, and copyright holders,
  72. # see the CONTRIBUTORS file.
  73. #
  74. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  75. # Copyright (C) 2000
  76. #
  77. # Author: DWS Systems Inc.
  78. # Web: http://www.sql-ledger.org
  79. #
  80. # Contributors: Thomas Bayen <bayen@gmx.de>
  81. # Antti Kaihola <akaihola@siba.fi>
  82. # Moritz Bunkus (tex)
  83. # Jim Rawlings <jim@your-dba.com> (DB2)
  84. #====================================================================
  85. =cut
  86. use CGI::Simple;
  87. $CGI::Simple::DISABLE_UPLOADS = 0;
  88. use Math::BigFloat;
  89. use LedgerSMB::Sysconfig;
  90. use Data::Dumper;
  91. use LedgerSMB::Auth;
  92. use LedgerSMB::Template;
  93. use LedgerSMB::Locale;
  94. use LedgerSMB::User;
  95. use strict;
  96. $CGI::Simple::POST_MAX = -1;
  97. package LedgerSMB;
  98. our $VERSION = '1.2.99';
  99. sub new {
  100. my $type = shift @_;
  101. my $argstr = shift @_;
  102. my %cookie;
  103. my $self = {};
  104. $self->{version} = $VERSION;
  105. $self->{dbversion} = "1.2.0";
  106. bless $self, $type;
  107. my $query = ($argstr) ? new CGI::Simple($argstr) : new CGI::Simple;
  108. my $params = $query->Vars;
  109. $self->{VERSION} = $VERSION;
  110. $self->merge($params);
  111. $self->{have_latex} = $LedgerSMB::Sysconfig::latex;
  112. # Adding this so that empty values are stored in the db as NULL's. If
  113. # stored procedures want to handle them differently, they must opt to do so.
  114. # -- CT
  115. for (keys %$self){
  116. if ($self->{$_} eq ''){
  117. $self->{$_} = undef;
  118. }
  119. }
  120. if ($self->is_run_mode('cgi', 'mod_perl')) {
  121. $ENV{HTTP_COOKIE} =~ s/;\s*/;/g;
  122. my @cookies = split /;/, $ENV{HTTP_COOKIE};
  123. foreach (@cookies) {
  124. my ( $name, $value ) = split /=/, $_, 2;
  125. $cookie{$name} = $value;
  126. }
  127. }
  128. $self->{action} =~ s/\W/_/g;
  129. $self->{action} = lc $self->{action};
  130. if ( $self->{path} eq "bin/lynx" ) {
  131. $self->{menubar} = 1;
  132. # Applying the path is deprecated. Use menubar instead. CT.
  133. $self->{lynx} = 1;
  134. $self->{path} = "bin/lynx";
  135. }
  136. else {
  137. $self->{path} = "bin/mozilla";
  138. }
  139. if ( ( $self->{script} =~ m#(\.\.|\\|/)# ) ) {
  140. $self->error("Access Denied");
  141. }
  142. if (!$self->{script}) {
  143. $self->{script} = 'login.pl';
  144. }
  145. # if ($self->{action} eq 'migrate_user'){
  146. # return $self;
  147. # }
  148. if ($self->{script} eq 'login.pl' &&
  149. ($self->{action} eq 'authenticate' || $self->{action} eq '__default'
  150. || !$self->{action})){
  151. return $self;
  152. }
  153. if (!$self->{company} && $self->is_run_mode('cgi', 'mod_perl')){
  154. my $ccookie = $cookie{LedgerSMB};
  155. $ccookie =~ s/.*:([^:]*)$/$1/;
  156. $self->{company} = $ccookie;
  157. }
  158. $self->_db_init;
  159. if ($self->is_run_mode('cgi', 'mod_perl')) {
  160. #check for valid session unless this is an inital authentication
  161. #request -- CT
  162. if (!LedgerSMB::Auth::session_check( $cookie{"LedgerSMB"}, $self) ) {
  163. print STDERR "Session did not check";
  164. $self->_get_password("Session Expired");
  165. exit;
  166. }
  167. $self->{_user} = LedgerSMB::User->fetch_config($self);
  168. }
  169. my %date_setting = (
  170. 'mm/dd/yy' => "SQL, US",
  171. 'mm-dd-yy' => "POSTGRES, US",
  172. 'dd/mm/yy' => "SQL, EUROPEAN",
  173. 'dd-mm-yy' => "POSTGRES, EUROPEAN",
  174. 'dd.mm.yy' => "GERMAN",
  175. );
  176. $self->{dbh}->do("set DateStyle to '"
  177. .$date_setting{$self->{_user}->{dateformat}}."'");
  178. #my $locale = LedgerSMB::Locale->get_handle($self->{_user}->{countrycode})
  179. $self->{_locale} = LedgerSMB::Locale->get_handle('en') # temporary
  180. or $self->error(__FILE__.':'.__LINE__.": Locale not loaded: $!\n");
  181. $self->{stylesheet} = $self->{_user}->{stylesheet};
  182. return $self;
  183. }
  184. #This function needs to be moved into the session handler.
  185. sub _get_password {
  186. my ($self) = shift @_;
  187. $self->{sessionexpired} = shift @_;
  188. LedgerSMB::Auth::credential_prompt();
  189. exit;
  190. }
  191. sub debug {
  192. my $self = shift @_;
  193. my $args = shift @_;
  194. my $file;
  195. if (scalar keys %$args){
  196. $file = $args->{'file'};
  197. }
  198. my $d = Data::Dumper->new( [$self] );
  199. $d->Sortkeys(1);
  200. if ($file) {
  201. open( FH, '>', "$file" ) or die $!;
  202. print FH $d->Dump();
  203. close(FH);
  204. }
  205. else {
  206. print "\n";
  207. print $d->Dump();
  208. }
  209. }
  210. sub escape {
  211. my $self = shift;
  212. my %args = @_;
  213. my $str = $args{string};
  214. my $regex = qr/([^a-zA-Z0-9_.-])/;
  215. $str =~ s/$regex/sprintf("%%%02x", ord($1))/ge;
  216. return $str;
  217. }
  218. sub is_blank {
  219. my $self = shift @_;
  220. my %args = @_;
  221. my $name = $args{name};
  222. if (not defined $name){
  223. # TODO: Raise error
  224. }
  225. my $rc;
  226. if ( $self->{$name} =~ /^\s*$/ ) {
  227. $rc = 1;
  228. }
  229. else {
  230. $rc = 0;
  231. }
  232. $rc;
  233. }
  234. sub is_run_mode {
  235. my $self = shift @_;
  236. my $mode = lc shift @_;
  237. my $rc = 0;
  238. if ( $mode eq 'cgi' && $ENV{GATEWAY_INTERFACE} ) {
  239. $rc = 1;
  240. }
  241. elsif ( $mode eq 'cli' && !( $ENV{GATEWAY_INTERFACE} || $ENV{MOD_PERL} ) ) {
  242. $rc = 1;
  243. }
  244. elsif ( $mode eq 'mod_perl' && $ENV{MOD_PERL} ) {
  245. $rc = 1;
  246. }
  247. $rc;
  248. }
  249. sub num_text_rows {
  250. my $self = shift @_;
  251. my %args = @_;
  252. my $string = $args{string};
  253. my $cols = $args{cols};
  254. my $maxrows = $args{max};
  255. my $rows = 0;
  256. for ( split /\n/, $string ) {
  257. my $line = $_;
  258. while ( length($line) > $cols ) {
  259. my $fragment = substr( $line, 0, $cols + 1 );
  260. $fragment =~ s/^(.*)\W.*$/$1/;
  261. $line =~ s/$fragment//;
  262. if ( $line eq $fragment ) { # No word breaks!
  263. $line = "";
  264. }
  265. ++$rows;
  266. }
  267. ++$rows;
  268. }
  269. if ( !defined $maxrows ) {
  270. $maxrows = $rows;
  271. }
  272. return ( $rows > $maxrows ) ? $maxrows : $rows;
  273. }
  274. sub redirect {
  275. my $self = shift @_;
  276. my %args = @_;
  277. my $msg = $args{msg};
  278. if ( $self->{callback} || !$msg ) {
  279. main::redirect();
  280. exit;
  281. }
  282. else {
  283. $self->info($msg);
  284. }
  285. }
  286. # TODO: Either we should have an amount class with formats and such attached
  287. # Or maybe we should move this into the user class...
  288. sub format_amount {
  289. # Based on SQL-Ledger's Form::format_amount
  290. my $self = shift @_;
  291. my %args = @_;
  292. my $myconfig = $args{user} || $self->{_user};
  293. my $amount = $args{amount};
  294. my $places = $args{precision};
  295. my $dash = $args{neg_format};
  296. my $negative;
  297. if ($amount) {
  298. $amount = $self->parse_amount( 'user' => $myconfig, 'amount' => $amount );
  299. $negative = ( $amount < 0 );
  300. $amount =~ s/-//;
  301. }
  302. if ( $places =~ /\d+/ ) {
  303. #$places = 4 if $places == 2;
  304. $amount = $self->round_amount( $amount, $places );
  305. }
  306. # is the amount negative
  307. # Parse $myconfig->{numberformat}
  308. my ( $ts, $ds ) = ( $1, $2 );
  309. if ($amount) {
  310. if ( $myconfig->{numberformat} ) {
  311. my ( $whole, $dec ) = split /\./, "$amount";
  312. $amount = join '', reverse split //, $whole;
  313. if ($places) {
  314. $dec .= "0" x $places;
  315. $dec = substr( $dec, 0, $places );
  316. }
  317. if ( $myconfig->{numberformat} eq '1,000.00' ) {
  318. $amount =~ s/\d{3,}?/$&,/g;
  319. $amount =~ s/,$//;
  320. $amount = join '', reverse split //, $amount;
  321. $amount .= "\.$dec" if ( $dec ne "" );
  322. }
  323. elsif ( $myconfig->{numberformat} eq '1 000.00' ) {
  324. $amount =~ s/\d{3,}?/$& /g;
  325. $amount =~ s/\s$//;
  326. $amount = join '', reverse split //, $amount;
  327. $amount .= "\.$dec" if ( $dec ne "" );
  328. }
  329. elsif ( $myconfig->{numberformat} eq "1'000.00" ) {
  330. $amount =~ s/\d{3,}?/$&'/g;
  331. $amount =~ s/'$//;
  332. $amount = join '', reverse split //, $amount;
  333. $amount .= "\.$dec" if ( $dec ne "" );
  334. }
  335. elsif ( $myconfig->{numberformat} eq '1.000,00' ) {
  336. $amount =~ s/\d{3,}?/$&./g;
  337. $amount =~ s/\.$//;
  338. $amount = join '', reverse split //, $amount;
  339. $amount .= ",$dec" if ( $dec ne "" );
  340. }
  341. elsif ( $myconfig->{numberformat} eq '1000,00' ) {
  342. $amount = "$whole";
  343. $amount .= ",$dec" if ( $dec ne "" );
  344. }
  345. elsif ( $myconfig->{numberformat} eq '1000.00' ) {
  346. $amount = "$whole";
  347. $amount .= ".$dec" if ( $dec ne "" );
  348. }
  349. if ( $dash =~ /-/ ) {
  350. $amount = ($negative) ? "($amount)" : "$amount";
  351. }
  352. elsif ( $dash =~ /DRCR/ ) {
  353. $amount = ($negative) ? "$amount DR" : "$amount CR";
  354. }
  355. else {
  356. $amount = ($negative) ? "-$amount" : "$amount";
  357. }
  358. }
  359. }
  360. else {
  361. if ( $dash eq "0" && $places ) {
  362. if ( $myconfig->{numberformat} =~ /0,00$/ ) {
  363. $amount = "0" . "," . "0" x $places;
  364. }
  365. else {
  366. $amount = "0" . "." . "0" x $places;
  367. }
  368. }
  369. else {
  370. $amount = ( $dash ne "" ) ? "$dash" : "";
  371. }
  372. }
  373. $amount;
  374. }
  375. # This should probably go to the User object too.
  376. sub parse_amount {
  377. my $self = shift @_;
  378. my %args = @_;
  379. my $myconfig = $args{user} || $self->{_user};
  380. my $amount = $args{amount};
  381. if ( $amount eq '' or ! defined $amount) {
  382. return 0;
  383. }
  384. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  385. { # Amount may not be an object
  386. return $amount;
  387. }
  388. my $numberformat = $myconfig->{numberformat};
  389. if ( ( $numberformat eq '1.000,00' )
  390. || ( $numberformat eq '1000,00' ) )
  391. {
  392. $amount =~ s/\.//g;
  393. $amount =~ s/,/./;
  394. }
  395. elsif ( $numberformat eq '1 000.00' ) {
  396. $amount =~ s/\s//g;
  397. }
  398. elsif ( $numberformat eq "1'000.00" ) {
  399. $amount =~ s/'//g;
  400. }
  401. $amount =~ s/,//g;
  402. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  403. $amount = $1 * -1;
  404. }
  405. elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  406. $amount = $1 * -1;
  407. }
  408. $amount =~ s/\s?CR//;
  409. $amount = new Math::BigFloat($amount);
  410. if ($amount->is_nan){
  411. $self->error("Invalid number detected during parsing");
  412. }
  413. return ( $amount * 1 );
  414. }
  415. sub round_amount {
  416. my ( $self, $amount, $places ) = @_;
  417. # These rounding rules follow from the previous implementation.
  418. # They should be changed to allow different rules for different accounts.
  419. if ($amount >= 0) {
  420. Math::BigFloat->round_mode('+inf');
  421. }
  422. else {
  423. Math::BigFloat->round_mode('-inf');
  424. }
  425. if ($places >= 0) {
  426. $amount = Math::BigFloat->new($amount)->ffround( -$places );
  427. }
  428. else {
  429. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) );
  430. }
  431. $amount->precision(undef);
  432. return $amount;
  433. }
  434. sub call_procedure {
  435. my $self = shift @_;
  436. my %args = @_;
  437. my $procname = $args{procname};
  438. my @call_args;
  439. @call_args = @{ $args{args} } if defined $args{args};
  440. my $order_by = $args{order_by};
  441. my $argstr = "";
  442. my @results;
  443. if (!defined $procname){
  444. $self->error('Undefined function in call_procedure.');
  445. }
  446. $procname = $self->{dbh}->quote_identifier($procname);
  447. for ( 1 .. scalar @call_args ) {
  448. $argstr .= "?, ";
  449. }
  450. $argstr =~ s/\, $//;
  451. my $query = "SELECT * FROM $procname()";
  452. if ($order_by){
  453. $query .= " ORDER BY $order_by";
  454. }
  455. $query =~ s/\(\)/($argstr)/;
  456. my $sth = $self->{dbh}->prepare($query);
  457. if (scalar @call_args){
  458. $sth->execute(@call_args) || $self->error($self->{dbh}->errstr);
  459. } else {
  460. $sth->execute() || $self->error($self->{dbh}->errstr . ':' . $query);
  461. }
  462. my @types = @{$sth->{TYPE}};
  463. my @names = @{$sth->{NAME_lc}};
  464. while ( my $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  465. for (0 .. $#names){
  466. # numeric float4/real
  467. if ($types[$_] == 3 or $types[$_] == 2) {
  468. $ref->{$names[$_]} = Math::BigFloat->new($ref->{$names[$_]});
  469. }
  470. }
  471. push @results, $ref;
  472. }
  473. @results;
  474. }
  475. # Keeping this here due to common requirements
  476. sub is_allowed_role {
  477. my $self = shift @_;
  478. my %args = @_;
  479. my @roles = @{$args{allowed_roles}};
  480. for my $role (@roles){
  481. if (scalar(grep /^$role$/, $self->{_roles})){
  482. return 1;
  483. }
  484. }
  485. return 1; # TODO change to 0 when the role system is implmented
  486. }
  487. # This should probably be moved to User too...
  488. sub date_to_number {
  489. #based on SQL-Ledger's Form::datetonum
  490. my $self = shift @_;
  491. my %args = @_;
  492. my $myconfig = $args{user};
  493. my $date = $args{date};
  494. my ( $yy, $mm, $dd );
  495. if ( $date && $date =~ /\D/ ) {
  496. if ( $date =~ /^\d{4}-\d\d-\d\d$/ ) {
  497. ( $yy, $mm, $dd ) = split /\D/, $date;
  498. } elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  499. ( $yy, $mm, $dd ) = split /\D/, $date;
  500. } elsif ( $myconfig->{dateformat} =~ /^mm/ ) {
  501. ( $mm, $dd, $yy ) = split /\D/, $date;
  502. } elsif ( $myconfig->{dateformat} =~ /^dd/ ) {
  503. ( $dd, $mm, $yy ) = split /\D/, $date;
  504. }
  505. $dd *= 1;
  506. $mm *= 1;
  507. $yy += 2000 if length $yy == 2;
  508. $dd = substr( "0$dd", -2 );
  509. $mm = substr( "0$mm", -2 );
  510. $date = "$yy$mm$dd";
  511. }
  512. $date;
  513. }
  514. # To be replaced with a generic interface to an Error class
  515. sub error {
  516. my ( $self, $msg ) = @_;
  517. if ( $ENV{GATEWAY_INTERFACE} ) {
  518. $self->{msg} = $msg;
  519. $self->{format} = "html";
  520. delete $self->{pre};
  521. print qq|Content-Type: text/html; charset=utf-8\n\n|;
  522. print "<head></head>";
  523. print
  524. qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;
  525. exit;
  526. }
  527. else {
  528. if ( $ENV{error_function} ) {
  529. &{ $ENV{error_function} }($msg);
  530. }
  531. die "Error: $msg\n";
  532. }
  533. }
  534. # Database routines used throughout
  535. sub _db_init {
  536. my $self = shift @_;
  537. my %args = @_;
  538. my $creds = LedgerSMB::Auth::get_credentials();
  539. $self->{login} = $creds->{login};
  540. if (!$self->{company}){
  541. $self->{company} = $LedgerSMB::Sysconfig::default_db;
  542. }
  543. my $dbname = $self->{company};
  544. # Note that we have to request the login/password again if the db
  545. # connection fails since this probably means bad credentials are entered.
  546. # Just in case, however, I think it is a good idea to include the DBI
  547. # error string. CT
  548. $self->{dbh} = DBI->connect(
  549. "dbi:Pg:dbname=$dbname", "$creds->{login}", "$creds->{password}", { AutoCommit => 0 }
  550. );
  551. my $dbh = $self->{dbh};
  552. if (($self->{script} eq 'login.pl') && ($self->{action} eq
  553. 'authenticate')){
  554. return;
  555. }
  556. elsif (!$dbh){
  557. $self->_get_password;
  558. }
  559. $dbh->{pg_server_prepare} = 0;
  560. $dbh->{pg_enable_utf8} = 1;
  561. # This is the general version check
  562. my $sth = $dbh->prepare("
  563. SELECT value FROM defaults
  564. WHERE setting_key = 'version'");
  565. $sth->execute;
  566. my ($dbversion) = $sth->fetchrow_array;
  567. if ($dbversion ne $self->{dbversion}){
  568. $self->error("Database is not the expected version. Was $dbversion, expected $self->{dbversion}");
  569. }
  570. my $query = "SELECT t.extends,
  571. coalesce (t.table_name, 'custom_' || extends)
  572. || ':' || f.field_name as field_def
  573. FROM custom_table_catalog t
  574. JOIN custom_field_catalog f USING (table_id)";
  575. my $sth = $self->{dbh}->prepare($query);
  576. $sth->execute;
  577. my $ref;
  578. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  579. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  580. $ref->{field_def};
  581. }
  582. }
  583. # Deprecated, only here for old code
  584. sub dberror{
  585. my $self = shift @_;
  586. $self->error(@_);
  587. }
  588. sub redo_rows {
  589. my $self = shift @_;
  590. my %args = @_;
  591. my @flds = @{ $args{fields} };
  592. my $count = $args{count};
  593. my $index = ( $args{index} ) ? $args{index} : 'runningnumber';
  594. my @rows;
  595. my $i; # incriment counter use only
  596. for $i ( 1 .. $count ) {
  597. my $temphash = { _inc => $i };
  598. for my $fld (@flds) {
  599. $temphash->{$fld} = $self->{ "$fld" . "_$i" };
  600. }
  601. push @rows, $temphash;
  602. }
  603. $i = 1;
  604. for my $row ( sort { $a->{index} <=> $b->{index} } @rows ) {
  605. for my $fld (@flds) {
  606. $self->{ "$fld" . "_$i" } = $row->{$fld};
  607. }
  608. ++$i;
  609. }
  610. }
  611. sub merge {
  612. my ( $self, $src ) = @_;
  613. for my $arg ( $self, $src ) {
  614. shift;
  615. }
  616. my %args = @_;
  617. my @keys;
  618. if (defined $args{keys}){
  619. @keys = @{ $args{keys} };
  620. }
  621. my $index = $args{index};
  622. if ( !scalar @keys ) {
  623. @keys = keys %{$src};
  624. }
  625. for my $arg ( @keys ) {
  626. my $dst_arg;
  627. if ($index) {
  628. $dst_arg = $arg . "_$index";
  629. }
  630. else {
  631. $dst_arg = $arg;
  632. }
  633. $self->{$dst_arg} = $src->{$arg};
  634. }
  635. }
  636. sub type {
  637. my $self = shift @_;
  638. if (!$ENV{REQUEST_METHOD} or
  639. ( !grep {$ENV{REQUEST_METHOD} eq $_} ("HEAD", "GET", "POST") ) ) {
  640. $self->error("Request method unset or set to unknown value");
  641. }
  642. return $ENV{REQUEST_METHOD};
  643. }
  644. sub DESTROY {}
  645. sub set {
  646. my $self = shift @_;
  647. my %args = @_;
  648. for my $arg (keys(%args)) {
  649. $self->{$arg} = $args{$arg};
  650. }
  651. return 1;
  652. }
  653. sub remove_cgi_globals {
  654. my ($self) = @_;
  655. for my $key (keys %$self){
  656. if ($key =~ /^\./){
  657. delete $self->{key}
  658. }
  659. }
  660. }
  661. sub take_top_level {
  662. my ($self) = @_;
  663. my $return_hash = {};
  664. for my $key (keys %$self){
  665. if (!ref($self->{$key}) && $key !~ /^\./){
  666. $return_hash->{$key} = $self->{$key}
  667. }
  668. }
  669. return $return_hash;
  670. }
  671. 1;