#!/usr/bin/perl
# Create Functions for Perl/PostgreSQL version 1.0
# Copyright 2001, Mark Nielsen
# All rights reserved.
# This Copyright notice was copied and modified from the Perl
# Copyright notice.
# This program is free software; you can redistribute it and/or modify
# it under the terms of either:
# a) the GNU General Public License as published by the Free
# Software Foundation; either version 1, or (at your option) any
# later version, or
# b) the "Artistic License" which comes with this Kit.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
# the GNU General Public License or the Artistic License for more details.
# You should have received a copy of the Artistic License with this
# Kit, in the file named "Artistic". If not, I'll be glad to provide one.
# You should also have received a copy of the GNU General Public License
# along with this program in the file named "Copying". If not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA or visit their web page on the internet at
# http://www.gnu.org/copyleft/gpl.html.
### SAMPLE users SCRIPTNAME
#print "Content-type: text/html\n\n\n";
#-------------------------------------------------
# Load the standard modules and initiaize variables.
use strict;
use SAMPLE::Misc;
use SAMPLE::Constants;
use SAMPLE::Get_Info;
use SAMPLE::Set_Info;
use CGI;
my $Global = SAMPLE::Constants::Get_Constants;
### Get the database connection.
my $dbh = $Global->{'dbh'};
## Load the get info module.
my $Info = new SAMPLE::Get_Info;
## Grab the user_id, if we don't get one, abort.
my $User_Id = $Info->{'users'}->{'users_id'};
## If $ENV{'REMOTE_USER'} is not defined, and that username doesn't
## exist in the users table, abort. We assume that is $ENV{'REMOTE_USER'}
## is defined that they logged in correctly.
my $User_Okay = 1;
if ($User_Id < 1) {print "Location: Error_No_User.pl\n\n";}
#------------------------------------------------------------
# Print Content-Type and get the queried variables.
print "Content-type: text/html\n\n\n";
my $query = new CGI;
my $Update = $query->param('update');
my $Action = $query->param('action');
my @Params = $query->param();
my $Id_Encrypted = $query->param('id');
my $Id = 0;
if ($Id_Encrypted ne "") {$Id = SAMPLE::Misc->Decrypt($Id_Encrypted);}
my $Ids = {};
my $Ids_Encrypted= {};
## Get the ids of the foriegn tables.
$Ids_Encrypted->{'contact_id'} = $query->param('contact_id');
$Ids->{'contact_id'} = 0;
if ($Ids_Encrypted->{'contact_id'} ne "")
{$Ids->{'contact_id'} = SAMPLE::Misc->Decrypt($Ids_Encrypted->{'contact_id'});}
#-------------------------------------------
# Execute any perl methods, which in turn correspond to a database method.
# We could create a generic perl method for this for any table.
# All we would have to do is submit a list of fields for "new" and
# "update" plus the id.
if ($Action eq "new") {$Id = "";}
elsif ($Action eq "delete")
{
my $Delete = new SAMPLE::Set_Info;
my $Result = $Delete->Set_users('id'=>$Id,'command'=>'delete');
$Id = "";
}
elsif ($Action eq "undelete")
{
my $Undelete = new SAMPLE::Set_Info;
my $Result = $Undelete->Set_users('id'=>$Id,'command'=>'undelete');
}
elsif ($Action eq "purge")
{
my $Purge = new SAMPLE::Set_Info;
my $Result = $Purge->Set_users('id'=>$Id,'command'=>'delete');
my $Result = $Purge->Set_users('id'=>$Id,'command'=>'purgeone');
$Id = "";
}
elsif ($Update eq "update")
{
### Update the title of the list
my $Update = new SAMPLE::Set_Info;
### Create the array that we want to submit to the function.
my $Fields = {};
$Fields->{'password'} = $query->param('password');
$Fields->{'user_type'} = $query->param('user_type');
$Fields->{'username'} = $query->param('username');
$Fields->{'contact_id'} = $Ids->{'contact_id'};
### Update the table and submit the $Fields array.
my $Result = $Update->Set_users('id'=>$Id,'command'=>'change','fields'=>$Fields);
}
elsif ($Update eq "new")
{
my $Insert = new SAMPLE::Set_Info;
### Insert new thing into users table.
$Id = $Insert->Set_users('command'=>'insert');
### Create the array that we want to submit to the function.
my $Fields = {};
$Fields->{'password'} = $query->param('password');
$Fields->{'user_type'} = $query->param('user_type');
$Fields->{'username'} = $query->param('username');
$Fields->{'contact_id'} = $Ids->{'contact_id'};
### Hardcode the users_id if you don't want it to change.
# $Fields->{'users_id'} = $User_Id;
# $query->param(-delete=>'users_id');
# $query->param(-name=>'users_id',value=>$Info->{'users'}->{'users_id'});
my $Result = $Insert->Set_users('id'=>$Id,'command'=>'change',"fields"=>$Fields);
}
#----------------------------------------------------------------------
# Now create a list of all the items in this table. Then we can choose
# which ones we want to edit. This could be put into a perl method
# instead of duplicating it for each webpage.
### Now taken care of by the module Get_users
### Create the main list of items we are editing.
### If there is a users_id in the table, only select the ones that we own.
my $List = "";
my $Id_Values = {};
### You might want to modify this sql so that you use more descriptive titles.
my $Temp_List = $Info->List('users','all','none');
foreach my $Key (sort keys %$Temp_List)
{
my $ref = $Temp_List->{$Key};
my $Selected = "";
if ($ref->{'users_id'} eq $Id) {$Selected = " selected";}
my $Id_Encrypt = SAMPLE::Misc->Encrypt($ref->{'users_id'});
my $Status = "Active";
if ($ref->{'active'} == 0) {$Status = "Inactive";}
my $Title = $ref->{'users_id'};
### We are looking for the field "title", but change it so what the field
### should be. This is just an example.
if (exists $ref->{'title'}) {$Title = $ref->{'title'}}
$List .= qq(