Subversion Repositories ORC

Rev

Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 - 1
#!/usr/bin/perl
2
 
3
use strict;
4
use WebDB;
5
use HTML::Tiny;
6
use RollerCon;
7
use CGI qw/param header start_html url/;
8
my $h = HTML::Tiny->new( mode => 'html' );
9
 
10
my %F;
11
 
12
my $cookie_string = authenticate (1) || die;
13
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
14
my $user = getUser ($EML);
15
$user->{department} = convertDepartments $user->{department};
16
my $DepartmentNames = getDepartments ();
17
my $username = $user->{derby_name};
18
my $RCid = $user->{RCid};
19
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
20
my $YEAR = "2022";
21
 
22
 
23
my $pageTitle = "Block Personal Time";
24
my $homeURL = "/";
25
my $DBTable = "shift";
26
my %FIELDS = (
27
	id          => [qw(ID        5    auto      static )],
28
#	dept        => [qw(Department    10    select      required )],
29
	role        => [qw(Brief         15    text      required )],
30
#	type        => [qw(Type          20    select      required )],
31
	date        => [qw(Date          25    date        required )],
32
	location    => [qw(Location      30    text       )],
33
	start_time  => [qw(Start         35    time        required )],
34
	end_time    => [qw(End           40    time        required )],
35
	note        => [qw(Notes         45    textarea         )],
36
	assignee_id => [qw(AssigneeID    50    readonly         )],
37
);
38
 
39
 
40
my %fieldDisplayName = map  { $_ => $FIELDS{$_}->[0]   } keys %FIELDS;
41
my %fieldType        = map  { $_ => $FIELDS{$_}->[2]   } keys %FIELDS;
42
my @requiredFields   = sort fieldOrder grep { defined $FIELDS{$_}->[3] } keys %FIELDS;
43
my @DBFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(text|select|date|time|auto)/ } keys %FIELDS;
44
my $primary = $DBFields[0];
45
 
46
sub fieldOrder {
47
	$FIELDS{$a}->[1] <=> $FIELDS{$b}->[1];
48
}
49
 
50
sub saveForm {
51
  my $FTS = shift;
52
 
53
  my $dbh = WebDB::connect ();
54
 
55
  if (findConflict ($RCid, [$FTS->{date}, $FTS->{start_time}, $FTS->{end_time}], "personal")) {
56
 
57
    error ("You already have a shift that conflicts with that time.");
58
 
59
    return;
60
  }
61
  if ($FTS->{$DBFields[0]} eq "NEW") {
62
    $dbh->do (
63
  	  "INSERT INTO $DBTable
64
      (dept,role,type,date,location,start_time,end_time,note,assignee_id)
65
      VALUES(?,?,?,?,?,?,?,?,?)",
66
  	  undef,
67
  	  "PER", $FTS->{role}, "personal", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}, $RCid);
68
  	($FTS->{id}) = $dbh-> selectrow_array ("select max(id) from $DBTable where dept = ? and role = ? and type = ? and date = ? and location = ? and start_time = ? and end_time = ? and note = ? and assignee_id = ?", undef, "PER", $FTS->{role}, "personal", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}, $RCid);
69
    logit ($RCid, "$username created personal time ($FTS->{id}, $FTS->{role}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
70
  } else {
71
    $dbh->do (
72
  	  "REPLACE INTO $DBTable
73
      (id,dept,role,type,date,location,start_time,end_time,note,assignee_id)
74
      VALUES(?,?,?,?,?,?,?,?,?,?)",
75
  	  undef,
76
  	  $FTS->{id}, "PER", $FTS->{role}, "personal", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}, $RCid);
77
    logit ($RCid, "$username updated personal time ($FTS->{id}, $FTS->{role}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
78
	}
79
 
80
	$dbh->disconnect ();	 # stored into database successfully.
81
	return $FTS->{id};
82
}
83
 
