diff options
-rw-r--r-- | LedgerSMB/DBObject/Admin.pm | 15 | ||||
-rw-r--r-- | LedgerSMB/DBObject/Location.pm | 73 | ||||
-rw-r--r-- | LedgerSMB/DBObject/User.pm | 8 | ||||
-rw-r--r-- | UI/Admin/edit_user.html | 222 | ||||
-rw-r--r-- | scripts/admin.pl | 139 | ||||
-rw-r--r-- | sql/modules/Employee.sql | 2 | ||||
-rw-r--r-- | sql/modules/Location.sql | 65 | ||||
-rw-r--r-- | sql/modules/Person.sql | 106 | ||||
-rw-r--r-- | sql/modules/admin.sql | 1 |
9 files changed, 524 insertions, 107 deletions
diff --git a/LedgerSMB/DBObject/Admin.pm b/LedgerSMB/DBObject/Admin.pm index e6e519ac..061cbd72 100644 --- a/LedgerSMB/DBObject/Admin.pm +++ b/LedgerSMB/DBObject/Admin.pm @@ -247,9 +247,20 @@ sub get_roles { my @rows; for my $role (@s_rows) { my $rolname = $role->{'admin__get_roles'}; - $rolname =~ s/lsmb_ledgersmb_13__//gi; + my $company = $self->{company}; + $rolname =~ s/lsmb_${company}__//gi; push @rows, $rolname; } return \@rows; } -1; + +sub get_countries { + + my $self = shift @_; + + my $sth = $self->{dbh}->prepare("SELECT id, name FROM country"); + my $code = $sth->execute(); + + return $sth->fetchall_arrayref( {} ); # returns an array of hashrefs. +} +1;
\ No newline at end of file diff --git a/LedgerSMB/DBObject/Location.pm b/LedgerSMB/DBObject/Location.pm new file mode 100644 index 00000000..3deb967f --- /dev/null +++ b/LedgerSMB/DBObject/Location.pm @@ -0,0 +1,73 @@ +package LedgerSMB::DBObject::Location; + +use base LedgerSMB::DBObject; + +sub create { + + my $self = shift @_; +} + +sub save { + + my $self = shift @_; + my $type = shift @_; + + # assumes all the parameters are present... + + my ($ret) = $self->exec_method(funcname=>$type."__save_location", args=>[ + $self->{user_id}, # entity_id + $self->{location_id}, # location_id + $self->{address1}, + $self->{address2}, + $self->{address3}, # address info + $self->{city}, # city + $self->{state}, # state/province + $self->{zipcode}, + $self->{country} # obviously, country. + ]); + $self->{id} = $ret->[0]; + return $self->{id}; +} + +sub delete { + + my $self = shift @_; + my $id = shift @_; + my $e_id = shift @_; + my $type = shift @_; + + + # e_id is an entity of some variety + + + if (!$id && !$self->{location_id}) { + $self->error("Must call delete with an ID..."); + } + unless ($id) { + $id = $self->{location_id}; + } + + my ($res) = $self->exec_method(funcname=>$type."__delete_location", args=>[$e_id,$id]); + + return $res->[0]; +} + +sub get { + + my $self = shift @_; + my $id = shift @_; + + my ($ret) = $self->exec_method(funcname=>"location__get", args=>[$id]); + + return $ret->[0]; +} + +sub get_all { + + my $self = shift @_; + my $user_id = shift @_; + my $type = shift @_; + + my @locations = $self->exec_method(funcname=>$type."__all_locations", args=>[$user_id]); +} +1;
\ No newline at end of file diff --git a/LedgerSMB/DBObject/User.pm b/LedgerSMB/DBObject/User.pm index 18bfca9a..7682acbe 100644 --- a/LedgerSMB/DBObject/User.pm +++ b/LedgerSMB/DBObject/User.pm @@ -74,15 +74,19 @@ sub get { funcname=>'person__list_locations', args=>[ $self->{user}->{entity_id} ] ); - $self->{location} = \@loc; + $self->{locations} = \@loc; my @contacts = $self->exec_method( funcname=>"person__list_contacts", args=>[$self->{user}->{entity_id} ] ); + $self->{contacts} = \@contacts; my @rolstore; for my $role (@roles) { - push @rolstore, $role->{'admin__get_roles_for_user'}; # Only one key=>value pair + my $rolname = $role->{'admin__get_roles_for_user'}; + my $company = $self->{company}; + $rolname =~ s/lsmb_${company}__//gi; + push @rolstore, $rolname; # Only one key=>value pair } $self->{roles} = \@rolstore; diff --git a/UI/Admin/edit_user.html b/UI/Admin/edit_user.html index 71b86226..6128e527 100644 --- a/UI/Admin/edit_user.html +++ b/UI/Admin/edit_user.html @@ -9,9 +9,18 @@ </strong> </font> - <form method="POST" action="admin.pl?action=new_user"> - + <form method="POST" action="admin.pl"> + <input type="hidden" name="action" value="<?lsmb IF user.user.username?>edit_user<?lsmb ELSE?>new_user<?lsmb END?>"/> <table> + + <?lsmb UNLESS user.user.username?> + <tr> + <td>Username</td> + <td> + <input type="textarea" name="username" value="<?lsmb user.user.username?>"/> + </td> + </tr> + <?lsmb END?> <tr> <td> @@ -42,91 +51,170 @@ <input type="textarea" name="employeenumber" value="<?lsmb user.employee.employeenumber?>"/> </td> </tr> - <hr/> - <?lsmb UNLESS user.user.username?> - <tr> - <td>Username</td> - <td> - <input type="textarea" name="username" value="<?lsmb user.user.username?>"/> - </td> - </tr> + </table> + <input type="submit" value="Save User" /> + </form> + <?lsmb IF user.user.username?> + <form name="location" method="POST" action="admin.pl"> + <?lsmb IF location?> + <input type="hidden" name="location_id" value="<?lsmb location.id?>"/> <?lsmb END?> + <input type="hidden" name="action" value="save_location"/> + <input type="hidden" name="user_id" value="<?lsmb user.user.entity_id?>"/> + <table> + + <tr> + <td colspan="8"> + <hr/> + Location + </td> + </tr> - <tr> - <td><br/></td> - <td> - <hr/> - </td> - </tr> - - <tr> - <td>Company Name</td> - <td> - <input type="textarea" name="companyname" value="<?lsmb user.companyname?>"/> - </td> - </tr> + <tr> + <td></td> + <td> + Address + </td> + <td> + City + </td> + <td> + State/Province + </td> + <td> + Zip/Postal Code + </td> + <td> + Country + </td> + </tr> - <tr> - <td> - Address - </td> - <td> - City - </td> - <td> - State/Province - </td> - <td> - Zip/Postal Code - </td> - <td> - Country - </td> - </tr> - - <?lsmb FOR location IN user.locations ?> + <?lsmb FOR location IN user.locations ?> + <tr> + <td> + <a href="admin.pl?action=edit_location&id=<?lsmb location.id?>">Edit</a> + <a href="admin.pl?action=delete_location&id=<?lsmb location.id?>">Delete</a> + <td> + + <?lsmb location.line_one?> + <?lsmb location.line_two?> + <?lsmb location.line_three?> + </a> + </td> + <td> + <?lsmb location.city?> + </td> + <td> + <?lsmb location.state?> + </td> + <td> + <?lsmb location.zipcode?> + </td> + <td> + <?lsmb location.country?> + </td> + </tr> + + <?lsmb END?> + <!-- The editable one. --> <tr> + <td></td> <td> - <input type="textarea" name="address1" value="<?lsmb location.lineone?>"/> - <input type="textarea" name="address2" value="<?lsmb location.linetwo?>"/> - <input type="textarea" name="address3" value="<?lsmb location.linethree?>"/> + <input type="textarea" name="address1" value="<?lsmb location.line_one?>"/> + <input type="textarea" name="address2" value="<?lsmb location.line_two?>"/> + <input type="textarea" name="address3" value="<?lsmb location.line_three?>" /> </td> - <td> + <td valign="top"> <input type="textarea" name="city" value="<?lsmb location.city?>"/> </td> - <td> + <td valign="top"> <input type="textarea" name="state" value="<?lsmb location.state?>"/> </td> - <td> + <td valign="top"> <input type="textarea" name="zipcode" value="<?lsmb location.zipcode?>"/> </td> - <td> - <input type="textarea" name="country" value="<?lsmb location.country?>"/> - </td> + <td valign="top"> + <select name="country"> + <?lsmb FOREACH country IN countries?> + <option value="<?lsmb country.id?>"><?lsmb country.name?></option> + <?lsmb END?> + </select> + </td> </tr> - - <?lsmb END?> + <tr> + <td> + <?lsmb IF location?> + <input type="submit" value="Edit Location"> + <input type="hidden" name="location_id" value="<?lsmb location.id?>"/> + <?lsmb ELSE?> + <input type="submit" value="Save Location"> + <?lsmb END?> + </td> + </tr> + </form> + </table> + + + + <table> + <form name="contacts" method="POST" action="admin.pl"> + <input type="hidden" name="action" value="save_contact"/> + <input type="hidden" name="user_id" value="<?lsmb user.user.id?>"/> + <tr> + <td colspan="8"><hr/></td> + </tr> - <tr> - <td><br/></td> - <td><hr/></td> + <td></td> + + <td> + Contact type + </td> + <td> + Contact + </td> </tr> - <?lsmb FOR contact IN user.contacts?> + <?lsmb FOR l_contact IN user.contacts?> <tr> - <td><?lsmb contact.class?></td> - <td><?lsmb contact.contact?></td> + <td> + <a href="admin.pl?edit_contact&contact_id=<?lsmb l_contact.id?>&user_id=<?lsmb user.user.id?>">Edit</a> + <a href="admin.pl?delete_contact&contact_id=<?lsmb l_contact.id?>&user_id=<?lsmb user.user.id?>">Delete</a> + </td> + <td><?lsmb l_contact.class?></td> + <td><?lsmb l_contact.contact?></td> </tr> <?lsmb END?> - + <tr> + <td></td> + <td> + <select name="class"> + <?lsmb FOREACH class IN contact_classes?> + <option name="<?lsmb class.id?>" <?lsmb IF contact.contact_class_id == class.id?>selected<?lsmb END?>><?lsmb class.contact?></option> + <?lsmb END?> + </select> + </td> + <td> + <input type="textarea" name="contact" value="<?lsmb contact.contact?>"/> + </td> + </tr> + <tr> + <td> + <?lsmb IF contact?> + <input type="submit" value="Edit Contact"> + <input type="hidden" name="contact_id" value="<?lsmb contact.id?>"/> + <?lsmb ELSE?> + <input type="submit" value="Save Contact"> + <?lsmb END?> + </td> + </tr> + </form> </table> <table> - <!-- Groups section --> <tr> <?lsmb FOREACH role IN roles ?> @@ -134,7 +222,15 @@ </tr> <tr> <?lsmb END?> - <td><input type="checkbox" name="<?lsmb loop.index?>" value="1"/><?lsmb role?></td> + <td> + <input type="checkbox" name="<?lsmb loop.index?>" value="1" + <?lsmb FOREACH rolname IN user.roles ?> + <?lsmb IF role == rolname?> + checked + <?lsmb END?> + <?lsmb END?> /> + <?lsmb role?> + </td> <?lsmb END?> </tr> </table> @@ -145,5 +241,5 @@ <td><button name="method" value="cancel">Cancel</td> </tr> </table> - </form> + <?lsmb END?> </div>
\ No newline at end of file diff --git a/scripts/admin.pl b/scripts/admin.pl index 7ba5a40e..a9e11177 100644 --- a/scripts/admin.pl +++ b/scripts/admin.pl @@ -6,6 +6,7 @@ require 'lsmb-request.pl'; use LedgerSMB::Template; use LedgerSMB::DBObject::Admin; use LedgerSMB::DBObject::User; +use LedgerSMB::DBObject::Location; use Data::Dumper; sub new_user { @@ -49,7 +50,8 @@ sub new_user { $template->render( { salutations=>$sal, - roles=>$groups + roles=>$groups, + countries=>$admin->get_countries(), } ); } @@ -84,19 +86,29 @@ sub edit_user { roles=>$all_roles, user_roles=>$admin->get_user_roles($request->{username}), salutations=>$admin->get_salutations(), + locations=>$location->get_all($u_id,"person"), + countries=>$admin->get_countries(), } ); } else { # print STDERR Dumper($user); # print STDERR Dumper(@all_roles); -# print STDERR Dumper($user->{roles}); + my $loc; + my $location = LedgerSMB::DBObject::Location->new(base=>$request); + if ($request->{location_id}) { + $loc = $location->get($request->{location_id}); + } + print STDERR Dumper($admin->get_salutations()); $template->render( { user=>$user, roles=>@all_roles, user_roles=>$user->{roles}, salutations=>$admin->get_salutations(), + location=>$loc, + locations=>$location->get_all($u_id,"person"), + countries=>$admin->get_countries(), } ); } @@ -247,6 +259,129 @@ sub main { $template->render( { users=>$user->{users} } ); } +sub edit_contact { + + my $request = shift @_; + + # Only ever a post, but check anyway + if ($request->type eq "POST") { + + # We have a contact ID, ie, something we made up. + my $c_id = $request->{contact_id}; + my $u_id = $request->{user_id}; + my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id'); + $user->get($u_id); + + # so we have a user object. + # ->{contacts} is an arrayref to the list of contacts this user has + # $request->{contact_id} is a reference to this structure. + + } +} + +sub delete_contact { + + + my $request = shift @_; + + # Only ever a post, but check anyway + if ($request->type eq "POST") { + + # We have a contact ID, ie, something we made up. + my $c_id = $request->{contact_id}; + my $u_id = $request->{user_id}; + my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id'); + $user->get($u_id); + + # so we have a user object. + # ->{contacts} is an arrayref to the list of contacts this user has + # $request->{contact_id} is a reference to this structure. + + $user->delete_contact($c_id); + # Boom. Done. + # Now, just call the main edit user page. + edit_user($request); + } +} + +sub new_contact { + + my $request = shift @_; + + +} + +sub save_location { + + my $request = shift @_; + + # Only ever a post, but check anyway + if ($request->type eq "POST") { + + my $u_id = $request->{user_id}; # this is an entity_id + + my $location = LedgerSMB::DBObject::Location->new(base=>$request, copy=>'all'); + + # So there's a pile of stuff we need. + # lineone + # linetwo + # linethree + # city + # state + # zipcode + # country + print STDERR "Attempting to save location...\n"; + my $id = $location->save("person"); + # Done and done. + + my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id'); + my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id'); + + $user->get($request->{user_id}); + + my @all_roles = $admin->get_roles(); + + my $template = LedgerSMB::Template->new( + user => $user, + template => 'Admin/edit_user', + language => $user->{language}, + format => 'HTML', + path=>'UI' + ); + $template->render( + { + user=>$user, + roles=>@all_roles, + user_roles=>$user->{roles}, + salutations=>$admin->get_salutations(), + locations=>$location->get_all($u_id,"person"), + location=>$location->get($id), + countries=>$admin->get_countries(), + } + ); + } +} + + +sub delete_location { + + my $request = shift @_; + + # Only ever a post, but check anyway + if ($request->type eq "POST") { + + my $l_id = $request->{location_id}; + my $u_id = $request->{user_id}; + + my $location = LedgerSMB::DBObject::Location->new(base=>$request, copy=>"location_id"); + + $location->person_delete($l_id,$u_id); + # Boom. Done. + # Now, just call the main edit user page. + edit_user($request); + } +} + #eval { do "scripts/custom/admin.pl"}; 1; diff --git a/sql/modules/Employee.sql b/sql/modules/Employee.sql index ae2efc86..0048b873 100644 --- a/sql/modules/Employee.sql +++ b/sql/modules/Employee.sql @@ -23,7 +23,7 @@ returns int AS $$ select * into p from person where id = in_person; IF NOT FOUND THEN - RAISE EXCEPTION 'No person found for ID %', in_person; + RAISE EXCEPTION 'No person found for ID %', in_perso; END IF; -- Okay, we're good. Check to see if we update or insert. diff --git a/sql/modules/Location.sql b/sql/modules/Location.sql index 4e0cb6b1..e266f277 100644 --- a/sql/modules/Location.sql +++ b/sql/modules/Location.sql @@ -27,7 +27,7 @@ END; $$ language plpgsql; CREATE OR REPLACE FUNCTION location_save -(in_address1 text, in_address2 text, in_address3 text, +(in_location_id int, in_address1 text, in_address2 text, in_address3 text, in_city text, in_state text, in_zipcode text, in_country int) returns integer AS $$ @@ -36,29 +36,54 @@ DECLARE location_row RECORD; BEGIN - SELECT * INTO location_row FROM location - WHERE line_one = in_address1 AND - coalesce(line_two, '') = coalesce(in_address2, '') AND - coalesce(line_three, '') = coalesce(in_address3, '') AND - city = in_city AND - coalesce(state, '') = coalesce(in_state, '') AND - coalesce(mail_code, '') = coalesce(in_zipcode, '') AND - country_id = in_country - LIMIT 1; - IF FOUND THEN - return location_row.id; + IF in_location_id IS NULL THEN + -- Straight insert. + location_id = nextval('location_id_seq'); + INSERT INTO location ( + id, + line_one, + line_two, + line_three, + city, + state, + zipcode, + country) + VALUES ( + location_id, + in_address1, + in_address2, + in_address3, + in_city, + in_state, + in_zipcode, + in_country + ); + return location_id; + ELSE + -- Test it. + SELECT * INTO location_row WHERE id = in_location_id; + IF NOT FOUND THEN + -- Tricky users are lying to us. + RAISE EXCEPTION "location_save called with nonexistant location ID %", in_location_id; + ELSE + -- Okay, we're good. + + UPDATE location SET + line_one = in_address1, + line_two = in_address2, + line_three = in_address3, + city = in_city, + state = in_state, + zipcode = in_zipcode, + country = in_country + WHERE id = in_location_id; + return in_location_id; + END IF; END IF; - INSERT INTO location - (line_one, line_two, line_three, city, state, mail_code, country_id, - created) - VALUES - (in_address1, in_address2, in_address3, in_city, in_state, - in_zipcode, in_country, now()); - SELECT currval('location_id_seq') INTO location_id; - return location_id; END; $$ LANGUAGE PLPGSQL; + COMMENT ON function location_save (in_companyname text, in_address1 text, in_address2 text, in_city text, in_state text, in_zipcode text, in_country int) IS diff --git a/sql/modules/Person.sql b/sql/modules/Person.sql index 9eda8335..f942b137 100644 --- a/sql/modules/Person.sql +++ b/sql/modules/Person.sql @@ -131,36 +131,108 @@ BEGIN END; $$ LANGUAGE PLPGSQL; -create or replace function person_location_save( - in_entity_id int, in_location_id int, - in_line_one text, in_line_two text, - in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text, + +/*( + unknown, + unknown, + unknown, + unknown, + unknown, + unknown, + unknown, + unknown) + +*/ +create or replace function person__save_location( + in_entity_id int, + in_location_id int, + in_line_one text, + in_line_two text, + in_line_three text, + in_city TEXT, + in_state TEXT, + in_mail_code text, in_country_code int ) returns int AS $$ DECLARE l_row location; l_id INT; - t_person_id int; + t_person_id int; BEGIN SELECT id INTO t_person_id FROM person WHERE entity_id = in_entity_id; + -- why does it delete? + + select * into l_row FROM location + WHERE id = in_location_id; + + IF NOT FOUND THEN + -- Create a new one. + l_id := location_save( + in_location_id, + in_line_one, + in_line_two, + in_line_three, + in_city, + in_state, + in_mail_code, + in_country_code); + + INSERT INTO person_to_location + (person_id, location_id) + VALUES (t_person_id, l_id); + ELSE + l_id := location_save( + in_location_id, + in_line_one, + in_line_two, + in_line_three, + in_city, + in_state, + in_mail_code, + in_country_code); + -- Update the old one. + END IF; + return l_id; + END; +$$ language 'plpgsql'; - DELETE FROM person_to_location - WHERE person_id = t_person_id - AND location_id = in_location_id; +CREATE OR REPLACE FUNCTION person__delete_location ( + in_entity_id INT, in_location_id INT +) returns int AS $$ - SELECT location_save(in_line_one, in_line_two, in_line_three, in_city, - in_state, in_mail_code, in_country_code) - INTO l_id; +DECLARE + v_loc location; + +BEGIN + + select loc.* into v_loc FROM location loc + JOIN person_to_location ptl ON loc.id = ptl.location_id + JOIN person p ON p.id = ptl.person_id + WHERE p.entity_id = in_entity_id + AND loc.id = in_location_id; + + IF NOT FOUND THEN + RAISE EXCEPTION "Cannot find records to delete for entity % and location %", in_entity_id, in_location_id; + ELSE + DELETE FROM people_to_location WHERE location_id = in_location_id; + DELETE FROM location WHERE location_id = in_location_id; + END IF; + +END; - INSERT INTO person_to_location - (person_id, location_id) - VALUES (t_person_id, l_id); +$$ language plpgsql; - RETURN l_id; - END; +CREATE OR REPLACE FUNCTION person__all_locations ( + in_entity_id int +) returns setof location AS $$ -$$ language 'plpgsql'; + SELECT l.* FROM location l + JOIN person_to_location ptl ON ptl.location_id = l.id + JOIN person p on ptl.person_id = p.id + WHERE p.id = $1; + +$$ language sql; commit; diff --git a/sql/modules/admin.sql b/sql/modules/admin.sql index f590aa94..1055ff29 100644 --- a/sql/modules/admin.sql +++ b/sql/modules/admin.sql @@ -472,6 +472,7 @@ BEGIN pg_roles where rolname ~ ('^lsmb_' || in_database) + order by rolname ASC LOOP RETURN NEXT v_rol; END LOOP; |