bcaeb62c13f48795be25720f0f0df1a812e24cfe
[kivitendo-erp.git] / SL / Common.pm
1 #====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #====================================================================
8
9 package Common;
10
11 use utf8;
12 use strict;
13
14 use Carp;
15 use English qw(-no_match_vars);
16 use Time::HiRes qw(gettimeofday);
17 use Data::Dumper;
18 use File::Copy ();
19 use File::stat;
20 use File::Slurp;
21 use File::Spec;
22 use List::MoreUtils qw(apply);
23 use POSIX ();
24 use Encode qw(decode);
25
26 use SL::DBUtils;
27
28 sub unique_id {
29   my ($a, $b) = gettimeofday();
30   return "${a}-${b}-${$}";
31 }
32
33 sub tmpname {
34   return "/tmp/kivitendo-tmp-" . unique_id();
35 }
36
37 sub truncate {
38   my ($text, %params) = @_;
39
40   $params{at}       //= 50;
41   $params{at}         =  3 if 3 > $params{at};
42
43   $params{strip}    //= '';
44
45   $text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x;
46   $text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?:     newlines? | full )$/x;
47
48   return $text if length($text) <= $params{at};
49   return substr($text, 0, $params{at} - 3) . '...';
50 }
51
52 sub retrieve_parts {
53   $main::lxdebug->enter_sub();
54
55   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
56
57   my $dbh = $form->dbconnect($myconfig);
58
59   my (@filter_values, $filter);
60
61   foreach (qw(partnumber description ean)) {
62     next unless $form->{$_};
63
64     $filter .= qq| AND ($_ ILIKE ?)|;
65     push @filter_values, '%' . $form->{$_} . '%';
66   }
67
68   if ($form->{no_assemblies}) {
69     $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
70   }
71   if ($form->{assemblies}) {
72     $filter .= qq| AND assembly=TRUE|;
73   }
74
75   if ($form->{no_services}) {
76     $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|;
77   }
78
79   substr($filter, 1, 3) = "WHERE" if ($filter);
80
81   $order_by =~ s/[^a-zA-Z_]//g;
82   $order_dir = $order_dir ? "ASC" : "DESC";
83
84   my $query =
85     qq|SELECT id, partnumber, description, ean, | .
86     qq|       warehouse_id, bin_id | .
87     qq|FROM parts $filter | .
88     qq|ORDER BY $order_by $order_dir|;
89   my $sth = $dbh->prepare($query);
90   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
91   my $parts = [];
92   while (my $ref = $sth->fetchrow_hashref()) {
93     push(@{$parts}, $ref);
94   }
95   $sth->finish();
96   $dbh->disconnect();
97
98   $main::lxdebug->leave_sub();
99
100   return $parts;
101 }
102
103 sub retrieve_projects {
104   $main::lxdebug->enter_sub();
105
106   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
107
108   my $dbh = $form->dbconnect($myconfig);
109
110   my (@filter_values, $filter);
111   if ($form->{"projectnumber"}) {
112     $filter .= qq| AND (projectnumber ILIKE ?)|;
113     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
114   }
115   if ($form->{"description"}) {
116     $filter .= qq| AND (description ILIKE ?)|;
117     push(@filter_values, '%' . $form->{"description"} . '%');
118   }
119   substr($filter, 1, 3) = "WHERE" if ($filter);
120
121   $order_by =~ s/[^a-zA-Z_]//g;
122   $order_dir = $order_dir ? "ASC" : "DESC";
123
124   my $query =
125     qq|SELECT id, projectnumber, description | .
126     qq|FROM project $filter | .
127     qq|ORDER BY $order_by $order_dir|;
128   my $sth = $dbh->prepare($query);
129   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
130   my $projects = [];
131   while (my $ref = $sth->fetchrow_hashref()) {
132     push(@{$projects}, $ref);
133   }
134   $sth->finish();
135   $dbh->disconnect();
136
137   $main::lxdebug->leave_sub();
138
139   return $projects;
140 }
141
142 sub retrieve_employees {
143   $main::lxdebug->enter_sub();
144
145   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
146
147   my $dbh = $form->dbconnect($myconfig);
148
149   my (@filter_values, $filter);
150   if ($form->{"name"}) {
151     $filter .= qq| AND (name ILIKE ?)|;
152     push(@filter_values, '%' . $form->{"name"} . '%');
153   }
154   substr($filter, 1, 3) = "WHERE" if ($filter);
155
156   $order_by =~ s/[^a-zA-Z_]//g;
157   $order_dir = $order_dir ? "ASC" : "DESC";
158
159   my $query =
160     qq|SELECT id, name | .
161     qq|FROM employee $filter | .
162     qq|ORDER BY $order_by $order_dir|;
163   my $sth = $dbh->prepare($query);
164   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
165   my $employees = [];
166   while (my $ref = $sth->fetchrow_hashref()) {
167     push(@{$employees}, $ref);
168   }
169   $sth->finish();
170   $dbh->disconnect();
171
172   $main::lxdebug->leave_sub();
173
174   return $employees;
175 }
176
177 sub retrieve_customers_or_vendors {
178   $main::lxdebug->enter_sub();
179
180   my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
181
182   my $dbh = $form->dbconnect($myconfig);
183
184   my (@filter_values, $filter);
185   if ($form->{"name"}) {
186     $filter .= " AND (TABLE.name ILIKE ?)";
187     push(@filter_values, '%' . $form->{"name"} . '%');
188   }
189   if (!$form->{"obsolete"}) {
190     $filter .= " AND NOT TABLE.obsolete";
191   }
192   substr($filter, 1, 3) = "WHERE" if ($filter);
193
194   $order_by =~ s/[^a-zA-Z_]//g;
195   $order_dir = $order_dir ? "ASC" : "DESC";
196
197   my (@queries, @query_parameters);
198
199   if ($allow_both || !$is_vendor) {
200     my $c_filter = $filter;
201     $c_filter =~ s/TABLE/c/g;
202     push(@queries, qq|SELECT
203                         c.id, c.name, 0 AS customer_is_vendor,
204                         c.street, c.zipcode, c.city,
205                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
206                       FROM customer c
207                       LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
208                       $c_filter|);
209     push(@query_parameters, @filter_values);
210   }
211
212   if ($allow_both || $is_vendor) {
213     my $v_filter = $filter;
214     $v_filter =~ s/TABLE/v/g;
215     push(@queries, qq|SELECT
216                         v.id, v.name, 1 AS customer_is_vendor,
217                         v.street, v.zipcode, v.city,
218                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
219                       FROM vendor v
220                       LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
221                       $v_filter|);
222     push(@query_parameters, @filter_values);
223   }
224
225   my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
226   my $sth = $dbh->prepare($query);
227   $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
228   my $customers = [];
229   while (my $ref = $sth->fetchrow_hashref()) {
230     push(@{$customers}, $ref);
231   }
232   $sth->finish();
233   $dbh->disconnect();
234
235   $main::lxdebug->leave_sub();
236
237   return $customers;
238 }
239
240 sub retrieve_delivery_customer {
241   $main::lxdebug->enter_sub();
242
243   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
244
245   my $dbh = $form->dbconnect($myconfig);
246
247   my (@filter_values, $filter);
248   if ($form->{"name"}) {
249     $filter .= qq| (name ILIKE ?) AND|;
250     push(@filter_values, '%' . $form->{"name"} . '%');
251   }
252
253   $order_by =~ s/[^a-zA-Z_]//g;
254   $order_dir = $order_dir ? "ASC" : "DESC";
255
256   my $query =
257     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
258     qq!FROM customer ! .
259     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
260     qq!ORDER BY $order_by $order_dir!;
261   my $sth = $dbh->prepare($query);
262   $sth->execute(@filter_values) ||
263     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
264   my $delivery_customers = [];
265   while (my $ref = $sth->fetchrow_hashref()) {
266     push(@{$delivery_customers}, $ref);
267   }
268   $sth->finish();
269   $dbh->disconnect();
270
271   $main::lxdebug->leave_sub();
272
273   return $delivery_customers;
274 }
275
276 sub retrieve_vendor {
277   $main::lxdebug->enter_sub();
278
279   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
280
281   my $dbh = $form->dbconnect($myconfig);
282
283   my (@filter_values, $filter);
284   if ($form->{"name"}) {
285     $filter .= qq| (name ILIKE ?) AND|;
286     push(@filter_values, '%' . $form->{"name"} . '%');
287   }
288
289   $order_by =~ s/[^a-zA-Z_]//g;
290   $order_dir = $order_dir ? "ASC" : "DESC";
291
292   my $query =
293     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
294     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = ?') ! .
295     qq!ORDER BY $order_by $order_dir!;
296   push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
297   my $sth = $dbh->prepare($query);
298   $sth->execute(@filter_values) ||
299     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
300   my $vendors = [];
301   while (my $ref = $sth->fetchrow_hashref()) {
302     push(@{$vendors}, $ref);
303   }
304   $sth->finish();
305   $dbh->disconnect();
306
307   $main::lxdebug->leave_sub();
308
309   return $vendors;
310 }
311
312 sub mkdir_with_parents {
313   $main::lxdebug->enter_sub();
314
315   my ($full_path) = @_;
316
317   my $path = "";
318
319   $full_path =~ s|/+|/|;
320
321   foreach my $part (split(m|/|, $full_path)) {
322     $path .= "/" if ($path);
323     $path .= $part;
324
325     die("Could not create directory '$path' because a file exists with " .
326         "the same name.\n") if (-f $path);
327
328     if (! -d $path) {
329       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
330                                 "OS error: $!\n");
331     }
332   }
333
334   $main::lxdebug->leave_sub();
335 }
336
337 sub webdav_folder {
338   $main::lxdebug->enter_sub();
339
340   my ($form) = @_;
341
342   return $main::lxdebug->leave_sub()
343     unless ($::instance_conf->get_webdav && $form->{id});
344
345
346
347   $form->{WEBDAV} = [];
348
349   my ($path, $number) = get_webdav_folder($form);
350   return $main::lxdebug->leave_sub() unless ($path && $number);
351
352   if (!-d $path) {
353     mkdir_with_parents($path);
354
355   } else {
356     my $base_path = $ENV{'SCRIPT_NAME'};
357     $base_path =~ s|[^/]+$||;
358     if (opendir my $dir, $path) {
359       foreach my $file (sort { lc $a cmp lc $b } map { decode("UTF-8", $_) } readdir $dir) {
360         next if (($file eq '.') || ($file eq '..'));
361
362         my $fname = $file;
363         $fname  =~ s|.*/||;
364
365         my $is_directory = -d "$path/$file";
366
367         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
368         $file .=  '/' if ($is_directory);
369
370         push @{ $form->{WEBDAV} }, {
371           'name' => $fname,
372           'link' => $base_path . $file,
373           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
374         };
375       }
376
377       closedir $dir;
378     }
379   }
380
381   $main::lxdebug->leave_sub();
382 }
383
384 sub get_vc_details {
385   $main::lxdebug->enter_sub();
386
387   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
388
389   $vc = $vc eq "customer" ? "customer" : "vendor";
390
391   my $dbh = $form->dbconnect($myconfig);
392
393   my $query;
394
395   $query =
396     qq|SELECT
397          vc.*,
398          pt.description AS payment_terms,
399          b.description AS business,
400          l.description AS language,
401          dt.description AS delivery_terms
402        FROM ${vc} vc
403        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
404        LEFT JOIN business b ON (vc.business_id = b.id)
405        LEFT JOIN language l ON (vc.language_id = l.id)
406        LEFT JOIN delivery_terms dt ON (vc.delivery_term_id = dt.id)
407        WHERE vc.id = ?|;
408   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
409
410   if (!$ref) {
411     $dbh->disconnect();
412     $main::lxdebug->leave_sub();
413     return 0;
414   }
415
416   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
417
418   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
419
420   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
421   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
422
423   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
424   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
425
426   # Only show default pricegroup for customer, not vendor, which is why this is outside the main query
427   ($form->{pricegroup}) = selectrow_query($form, $dbh, qq|SELECT pricegroup FROM pricegroup WHERE id = ?|, $form->{klass});
428
429   $dbh->disconnect();
430
431   $main::lxdebug->leave_sub();
432
433   return 1;
434 }
435
436 sub get_shipto_by_id {
437   $main::lxdebug->enter_sub();
438
439   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
440
441   $prefix ||= "";
442
443   my $dbh = $form->dbconnect($myconfig);
444
445   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
446   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
447
448   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
449
450   $dbh->disconnect();
451
452   $main::lxdebug->leave_sub();
453 }
454
455 sub save_email_status {
456   $main::lxdebug->enter_sub();
457
458   my ($self, $myconfig, $form) = @_;
459
460   my ($table, $query, $dbh);
461
462   if ($form->{script} eq 'oe.pl') {
463     $table = 'oe';
464
465   } elsif ($form->{script} eq 'is.pl') {
466     $table = 'ar';
467
468   } elsif ($form->{script} eq 'ir.pl') {
469     $table = 'ap';
470
471   } elsif ($form->{script} eq 'do.pl') {
472     $table = 'delivery_orders';
473   }
474
475   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
476
477   $dbh = $form->get_standard_dbh($myconfig);
478
479   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
480
481   $intnotes =~ s|\r||g;
482   $intnotes =~ s|\n$||;
483
484   $intnotes .= "\n\n" if ($intnotes);
485
486   my $cc  = $form->{cc}  ? $main::locale->text('Cc') . ": $form->{cc}\n"   : '';
487   my $bcc = $form->{bcc} ? $main::locale->text('Bcc') . ": $form->{bcc}\n" : '';
488   my $now = scalar localtime;
489
490   $intnotes .= $main::locale->text('[email]') . "\n"
491     . $main::locale->text('Date') . ": $now\n"
492     . $main::locale->text('To (email)') . ": $form->{email}\n"
493     . "${cc}${bcc}"
494     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
495     . $main::locale->text('Message') . ": $form->{message}";
496
497   $intnotes =~ s|\r||g;
498
499   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
500
501   $form->save_status($dbh);
502
503   $dbh->commit();
504
505   $main::lxdebug->leave_sub();
506 }
507
508 sub check_params {
509   my $params = shift;
510
511   foreach my $key (@_) {
512     if ((ref $key eq '') && !defined $params->{$key}) {
513       my $subroutine = (caller(1))[3];
514       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
515       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
516       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
517
518     } elsif (ref $key eq 'ARRAY') {
519       my $found = 0;
520       foreach my $subkey (@{ $key }) {
521         if (defined $params->{$subkey}) {
522           $found = 1;
523           last;
524         }
525       }
526
527       if (!$found) {
528         my $subroutine = (caller(1))[3];
529         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
530         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
531         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
532       }
533     }
534   }
535 }
536
537 sub check_params_x {
538   my $params = shift;
539
540   foreach my $key (@_) {
541     if ((ref $key eq '') && !exists $params->{$key}) {
542       my $subroutine = (caller(1))[3];
543       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
544
545     } elsif (ref $key eq 'ARRAY') {
546       my $found = 0;
547       foreach my $subkey (@{ $key }) {
548         if (exists $params->{$subkey}) {
549           $found = 1;
550           last;
551         }
552       }
553
554       if (!$found) {
555         my $subroutine = (caller(1))[3];
556         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
557       }
558     }
559   }
560 }
561
562 sub get_webdav_folder {
563   $main::lxdebug->enter_sub();
564
565   my ($form) = @_;
566
567   croak "No client set in \$::auth" unless $::auth->client;
568
569   my ($path, $number);
570
571   # dispatch table
572   if ($form->{type} eq "sales_quotation") {
573     ($path, $number) = ("angebote", $form->{quonumber});
574   } elsif ($form->{type} eq "sales_order") {
575     ($path, $number) = ("bestellungen", $form->{ordnumber});
576   } elsif ($form->{type} eq "request_quotation") {
577     ($path, $number) = ("anfragen", $form->{quonumber});
578   } elsif ($form->{type} eq "purchase_order") {
579     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
580   } elsif ($form->{type} eq "sales_delivery_order") {
581     ($path, $number) = ("verkaufslieferscheine", $form->{donumber});
582   } elsif ($form->{type} eq "purchase_delivery_order") {
583     ($path, $number) = ("einkaufslieferscheine", $form->{donumber});
584   } elsif ($form->{type} eq "credit_note") {
585     ($path, $number) = ("gutschriften", $form->{invnumber});
586   } elsif ($form->{type} eq "letter") {
587     ($path, $number) = ("briefe", $form->{letternumber} );
588   } elsif ($form->{vc} eq "customer") {
589     ($path, $number) = ("rechnungen", $form->{invnumber});
590   } elsif ($form->{vc} eq "vendor") {
591     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
592   } else {
593     $main::lxdebug->leave_sub();
594     return undef;
595   }
596
597   $number =~ s|[/\\]|_|g;
598
599   $path = "webdav/" . $::auth->client->{id} . "/${path}/${number}";
600
601   $main::lxdebug->leave_sub();
602
603   return ($path, $number);
604 }
605
606 sub copy_file_to_webdav_folder {
607   $::lxdebug->enter_sub();
608
609   my ($form) = @_;
610   my ($last_mod_time, $latest_file_name, $complete_path);
611
612   # checks
613   foreach my $item (qw(tmpdir tmpfile type)){
614     next if $form->{$item};
615     $::lxdebug->message(LXDebug::WARN(), 'Missing parameter:' . $item);
616     $::form->error($::locale->text("Missing parameter for WebDAV file copy"));
617   }
618
619   my ($webdav_folder, $document_name) =  get_webdav_folder($form);
620
621   if (! $webdav_folder){
622     $::lxdebug->leave_sub();
623     $::lxdebug->message(LXDebug::WARN(), 'Cannot check correct WebDAV folder');
624     $::form->error($::locale->text("Cannot check correct WebDAV folder"));
625     return undef;
626   }
627
628   $complete_path =  File::Spec->catfile($form->{cwd},  $webdav_folder);
629
630   # maybe the path does not exist (automatic printing), see #2446
631   if (!-d $complete_path) {
632     # we need a chdir and restore old dir
633     my $current_dir = POSIX::getcwd();
634     chdir("$form->{cwd}");
635     mkdir_with_parents($webdav_folder);
636     chdir($current_dir);
637   }
638
639   opendir my $dh, $complete_path or die "Could not open $complete_path: $!";
640
641   my ($newest_name, $newest_time);
642   while ( defined( my $file = readdir( $dh ) ) ) {
643     my $path = File::Spec->catfile( $complete_path, $file );
644     next if -d $path; # skip directories, or anything else you like
645     ( $newest_name, $newest_time ) = ( $file, -M _ ) if( ! defined $newest_time or -M $path < $newest_time );
646   }
647
648   closedir $dh;
649
650   $latest_file_name    = File::Spec->catfile($complete_path, $newest_name);
651   my $filesize         = stat($latest_file_name)->size;
652
653   my $current_file     = File::Spec->catfile($form->{tmpdir}, apply { s:.*/:: } $form->{tmpfile});
654   my $current_filesize = -f $current_file ? stat($current_file)->size : 0;
655
656   if ($current_filesize == $filesize) {
657     $::lxdebug->leave_sub();
658     return;
659   }
660
661   my $timestamp =  get_current_formatted_time();
662   my $new_file  =  File::Spec->catfile($form->{cwd}, $webdav_folder, $form->generate_attachment_filename());
663   $new_file =~ s{(.*)\.}{$1$timestamp\.};
664
665   if (!File::Copy::copy($current_file, $new_file)) {
666     $::lxdebug->message(LXDebug::WARN(), "Copy file from $current_file to $new_file failed: $ERRNO");
667     $::form->error($::locale->text("Copy file from #1 to #2 failed: #3", $current_file, $new_file, $ERRNO));
668   }
669
670   $::lxdebug->leave_sub();
671 }
672
673 sub get_current_formatted_time {
674   return POSIX::strftime('_%Y%m%d_%H%M%S', localtime());
675 }
676
677 1;
678 __END__
679
680 =pod
681
682 =encoding utf8
683
684 =head1 NAME
685
686 Common - Common routines used in a lot of places.
687
688 =head1 SYNOPSIS
689
690   my $short_text = Common::truncate($long_text, at => 10);
691
692 =head1 FUNCTIONS
693
694 =over 4
695
696 =item C<truncate $text, %params>
697
698 Truncates C<$text> at a position and insert an ellipsis if the text is
699 longer. The maximum number of characters to return is given with the
700 paramter C<at> which defaults to 50.
701
702 The optional parameter C<strip> can be used to remove unwanted line
703 feed/carriage return characters from the text before truncation. It
704 can be set to C<1> (only strip those at the end of C<$text>) or
705 C<full> (replace consecutive line feed/carriage return characters in
706 the middle by a single space and remove tailing line feed/carriage
707 return characters).
708
709 =back
710
711 =head1 BUGS
712
713 Nothing here yet.
714
715 =head1 AUTHOR
716
717 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
718 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
719
720 =cut