#!/usr/bin/perl # MimerDesk # Web based groupware and eLearning environment # www.mimerdesk.org # # Copyright (C) 2001 Ionstream Ltd. # www.ionstream.fi # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed with a hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # $Id: calendar_add_event.html,v 1.20 2002/07/15 19:24:47 inf Exp $ #"""""""""""""""""""""""""""""""""""""""""# # # # MimerDesk: Calendar event adder # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # (c) Ionstream Oy 1999 - 2001 # # # # Programmed by: Hannes Muurinen # # # #_________________________________________# use lib::MimerDesk; use strict; use vars qw($form $ACTIVEGRP $APPLICATION $USER $IP $LAST_ACT $FORWARDED $TIME_USED $IDLE $event_added_error_msg $admin_status $GROUPACTIVOR $CALENDAR $CALENDARUSER @default_categories $gettext %errorhash $event_success_msg); use CGI::Carp "fatalsToBrowser"; $APPLICATION = 'Personal - Calendar'; read_config('../config/mimerdesk.cfg'); initialize(); $form = decode_multipart(); $form->{'ID'} =~ tr/0-9//cd; $form->{'auth'} =~ tr/0-9a-z//cd; ($USER, $IP, $LAST_ACT, $FORWARDED, $TIME_USED, $IDLE, $ACTIVEGRP) = authenticate($form->{'ID'}, $form->{'auth'}, $form->{'changeGroup'}); $gettext = lib::MimerDesk->new_gettext(program => 'calendar_add',language => $config{'language'}); $config{'theme'} = 'default'; $APPLICATION = $gettext->gettext('Personal - Calendar - Add/Modify Event'); $CALENDAR = 'calendar'; # the database table name $CALENDARUSER = ''; # the user of the calendar (group name for public group calendars, user's login name for private calendar) # the default categories for the events. Own categories can also be added. @default_categories = $gettext->gettext('None','Business','School','Hobby'); sub print_template; sub replace_eventOptionsTable_tags; sub make_event_duration_options; sub make_event_reminder_options; sub make_event_remindertime_options; sub make_event_recrule_options; sub make_submit_buttons; sub check_form_arguments; sub date_after_x_days; sub day_num; sub days_in_between; sub days_in_month; sub floor; sub process_add_event; sub calendar_error_message; sub check_form_data; # The main program ################### $CALENDARUSER = $USER; # alustavasti if ($form->{'grouptool'} =~ /^\d+$/) { lock_tables('READ', 'groupusers'); db_list("SELECT RIGHTS,USER FROM groupusers where USER = '$USER' and GID = '$form->{'grouptool'}'"); while (my $ref = $sth->fetchrow_hashref()) { $admin_status = $ref->{'RIGHTS'}; $GROUPACTIVOR = $ref->{'USER'}; } db_end(); unlock_tables(); if (!$GROUPACTIVOR) { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); $form->{'grouptool'} = ''; } else { $CALENDAR = "$form->{'grouptool'}_$CALENDAR"; $CALENDARUSER = $form->{'grouptool'}; } if($admin_status !~ /C0/) { $form->{'CANCEL'} = 'Cancel'; # pistetäänkö merkintä logeihin? joku on yrittänyt päästä lisäämään eventtejä group-kalenteriin ilman oikeuksia } } else { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); } $form->{'actionType'} = 'add' if $form->{'actionType'} eq ""; $event_added_error_msg = process_addEvent() if (($form->{'UPDATE'} ne '' or $form->{'DELETE'} ne '') and $form->{'CANCEL'} ne 'Cancel'); $event_success_msg = send_event() if ($form->{'sendevent'}); if ($event_added_error_msg ne 'ok' and $form->{'CANCEL'} ne 'Cancel') { print_template("$config{'theme'}_calendar_$form->{'actionType'}_event"); } elsif($form->{'addnewevent'} eq 'add new') { redirect("calendar_add_event.html?ID=$form->{'ID'}&auth=$form->{'auth'}&actionType=add&calview=$form->{'calview'}&grouptool=$form->{'grouptool'}"); } else { redirect("calendar.html?ID=$form->{'ID'}&auth=$form->{'auth'}&view=$form->{'calview'}&day=$form->{'oldcalday'}&month=$form->{'oldcalmonth'}&year=$form->{'oldcalyear'}&grouptool=$form->{'grouptool'}"); } # Subs: ########### # # print_template # # 1. prints the header # 2. concatenates the different parts of the page # 3. replaces the <<>>-tags # 4. prints the rest of the page # sub print_template { my ($template, $ref, $fulltemplate); $template = shift; print_header('pragma'); $ref = get_template('maintemplate',$template); $ref->{'maintemplate'} =~ s/<>/$ref->{$template}/m; $fulltemplate = $ref->{'maintemplate'}; if ($GROUPACTIVOR) { $fulltemplate = create_buttons($fulltemplate, 'Group', 'Calendar', $form); $APPLICATION = $gettext->gettext('Group - Calendar - Add/Modify Event'); } else {$fulltemplate = create_buttons($fulltemplate, 'Personal', 'Calendar', $form);} $fulltemplate =~ s/<>/minea/gm; $ref = get_template('js_doClock', 'js_help', 'js_gotosite'); my $stuff = $ref->{'js_doClock'}.$ref->{'js_help'}.$ref->{'js_gotosite'}; $fulltemplate =~ s/<>/$stuff/ms; $fulltemplate =~ s/<>/MimerDesk\: $APPLICATION/ms; $fulltemplate = replace_eventOptionsTable_tags($fulltemplate); $fulltemplate = replace_eventError_tags($fulltemplate); $fulltemplate = replace_tags($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $fulltemplate = add_popups($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}); print $fulltemplate; db_end('disconnect'); exit; } ################################################################# # process_addEvent - adds/modifies events in database # #---------------------------------------------------------------# # process_addEvent() # # # # Adds/modifies events in database. The event data must # # be given in the form. Returns status ("error"/"ok") # # # # Usage: # # $event_add_status = process_addEvent() # # # ################################################################# sub process_addEvent { while(length($form->{'hour'}) < 2) { $form->{'hour'} = "0".$form->{'hour'}; } while(length($form->{'minute'}) < 2) { $form->{'minute'} = "0".$form->{'minute'}; } my $event_error_message = check_form_data(); if ($form->{'DELETE'} eq "Remove event") { if($form->{'rec_events_to_modify'} eq 'All recurring events') { #removes all the events in the recurrence: my ($ref) = db_select('prevrec',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); while($ref->{'prevrec'} != 0) # while there is a previous event in the recurrence, do: { my $eventID = $ref->{'prevrec'}; ($ref) = db_select('prevrec, reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete($CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$ref->{'reminderID'}'"); } ($ref) = db_select('nextrec',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); while($ref->{'nextrec'} != 0) # while there is a next event in the recurrence, do: { my $eventID = $ref->{'nextrec'}; ($ref) = db_select('nextrec, reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete($CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$ref->{'reminderID'}'"); } } elsif($form->{'rec_events_to_modify'} eq 'This and all future recurring events') { my ($ref) = db_select('nextrec, prevrec',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); #update the linked list on recurring events: my %db = ('nextrec' => '0' ); db_update($CALENDAR,\%db,"IDnumber = '$ref->{'prevrec'}' and user = '$CALENDARUSER'"); #removes only the future events in the recurrence: while($ref->{'nextrec'} != 0) # while there is a next event in the recurrence, do: { my $eventID = $ref->{'nextrec'}; ($ref) = db_select('nextrec, reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete($CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$ref->{'reminderID'}'"); } } else { #update the linked list on recurring events: my ($ref) = db_select('prevrec, nextrec, reminderID',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); my %db = ('prevrec' => $ref->{'prevrec'} ); db_update($CALENDAR,\%db,"IDnumber = '$ref->{'nextrec'}' and user = '$CALENDARUSER'"); %db = ('nextrec' => $ref->{'nextrec'} ); db_update($CALENDAR,\%db,"IDnumber = '$ref->{'prevrec'}' and user = '$CALENDARUSER'"); db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$form->{'eventID'}/$form->{'grouptool'}' and LINE = '$ref->{'reminderID'}'"); } #delete the event db_delete($CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); db_end(); $event_error_message = 'ok'; } else { return $event_error_message if $event_error_message ne "ok"; # if event adding arguments are ok, we can continue process_rec_rule_data(); my ($eventtype, $firstrec, $nextrec, $previousrec); if ($form->{'neweventtype'} eq '') { $eventtype = $form->{'eventtype'};} else {$eventtype = $form->{'neweventtype'};} my($time,$epoch,$end_epoch,$rec_end_epoch); if($form->{'use_time'}) {$time = "$form->{'hour'}:$form->{'minute'}";} else {$time = 'full';} $epoch = local_date2utc_epoch(0,$form->{'minute'},$form->{'hour'},$form->{'day'},$form->{'month'},$form->{'year'}); $end_epoch = local_date2utc_epoch(0,$form->{'endminute'},$form->{'endhour'},$form->{'endday'},$form->{'endmonth'},$form->{'endyear'}); $rec_end_epoch = local_date2utc_epoch(0,$form->{'minute'},$form->{'hour'},$form->{'eday'},$form->{'emonth'},$form->{'eyear'})+($end_epoch-$epoch); ($form->{'recrule'},$eventtype,$epoch,$time,$end_epoch,$form->{'title'},$form->{'eventText'}) = prepare_fordb($form->{'recrule'},$eventtype,$epoch,$time,$end_epoch,$form->{'title'},$form->{'eventText'}); if ($form->{'UPDATE'} eq "Add event") { if($form->{'recrule'} eq 'norec') { my %db = ( 'IDnumber' => 'NULL', 'user' => $CALENDARUSER, 'epoch' => $epoch, 'time' => $time, 'end_epoch' => $end_epoch, 'type' => $eventtype, 'recrule' => $form->{'recrule'}, 'nextrec' => '', 'prevrec' => '', 'summary' => $form->{'title'}, 'text' => $form->{'eventText'}, 'reminderID' => 'NULL' ); db_insert($CALENDAR,\%db); my $event_id; db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$event_id = $ref->{'id'};} db_end(); if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $epoch - ($form->{'reminderday'}*86400 + $form->{'reminderminute'}*60 + $form->{'reminderhour'}*3600); my $reminderID; lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$event_id/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$reminderID = $ref->{'id'};} db_end(); unlock_tables(); my %db = ( 'reminderID' => $reminderID ); db_update($CALENDAR,\%db,"IDnumber = '$event_id' and user = '$CALENDARUSER'"); } } else { $previousrec = 'NULL'; my $reminderdifference = $form->{'reminderday'}*86400 + $form->{'reminderminute'}*60 + $form->{'reminderhour'}*3600; while($epoch < $rec_end_epoch) # insert the other parts of the recurring event { my %db = ( 'IDnumber' => 'NULL', 'user' => $CALENDARUSER, 'epoch' => $epoch, 'time' => $time, 'end_epoch' => $end_epoch, 'type' => $eventtype, 'recrule' => $form->{'recrule'}, 'nextrec' => '', 'prevrec' => $previousrec, 'summary' => $form->{'title'}, 'text' => $form->{'eventText'}, 'reminderID' => 'NULL' ); db_insert($CALENDAR,\%db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$nextrec = $ref->{'id'};} #update the previous event: %db = ( 'nextrec' => $nextrec, ); db_update($CALENDAR,\%db,"IDnumber = '$previousrec' and user = '$CALENDARUSER'"); db_end(); $previousrec = $nextrec; if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $epoch - $reminderdifference; my $reminderID; lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$nextrec/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$reminderID = $ref->{'id'};} db_end(); unlock_tables(); %db = ( 'reminderID' => $reminderID ); db_update($CALENDAR,\%db,"IDnumber = '$nextrec' and user = '$CALENDARUSER'"); } if($form->{'recrule'} eq 'daily') { $epoch += 86400; # seconds $end_epoch += 86400; } elsif($form->{'recrule'} eq 'weekly') { $epoch += 604800; $end_epoch += 604800; } elsif($form->{'recrule'} eq '2weeks') { $epoch += 1209600; $end_epoch += 1209600; } elsif($form->{'recrule'} eq 'monthly') { my $oldepoch = $epoch; my @datee = utc_epoch2date($epoch); my @datee2 = date_after_x_days($datee[3],$datee[4],$datee[5],days_in_month($datee[4],$datee[5])); #next month $epoch = local_date2utc_epoch($datee[0],$datee[1],$datee[2],$datee2[3],$datee2[4],$datee2[5]); $end_epoch += ($epoch-$oldepoch); } elsif($form->{'recrule'} eq 'yearly') { my $oldepoch = $epoch; my @datee = utc_epoch2date($epoch); $epoch = local_date2utc_epoch($datee[0],$datee[1],$datee[2],$datee[3],$datee[4],$datee[5]+1); $end_epoch += ($epoch-$oldepoch); } } #update the last event: my %db = ( 'nextrec' => 'NULL' ); db_update($CALENDAR,\%db,"IDnumber = '$previousrec' and user = '$CALENDARUSER'"); db_end(); } } elsif ($form->{'UPDATE'} eq "Modify event") { if($form->{'rec_events_to_modify'} eq 'All recurring events') { my %db = ( 'epoch' => $epoch, 'time' => $time, 'end_epoch' => $end_epoch, 'type' => $eventtype, 'summary' => $form->{'title'}, 'text' => $form->{'eventText'} ); #affects all the events in the recurrence: my ($ref) = db_select('prevrec, epoch, end_epoch',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); my ($epochdifference, $end_epochdifference, $reminderdifference) = ($epoch-$ref->{'epoch'}, $end_epoch-$ref->{'end_epoch'}, $form->{'reminderday'}*86400 + $form->{'reminderminute'}*60 + $form->{'reminderhour'}*3600); while($ref->{'prevrec'} != 0) # while there is a previous event in the recurrence, do: { my $eventID = $ref->{'prevrec'}; ($ref) = db_select('prevrec, epoch, end_epoch, reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); $db{'epoch'} = $ref->{'epoch'} + $epochdifference; $db{'end_epoch'} = $ref->{'end_epoch'} + $end_epochdifference; my $remID = $ref->{'reminderID'}; if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $db{'epoch'} - $reminderdifference; if($remID) # if there is already a reminder in the database, update the old one: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TIME' => $loctime, 'STATUS' => 'no', 'SEND_TIME' => $reminder_epoch, ); db_update('messages',\%message_db,"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'"); db_end(); unlock_tables(); } else # if there isn't an old reminder, create new: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$eventID/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$db{'reminderID'} = $ref->{'id'};} db_end(); unlock_tables(); } } else # else there is no reminder { $db{'reminderID'} = 'NULL'; db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'") if($remID); } db_update($CALENDAR,\%db,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); } ($ref) = db_select('nextrec',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); while($ref->{'nextrec'} != 0) # while there is a next event in the recurrence, do: { my $eventID = $ref->{'nextrec'}; ($ref) = db_select('nextrec, epoch, end_epoch, $reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); $db{'epoch'} = $ref->{'epoch'} + $epochdifference; $db{'end_epoch'} = $ref->{'end_epoch'} + $end_epochdifference; my $remID = $ref->{'reminderID'}; if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $db{'epoch'} - $reminderdifference; if($remID) # if there is already a reminder in the database, update the old one: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TIME' => $loctime, 'STATUS' => 'no', 'SEND_TIME' => $reminder_epoch, ); db_update('messages',\%message_db,"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'"); db_end(); unlock_tables(); } else # if there isn't an old reminder, create new: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$eventID/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$db{'reminderID'} = $ref->{'id'};} db_end(); unlock_tables(); } } else # else there is no reminder { $db{'reminderID'} = 'NULL'; db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'") if($remID); } db_update($CALENDAR,\%db,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); } } elsif($form->{'rec_events_to_modify'} eq 'This and all future recurring events') { my %db = ( 'epoch' => $epoch, 'time' => $time, 'end_epoch' => $end_epoch, 'type' => $eventtype, 'summary' => $form->{'title'}, 'text' => $form->{'eventText'} ); #affects only the future events in the recurrence: my ($ref) = db_select('nextrec, epoch, end_epoch',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); my ($epochdifference, $end_epochdifference, $reminderdifference) = ($epoch-$ref->{'epoch'}, $end_epoch-$ref->{'end_epoch'}, $form->{'reminderday'}*86400 + $form->{'reminderminute'}*60 + $form->{'reminderhour'}*3600); while($ref->{'nextrec'} != 0) # while there is a next event in the recurrence, do: { my $eventID = $ref->{'nextrec'}; ($ref) = db_select('nextrec, epoch, end_epoch, reminderID',$CALENDAR,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); $db{'epoch'} = $ref->{'epoch'} + $epochdifference; $db{'end_epoch'} = $ref->{'end_epoch'} + $end_epochdifference; my $remID = $ref->{'reminderID'}; if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $db{'epoch'} - $reminderdifference; if($remID) # if there is already a reminder in the database, update the old one: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TIME' => $loctime, 'STATUS' => 'no', 'SEND_TIME' => $reminder_epoch, ); db_update('messages',\%message_db,"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'"); db_end(); unlock_tables(); } else # if there isn't an old reminder, create new: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$eventID/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$db{'reminderID'} = $ref->{'id'};} db_end(); unlock_tables(); } } else # else there is no reminder { $db{'reminderID'} = 'NULL'; db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$eventID/$form->{'grouptool'}' and LINE = '$remID'") if($remID); } db_update($CALENDAR,\%db,"IDnumber = '$eventID' and user = '$CALENDARUSER'"); } } my %db = ( 'epoch' => $epoch, 'time' => $time, 'end_epoch' => $end_epoch, 'type' => $eventtype, 'summary' => $form->{'title'}, 'text' => $form->{'eventText'} ); db_update($CALENDAR,\%db,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); my ($ref) = db_select('reminderID',$CALENDAR,"IDnumber = '$form->{'eventID'}' and user = '$CALENDARUSER'"); my $remID = $ref->{'reminderID'}; db_end(); if($form->{'reminderType'} eq 'popup') { my $loctime = time; my $reminder_epoch = $db{'epoch'} - ($form->{'reminderday'}*86400 + $form->{'reminderminute'}*60 + $form->{'reminderhour'}*3600); if($remID) # if there is already a reminder in the database, update the old one: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TIME' => $loctime, 'STATUS' => 'no', 'SEND_TIME' => $reminder_epoch, ); db_update('messages',\%message_db,"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$form->{'eventID'}/$form->{'grouptool'}' and LINE = '$remID'"); db_end(); unlock_tables(); } else # if there isn't an old reminder, create new: { lock_tables('WRITE', 'messages'); my %message_db = ( 'TYPE' => 'REMINDER', 'TARGET' => $USER, 'SENDER' => $USER, 'TIME' => $loctime, 'STATUS' => 'no', 'MESSAGE' => "$form->{'eventID'}/$form->{'grouptool'}", 'SEND_TIME' => $reminder_epoch, ); my ($nimi); db_insert('messages',\%message_db); db_list("select LAST_INSERT_ID() as id"); while (my $ref = $sth->fetchrow_hashref()) {$db{'reminderID'} = $ref->{'id'};} db_end(); unlock_tables(); } } else # else there is no reminder { $db{'reminderID'} = 'NULL'; db_delete('messages',"TYPE = 'REMINDER' and SENDER = '$USER' and TARGET = '$USER' and MESSAGE = '$form->{'eventID'}/$form->{'grouptool'}' and LINE = '$remID'") if($remID); } } } return $event_error_message; } ######################################################################### # send_event - sends the event to selected user. # #-----------------------------------------------------------------------# # # # returns error/success message. # # # ######################################################################### sub send_event { my ($message, @eventID, @groupNames); if($form->{'target_user'} or $form->{'target_user_text'}) { my ($target); if($form->{'target_user_text'}) {$target = $form->{'target_user_text'};} else {$target = $form->{'target_user'};} lock_tables('READ', 'users'); my ($check_ref) = db_select('UID','users',"nimi = '$target'"); my $check_UID = $check_ref->{'UID'}; db_end(); unlock_tables(); return tagged('span',{class => 'error', content => $gettext->gettext('You must select user!')}) if !$check_UID; while(length($form->{'hour'}) < 2) { $form->{'hour'} = "0".$form->{'hour'}; } while(length($form->{'minute'}) < 2) { $form->{'minute'} = "0".$form->{'minute'}; } my $event_error_message = check_form_data(); return tagged('span',{class => 'error', content => $event_error_message}) if $event_error_message ne "ok"; my($time,$epoch,$end_epoch,$eventtype,$curr_epoch); $curr_epoch = time(); if ($form->{'neweventtype'} eq '') { $eventtype = $form->{'eventtype'};} else {$eventtype = $form->{'neweventtype'};} if($form->{'use_time'}) {$time = "$form->{'hour'}:$form->{'minute'}";} else {$time = 'full';} $epoch = local_date2utc_epoch(0,$form->{'minute'},$form->{'hour'},$form->{'day'},$form->{'month'},$form->{'year'}); $end_epoch = local_date2utc_epoch(0,$form->{'endminute'},$form->{'endhour'},$form->{'endday'},$form->{'endmonth'},$form->{'endyear'}); ($eventtype,$epoch,$time,$end_epoch,$form->{'title'},$form->{'eventText'}) = prepare_fordb($eventtype,$epoch,$time,$end_epoch,$form->{'title'},$form->{'eventText'}); lock_tables('WRITE', 'messages'); $eventtype =~ s/"//gsm; $form->{'title'} =~ s/"//gsm; $form->{'eventText'} =~ s/"//gsm; my ($nimi); next if db_select('MESSAGE','messages',"TYPE = 'CALENDAR' and TARGET = '$target' and MESSAGE = '0/$form->{'grouptool'}/$epoch/$end_epoch/$eventtype/$time/$form->{'title'}/$form->{'eventText'}'"); # groupID/events_information -- groupID == 0 => send event to private calendar, else send to group calendar with ID groupID db_list("insert into messages (TARGET,SENDER,TIME,STATUS,MESSAGE,TYPE) values ('$target', '$USER', '$curr_epoch', 'no','0/$form->{'grouptool'}/$epoch/$end_epoch/$eventtype/$time/$form->{'title'}/$form->{'eventText'}','CALENDAR')"); db_end(); unlock_tables(); $message .= sprintf($gettext->gettext('The event has been sent to %s'),$target); } else { return tagged('span',{class => 'error', content => $gettext->gettext("You must select user!")}); } return $message; } ################################################################################# # make_friendlist_dropdown - makes a dropdown with Your Friends in it # #-------------------------------------------------------------------------------# # make_friendlist_dropdown() # # # # Usage: # # $HTML_dropdown = make_friendlist_dropdown() # # # ################################################################################# sub make_friendlist_dropdown { my @dropitems = (''); my %dropvalues = ('' => $gettext->gettext('[ Select user from My Friends ]')); lock_tables('READ', 'friends', 'users'); db_list("select friends.friend,users.info from friends,users where friends.user = '$USER' and friends.friend = users.nimi order by friends.friend"); while (my $ref = $sth->fetchrow_hashref()) { push(@dropitems, $ref->{'friend'}); $dropvalues{$ref->{'friend'}} = "$ref->{'friend'} ($ref->{'info'})"; } db_end(); unlock_tables(); my %dropargs = (name => 'target_user', order => \@dropitems, 'values' => \%dropvalues, default => $form->{'target_user'} ); return dropdownmenu(%dropargs). ' '.$gettext->gettext('or send to user:').' '. textfield(name => 'target_user_text', value => $form->{'target_user_text'}, size => 20); } ################################################################################# # check_form_data - checks the form data and returns possible error messages # #-------------------------------------------------------------------------------# # check_form_data() # # # # Usage: # # $add_event_error_msg = check_form_data() # # # ################################################################################# sub check_form_data { my $event_add_error_msg = ""; if($form->{'hour'} > 24 or $form->{'hour'} < 0 or $form->{'hour'} !~ /^[0-9]+$/ or $form->{'minute'} > 59 or $form->{'minute'} < 0 or $form->{'minute'} !~ /^[0-9]+$/ or $form->{'day'} > days_in_month($form->{'month'},$form->{'year'}) or $form->{'day'} < 1 or $form->{'day'} !~ /^[0-9]+$/ or $form->{'month'} > 12 or $form->{'month'} < 1 or $form->{'month'} !~ /^[0-9]+$/ or $form->{'year'} !~ /^[0-9]+$/ ) { $event_add_error_msg .= $gettext->gettext('Invalid event starting time given!').tag("BR"); $errorhash{'startdateerror'} = '*'; } if($form->{'endhour'} > 24 or $form->{'endhour'} < 0 or $form->{'endhour'} !~ /^[0-9]+$/ or $form->{'endminute'} > 59 or $form->{'endminute'} < 0 or $form->{'endminute'} !~ /^[0-9]+$/ or $form->{'endday'} > days_in_month($form->{'endmonth'},$form->{'endyear'}) or $form->{'endday'} < 1 or $form->{'endday'} !~ /^[0-9]+$/ or $form->{'endmonth'} > 12 or $form->{'endmonth'} < 1 or $form->{'endmonth'} !~ /^[0-9]+$/ or $form->{'endyear'} !~ /^[0-9]+$/) { $event_add_error_msg .= $gettext->gettext('Invalid event ending time given!').tag("BR"); $errorhash{'enddateerror'} = '*'; } if($form->{'actionType'} ne 'modify') { if($form->{'sday'} > days_in_month($form->{'smonth'},$form->{'syear'}) or $form->{'sday'} < 1 or $form->{'sday'} !~ /^[0-9]+$/ or $form->{'smonth'} > 12 or $form->{'smonth'} < 1 or $form->{'smonth'} !~ /^[0-9]+$/ or $form->{'syear'} !~ /^[0-9]+$/) { $event_add_error_msg .= $gettext->gettext('Invalid recurrence starting time given!').tag("BR"); $errorhash{'recstarterror'} = '*'; } if($form->{'eday'} > days_in_month($form->{'emonth'},$form->{'eyear'}) or $form->{'eday'} < 1 or $form->{'eday'} !~ /^[0-9]+$/ or $form->{'emonth'} > 12 or $form->{'emonth'} < 1 or $form->{'emonth'} !~ /^[0-9]+$/ or $form->{'eyear'} !~ /^[0-9]+$/) { $event_add_error_msg .= $gettext->gettext('Invalid recurrence ending time given!').tag("BR"); $errorhash{'recenderror'} = '*'; } # if recurrence starting date is after ending date: if((day_num($form->{'sday'},$form->{'smonth'},$form->{'syear'}) > day_num($form->{'eday'},$form->{'emonth'},$form->{'eyear'})) and $form->{'recrule'} ne 'norec') { $event_add_error_msg .= $gettext->gettext('Recurrence date error: starting date is after the ending date!').tag("BR"); $errorhash{'recstarterror'} = '*'; $errorhash{'recenderror'} = '*'; } # if the date of the event is not inside the boundaries of the recurrence: if(((day_num($form->{'day'},$form->{'month'},$form->{'year'}) > day_num($form->{'eday'},$form->{'emonth'},$form->{'eyear'})) or (day_num($form->{'day'},$form->{'month'},$form->{'year'}) < day_num($form->{'sday'},$form->{'smonth'},$form->{'syear'}))) and $form->{'recrule'} ne 'norec') { $event_add_error_msg .= $gettext->gettext('Recurrence date error: the entered event date is outside the recurrence boundaries').tag("BR"); $errorhash{'recstarterror'} = '*'; $errorhash{'recenderror'} = '*'; $errorhash{'startdateerror'} = '*'; } } if($form->{'title'} eq '') { $event_add_error_msg .= $gettext->gettext('No Title. You have to enter a Title!')."
"; $errorhash{'titleerror'} = '*'; } # if event starting date is after ending date: if(local_date2utc_epoch(0,$form->{'minute'},$form->{'hour'},$form->{'day'},$form->{'month'},$form->{'year'}) > local_date2utc_epoch(0,$form->{'endminute'},$form->{'endhour'},$form->{'endday'},$form->{'endmonth'},$form->{'endyear'})) { $event_add_error_msg .= $gettext->gettext('Event date error: starting date is after the ending date!').tag("BR"); $errorhash{'startdateerror'} = '*'; $errorhash{'enddateerror'} = '*'; } $form->{'title'} =~ s/\///sgm; $form->{'neweventtype'} =~ s/\///sgm; $form->{'title'} =~ s/"//sgm; $form->{'neweventtype'} =~ s/"//sgm; $form->{'eventText'} =~ s/"//sgm; $event_add_error_msg = 'ok' if $event_add_error_msg eq ''; return $event_add_error_msg; } ######################################################################### # replace_eventOptionsTable_tags - replaces some tags # #-----------------------------------------------------------------------# # replace_eventOptionsTable_tags($some_text_with_tags) # # # # Usage: # # $HTML = replace_eventOptionsTable_tags($HTML) # # # ######################################################################### sub replace_eventOptionsTable_tags { my $template = shift; my ($some_HTML); check_form_arguments(); $some_HTML = ucfirst($form->{'actionType'}); $template =~ s/<>/$some_HTML event/ms; $template =~ s/<>/$form->{'title'}/ms; $template =~ s/<>/$form->{'eventText'}/ms; $some_HTML = make_event_reminder_options($form->{'reminderType'}); $template =~ s/<>/$some_HTML/ms; # $some_HTML = make_event_remindertime_options($form->{'reminderTimeType'}); # $template =~ s/<>/$some_HTML/ms; $some_HTML = make_event_recrule_options($form->{'recrule'}); $template =~ s/<>/$some_HTML/ms; my($time_selected,$time_selected_inv); if($form->{'use_time'} eq '0') {($time_selected,$time_selected_inv) = ('','checked');} else {($time_selected,$time_selected_inv) = ('checked','');} $template =~ s/<>/$time_selected/ms; $template =~ s/<>/$time_selected_inv/ms; if($form->{'recrule'} eq 'daily') { $some_HTML = $gettext->gettext('Daily');} elsif($form->{'recrule'} eq 'weekly') { $some_HTML = $gettext->gettext('Weekly');} elsif($form->{'recrule'} eq 'monthly') { $some_HTML = $gettext->gettext('Monthly');} elsif($form->{'recrule'} eq 'yearly') { $some_HTML = $gettext->gettext('Yearly');} elsif($form->{'recrule'} eq '2weeks') { $some_HTML = $gettext->gettext('Every two weeks');} else {$some_HTML = $gettext->gettext('No recurrence');} $template =~ s/<>/$some_HTML/ms; # if($form->{'recrule'} ne 'norec') {$some_HTML = qq[Remove all recurrence events connected to this one];} # else {$some_HTML = '';} # $template =~ s/<>/$some_HTML/ms; if($form->{'recrule'} ne 'norec') { my @d_items = ('This event only','All recurring events','This and all future recurring events'); my %d_values = ('This event only' => $gettext->gettext('This event only'), 'All recurring events' => $gettext->gettext('All recurring events'), 'This and all future recurring events' => $gettext->gettext('This and all future recurring events')); my %d_args = (name => 'rec_events_to_modify', order => \@d_items, 'values'=> \%d_values, default => 'This event only', ); my $ddmenu = dropdownmenu(%d_args); $some_HTML = startrow() . column(class => 'content', bgcolor => 'CEDFE8', align => 'left', width => '29%', content => $gettext->gettext('Events to affect')) . column(class => 'content', bgcolor => 'FFFFFF', width => '71%', align => 'left', content => $ddmenu) . endrow(); } else {$some_HTML = '';} $template =~ s/<>/$some_HTML/ms; $some_HTML = make_submit_buttons($form->{'actionType'}); $template =~ s/<>/$some_HTML/ms; $template =~ s/<>/$form->{'calview'}/ms; $template =~ s/<>/$form->{'oldcalday'}/ms; $template =~ s/<>/$form->{'oldcalmonth'}/ms; $template =~ s/<>/$form->{'oldcalyear'}/ms; $template =~ s/<>/$form->{'eventID'}/ms; $template =~ s/<>/$form->{'actionType'}/ms; $template =~ s/<>/$form->{'grouptool'}/ms; $template =~ s/<>/$form->{'neweventtype'}/ms; # check the categories: # toi checklisti-purkka pitää korvata array-grepillä! my @dropitems = @default_categories; my $checklist = ';'; foreach(@dropitems) {$checklist .= $_ . ';';} db_list("SELECT DISTINCT type FROM $CALENDAR where user = '$CALENDARUSER'"); while (my $ref = $sth->fetchrow_hashref()){ push @dropitems, "$ref->{'type'}" if($ref->{'type'} ne '' and $checklist !~ /;$ref->{'type'};/); } db_end(); sort(@dropitems); my %dropargs = (name => 'eventtype', order => \@dropitems, default => $form->{'eventtype'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #the minute/hour dropdowns: @dropitems = ('00'..'23'); %dropargs = ( name => 'hour', order => \@dropitems, default => $form->{'hour'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; %dropargs = ( name => 'endhour', order => \@dropitems, default => $form->{'endhour'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; %dropargs = ( name => 'reminderhour', order => \@dropitems, default => $form->{'reminderhour'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; @dropitems = ('00','05','10','15','20','25','30','35','40','45','50','55'); %dropargs = ( name => 'minute', order => \@dropitems, default => $form->{'minute'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; %dropargs = ( name => 'endminute', order => \@dropitems, default => $form->{'endminute'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; %dropargs = ( name => 'reminderminute', order => \@dropitems, default => $form->{'reminderminute'} ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #the date dropdowns: my @daydropitems = ('01'..'31'); my @monthdropitems = ('01'..'12'); my @yeardropitems; my @thisdate = utc_epoch2date(time); for (my $i = $thisdate[5]-5; $i <= $thisdate[5]+10; $i++) { push @yeardropitems, $i; } #the day & month values must be two digit numbers! $form->{'day'} = '0'.$form->{'day'} while length($form->{'day'}) < 2; $form->{'month'} = '0'.$form->{'month'} while length($form->{'month'}) < 2; $form->{'endday'} = '0'.$form->{'endday'} while length($form->{'endday'}) < 2; $form->{'endmonth'} = '0'.$form->{'endmonth'} while length($form->{'endmonth'}) < 2; $form->{'sday'} = '0'.$form->{'sday'} while length($form->{'sday'}) < 2; $form->{'smonth'} = '0'.$form->{'smonth'} while length($form->{'smonth'}) < 2; $form->{'eday'} = '0'.$form->{'eday'} while length($form->{'eday'}) < 2; $form->{'emonth'} = '0'.$form->{'emonth'} while length($form->{'emonth'}) < 2; #event start date: %dropargs = ( name => 'day', order => \@daydropitems, default => $form->{'day'}, ); $some_HTML = dropdownmenu(%dropargs); %dropargs = ( name => 'month', order => \@monthdropitems, default => $form->{'month'}, ); $some_HTML .= dropdownmenu(%dropargs); %dropargs = ( name => 'year', order => \@yeardropitems, default => $form->{'year'}, ); $some_HTML .= dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #event end date: %dropargs = ( name => 'endday', order => \@daydropitems, default => $form->{'endday'}, ); $some_HTML = dropdownmenu(%dropargs); %dropargs = ( name => 'endmonth', order => \@monthdropitems, default => $form->{'endmonth'}, ); $some_HTML .= dropdownmenu(%dropargs); %dropargs = ( name => 'endyear', order => \@yeardropitems, default => $form->{'endyear'}, ); $some_HTML .= dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #recurrence start date: %dropargs = ( name => 'sday', order => \@daydropitems, default => $form->{'sday'}, ); $some_HTML = dropdownmenu(%dropargs); %dropargs = ( name => 'smonth', order => \@monthdropitems, default => $form->{'smonth'}, ); $some_HTML .= dropdownmenu(%dropargs); %dropargs = ( name => 'syear', order => \@yeardropitems, default => $form->{'syear'}, ); $some_HTML .= dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #recurrence end date: %dropargs = ( name => 'eday', order => \@daydropitems, default => $form->{'eday'}, ); $some_HTML = dropdownmenu(%dropargs); %dropargs = ( name => 'emonth', order => \@monthdropitems, default => $form->{'emonth'}, ); $some_HTML .= dropdownmenu(%dropargs); %dropargs = ( name => 'eyear', order => \@yeardropitems, default => $form->{'eyear'}, ); $some_HTML .= dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; #reminder day: @daydropitems = ('00'..'30'); %dropargs = ( name => 'reminderday', order => \@daydropitems, default => $form->{'reminderday'}, ); $some_HTML = dropdownmenu(%dropargs); $template =~ s/<>/$some_HTML/ms; # $some_HTML = dropdownmenu(%dropargs); # %dropargs = ( name => 'remindermonth', # order => \@monthdropitems, # default => $form->{'remindermonth'}, # ); # $some_HTML .= dropdownmenu(%dropargs); # %dropargs = ( name => 'reminderyear', # order => \@yeardropitems, # default => $form->{'reminderyear'}, # ); # $some_HTML .= dropdownmenu(%dropargs); # $template =~ s/<>/$some_HTML/ms; $some_HTML = make_friendlist_dropdown(); $template =~ s/<>/$some_HTML/ms; return $template; } ######################################################################### # replace_eventError_tags - replaces some tags # #-----------------------------------------------------------------------# # replace_eventError_tags($some_text_with_tags) # # # # Usage: # # $HTML = replace_eventError_tags($HTML) # # # ######################################################################### sub replace_eventError_tags { my $template = shift; if ($event_added_error_msg ne '' and $event_added_error_msg ne 'ok') { my $text_to_print = tag('div', {class => 'error', align => 'center'}) . $gettext->gettext('Error in form data:') . tag('br') . $event_added_error_msg . tag('br') .$gettext->gettext('Please correct the fields marked with an asterisk (*).') . tag('br') . endtag('div'); $template =~ s/<>/$text_to_print/ms; } else { my($text_to_print); $text_to_print = tagged('div', {class => 'success', align => 'center', content => $event_success_msg}) .tag('br') if $event_success_msg; $template =~ s/<>/$text_to_print/ms; } foreach('startdateerror','enddateerror','titleerror','recstarterror','recenderror','remindertimeerror') { $template =~ s/<<${_}>>/$errorhash{$_}/ms; } return $template; } ######################################################################### # make_event_reminder_options - makes HTML "select" options # #-----------------------------------------------------------------------# # make_event_reminder_options($reminder_type) # # # # Returns the "select" field options for event reminders in HTML. # # # # Usage: # # $HTML = make_event_reminder_options($form->{'reminderType'}) # # # ######################################################################### sub make_event_reminder_options { my $remtype = shift; my($HTML, %selected); if ($remtype eq 'noreminder') { $selected{'norem'} = 'SELECTED'; } elsif ($remtype eq 'popup') { $selected{'popup'} = 'SELECTED'; } $HTML = qq[