Subversion Repositories ORC

Rev

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