84
sub delete_item {
85
  my $X = shift;
86
  my $dbh = WebDB::connect ();
87
  $dbh->do ("delete from $DBTable where $primary = ? and assignee_id = ?", undef, $X->{$primary}, $RCid);
88
  $dbh->disconnect ();
89
  logit ($RCid, "$username deleted personal time ($X->{$primary})");
90
  print "Shift Deleted: $X->{$primary}", $h->br;
91
  print &formField ("Cancel", "Back", "POSTSAVE");
92
}
93
 
94
 
95
#sub select_dept {
96
#	my $selection = shift;
97
#	my @optionList;
98
#
99
#  if ($LVL > 4) {
100
#    @optionList = grep { !/^PER$/ } sort keys %{ $DepartmentNames };
101
#  } else {
102
#    @optionList = grep { $user->{department}->{$_} > 2 } keys %{ $user->{department} };
103
#  }
104
#
105
#  return $h->select ({ name=>"dept" },
106
#    [ map { $selection eq $_ ?
107
#              $h->option ({ value=>$_, selected=>[] }, $DepartmentNames->{$_}) :
108
#              $h->option ({ value=>$_ }, $DepartmentNames->{$_})
109
#          } "", @optionList ]);
110
#};
111
 
112
#sub select_type {
113
#  my $value = shift // "";
114
#
115
#  return $h->select ({ name=>"type" },
116
#    [ map { $value eq $_ ?
117
#              $h->option ({ value=>$_, selected=>[] }, $_) :
118
#              $h->option ({ value=>$_ }, $_)
119
#          } "", qw(open lead manager selected)]);
120
#};
121
 
122
 
123
 
124
 
125
 
126
print header (),
127
			start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );
128
 
129
print $h->div ({ class => "accent pageheader" }, [
130
  $h->h1 ($pageTitle),
131
  $h->div ({ class=>"sp0" }, [
132
    $h->div ({ class=>"spLeft" }, [
133
    ]),
134
    $h->div ({ class=>"spRight" }, [
135
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
136
    ]),
137
  ]),
138
]);
139
 
140
print $h->div (["Personal time isn't visible to other RollerCon Volunteers or most of leadership*,", $h->br,
141
                "but it will block your schedule from signing up for conflicting shifts."]);
142
print $h->h5 (["* It is visible in the database..."]);
143
 
144
my $choice = param ("choice") // "";
145
if ($choice eq "Save") {
146
	process_form ();
147
} elsif (defined (param ($primary))) {
148
  my $thing = param ($primary);
149
  if ($choice eq "Delete") {
150
    delete_item ({ $primary => $thing });
151
  } else {
152
	  display_form ({ $primary => $thing }, $choice);
153
	}
154
} else {
155
	display_form (); # blank form
156
}
157
 
158
print $h->close ("html");
159
 
