Subversion Repositories ORC

Rev

Rev 8 | Details | Compare with Previous | Last modification | View Log | RSS feed

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