#!/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: todo_add.html ########################################### # # # MimerDesk: Task editor # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # (c) Ionstream OY 1999 - 2001 # # # # Programmed by: Teemu Vainio # # Modified by: Hannes Muurinen # # # ########################################### use strict; use vars qw ($APPLICATION $ACTIVEGRP $ARTICLE $MONTH $USER $IP $LAST_ACT $FORWARDED $trans $TIME_USED $IDLE $form $ref $sth $action $submit_buttons $summary $category $description $prioritybutton $duedatebutton $categorybutton %priority_text); use lib::MimerDesk; use CGI::Carp "fatalsToBrowser"; $APPLICATION = 'Personal - Tasks'; sub add_todo; sub print_template; sub check_action; sub modify_todo; sub remove_todo; sub datebutton; # 100% Xylitol ################ # Main Program # ################ # # 1. Usual stuff # 2. Adds, modifies or removes todo if form is filled # 3. Prepares template # 4. Prints template # 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'}); $trans = lib::MimerDesk->new_gettext(program => 'todo_add',language => $config{'language'}); $APPLICATION = $trans->gettext('Personal - Tasks'); if ($form->{'todo'}) { lock_tables('READ', 'todo'); db_list("SELECT USER FROM todo WHERE ID = '".prepare_fordb($form->{'todo'})."'"); my $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if ($ref->{'USER'} ne $USER) { write_log("$USER tried to access someone elses todo entry!",'warning'); redirect("todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}"); exit; } } %priority_text = ( 1 => $trans->gettext('Highest'), 2 => $trans->gettext('High'), 3 => $trans->gettext('Medium'), 4 => $trans->gettext('Low'), 5 => $trans->gettext('Lowest'), ); my $errormessages; $errormessages .= send_task() if $form->{'sendtask'}; add_todo('Add new') if $form->{'SAVEADD'}; add_todo() if ($form->{'ADD'} eq 'Add'); remove_todo() if ($form->{'DELETE'} eq 'Remove'); modify_todo() if ($form->{'UPDATE'} eq 'Modify'); check_action(); print_template("$config{'theme'}_todo_add",$errormessages); ########################## # ___ # # ___ | | | \ ___ # # / \ | | | / / \ # # \___ | | |--< \___ # # \ | | | \ \ # # \___/ \__/ |___/ \___/ # # # ########################## ################ # check_action # ################ # # Modifies templatevalues # # 1. Checks if user is going to add or edit # 2. Changes $submit_buttons, $action, $prioritybutton and $duedatebutton # 3. If action isn't add or edit redirects to todo.html sub check_action { if ($form->{'action'} eq 'add') { $submit_buttons = hiddenfield('ADD',''). hiddenfield('SAVEADD',''). tagged('a',{href => 'javascript:ADD()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/save.gif", alt => $trans->gettext('Save task'))}). '  '. tagged('a',{href => 'javascript:SAVEADD()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/saveaddnew.gif", alt => $trans->gettext('Save changes & add new'))}). '  '. tagged('a',{href => 'javascript:if(confirm(\''. $trans->gettext('Are you sure you want to reset?'). '\'))document.new_todo.reset();void(0);', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/reset.gif")}). '  '. tagged('a',{href => "todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}", onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/back2.gif", alt => 'Back')}); $action = $trans->gettext('Add task'); my ($duedate, $priority) = ('default', '1'); $summary = $form->{'summary'} if $summary eq ''; $description = $form->{'description'} if $description eq ''; $priority = $form->{'priority'} if $form->{'priority'} ne ''; $category = $form->{'category'} if $category eq ''; if ($form->{'due_date'} eq "yes"){$duedate = local_date2utc_epoch(undef,undef,undef,$form->{'day'},$form->{'month'},$form->{'year'});} else {$duedate = 'default';} foreach (1..5) { my $selected = undef; $selected = 'SELECTED' if $_ == $priority; my $content = $priority_text{$_}; # $content .= ' (highest)' if $_ == 1; # $content .= ' (lowest)' if $_ == 5; $prioritybutton .= tagged('option',{value => $_, content => $content},$selected); } $duedatebutton = datebutton($duedate); $categorybutton = tag('select',{name => 'categories'}). tagged('option',{value => '',content => $trans->gettext('Select category')}); lock_tables('READ', 'todo'); db_list("SELECT DISTINCT category FROM todo WHERE USER = '$USER' order by category"); while (my $ref = $sth->fetchrow_hashref()) { next if ($ref->{'category'} eq ''); $categorybutton .= tagged('option',{value => html_escape($ref->{'category'}), content => html_escape($ref->{'category'})}); } $categorybutton .= tag('/select'); db_end(); unlock_tables(); } elsif ($form->{'action'} eq 'edit') { $submit_buttons = hiddenfield('UPDATE',''). hiddenfield('DELETE',''). tagged('a',{href => 'javascript:MODIFY()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/savechanges.gif", alt => $trans->gettext('Save changes'))}). '  '. tagged('a',{href => 'javascript:REMOVE()', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/remove.gif")}). '  '. tagged('a',{href => 'javascript:if(confirm(\''. $trans->gettext('Are you sure you want to reset?'). '\'))document.new_todo.reset();void(0);', onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/reset.gif")}). '  '. tagged('a',{href => "todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}", onMouseOver => 'doClock(); return true', content => image(src => "$config{'loc_pictures'}/back2.gif", alt => 'Back')}); $action = $trans->gettext('Edit task'); my (@row); my ($duedate, $priority); lock_tables('READ','todo'); db_list("SELECT * FROM todo WHERE ID = '".prepare_fordb($form->{'todo'})."'"); while (my $ref = $sth->fetchrow_hashref()) { $summary = $ref->{'SUMMARY'} if $summary eq ''; $description = $ref->{'INFO'} if $description eq ''; $priority = $ref->{'PRIORITY'} if $priority eq ''; $category = $ref->{'CATEGORY'} if $category eq ''; $duedate = $ref->{'DUE_DATE'} if $duedate eq ''; } db_end(); unlock_tables(); foreach (1..5) { my $selected = undef; $selected = 'SELECTED' if $_ == $priority; my $content = $priority_text{$_}; # $content .= ' (highest)' if $_ == 1; # $content .= ' (lowest)' if $_ == 5; $prioritybutton .= tagged('option',{value => $_, content => $content},$selected); } $duedatebutton = datebutton("$duedate"); $categorybutton = tag('select',{name => 'categories'}). tagged('option',{value => '',content => $trans->gettext('Select category')}); lock_tables('READ', 'todo'); db_list("SELECT DISTINCT category FROM todo WHERE USER = '$USER' order by category"); while (my $ref = $sth->fetchrow_hashref()) { if ($ref->{'category'} eq $category) { $categorybutton .= tagged('option',{value => html_escape($ref->{'category'}), content => html_escape($ref->{'category'})},'selected'); $category = undef; } else { next if ($ref->{'category'} eq undef); $categorybutton .= tagged('option',{value => html_escape($ref->{'category'}), content => html_escape($ref->{'category'})}); } } $categorybutton .= tag('/select'); db_end(); unlock_tables(); } else { redirect("todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=".encodeurl($form->{'order'})."&viewmode=".encodeurl($form->{'viewmode'})."&viewcategory=".encodeurl($form->{'viewcategory'})); exit; } } ############ # add_todo # ############ # # Adds new todo into database # # 1. forms due date into correct form for database # or 'NULL' if due date is not given. # 2. removes dangerous stuff from $form values # 3. inserts values into database # 4. redirects user to todolist sub add_todo { my ($duedate); my $addnew = shift; if ($form->{'due_date'} eq "yes"){$duedate = local_date2utc_epoch(undef,undef,undef,$form->{'day'},$form->{'month'},$form->{'year'});} else {$duedate = "NULL";} if (!$form->{'category'}) {$form->{'category'} = $form->{'categories'};} my $summary = $form->{'summary'}; lock_tables('WRITE', 'todo'); db_list("select SUMMARY from todo where USER = '$USER' and SUMMARY = '".prepare_fordb($form->{'summary'})."'"); my ($check) = $sth->fetchrow_array(); db_end(); if ($check) { unlock_tables(); check_action(); print_template("$config{'theme'}_todo_add", sprintf($trans->gettext("Task '%s' already exists!"),$summary). tag('br'),'error'); } if ($form->{'summary'} =~ /^\s*$/) { unlock_tables(); check_action(); print_template("$config{'theme'}_todo_add",$trans->gettext('You must write a summary!').tag('br'),'error'); } db_insert('todo',{ USER => $USER, SUMMARY => prepare_fordb($form->{'summary'}), INFO => prepare_fordb($form->{'description'}), PRIORITY => prepare_fordb($form->{'priority'}), DUE_DATE => prepare_fordb($duedate), CATEGORY => prepare_fordb($form->{'category'}), STATUS => 1 }); db_end(); unlock_tables(); if ($addnew) { #clear the values: $form->{'summary'} = ''; $form->{'description'} = ''; $form->{'priority'} = 1; $form->{'category'} = ''; $form->{'due_date'} = ''; check_action(); print_template("$config{'theme'}_todo_add", sprintf($trans->gettext("Task '%s' successfully added"),$summary). tag('br'),'success'); } redirect("todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=".encodeurl($form->{'order'})."&viewmode=".encodeurl($form->{'viewmode'})."&viewcategory=".encodeurl($form->{'viewcategory'})); exit; } ############### # modify_todo # ############### # # Modifies todo # # 1. forms due date into correct form for database # or 'NULL' if due date is not given. # 2. removes dangerous stuff from $form values # 3. updates values in database # 4. redirects user to todolist sub modify_todo { my ($duedate); if ($form->{'due_date'} eq "yes") {$duedate = local_date2utc_epoch(undef,undef,undef,$form->{'day'},$form->{'month'},$form->{'year'});} else {$duedate = "NULL";} if (!$form->{'category'}) {$form->{'category'} = $form->{'categories'};} my(%db); %db = ( 'SUMMARY' => prepare_fordb($form->{'summary'}), 'INFO' => prepare_fordb($form->{'description'}), 'PRIORITY' => prepare_fordb($form->{'priority'}), 'DUE_DATE' => prepare_fordb($duedate), 'CATEGORY' => prepare_fordb($form->{'category'}), ); lock_tables('WRITE','todo'); db_update("todo",\%db,"ID = '$form->{'todo'}'"); db_end(); unlock_tables(); redirect("todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=".encodeurl($form->{'order'})."&viewmode=".encodeurl($form->{'viewmode'})."&viewcategory=".encodeurl($form->{'viewcategory'})); exit; } ############### # remove_todo # ############### # # Removes todo from database # sub remove_todo { lock_tables('WRITE','todo'); db_delete('todo',"ID = '".prepare_fordb($form->{'todo'})."'"); db_end(); unlock_tables(); redirect("todo.html?ID=$form->{'ID'}&auth=$form->{'auth'}&order=".encodeurl($form->{'order'})."&viewmode=".encodeurl($form->{'viewmode'})."&viewcategory=".encodeurl($form->{'viewcategory'})); exit; } ################## # print_template # ################## # # Prints template # sub print_template { my ($ref, $fulltemplate); my ($template,$message,$colorclass) = @_; print_header('pragma'); $ref = get_template('maintemplate',$template); $ref->{'maintemplate'} =~ s/<>/$ref->{$template}/m; $fulltemplate = $ref->{'maintemplate'}; $fulltemplate = create_buttons($fulltemplate, 'Personal', 'Tasks', $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'}; ($description,$summary) = html_escape($description,$summary); $fulltemplate =~ s/<>/$stuff/ms; $fulltemplate =~ s/<>/MimerDesk\: $APPLICATION/ms; $fulltemplate =~ s/<>/$action/gm; $fulltemplate =~ s/<>/html_escape($form->{'action'})/gme; $fulltemplate =~ s/<>/$submit_buttons/ms; $fulltemplate =~ s/<>/html_escape($form->{'todo'})/mse; $fulltemplate =~ s/<>/$summary/ms; $fulltemplate =~ s/<>/$duedatebutton/ms; $fulltemplate =~ s/<>/$description/ms; $fulltemplate =~ s/<>/$category/ms; $fulltemplate =~ s/<>/$prioritybutton/ms; $fulltemplate =~ s/<>/$colorclass/ms; $fulltemplate =~ s/<>/$message/ms; $fulltemplate =~ s/<>/$duedatebutton/ms; $fulltemplate =~ s/<>/$categorybutton/ms; $fulltemplate =~ s/<>/html_escape($form->{'order'})/gme; $fulltemplate =~ s/<>/html_escape($form->{'viewmode'})/gme; $fulltemplate =~ s/<>/html_escape($form->{'viewcategory'})/gme; my $friendlist = make_friendlist_dropdown(); $fulltemplate =~ s/<>/$friendlist/gm; $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; } ################################################################################# # 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 = ('' => $trans->gettext('[ Select user from My Friends ]')); lock_tables('READ', 'friends', 'users'); db_list("select friends.friend,users.info from users,friends where friends.user = '$USER' and friends.friend = users.nimi order by friends.friend"); while (my $ref = $sth->fetchrow_hashref()) { push(@dropitems, html_escape($ref->{'friend'})); $dropvalues{html_escape($ref->{'friend'})} = html_escape("$ref->{'friend'} ($ref->{'info'})"); } db_end(); unlock_tables(); my %dropargs = (name => 'target_user', order => \@dropitems, 'values' => \%dropvalues, default => html_escape($form->{'target_user'}) ); return dropdownmenu(%dropargs). $trans->gettext(' or send to user: '). textfield(name => 'target_user_text', value => html_escape($form->{'target_user_text'}), size => 20); } ################################################################################# # send_task - sends task to user # #-------------------------------------------------------------------------------# # send_task() # # # # arguments are read from the form # # # # Usage: # # $message = send_task() # # # ################################################################################# sub send_task { my ($returnmessage, @sendlist, $the_target, $duedate); if(($form->{'target_user'} or $form->{'target_user_text'}) and $form->{'summary'}) { $form->{'target_user'} = $form->{'target_user_text'} if $form->{'target_user_text'}; #check if user exist lock_tables('READ', 'users'); my($ref) = db_select('nimi','users',"nimi = '".prepare_fordb($form->{'target_user'})."'"); my $targetname = $ref->{'nimi'}; db_end(); unlock_tables(); return tag('div',{class => 'error'}). sprintf($trans->gettext('Invalid user %s!'),$form->{'target_user'}). tag('br'). endtag('div'). tag('br') if(!$targetname); my $send_time = time; if ($form->{'due_date'} eq "yes"){$duedate = local_date2utc_epoch(undef,undef,undef,$form->{'day'},$form->{'month'},$form->{'year'});} else {$duedate = "NULL";} my ($category, $summary) = ($form->{'category'},$form->{'summary'}); if (!$category) {$category = $form->{'categories'};} my %db = (TARGET => prepare_fordb($form->{'target_user'}), SENDER => $USER, TIME => prepare_fordb($send_time), STATUS => 'no', MESSAGE => prepare_fordb("$duedate/$form->{'priority'}/$category/$summary/$form->{'description'}"), TYPE => 'TODO'); lock_tables('WRITE', 'messages'); db_insert('messages',\%db); db_end(); unlock_tables(); $returnmessage = tag('div',{class => 'success'}). sprintf($trans->gettext('The task request was sent to user %s'),html_escape($form->{'target_user'})). endtag('div'). tag('br'). $returnmessage; } else { $returnmessage = ''; $returnmessage .= tag('div',{class => 'error'}). $trans->gettext("You must select user!"). endtag('div'). tag('br') if (!$form->{'target_user'} and !$form->{'target_user_text'}); $returnmessage .= tag('div',{class => 'error'}). $trans->gettext("You must write a summary!"). endtag('div'). tag('br') if !$form->{'summary'}; } return $returnmessage; } ############## # datebutton # ############## # # Forms the god dääm funky datebutton # # 1. checks if due date is given and makes datebutton show that value by default. # Othervise today is the default date and due date radiobutton is not checked. # # sub datebutton { my ($date) = @_; my ($button, $selected, $checked, @order, %dropvalues, $day,$month,$year); my @months = ( '', $trans->gettext('January'), $trans->gettext('February'), $trans->gettext('March'), $trans->gettext('April'), $trans->gettext('May'), $trans->gettext('June'), $trans->gettext('July'), $trans->gettext('August'), $trans->gettext('September'), $trans->gettext('October'), $trans->gettext('November'), $trans->gettext('December') ); my (@date) = utc_epoch2date(time()); if ($date eq 'default' || !$date) { (undef,undef,undef,$day,$month,$year) = utc_epoch2date(time()); $checked = 'no'; } else { (undef,undef,undef,$day,$month,$year) = utc_epoch2date($date); $checked = 'yes'; } my $radiobuttons = radiobuttons(name => 'due_date', values => ['yes','no'], default => $checked); $button = $radiobuttons->{'yes'}; $day =~ s/^0(\d)$/$1/; $button .= dropdownmenu(name => 'day',order => [1..31],default => $day); foreach (1..12) { $_ = '0'.$_ if $_ < 10; push @order, $_; $dropvalues{$_} = $months[$_]; } $button .= dropdownmenu(name => 'month',order => \@order,values => \%dropvalues,default => $month); $button .= dropdownmenu(name => 'year',order => [$date[5]-2..$date[5]+10],default => $year); $button .= $radiobuttons->{'no'}.$trans->gettext(' No due date'); return $button; }