160
sub display_form  {
161
  my $R = shift;
162
  my $view = shift // "";
163
	my $actionbutton;
164
 
165
  if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {
166
      print &formField ("Cancel", "Back", "POSTSAVE");
167
      return;
168
  }
169
 
170
  if ($R) {
171
    # we're dealing with an existing thing.  Get the current values out of the DB...
172
    my $dbh = WebDB::connect ();
173
 
174
	  @F{@DBFields} = $dbh->selectrow_array (
175
                     "SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ? and dept = 'PER' and assignee_id = ?",
176
                      undef, $R->{$primary}, $RCid);
177
	  $dbh->disconnect ();
178
 
179
	  # did we find a record?
180
	  error ("You don't seem to have Personal Time with that database ID ($R->{$primary}).") unless defined $F{$DBFields[0]};
181
 
182
    if ($view eq "Update") {
183
      # We'd like to update that thing, give the user a form...
184
      print $h->p ("Updating Personal Time: $R->{$primary}...");
185
 
186
      foreach (@DBFields) {
187
        $F{$_} = formField ($_, $F{$_});
188
      }
189
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
190
 
191
      $actionbutton = formField ("choice", "Save");
192
      $actionbutton .= formField ("Cancel");
193
    } elsif ($view eq "Copy") {
194
      # We'd like to copy that thing, give the user a form...
195
      print $h->p ("Copying Personal Time: $R->{$primary}...");
196
 
197
      foreach (@DBFields) {
198
        $F{$_} = formField ($_, $F{$_});
199
      }
200
      $F{$DBFields[0]} = "COPY".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
201
 
202
      $actionbutton = formField ("choice", "Save");
203
      $actionbutton .= formField ("Cancel");
204
    } else {
205
      # We're just looking at it...
206
      print $h->p ("Viewing Personal Time: $R->{$primary}...");
207
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
208
      $actionbutton = formField ("choice", "Update");
209
      if ($view eq "POSTSAVE") {
210
        $actionbutton .= formField ("Cancel", "Back", "POSTSAVE");
211
      } else {
212
        $actionbutton .= formField ("Cancel", "Back");
213
      }
214
    }
215
  } else {
216
    print $h->p ("Adding new Personal Time...");
217
 
218
    foreach (@DBFields) {
219
      $F{$_} = formField ($_);
220
    }
221
		$F{$DBFields[0]} = "NEW".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
222
 
223
    $actionbutton = formField ("choice", "Save");
224
    $actionbutton .= formField ("Cancel");
225
  }
226
 
227
 
228
	print $h->open ("form", { action => url (), method=>"POST" });
229
	print $h->div ({ class=>"sp0" },
230
	  $h->div ({ class=>"rTable" }, [ map ({
231
      $h->div ({ class=>"rTableRow" }, [
232
        $h->div ({ class=>"rTableCell right top" }, "$fieldDisplayName{$_}: "),
233
        $h->div ({ class=>"rTableCell" }, $F{$_})
234
      ])
235
      } @DBFields),
236
    ])
237
  );
238
 
239
  print $actionbutton;
240
  print $h->close ("form");
241
 
242
}
243
 
244
sub process_form  {
245
  my %FORM;
246
  foreach (keys %FIELDS) {
247
  	if ($fieldType{$_} =~ /^text/) {
248
  		$FORM{$_} = WebDB::trim param ($_) // "";
249
  	} else {
250
	  	$FORM{$_} = param ($_) // "";
251
  	}
252
  }
253
 
254
  	 # check for required fields
255
	my @errors = ();
256
	foreach (@requiredFields) {
257
		push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "";
258
	}
259
 
260
  if (@errors)	 {
261
    print $h->div ({ class=>"error" }, [
262
  	  $h->p ("The following errors occurred:"),
263
  	  $h->ul ($h->li (@errors)),
264
  	  $h->p ("Please click your Browser's Back button to\n"
265
  	  	   . "return to the previous page and correct the problem.")
266
  	]);
267
  	return;
268
  }	 # Form was okay.
269
 
270
  $FORM{id} = saveForm (\%FORM);
271
 
272
	print $h->p ({ class=>"success" }, "Shift successfully saved.");
273
 
274
  display_form ({ $primary=>$FORM{id} }, "POSTSAVE");
275
}
276
 
277
sub error {
278
	my $msg = shift;
279
	print $h->p ({ class=>"error" }, "Error: $msg");
280
  print $h->close("html");
281
	exit (0);
282
}
283
 
284
sub formField {
285
	my $name  = shift;
286
	my $value = shift // '';
287
	my $context = shift // '';
288
	my $type = $fieldType{$name} // "button";
289
 
290
  if ($type eq "button") {
291
		if ($name eq "Cancel") {
292
		  if ($context eq "POSTSAVE") {
293
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"/\"; return false;" });
294
		  } else {
295
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" })
296
		  }
297
		} else {
298
			return $h->input ({ type=>"submit", value => $value, name=>$name })
299
		}
300
 
301
	} elsif ($type eq "textarea") {
302
	  return $h->tag ("textarea", {
303
	    name => $name,
304
	    override => 1,
305
			cols => 30,
306
			rows => 4
307
	  }, $value);
308
 
309
  } elsif ($type eq "select") {
310
    no strict;
311
    return &{"select_".$name} ($value);
312
	}	elsif ($type eq "auto") {
313
	  return $value;
314
  }	else {
315
	  return $h->input ({
316
	    name => $name,
317
	    type => $type,
318
	    value => $value,
319
	    required => [],
320
	    override => 1,
321
	    size => 30
322
	  });
323
	}
324
}
325