Subversion Repositories ORC

Rev

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