Subversion Repositories ORC

Rev

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