[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[6358] trunk



Revision: 6358
          http://sourceforge.net/p/ledger-smb/code/6358
Author:   einhverfr
Date:     2014-01-03 07:48:46 +0000 (Fri, 03 Jan 2014)
Log Message:
-----------
Adding load_templates.pl utility.  This is a dev tool used basically to load templates into the db, particularly in bulk.
I will also supply the existing forms of templates as sql files so that they can be run separately from the program.

Modified Paths:
--------------
    trunk/LedgerSMB/AM.pm
    trunk/bin/am.pl

Added Paths:
-----------
    trunk/tools/load_templates.pl

Modified: trunk/LedgerSMB/AM.pm
===================================================================
--- trunk/LedgerSMB/AM.pm	2014-01-02 23:07:24 UTC (rev 6357)
+++ trunk/LedgerSMB/AM.pm	2014-01-03 07:48:46 UTC (rev 6358)
@@ -1078,61 +1078,6 @@
     }
 }
 
-=item AM->load_template($myconfig, $form);
-
-Populates $form->{body} with the contents of the file $form->{file}.
-
-=cut
-
-sub load_template {
-
-    my ( $self, $myconfig, $form ) = @_;
-    my $testval = 0;
-
-    $form->{file} ||= lc "$myconfig->{templates}/$form->{template}.$form->{format}";
-    $self->check_template_name( \%$myconfig, \%$form );
-    open( TEMPLATE, '<', "$form->{file}" ) || ($testval = 1);
-    if ($testval == 1 && ($! eq 'No such file or directory')){
-      my $file = $form->{file};
-      $file =~ s|$form->{code}/|| if $form->{code};
-      open( TEMPLATE, '<', "$file" ) ||  $form->error(
-                    "Template not found: $file"
-      );
-    } elsif ($testval == 1){
-       $form->error("$form->{file} : $!");
-    }
-    while (<TEMPLATE>) {
-        $form->{body} .= $_;
-    }
-
-    close(TEMPLATE);
-
-}
-
-=item AM->save_template($myconfig, $form);
-
-Overwrites the file $form->{file} with the contents of $form->{body}, excluding
-carriage returns.
-
-=cut
-
-sub save_template {
-
-    my ( $self, $myconfig, $form ) = @_;
-
-    $form->{file} ||= lc "$myconfig->{templates}/$form->{template}.$form->{format}";
-    $self->check_template_name( \%$myconfig, \%$form );
-    open( TEMPLATE, '>', "$form->{file}" )
-      or $form->error("$form->{file} : $!");
-
-    # strip
-    $form->{body} =~ s/\r//g;
-    print TEMPLATE $form->{body};
-
-    close(TEMPLATE);
-
-}
-
 =item AM->taxes($myconfig, $form);
 
 Retrieve details about all taxes in the database.  $form->{taxrates} refers to a

Modified: trunk/bin/am.pl
===================================================================
--- trunk/bin/am.pl	2014-01-02 23:07:24 UTC (rev 6357)
+++ trunk/bin/am.pl	2014-01-03 07:48:46 UTC (rev 6358)
@@ -710,168 +710,6 @@
 
 }
 
