summaryrefslogtreecommitdiff
path: root/LedgerSMB.pm
blob: 08bf1a380d78cbfb8d126dc784592edfdfe2494b (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::Sysconfig::cookie_name}};
  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::Sysconfig::cookie_name}}, $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 = (ref($_[0]) eq 'HASH')? %{$_[0]}: @_;
  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 $format = $args{format};
  297. if (!defined $format){
  298. $format = $myconfig->{numberformat}
  299. }
  300. if (!defined $args{precision} and defined $args{money}){
  301. $places = $LedgerSMB::Sysconfig::decimal_places;
  302. }
  303. my $negative;
  304. if ($amount) {
  305. $amount = $self->parse_amount( 'user' => $myconfig, 'amount' => $amount );
  306. $negative = ( $amount < 0 );
  307. $amount =~ s/-//;
  308. }
  309. if ( $places =~ /\d+/ ) {
  310. #$places = 4 if $places == 2;
  311. $amount = $self->round_amount( $amount, $places );
  312. }
  313. # is the amount negative
  314. # Parse $myconfig->{numberformat}
  315. my ( $ts, $ds ) = ( $1, $2 );
  316. if ($amount) {
  317. if ( $format ) {
  318. my ( $whole, $dec ) = split /\./, "$amount";
  319. $amount = join '', reverse split //, $whole;
  320. if ($places) {
  321. $dec .= "0" x $places;
  322. $dec = substr( $dec, 0, $places );
  323. }
  324. if ( $format eq '1,000.00' ) {
  325. $amount =~ s/\d{3,}?/$&,/g;
  326. $amount =~ s/,$//;
  327. $amount = join '', reverse split //, $amount;
  328. $amount .= "\.$dec" if ( $dec ne "" );
  329. }
  330. elsif ( $format eq '1 000.00' ) {
  331. $amount =~ s/\d{3,}?/$& /g;
  332. $amount =~ s/\s$//;
  333. $amount = join '', reverse split //, $amount;
  334. $amount .= "\.$dec" if ( $dec ne "" );
  335. }
  336. elsif ( $format eq "1'000.00" ) {
  337. $amount =~ s/\d{3,}?/$&'/g;
  338. $amount =~ s/'$//;
  339. $amount = join '', reverse split //, $amount;
  340. $amount .= "\.$dec" if ( $dec ne "" );
  341. }
  342. elsif ( $format eq '1.000,00' ) {
  343. $amount =~ s/\d{3,}?/$&./g;
  344. $amount =~ s/\.$//;
  345. $amount = join '', reverse split //, $amount;
  346. $amount .= ",$dec" if ( $dec ne "" );
  347. }
  348. elsif ( $format eq '1000,00' ) {
  349. $amount = "$whole";
  350. $amount .= ",$dec" if ( $dec ne "" );
  351. }
  352. elsif ( $format eq '1000.00' ) {
  353. $amount = "$whole";
  354. $amount .= ".$dec" if ( $dec ne "" );
  355. }
  356. if ( $dash =~ /-/ ) {
  357. $amount = ($negative) ? "($amount)" : "$amount";
  358. }
  359. elsif ( $dash =~ /DRCR/ ) {
  360. $amount = ($negative) ? "$amount DR" : "$amount CR";
  361. }
  362. else {
  363. $amount = ($negative) ? "-$amount" : "$amount";
  364. }
  365. }
  366. }
  367. else {
  368. if ( $dash eq "0" && $places ) {
  369. if ( $format =~ /0,00$/ ) {
  370. $amount = "0" . "," . "0" x $places;
  371. }
  372. else {
  373. $amount = "0" . "." . "0" x $places;
  374. }
  375. }
  376. else {
  377. $amount = ( $dash ne "" ) ? "$dash" : "";
  378. }
  379. }
  380. $amount;
  381. }
  382. # This should probably go to the User object too.
  383. sub parse_amount {
  384. my $self = shift @_;
  385. my %args = @_;
  386. my $myconfig = $args{user} || $self->{_user};
  387. my $amount = $args{amount};
  388. if ( $amount eq '' or ! defined $amount) {
  389. return 0;
  390. }
  391. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  392. { # Amount may not be an object
  393. return $amount;
  394. }
  395. my $numberformat = $myconfig->{numberformat};
  396. if ( ( $numberformat eq '1.000,00' )
  397. || ( $numberformat eq '1000,00' ) )
  398. {
  399. $amount =~ s/\.//g;
  400. $amount =~ s/,/./;
  401. }
  402. elsif ( $numberformat eq '1 000.00' ) {
  403. $amount =~ s/\s//g;
  404. }
  405. elsif ( $numberformat eq "1'000.00" ) {
  406. $amount =~ s/'//g;
  407. }
  408. $amount =~ s/,//g;
  409. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  410. $amount = $1 * -1;
  411. }
  412. elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  413. $amount = $1 * -1;
  414. }
  415. $amount =~ s/\s?CR//;
  416. $amount = new Math::BigFloat($amount);
  417. if ($amount->is_nan){
  418. $self->error("Invalid number detected during parsing");
  419. }
  420. return ( $amount * 1 );
  421. }
  422. sub round_amount {
  423. my ( $self, $amount, $places ) = @_;
  424. #
  425. # We will grab the default value, if it isnt defined
  426. #
  427. if (!$places){
  428. $places = ${LedgerSMB::Sysconfig::decimal_places};
  429. }
  430. # These rounding rules follow from the previous implementation.
  431. # They should be changed to allow different rules for different accounts.
  432. if ($amount >= 0) {
  433. Math::BigFloat->round_mode('+inf');
  434. }
  435. else {
  436. Math::BigFloat->round_mode('-inf');
  437. }
  438. if ($places >= 0) {
  439. $amount = Math::BigFloat->new($amount)->ffround( -$places );
  440. }
  441. else {
  442. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) );
  443. }
  444. $amount->precision(undef);
  445. return $amount;
  446. }
  447. sub call_procedure {
  448. my $self = shift @_;
  449. my %args = @_;
  450. my $procname = $args{procname};
  451. my @call_args;
  452. @call_args = @{ $args{args} } if defined $args{args};
  453. my $order_by = $args{order_by};
  454. my $argstr = "";
  455. my @results;
  456. if (!defined $procname){
  457. $self->error('Undefined function in call_procedure.');
  458. }
  459. $procname = $self->{dbh}->quote_identifier($procname);
  460. for ( 1 .. scalar @call_args ) {
  461. $argstr .= "?, ";
  462. }
  463. $argstr =~ s/\, $//;
  464. my $query = "SELECT * FROM $procname()";
  465. if ($order_by){
  466. $query .= " ORDER BY $order_by";
  467. }
  468. $query =~ s/\(\)/($argstr)/;
  469. my $sth = $self->{dbh}->prepare($query);
  470. if (scalar @call_args){
  471. $sth->execute(@call_args) || $self->dberror($self->{dbh}->errstr);
  472. } else {
  473. $sth->execute() || $self->dberror($self->{dbh}->errstr . ':' . $query);
  474. }
  475. my @types = @{$sth->{TYPE}};
  476. my @names = @{$sth->{NAME_lc}};
  477. while ( my $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  478. for (0 .. $#names){
  479. # numeric float4/real
  480. if ($types[$_] == 3 or $types[$_] == 2) {
  481. $ref->{$names[$_]} = Math::BigFloat->new($ref->{$names[$_]});
  482. }
  483. }
  484. push @results, $ref;
  485. }
  486. @results;
  487. }
  488. # Keeping this here due to common requirements
  489. sub is_allowed_role {
  490. my ($self, $args) = @_;
  491. my @roles = @{$args->{allowed_roles}};
  492. for my $role (@roles){
  493. my @roleset = grep m/^$role$/, @{$self->{_roles}};
  494. if (scalar @roleset){
  495. return 1;
  496. }
  497. }
  498. return 0; # TODO change to 0 when the role system is implmented
  499. }
  500. # This should probably be moved to User too...
  501. sub date_to_number {
  502. #based on SQL-Ledger's Form::datetonum
  503. my $self = shift @_;
  504. my %args = @_;
  505. my $myconfig = $args{user};
  506. my $date = $args{date};
  507. my ( $yy, $mm, $dd );
  508. if ( $date && $date =~ /\D/ ) {
  509. if ( $date =~ /^\d{4}-\d\d-\d\d$/ ) {
  510. ( $yy, $mm, $dd ) = split /\D/, $date;
  511. } elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  512. ( $yy, $mm, $dd ) = split /\D/, $date;
  513. } elsif ( $myconfig->{dateformat} =~ /^mm/ ) {
  514. ( $mm, $dd, $yy ) = split /\D/, $date;
  515. } elsif ( $myconfig->{dateformat} =~ /^dd/ ) {
  516. ( $dd, $mm, $yy ) = split /\D/, $date;
  517. }
  518. $dd *= 1;
  519. $mm *= 1;
  520. $yy += 2000 if length $yy == 2;
  521. $dd = substr( "0$dd", -2 );
  522. $mm = substr( "0$mm", -2 );
  523. $date = "$yy$mm$dd";
  524. }
  525. $date;
  526. }
  527. # To be replaced with a generic interface to an Error class
  528. sub error {
  529. my ( $self, $msg ) = @_;
  530. if ( $ENV{GATEWAY_INTERFACE} ) {
  531. $self->{msg} = $msg;
  532. $self->{format} = "html";
  533. delete $self->{pre};
  534. print qq|Content-Type: text/html; charset=utf-8\n\n|;
  535. print "<head></head>";
  536. print
  537. qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;
  538. exit;
  539. }
  540. else {
  541. if ( $ENV{error_function} ) {
  542. &{ $ENV{error_function} }($msg);
  543. }
  544. die "Error: $msg\n";
  545. }
  546. }
  547. # Database routines used throughout
  548. sub _db_init {
  549. my $self = shift @_;
  550. my %args = @_;
  551. my $creds = LedgerSMB::Auth::get_credentials();
  552. $self->{login} = $creds->{login};
  553. if (!$self->{company}){
  554. $self->{company} = $LedgerSMB::Sysconfig::default_db;
  555. }
  556. my $dbname = $self->{company};
  557. # Note that we have to request the login/password again if the db
  558. # connection fails since this probably means bad credentials are entered.
  559. # Just in case, however, I think it is a good idea to include the DBI
  560. # error string. CT
  561. $self->{dbh} = DBI->connect(
  562. "dbi:Pg:dbname=$dbname", "$creds->{login}", "$creds->{password}", { AutoCommit => 0 }
  563. );
  564. my $dbh = $self->{dbh};
  565. if (($self->{script} eq 'login.pl') && ($self->{action} eq
  566. 'authenticate')){
  567. return;
  568. }
  569. elsif (!$dbh){
  570. $self->_get_password;
  571. }
  572. $dbh->{pg_server_prepare} = 0;
  573. $dbh->{pg_enable_utf8} = 1;
  574. # This is the general version check
  575. my $sth = $dbh->prepare("
  576. SELECT value FROM defaults
  577. WHERE setting_key = 'version'");
  578. $sth->execute;
  579. my ($dbversion) = $sth->fetchrow_array;
  580. if ($dbversion ne $self->{dbversion}){
  581. $self->error("Database is not the expected version. Was $dbversion, expected $self->{dbversion}");
  582. }
  583. my $query = "SELECT t.extends,
  584. coalesce (t.table_name, 'custom_' || extends)
  585. || ':' || f.field_name as field_def
  586. FROM custom_table_catalog t
  587. JOIN custom_field_catalog f USING (table_id)";
  588. my $sth = $self->{dbh}->prepare($query);
  589. $sth->execute;
  590. my $ref;
  591. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  592. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  593. $ref->{field_def};
  594. }
  595. # Adding role list to self
  596. $self->{_roles} = [];
  597. $query = "select rolname from pg_roles
  598. where pg_has_role(SESSION_USER, 'USAGE')";
  599. $sth = $dbh->prepare($query);
  600. $sth->execute();
  601. while (my @roles = $sth->fetchrow_array){
  602. push @{$self->{_roles}}, $roles[0];
  603. }
  604. }
  605. # Deprecated, only here for old code
  606. sub dberror{
  607. my $self = shift @_;
  608. if ($self->{dbh}->state eq '42501'){
  609. $_[0] = $self->{_locale}->text('Access Denied');
  610. }
  611. print STDERR "Logging SQL State ".$self->{dbh}->state.", error ".
  612. $self->{dbh}->err . ", string " .$self->{dbh}->errstr . "\n";
  613. $self->error(@_);
  614. }
  615. sub redo_rows {
  616. my $self = shift @_;
  617. my %args = @_;
  618. my @flds = @{ $args{fields} };
  619. my $count = $args{count};
  620. my $index = ( $args{index} ) ? $args{index} : 'runningnumber';
  621. my @rows;
  622. my $i; # incriment counter use only
  623. for $i ( 1 .. $count ) {
  624. my $temphash = { _inc => $i };
  625. for my $fld (@flds) {
  626. $temphash->{$fld} = $self->{ "$fld" . "_$i" };
  627. }
  628. push @rows, $temphash;
  629. }
  630. $i = 1;
  631. for my $row ( sort { $a->{index} <=> $b->{index} } @rows ) {
  632. for my $fld (@flds) {
  633. $self->{ "$fld" . "_$i" } = $row->{$fld};
  634. }
  635. ++$i;
  636. }
  637. }
  638. sub merge {
  639. my ( $self, $src ) = @_;
  640. for my $arg ( $self, $src ) {
  641. shift;
  642. }
  643. my %args = @_;
  644. my @keys;
  645. if (defined $args{keys}){
  646. @keys = @{ $args{keys} };
  647. }
  648. my $index = $args{index};
  649. if ( !scalar @keys ) {
  650. @keys = keys %{$src};
  651. }
  652. for my $arg ( @keys ) {
  653. my $dst_arg;
  654. if ($index) {
  655. $dst_arg = $arg . "_$index";
  656. }
  657. else {
  658. $dst_arg = $arg;
  659. }
  660. $self->{$dst_arg} = $src->{$arg};
  661. }
  662. }
  663. sub type {
  664. my $self = shift @_;
  665. if (!$ENV{REQUEST_METHOD} or
  666. ( !grep {$ENV{REQUEST_METHOD} eq $_} ("HEAD", "GET", "POST") ) ) {
  667. $self->error("Request method unset or set to unknown value");
  668. }
  669. return $ENV{REQUEST_METHOD};
  670. }
  671. sub DESTROY {}
  672. sub set {
  673. my $self = shift @_;
  674. my %args = @_;
  675. for my $arg (keys(%args)) {
  676. $self->{$arg} = $args{$arg};
  677. }
  678. return 1;
  679. }
  680. sub remove_cgi_globals {
  681. my ($self) = @_;
  682. for my $key (keys %$self){
  683. if ($key =~ /^\./){
  684. delete $self->{key}
  685. }
  686. }
  687. }
  688. sub take_top_level {
  689. my ($self) = @_;
  690. my $return_hash = {};
  691. for my $key (keys %$self){
  692. if (!ref($self->{$key}) && $key !~ /^\./){
  693. $return_hash->{$key} = $self->{$key}
  694. }
  695. }
  696. return $return_hash;
  697. }
  698. 1;