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

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



Revision: 3661
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3661&view=rev
Author:   einhverfr
Date:     2011-08-16 22:33:15 +0000 (Tue, 16 Aug 2011)

Log Message:
-----------
Database utility now works to set up new databases

Modified Paths:
--------------
    trunk/LedgerSMB/DBObject/Admin.pm
    trunk/UI/setup/new_user.html
    trunk/UI/setup/select_coa.html
    trunk/scripts/setup.pl
    trunk/sql/modules/Person.sql

Modified: trunk/LedgerSMB/DBObject/Admin.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Admin.pm	2011-08-16 00:03:56 UTC (rev 3660)
+++ trunk/LedgerSMB/DBObject/Admin.pm	2011-08-16 22:33:15 UTC (rev 3661)
@@ -75,11 +75,11 @@
 
     # I deleted some assignments which didn't play well with strict mode
     # and by my reading probably broke things. --CT
-
     my $employee = LedgerSMB::DBObject::Employee->new( base=>$self);
+    if (!$employee->{entity_id}){
+         $employee->save();
+    }
     
-    $employee->save();
-    
     my $user = LedgerSMB::DBObject::User->new(base=>$self, copy=>'list',
         merge=>[
             'username',
@@ -91,6 +91,7 @@
     );
     $user->{entity_id} = $employee->{entity_id};
     if ($user->save() == 8){ # Duplicate User exception --CT
+        $user->{dbh}->rollback;
         return 8;
     }
     $self->{user} = $user;
@@ -271,14 +272,7 @@
 sub get_salutations {
     
     my $self = shift;
-
-    # Adding SQL queries like this into the code directly is bad practice. --CT
-    my $sth = $self->{dbh}->prepare("SELECT * FROM salutation ORDER BY id ASC");
-    
-    $sth->execute();
-    
-    # Returns a list of hashrefs
-    return $sth->fetchall_arrayref( {} );
+    return $self->exec_method({funcname => 'person__list_salutations' });
 }
 
 

Modified: trunk/UI/setup/new_user.html
===================================================================
--- trunk/UI/setup/new_user.html	2011-08-16 00:03:56 UTC (rev 3660)
+++ trunk/UI/setup/new_user.html	2011-08-16 22:33:15 UTC (rev 3661)
@@ -1,7 +1,9 @@
-<?lsmb INCLUDE "ui-header.html" stylesheet="UI/setup/stylesheet.css" ?>
+<?lsmb INCLUDE "ui-header.html" 
+stylesheet="ledgersmb.css"
+include_stylesheet=["UI/setup/stylesheet.css"] ?>
 <?lsmb PROCESS elements.html ?>
 <body>
-<div class="listtop"><?lsmb text('Enter User') ?>
+<div class="listtop"><?lsmb text('Enter User') ?></div>
 <form action="setup.pl" method="POST" name="new_user">
 <?lsmb INCLUDE input element_data = {
     name = 'database'
@@ -27,6 +29,7 @@
 </div>
 <div class="input_row">
 <label><?lsmb text('Import') ?></label>
+<div id="yesno">
                 <?lsmb INCLUDE input element_data = {
                            label   = 'Yes'
                            value   = '1'
@@ -42,11 +45,13 @@
                            type    = 'radio'
                    } ?>
 </div>
+</div>
 <div class="input_row">
 <?lsmb INCLUDE select element_data = {
           options = salutations
         text_attr = 'salutation'
        value_attr = 'id'
+             name = 'salutation_id'
             label = 'Salutation'
 } ?>
 </div>
@@ -56,6 +61,7 @@
    class = 'name'
     type = 'text'
     size = '15'
+   label = text('First Name') #'
 } ?>
 </div>
 <div class="input_row">
@@ -64,6 +70,7 @@
    class = 'name'
     type = 'text'
     size = '15'
+   label = text('Last name') #'
 } ?>
 </div>
 <div class="input_row">
@@ -72,14 +79,16 @@
    class = 'control_code'
     type = 'text'
     size = '15'
+   label = text('Employee Number') #'
 } ?>
 </div>
 <div class="input_row">
-<?lsmb  countries.shift({});
+<?lsmb  countries.unshift({});
 INCLUDE select element_data = {
           options = countries
         text_attr = 'name'
        value_attr = 'id'
+             name = 'country_id'
             label = 'Country'
 } ?>
 </div>
@@ -88,6 +97,7 @@
           options = perm_sets
         text_attr = 'label'
        value_attr = 'id'
+             name = 'perms'
             label = 'Assign Permissions'
 } ?>
 </div>

Modified: trunk/UI/setup/select_coa.html
===================================================================
--- trunk/UI/setup/select_coa.html	2011-08-16 00:03:56 UTC (rev 3660)
+++ trunk/UI/setup/select_coa.html	2011-08-16 22:33:15 UTC (rev 3661)
@@ -6,11 +6,6 @@
 <h2>Database Management Console</h2>
 <div class="listtop"><?lsmb title ?></div>
 <form action="setup.pl" method="POST" name="credentials">
-<?lsmb INCLUDE input element_data = {
-    name = 'database'
-    type = 'hidden'
-   value = database
-} ?>
 <?lsmb IF coa_lc;
    INCLUDE input element_data = {
         name = 'coa_lc'
@@ -22,7 +17,7 @@
 <?lsmb IF coa_lc; 
     INCLUDE select element_data = {
          options = charts
-            name = 'coa'
+            name = 'chart'
        text_attr = 'name'
       value_attr = 'name'
     }; 

Modified: trunk/scripts/setup.pl
===================================================================
--- trunk/scripts/setup.pl	2011-08-16 00:03:56 UTC (rev 3660)
+++ trunk/scripts/setup.pl	2011-08-16 22:33:15 UTC (rev 3661)
@@ -203,6 +203,8 @@
 =cut
 
 sub select_coa {
+    use LedgerSMB::Sysconfig;
+    use DBI;
     my ($request) = @_;
 
     if ($request->{coa_lc} =~ /\.\./){
@@ -210,9 +212,65 @@
     }
     if ($request->{coa_lc}){
         if ($request->{chart}){
-            # new db instance
-            # load file
-            # render admin user template
+           my $creds = LedgerSMB::Auth::get_credentials();
+       
+           # ENVIRONMENT NECESSARY
+           $ENV{PGUSER} = $creds->{login};
+           $ENV{PGPASSWORD} = $creds->{password};
+           $ENV{PGDATABASE} = $request->{database};
+
+           my $database = LedgerSMB::Database->new(
+                      {username => $creds->{login},
+                   company_name => $request->{database},
+                       password => $creds->{password}}
+           );
+           my $logfile = $LedgerSMB::tempdir . "/dblog";
+
+           $database->exec_script(
+                    {script => "sql/coa/$request->{coa_lc}/chart/$request->{chart}", 
+                    logfile => $logfile}
+           );
+           if (-f "sql/coa/$request->{coa_lc}/gifi/$request->{chart}"){
+                 $database->exec_script(
+                    {script => "sql/coa/$request->{coa_lc}/gifi/$request->{chart}",
+                    logfile => $logfile}
+                );
+            }
+
+
+            # One thing to remember here is that the setup.pl does not get the
+            # benefit of the automatic db connection.  So in order to build this
+            # form, we have to manage that ourselves. 
+            #
+            # However we get the benefit of having had to set the environment
+            # variables for the Pg connection above, so don't need to pass much
+            # info. 
+            #
+            # Also I am opting to use the lower-level call_procedure interface
+            # here in order to avoid creating objects just to get argument
+            # mapping going. --CT
+
+            $request->{dbh} = DBI->connect("dbi:Pg:dbname=$request->{database}");
+
+           @{$request->{salutations}} 
+            = $request->call_procedure(procname => 'person__list_salutations' ); 
+          
+           @{$request->{countries}} 
+            = $request->call_procedure(procname => 'location_list_country' ); 
+
+           $locale = $request->{_locale};
+
+           @{$request->{perm_sets}} = (
+               {id => '0', label => $locale->text('Manage Users')},
+               {id => '1', label => $locale->text('Full Permissions')},
+           );
+
+           $template = LedgerSMB::Template->new(
+                   path => 'UI/setup',
+                   template => 'new_user',
+	           format => 'HTML',
+           );
+           $template->render($request);
         } else {
             opendir(COA, "sql/coa/$request->{coa_lc}/chart");
             my @coa = grep !/^(\.|[Ss]ample.*)/, readdir(COA);
@@ -240,6 +298,50 @@
     $template->render($request);
 }
 
+=item save_user
+
+Saves the administrative user, and then directs to the login page.
+
+=cut
+
+sub save_user {
+    use LedgerSMB::DBObject::Admin;
+    my ($request) = @_;
+    my $creds = LedgerSMB::Auth::get_credentials();
+    $request->{dbh} = DBI->connect("dbi:Pg:dbname=$request->{database}",
+                                   $creds->{login},
+                                   $creds->{password});
+    my $user = LedgerSMB::DBObject::Admin->new({base => $request});
+    $user->save_user;
+    if ($request->{perms} == 1){
+         for my $role (
+                $request->call_procedure(procname => 'admin__get_roles')
+         ){
+             $request->call_procedure(procname => 'admin__add_user_to_role',
+                                      args => [ $request->{username}, 
+                                                $role->{rolname}
+                                              ]);
+         }
+    } elsif ($request->{perms} == 0) {
+        $request->call_procedure(procname => 'admin__add_user_to_role',
+                                 args => [ $request->{username},
+                                           "lsmb_$request->{database}__".
+                                            "manage_users",
+                                         ]
+        );
+    } else {
+        $request->error($request->{_locale}->text('No Permissions Assigned'));
+   }
+   $request->{dbh}->commit;
+   
+    $template = LedgerSMB::Template->new(
+            path => 'UI/setup',
+            template => 'complete',
+	    format => 'HTML',
+    );
+    $template->render($request);
+}
+
 =item cancel
 
 Cancels work.  If the confirm is set to no, returns to the credential screen

Modified: trunk/sql/modules/Person.sql
===================================================================
--- trunk/sql/modules/Person.sql	2011-08-16 00:03:56 UTC (rev 3660)
+++ trunk/sql/modules/Person.sql	2011-08-16 22:33:15 UTC (rev 3661)
@@ -12,6 +12,14 @@
 COMMENT ON FUNCTION person__get_my_entity_id() IS
 $$ Returns the entity_id of the current, logged in user.$$;
 
+
+CREATE OR REPLACE FUNCTION person__list_salutations() 
+RETURNS SETOF salutation AS
+$$ SELECT * FROM salutation ORDER BY id ASC $$ language sql;
+
+COMMENT ON FUNCTION person__list_salutations() IS
+$$ Returns a list of salutations ordered by id.$$; 
+
 CREATE OR REPLACE FUNCTION person__save
 (in_entity_id integer, in_salutation_id int, 
 in_first_name text, in_middle_name text, in_last_name text,


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