-sub list_templates {
-
-    AM->language( \%myconfig, \%$form );
-
-    if (uc($form->{format}) eq 'LATEX') {
-        $form->{file} = "$form->{template}.tex";
-    } elsif (uc($form->{format}) eq 'HTML') {
-        $form->{file} = "$form->{template}.html";
-    } elsif (uc($form->{format}) eq 'TEXT'){
-        $form->{file} = "$form->{template}.txt";
-    }
-
-    if ( !@{ $form->{ALL} } ) {
-        &display_form;
-        $form->finalize_request();
-    }
-
-    unshift @{ $form->{ALL} },
-      { code => '.', description => $locale->text('Default Template') };
-
-    my $href =
-"$form->{script}?action=list_templates&direction=$form->{direction}&oldsort=$form->{oldsort}&file=$form->{file}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
-
-    $form->sort_order();
-
-    $form->{callback} =
-"$form->{script}?action=list_templates&direction=$form->{direction}&oldsort=$form->{oldsort}&file=$form->{file}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
-
-    my $callback = $form->escape( $form->{callback} );
-
-    chomp $myconfig{templates};
-    $form->{file} =~ s/$myconfig{templates}//;
-    $form->{file} =~ s/\///;
-    $form->{title} = "$form->{format}: $form->{template}";
-
-    my @column_index = $form->sort_columns(qw(code description));
-
-    $column_header{code} = { text => $locale->text('Code'),
-        href => "$href&sort=code" };
-    $column_header{description} = { text => $locale->text('Description'),
-        href => "$href&sort=description" };
-
-    my @rows;
-    my $i = 0;
-    foreach my $ref ( @{ $form->{ALL} } ) {
-
-        my %column_data;
-        $i++;
-        $i %= 2;
-        $column_data{i} = $i;
-
-        $column_data{code} = { text => $ref->{code}, href =>
-            qq|$form->{script}?action=display_form&file=$myconfig{templates}/$ref->{code}/$form->{file}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&code=$ref->{code}&callback=$callback|};
-        $column_data{description} = $ref->{description};
-        if ($ref->{code} eq '.'){
-            $column_data{code}->{text} = $locale->text('Default');
-        }
-	push @rows, \%column_data;
-    
-    }
-
-    $form->{type} = 'language';
-    my @hiddens = qw(sessionid login path calllback type);
-
-## SC: Temporary removal
-##    if ( $form->{lynx} ) {
-##        require "bin/menu.pl";
-##        &menubar;
-##    }
-
-    # SC: I'm not concerned about the wider description as code is 6 chars max
-    my $template = LedgerSMB::Template->new_UI(
-        user => \%myconfig, 
-        locale => $locale,
-        template => 'am-list-departments');
-    $template->render({
-        form => $form,
-        columns => \@column_index,
-        heading => \%column_header,
-        rows => \@rows,
-        hiddens => \@hiddens,
-    });
-}
-
-sub display_form {
-
-    my %hiddens;
-    AM->load_template( \%myconfig, \%$form );
-
-    $form->{title} = $form->{file};
-
-    $form->{body} =~
-s/<%include (.*?)%>/<a href=$form->{script}\?action=display_form&file=$myconfig{templates}\/$form->{code}\/$1&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}>$1<\/a>/g;
-
-    $form->{type} = "template";
-    $hiddens{$_} = $form->{$_} foreach qw(file type path login sessionid code);
-
-##SC: Temporary commenting
-##    if ( $form->{lynx} ) {
-##        require "bin/menu.pl";
-##        &menubar;
-##    }
-
-    my @buttons = ({
-        name => 'action',
-        value => 'edit',
-        text => $locale->text('Edit Template'),
-        });
-    my $template = LedgerSMB::Template->new_UI(
-        user => \%myconfig, 
-        locale => $locale,
-        template => 'am-display-template');
-    $template->render({
-        form => $form,
-        buttons => \@buttons,
-	hiddens => \%hiddens,
-    });
-}
-
-sub edit_template {
-
-    AM->load_template( \%myconfig, \%$form );
-    my %hiddens;
-
-    $form->{title} = $locale->text('Edit Template');
-
-    # convert &nbsp to &amp;nbsp;
-    $form->{body} =~ s/&nbsp;/&amp;nbsp;/gi;
-
-    $hiddens{type} = 'template';
-    $hiddens{$_} = $form->{$_} foreach qw(file path login sessionid code);
-    $hiddens{callback} = qq|$form->{script}?action=display_form&file=$form->{file}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}|;
-
-##SC: Temporary commenting
-##    if ( $form->{lynx} ) {
-##        require "bin/menu.pl";
-##        &menubar;
-##    }
-
-    my @buttons = ({
-        name => 'action',
-        value => 'save',
-        text => $locale->text('Save Template'),
-        });
-    my $template = LedgerSMB::Template->new_UI(
-        user => \%myconfig, 
-        locale => $locale,
-        template => 'am-edit-template');
-    $template->render({
-        form => $form,
-        buttons => \@buttons,
-	hiddens => \%hiddens,
-    });
-}
-
-sub save_template {
-
-    AM->save_template( \%myconfig, \%$form );
-    $form->redirect( $locale->text('Template saved!') );
-
-}
-
 sub taxes {
 
     # get tax account numbers

Added: trunk/tools/load_templates.pl
===================================================================
--- trunk/tools/load_templates.pl	                        (rev 0)
+++ trunk/tools/load_templates.pl	2014-01-03 07:48:46 UTC (rev 6358)
@@ -0,0 +1,119 @@
+#!/usr/bin/perl
+#
+# load_templates.pl
+use LedgerSMB::App_State;
+use LedgerSMB::Template::DB;
+use DBI;
+
+my $help_msg = "
+
+perl load_templates.pl file_or_directory [language] [database]
+
+This is a simple script to load templates into the database from the 
+filesystem.  It loads them into the database via the LedgerSMB::Template::DB
+module.
+
+This can be run either on a single file or on a directory which is given in the 
+first argument.  The arguments are as follows:
+
+file_or_directory:  the file or directory to be loaded.  This is not done 
+recursively.  If you want to load recursively, use with find.  This decision was
+made because it makes some sense to store language-specific templates in 
+subdirectories.
+
+language:  This is the language code (i.e. en or en_US) to attach to the 
+template.  If omitted the templates are assumed not to be language specific.  
+You would use this if you wanted to change the layout for a specific language.
+
+database:  The database to connect to.  If PGDATABASE is set this takes 
+precedence only in the 3 arg form.  Use '' to indicate a missing language code 
+in that case.
+
+Examples:
+
+PGDATABASE=lsmbdemo perl load_templates.pl templates/demo
+perl load_templates.pl templates/demo/en en lsmbdemo
+PGDATABASE=lsmbdemo perl load_templates.pl templates/demo/en en
+perl load_templates.pl file_or_directory templates/demo '' lsmbdemo
+
+";
+
+
+
+#### ARG HANDLING (before functions because it should be read first)
+#
+my ($script, $to_load, $language, $database) = @_;
+
+# handle 2-arg form:
+
+if (!$database){
+   $database = $ENV{PGDATABASE};
+   if (!$database){
+       $database = $language;
+       $language = undef;
+   }
+}
+
+$language  ||= undef; # false values used as placeholders only.
+
+if ($to_load eq '--help' or $to_load =~ /^-/ or !$to_load){
+    print $help_msg;
+    exit 0;
+};
+
+### FUNCTIONS
+
+sub load_template {
+    my ($path) = @_;
+    my $fname = $path;
+    if ($path =~ m|/.*:| ){
+       die 'Cannot run on NTFS alternate data stream!';
+    }
+    $fname =~ s|.*/?([^/]+)|$1|;
+    my ($template_name, $format) = split /./, $fname;
+    my $content = '';
+    open TEMP, '<', $path;
+    $content .= $_ while <TEMP>;
+    my %args = (
+           template_name => $template_name,
+           format => $format,
+           template => $template,
+    );
+    $args{language_code} = $language_code if $language_code;
+    my $dbtemp = LedgerSMB::Template::DB->new(%args);
+    $dbtemp->save;
+}
+
+### SETUP
+
+# db connection
+
+my $dbh = DBI->connect("dbi:Pg:dbname=$database") 
+  or die "Unable to connect to database!"; # autocommit on, no need to turn off
+
+$LedgerSMB::App_State::DBH = $dbh;
+
+### LOADING LOGIC
+
+# is file or directory?
+
+my $type = undef;
+
+$type = 'file' if -f $to_load;
+$type = 'dir' if -d $to_load;
+
+die 'Bad file type: Must be a file or directory or file does not exist' 
+   unless $type;
+
+# load
+
+if ($type eq 'file'){
+   load_template($to_load);
+} else {
+   opendir(DIR, $to_load);
+   while (readdir(DIR)){
+      load_template("$to_load/$_") if -f "$to_load/$_";
+   }
+}
+
+exit 0;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits