Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload a file while creating a calendar event

Status
Not open for further replies.

hktektips

Programmer
Joined
Jun 14, 2001
Messages
2
Location
US
Hi,

We have already implemented a "Calendar" on our intranet to post an event (meeting, minutes, presentation, etc.). Which in turn creates an Event record (in form of DBM files). Now we need to incorporate a change to upload a file for that event. In other words while creating the event the employee/user creating that event can store(upload from local machine) a file (.ppt, .doc, etc.) alongwith that.

Just to let you know I already have written a sample program to upload a file using a multi_part form. The same way I'm trying to call the "upload_file" subroutine when I do a "perform" - (Create Event)

Problem:
--------

I've added filefield to get the file (to be uploaded) from the local machine, as per the requirement there can be maximum of 5 attachment for the event.

I have written 3 (.pm) to handle the whole thing.
- EventEditForm.pm (Generates the form)
- Event.pm (Validate the above form param)
- EventNew.pm (Create the Event)

I have reached to a point where I can read the file names from the form param and when I press the "Create Event" button on my form (behind the scene the EventNew::perform is called and create a DBM record for that event) but since I need to upload those files I need to create the file handle for the files being uploaded and write them to predefined location on the server.

So while I do EventNew::perform, within that I call my "upload_file" subroutine to upload the files but inside the "upload_file" all I get is the file param value (just the file name), the file is empty. I checked that by creating a file handle and tried to print the content of the file, but it doesn't.

Any hints/help will be very much appreciated.

Here is the code for "perform":

sub perform {
my $self = shift;
my ($date, $eventText, $popupText, $popupText1, $popupText2, $popupText3, $popupText4, $popupText5, $export,
$border, $bgColor, $fgColor, $category,
$mailTo, $mailCC, $mailBCC, $mailText,
$reminderTime, $reminderTime2, $reminderAddress,
$ignoreConflict, $ignoreFuture, $editedEventID, $copying) =
$self->getParams (qw (Date EventText PopupText PopupText1 PopupText2 PopupText3 PopupText4 PopupText5
ExportPopup BorderCheckbox
BackgroundColor ForegroundColor
Category
MailTo MailCC MailBCC
MailComments MailReminder
MailReminder2 ReminderAddress
IgnoreTimeConflict
IgnoreFutureLimit
OldEventID CopyEvent));

print "in perform $popupText1\n";
# Strip leading/trailing spaces from strings coming from text fields
foreach ($eventText, $popupText, $popupText1, $popupText2, $popupText3, $popupText4, $popupText5, $bgColor, $fgColor,
$mailTo, $mailCC, $mailBCC, $mailText) {
next unless defined;
s/^\s+//;
s/\s+$//;
}
open (FFN,"$popupText1");
print "AFTER OPEN .... $popupText1\n";
while (<FFN>) {
print $_;
}
close FFN;


my $action = $copying ? 'Copying'
: ($editedEventID ? 'Replacing' : 'Adding New');

unless ($eventText) {
$self->_errorPage ($action,
$self->I18N->get ('You cannot create a blank event')
. '<br>');
$self->{audit_error} = 'blank event';
return;
}

my ($eventDate, $startTime, $endTime, $endDate, $errorMessage)
= ValidateDate->getAndValidateDateAndTimes ($self);

if ($errorMessage) {
$self->_errorPage ($action, $errorMessage);
$self->{audit_error} = 'bad date or time';
return;
}

# if reminders specified, must have email address
if (($reminderTime or $reminderTime2) and !$reminderAddress) {
$self->_errorPage ($action,
$self->I18N->get ('You must specify an email ' .
'address if Email Reminders ' .
'are specified.')
. '<br>');
$self->{audit_error} = 'reminder w/no address';
return;
}

my ($link, $popup);

# If the popup looks like a URL, we call it a link. Otherwise, a popup!
# Note that this is not a very good test. But probably good enough.
# (Basically, anything that is or starts http:, mailto:, ftp:,
# etc.)
if ($popupText and
$popupText =~ /^((https?|mailto|ftp|file):)|^www\.[^ .]+\.[^ .]/s) {
$link = $1 ? $popupText : &quot; $popup = '';
} else {
$popup = $popupText;
$link = '';
}

foreach ($bgColor, $fgColor) {
s/\s*default\s*//i; # If colors are 'Default', get rid of 'em
s/\W//g; # And ensure there's nothing silly going on
$_ = ('#' . $_) if /^\d+$/; # And prepend numerics with the #
}

$category = undef if $category eq '-';

my ($repeatObject);
my ($repeatType) = $self->getParams ('RepeatRadio');

# Handle repeat stuff, maybe
if (!$repeatType || $repeatType =~ /none/i || $repeatType eq '') {
$repeatObject = undef;
} else {
my ($frequency, $period, $monthWeek, $monthMonth,
$untilRadio, $untilYear, $untilMonth, $untilDay, $skipWeekends) =
$self->getParams (qw (Frequency Period MonthWeek MonthMonth
RepeatUntilRadio UntilYearPopup
UntilMonthPopup UntilDayPopup
SkipWeekends));
if ($repeatType =~ /ByWeek/i) {
$period = $frequency = undef;
} else { # it must be Repeat Every Third Day type
$monthWeek = $monthMonth = undef;
}
$repeatObject = RepeatInfo->new ($eventDate, $endDate,
$period, $frequency,
$monthWeek, $monthMonth,
$skipWeekends);
# If we got here from modifing a repeating event, check for exclusions
my ($exclusionString) = $self->getParams ('ExcludedDates');
if ($exclusionString) {
my @excludedDates;
my @excludedStrings = split /\s/, $exclusionString;
foreach my $excludedDate (@excludedStrings) {
push @excludedDates, Date->new ($excludedDate);
}
$repeatObject->exclusionList (\@excludedDates);
}
}

my $reminders = ($reminderTime || '') . ' ' . ($reminderTime2 || '');
$reminders =~ s/^\s//;
$reminders =~ s/\s$//;
$reminderAddress = undef unless $reminders; # since it has a default

# make the new event
my $newEvent = Event->new ('text' => $eventText,
'link' => $link,
'popup' => $popup,
'popup1' => $popupText1,
'popup2' => $popupText2,
'popup3' => $popupText3,
'popup4' => $popupText4,
'popup5' => $popupText5,
'export' => $export,
'startTime' => $startTime,
'endTime' => $endTime,
'repeatInfo' => $repeatObject,
'drawBorder' => $border,
'owner' => $self->getUsername,
'bgColor' => $bgColor,
'fgColor' => $fgColor,
'category' => $category,
'mailTo' => $mailTo,
'mailCC' => $mailCC,
'mailBCC' => $mailBCC,
'mailText' => $mailText,
'reminderTo' => $reminderAddress,
'reminderTimes' => $reminders);

my $isWarning;
($errorMessage, $isWarning)
= ValidateDate->checkFutureLimit ($self, $eventDate, $endDate,
$ignoreFuture);
if ($errorMessage) {
$self->_errorPage ($action, $errorMessage, $isWarning);
$self->{audit_error} = 'future limit';
return;
}

# If we're checking Time Conflicts and adding a repeating event, we
# have to check on every occurrence. Ugh! Don't do &quot;forever&quot;, though;
# only check max of 5 years into the future.
my $prefs = $self->prefs;
if ($repeatObject and defined $startTime and
$prefs->TimeConflicts !~ /allow/i) {
my $occurences = {};
my $endDate = $repeatObject->endDate;
if ($repeatObject->startDate->deltaDays ($endDate) > 365*5) {
$endDate = $repeatObject->startDate + 365*5;
}
$newEvent->addToDateHash ($occurences, $repeatObject->startDate,
$endDate, $self->prefs);
foreach my $date (sort {Date->new($a) <=> Date->new($b)}
keys %$occurences) {
($errorMessage, $isWarning)
= ValidateDate->checkTimeConflicts ($self, Date->new ($date),
$startTime, $endTime,
$editedEventID,
$ignoreConflict);
if ($errorMessage) {
$self->_errorPage ($action, $errorMessage, $isWarning);
$self->{audit_error} = 'time conflict';
return;
}
}
} else {
($errorMessage, $isWarning)
= ValidateDate->checkTimeConflicts ($self, $eventDate, $startTime,
$endTime, $editedEventID,
$ignoreConflict);
if ($errorMessage) {
$self->_errorPage ($action, $errorMessage, $isWarning);
$self->{audit_error} = 'time conflict';
return;
}
}


# Stick it in the database
$self->db->insertEvent ($newEvent, $eventDate);

&upload_file;


# maybe send email notifications
if ($mailTo or $mailCC or $mailBCC) {
require Calendar::Mail::MailNotifier;
MailNotifier->send ($self, $newEvent, $eventDate, $editedEventID);
}
# maybe remember to remind
if (Defines->mailEnabled and $reminders) {
require Calendar::Mail::MailReminder;
MailReminder->add ($newEvent, $self->calendarName, $eventDate);
}

# keep track of things in case we're auditing
$self->{audit_event} = $newEvent;
$self->{audit_eventDate} = $eventDate;

my $showDate = defined ($editedEventID) ? $date : $eventDate;

# And redirect to the page to display. Note the silly double
# redirection; otherwise, we get a blank page, since it seems we can't
# redirect to the url we were called as. There must be a better way.
my $url = $self->makeURL ({Op => 'Redirect',
URL => ($self->makeURL ({Op => 'ShowDay',
Date => $showDate}))});
my $cgi = CGI->new;

print ($cgi->redirect ($url));
}







Thanks,
HK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top