Subversion Repositories VORC

Rev

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 (3) || 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 = "Manage Shift";
24
my $homeURL = "/";
25
my $DBTable = "shift";
26
my %FIELDS = (
27
	id          => [qw(ShiftID        5    auto      static )],
28
	dept        => [qw(Department    10    select      required )],
29
	role        => [qw(Role          15    text      required )],
30
	type        => [qw(Type          20    select      required )],
31
	date        => [qw(Date          25    date        required )],
32
	location    => [qw(Location      30    text      required )],
33
	start_time  => [qw(Start         35    time        required )],
34
	end_time    => [qw(End           40    time        required )],
35
	assignee_id => [qw(Assignee      50    auto         )],
36
	mod_time    => [qw(ModTime       45    number         )],
37
	note        => [qw(Notes         55    textarea       )],
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|number|date|time|auto)/ } keys %FIELDS;
45
my @ROFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(readonly)/ } keys %FIELDS;
46
my $primary = $DBFields[0];
47
 
48
sub fieldOrder {
49
	$FIELDS{$a}->[1] <=> $FIELDS{$b}->[1];
50
}
51
 
52
sub saveForm {
53
  my $FTS = shift;
54
 
55
  my $dbh = WebDB::connect ();
56
  if ($FTS->{$DBFields[0]} eq "NEW") {
57
    $dbh->do (
58
  	  "INSERT INTO $DBTable
59
      (dept,role,type,date,location,start_time,end_time,note)
60
      VALUES(?,?,?,?,?,?,?,?)",
61
  	  undef,
62
  	  $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}
63
  	);
64
  	($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 = ?", undef, $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note});
65
    logit ($RCid, "$username created new shift ($FTS->{id}, $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
66
  } else {
67
    if ($FTS->{mod_time}) {
68
      $dbh->do (
69
        "UPDATE $DBTable
70
        SET dept=?, role=?, type=?, date=?, location=?, start_time=?, end_time=?, mod_time=?, note=?
71
        WHERE id = ?",
72
        undef,
73
        $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{mod_time}, $FTS->{note}, $FTS->{id}
74
      );
75
    } else {
76
      $dbh->do (
77
        "UPDATE $DBTable
78
        SET dept=?, role=?, type=?, date=?, location=?, start_time=?, end_time=?, mod_time=null, note=?
79
        WHERE id = ?",
80
        undef,
81
        $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}, $FTS->{id}
82
      );
83
    }
84
    logit ($RCid, "$username updated shift ($FTS->{id}, $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
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 = ?", undef, $X->{$primary});
95
  $dbh->disconnect ();
96
  logit ($RCid, "$username deleted shift ($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
print header (),
130
			start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );
131
 
132
print $h->div ({ class => "accent pageheader" }, [
133
  $h->h1 ($pageTitle),
134
  $h->div ({ class=>"sp0" }, [
135
    $h->div ({ class=>"spLeft" }, [
136
    ]),
137
    $h->div ({ class=>"spRight" }, [
138
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
139
    ]),
140
  ]),
141
]);
142
 
143
my $choice = param ("choice") // "";
144
if ($choice eq "Save") {
145
	process_form ();
146
} elsif (defined (param ($primary))) {
147
  my $thing = param ($primary);
148
  if ($choice eq "Delete") {
149
    delete_item ({ $primary => $thing });
150
  } else {
151
	  display_form ({ $primary => $thing }, $choice);
152
	}
153
} else {
154
	display_form (); # blank form
155
}
156
 
157
print $h->close ("html");
158
 
159
sub display_form  {
160
  my $R = shift;
161
  my $view = shift // "";
162
	my $actionbutton;
163
 
164
  if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {
165
      print &formField ("Cancel", "Back", "POSTSAVE");
166
      return;
167
  }
168
 
169
  if ($R) {
170
    # we're dealing with an existing thing.  Get the current values out of the DB...
171
    my $dbh = WebDB::connect ();
172
 
173
	  @F{@DBFields} = $dbh->selectrow_array (
174
                     "SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ?",
175
                      undef, $R->{$primary});
176
	  $dbh->disconnect ();
177
 
178
	  # did we find a record?
179
	  error ("Cannot find a database entry for '$R->{$primary}'") unless defined $F{$DBFields[0]};
180
 
181
    if ($user->{dept}->{$F{dept}} < 3 and $LVL < 5) {
182
  	  error ("You're not a $DepartmentNames->{$F{dept}} Manager!");
183
    }
184
 
185
    if ($view eq "Update") {
186
      # We'd like to update that thing, give the user a form...
187
      print $h->p ("Updating Shift: $R->{$primary}...");
188
 
189
      foreach (@DBFields) {
190
        $F{$_} = formField ($_, $F{$_});
191
      }
192
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
193
 
194
      $actionbutton = formField ("choice", "Save");
195
      $actionbutton .= formField ("Cancel");
196
    } elsif ($view eq "Copy") {
197
      # We'd like to copy that thing, give the user a form...
198
      print $h->p ("Copying Shift: $R->{$primary}...");
199
 
200
      foreach (@DBFields) {
201
        $F{$_} = formField ($_, $F{$_});
202
      }
203
      $F{$DBFields[0]} = "COPY".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
204
 
205
      $actionbutton = formField ("choice", "Save");
206
      $actionbutton .= formField ("Cancel");
207
    } else {
208
      # We're just looking at it...
209
      print $h->p ("Viewing Shift: $R->{$primary}...");
210
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
211
      if ($F{assignee_id}) {
212
        my $temp;
213
        $temp = getUserDerbyName ($F{assignee_id});
214
        $temp .= "&nbsp;".$h->a ({ onClick=>"if (confirm('Really? You want to drop this person from the shift?')==true) { window.open('make_shift_change.pl?change=del&RCid=$F{assignee_id}&id=$F{id}','Confirm Shift Change','resizable,height=260,width=370'); return false; }" }, "[DROP]");
215
        $F{assignee_id} = $temp;
216
      } else {
217
        $F{assignee_id} = $h->a ({ onClick=>"window.open('make_shift_change.pl?change=lookup&id=$F{id}','Confirm Shift Change','resizable,height=260,width=370'); return false;" }, "[ADD USER]");
218
      }
219
 
220
      $actionbutton = formField ("choice", "Update");
221
      if ($view eq "POSTSAVE") {
222
        $actionbutton .= formField ("Cancel", "Back", "POSTSAVE");
223
      } else {
224
        $actionbutton .= formField ("Cancel", "Back");
225
      }
226
    }
227
  } else {
228
    print $h->p ("Adding a new Shift...");
229
 
230
    foreach (@DBFields) {
231
      $F{$_} = formField ($_);
232
    }
233
		$F{$DBFields[0]} = "NEW".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
234
 
235
    $actionbutton = formField ("choice", "Save");
236
    $actionbutton .= formField ("Cancel");
237
  }
238
 
239
 
240
	print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });
241
	print $h->div ({ class=>"sp0" },
242
	  $h->div ({ class=>"rTable" }, [ map ({
243
      $h->div ({ class=>"rTableRow" }, [
244
        $h->div ({ class=>"rTableCell right top" }, "$fieldDisplayName{$_}: "),
245
        $h->div ({ class=>"rTableCell" }, $F{$_})
246
      ])
247
#      } @DBFields),
248
       } sort fieldOrder keys %FIELDS),
249
   ])
250
  );
251
 
252
  print $actionbutton;
253
  print $h->close ("form");
254
 
255
}
256
 
257
sub process_form  {
258
  my %FORM;
259
  foreach (keys %FIELDS) {
260
  	if ($fieldType{$_} =~ /^text/) {
261
  		$FORM{$_} = WebDB::trim param ($_) // "";
262
  	} else {
263
	  	$FORM{$_} = param ($_) // "";
264
  	}
265
  }
266
 
267
  	 # check for required fields
268
	my @errors = ();
269
	foreach (@requiredFields) {
270
		push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "";
271
	}
272
 
273
  if (@errors)	 {
274
    print $h->div ({ class=>"error" }, [
275
  	  $h->p ("The following errors occurred:"),
276
  	  $h->ul ($h->li (@errors)),
277
  	  $h->p ("Please click your Browser's Back button to\n"
278
  	  	   . "return to the previous page and correct the problem.")
279
  	]);
280
  	return;
281
  }	 # Form was okay.
282
 
283
  $FORM{id} = saveForm (\%FORM);
284
 
285
	print $h->p ({ class=>"success" }, "Shift successfully saved.");
286
 
287
  display_form ({ $primary=>$FORM{id} }, "POSTSAVE");
288
}
289
 
290
sub error {
291
	my $msg = shift;
292
	print $h->p ({ class=>"error" }, "Error: $msg");
293
  print $h->close("html");
294
	exit (0);
295
}
296
 
297
sub formField {
298
	my $name  = shift;
299
	my $value = shift // '';
300
	my $context = shift // '';
301
	my $type = $fieldType{$name} // "button";
302
 
303
  if ($type eq "button") {
304
		if ($name eq "Cancel") {
305
		  if ($context eq "POSTSAVE") {
306
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"manage_shifts.pl\"; return false;" });
307
		  } else {
308
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" })
309
		  }
310
		} else {
311
			return $h->input ({ type=>"submit", value => $value, name=>$name })
312
		}
313
 
314
	} elsif ($type eq "textarea") {
315
	  return $h->tag ("textarea", {
316
	    name => $name,
317
	    override => 1,
318
			cols => 30,
319
			rows => 4
320
	  }, $value);
321
 
322
  } elsif ($type eq "select") {
323
    no strict;
324
    return &{"select_".$name} ($value);
325
	}	elsif ($type eq "auto") {
326
	  return $name eq "assignee_id" ? getUserDerbyName ($value) : $value;
327
  }	elsif ($type eq "time") {
328
	  return $h->input ({
329
	    name => $name,
330
	    type => $type,
331
	    value => $value,
332
	    step => 900,
333
	    required => [],
334
	    override => 1,
335
	    size => 30
336
	  });
337
  }	elsif ($type eq "number") {
338
    return $h->input ({ name=>$name, type=>"number", value=>$value, step=>.25 }),
339
	}	else {
340
	  use tableViewer;
341
	  if (inArray ($name, \@requiredFields)) {
342
  	  return $h->input ({
343
  	    name => $name,
344
  	    type => $type,
345
  	    value => $value,
346
  	    required => [],
347
  	    override => 1,
348
  	    size => 30
349
  	  });
350
	  } else {
351
  	  return $h->input ({
352
  	    name => $name,
353
  	    type => $type,
354
  	    value => $value,
355
  	    override => 1,
356
  	    size => 30
357
  	  });
358
  	}
359
	}
360
}
361