| 7 |
- |
1 |
#######################################################################
|
|
|
2 |
# A set of functions to assist the scan data display tool.
|
|
|
3 |
# Kept here to unclutter the main script.
|
|
|
4 |
#######################################################################
|
|
|
5 |
#
|
|
|
6 |
#
|
|
|
7 |
# $Log: scanFunctions.pm,v $
|
|
|
8 |
#
|
|
|
9 |
|
| 9 |
- |
10 |
use cPanelUserConfig;
|
| 7 |
- |
11 |
use DBI ();
|
|
|
12 |
use WebDB;
|
|
|
13 |
|
|
|
14 |
#my $internalDBH = &getDBConnection || die "Unable to connect to Database\n\n";
|
|
|
15 |
my $internalDBH = WebDB->connect () || die "Unable to connect to Database\n\n";
|
|
|
16 |
|
|
|
17 |
sub currentTime {
|
| 25 |
- |
18 |
use DateTime;
|
|
|
19 |
my $now = DateTime->now (time_zone => 'America/Los_Angeles');
|
|
|
20 |
$now =~ s/T/ at /;
|
| 7 |
- |
21 |
|
| 25 |
- |
22 |
return $now." US/Pacific";
|
| 7 |
- |
23 |
}
|
|
|
24 |
|
|
|
25 |
sub byfield { $colOrderHash{$a} <=> $colOrderHash{$b}; }
|
|
|
26 |
|
|
|
27 |
sub exportExcel {
|
|
|
28 |
my $listref = shift;
|
|
|
29 |
my $FN_Prefix = shift // "ORC_Export";
|
|
|
30 |
|
|
|
31 |
use Spreadsheet::WriteExcel;
|
|
|
32 |
|
|
|
33 |
my $date = `date +"%m%d%y%H%M%S"`; chomp $date;
|
|
|
34 |
my $filename = "${FN_Prefix}_${date}_$$.xls";
|
|
|
35 |
|
|
|
36 |
print "Content-type: application/vnd.ms-excel\n";
|
|
|
37 |
# The Content-Disposition will generate a prompt to save the file. If you want
|
|
|
38 |
# to stream the file to the browser, comment out the following line.
|
|
|
39 |
print "Content-Disposition: attachment; filename=$filename\n";
|
|
|
40 |
print "\n";
|
|
|
41 |
|
|
|
42 |
# Create a new workbook and add a worksheet. The special Perl filehandle - will
|
|
|
43 |
# redirect the output to STDOUT
|
|
|
44 |
#
|
|
|
45 |
my $workbook = Spreadsheet::WriteExcel->new(\*STDOUT);
|
|
|
46 |
my $worksheet = $workbook->add_worksheet();
|
|
|
47 |
|
|
|
48 |
my $format = $workbook->add_format();
|
|
|
49 |
$format->set_bold();
|
|
|
50 |
|
|
|
51 |
my $col = $row = 0;
|
|
|
52 |
|
|
|
53 |
foreach $f (@displayFields)
|
|
|
54 |
{ $worksheet->write($row, $col++, "$NAME{$f}", $format); }
|
|
|
55 |
|
|
|
56 |
foreach $t (sort @{ $listref }) # Unt now we print the tickets!
|
|
|
57 |
{
|
|
|
58 |
$col = 0;
|
|
|
59 |
$row++;
|
| 46 |
- |
60 |
foreach $f (@displayFields) {
|
|
|
61 |
if ($f eq "derby_name") {
|
|
|
62 |
if ($user->{department}->{"OFF"} < 2 and $t->{derby_name} and $t->{RCid} != $RCid and $LVL < 5) {
|
|
|
63 |
$t->{derby_name} = "FILLED";
|
|
|
64 |
}
|
| 7 |
- |
65 |
}
|
| 46 |
- |
66 |
$t->{$f} =~ s/<br>/\n/ig; $worksheet->write($row, $col++, "$t->{$f}");
|
|
|
67 |
}
|
| 7 |
- |
68 |
}
|
|
|
69 |
|
|
|
70 |
$workbook->close();
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
sub fetchColEntries {
|
|
|
76 |
my $colName = shift;
|
|
|
77 |
my $selection = shift;
|
|
|
78 |
my $table = $DBTABLE;
|
|
|
79 |
my $optionList = "";
|
|
|
80 |
|
|
|
81 |
if ($colName eq "derby_name" and $LVL < 2) {
|
|
|
82 |
# special case to anonymize assignees...
|
|
|
83 |
# my @opts = ("-blank-", $ORCUSER->{derby_name});
|
|
|
84 |
$optionList = join "", map { $selection eq $_ ? "<OPTION selected>$_</OPTION>" : "<OPTION>$_</OPTION>" } ("-blank-", $ORCUSER->{derby_name});
|
|
|
85 |
} else {
|
|
|
86 |
my $dept_where = $table eq "v_shift" ? "where dept != 'PER'" : "";
|
| 46 |
- |
87 |
my $orderby = $colName eq "dayofweek" ? "field(dayofweek, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')" : $colName;
|
|
|
88 |
my $cathan = $internalDBH->prepare("select distinct $colName from $table $dept_where order by $orderby");
|
| 7 |
- |
89 |
|
|
|
90 |
$cathan->execute();
|
|
|
91 |
while (my ($cat) = $cathan->fetchrow) {
|
|
|
92 |
if ($cat eq "") { $cat = "-blank-"; }
|
|
|
93 |
if ($cat eq $selection) {
|
|
|
94 |
$optionList .= "<OPTION selected>$cat</OPTION>";
|
|
|
95 |
} else {
|
|
|
96 |
$optionList .= "<OPTION>$cat</OPTION>";
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $optionList;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
sub fetchDerbyNameWithRCid {
|
|
|
105 |
my $DEPT = shift // "";
|
|
|
106 |
my $optionList = "";
|
|
|
107 |
|
|
|
108 |
my $cathan;
|
| 29 |
- |
109 |
if (!$DEPT or $DEPT eq "CMP") {
|
| 7 |
- |
110 |
$cathan = $internalDBH->prepare("select RCid, derby_name from official where level > 0 order by derby_name");
|
|
|
111 |
$cathan->execute();
|
|
|
112 |
} else {
|
| 16 |
- |
113 |
$cathan = $internalDBH->prepare("select RCid, derby_name from official where level > 0 and (department like ? and department not like ?) order by derby_name");
|
|
|
114 |
$cathan->execute("%".$DEPT."%", "%".$DEPT."-0%");
|
| 7 |
- |
115 |
}
|
|
|
116 |
|
|
|
117 |
while (my ($id, $cat) = $cathan->fetchrow) {
|
|
|
118 |
$optionList .= "<OPTION value=$id>$cat</OPTION>";
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
return $optionList;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
sub filter {
|
|
|
126 |
my $colName = shift;
|
|
|
127 |
my $filter = shift;
|
|
|
128 |
|
|
|
129 |
if (exists &{"filter_".$colName}) { return &{"filter_".$colName} ($colName, $filter); }
|
|
|
130 |
|
|
|
131 |
if ($colFilterTypeHash{$colName} eq 'select')
|
|
|
132 |
{
|
|
|
133 |
if (defined $filter) {
|
|
|
134 |
if ($filter eq "-blank-") {
|
|
|
135 |
return "($colName = '' or isNull($colName) = 1)";
|
|
|
136 |
}
|
|
|
137 |
# $filter = s/'/\'/g;
|
|
|
138 |
return "$colName = \"$filter\"";
|
|
|
139 |
}
|
|
|
140 |
else
|
|
|
141 |
{
|
|
|
142 |
my $thing = "filter-${colName}";
|
|
|
143 |
my $categories = &fetchColEntries($colName, $FORM{$thing});
|
|
|
144 |
my $Options = "<OPTION></OPTION>".$categories;
|
|
|
145 |
|
|
|
146 |
$Options =~ s/>($FORM{$thing})/ selected>$1/;
|
|
|
147 |
return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
|
|
|
148 |
}
|
|
|
149 |
} elsif ($colFilterTypeHash{$colName} eq 'text') {
|
|
|
150 |
if (defined $filter)
|
|
|
151 |
{
|
|
|
152 |
if ($filter =~ s/^=\s*//) { return "$colName = \"$filter\""; }
|
|
|
153 |
|
|
|
154 |
if ($filter !~ s/^\^\s*//) { $filter = "\%$filter"; }
|
|
|
155 |
if ($filter !~ s/\s*\$$//) { $filter = "$filter\%"; }
|
|
|
156 |
$filter =~ s/\*/%/g;
|
|
|
157 |
|
|
|
158 |
return "$colName like \"$filter\"";
|
|
|
159 |
}
|
|
|
160 |
else
|
|
|
161 |
{
|
|
|
162 |
my $thing = "filter-${colName}";
|
|
|
163 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
|
|
|
164 |
}
|
|
|
165 |
} elsif ($colFilterTypeHash{$colName} eq 'number') {
|
|
|
166 |
if (defined $filter)
|
|
|
167 |
{
|
|
|
168 |
if ($filter =~ /^[>=<]\s*/) { return "$colName $filter"; }
|
|
|
169 |
else { return "$colName = $filter"; }
|
|
|
170 |
}
|
|
|
171 |
else
|
|
|
172 |
{
|
|
|
173 |
my $thing = "filter-${colName}";
|
|
|
174 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=2 $onChange>";
|
|
|
175 |
}
|
|
|
176 |
} elsif ($colFilterTypeHash{$colName} eq 'date') {
|
|
|
177 |
if (defined $filter)
|
|
|
178 |
{
|
|
|
179 |
if ($filter =~ s/^<\s*//) { return "$colName < '$filter'"; }
|
|
|
180 |
if ($filter =~ s/^>\s*//) { return "$colName > '$filter'"; }
|
|
|
181 |
|
|
|
182 |
return "$colName = '$filter'";
|
|
|
183 |
}
|
|
|
184 |
else
|
|
|
185 |
{
|
|
|
186 |
my $thing = "filter-${colName}";
|
|
|
187 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
|
|
|
188 |
}
|
| 29 |
- |
189 |
} elsif ($colFilterTypeHash{$colName} eq 'boolean') {
|
|
|
190 |
if (defined $filter)
|
|
|
191 |
{
|
|
|
192 |
return "$colName = $filter";
|
|
|
193 |
}
|
|
|
194 |
else
|
|
|
195 |
{
|
|
|
196 |
my $thing = "filter-${colName}";
|
|
|
197 |
my $Options = "<OPTION></OPTION><OPTION>True</OPTION><OPTION>False</OPTION>";
|
|
|
198 |
|
|
|
199 |
$Options =~ s/>($FORM{$thing})/ selected>$1/;
|
|
|
200 |
return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
|
|
|
201 |
}
|
| 7 |
- |
202 |
} elsif ($colFilterTypeHash{$colName} eq 'ERROR') {
|
|
|
203 |
return "<center><strong><font color=red>ERROR!</font>";
|
|
|
204 |
} elsif ($colFilterTypeHash{$colName} eq 'none') {
|
|
|
205 |
return;
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
sub getData {
|
|
|
211 |
my $fields = shift;
|
|
|
212 |
my $whereClause = shift;
|
|
|
213 |
my $table = shift;
|
|
|
214 |
my $orderby = shift;
|
| 33 |
- |
215 |
my $curpage = shift; $curpage = 1 unless $curpage;
|
| 7 |
- |
216 |
my $pagelimit = shift // "All";
|
|
|
217 |
|
|
|
218 |
my $selected = '*';
|
|
|
219 |
$whereClause = scalar @{$whereClause} > 0 ? "where ".join " and ", @{$whereClause} : '';
|
|
|
220 |
|
|
|
221 |
if ($orderby eq "dayofweek") {
|
|
|
222 |
$orderby = "order by date, time";
|
|
|
223 |
} elsif ($orderby eq "eventid") { # only applicable to the log viewer...
|
|
|
224 |
$orderby = "order by eventid desc";
|
|
|
225 |
} else {
|
|
|
226 |
$orderby = $orderby eq "" ? "" : "order by $orderby";
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
my $getMe;
|
|
|
230 |
if ($pagelimit eq "All") {
|
|
|
231 |
$getMe = "select distinct * from $table $whereClause $orderby";
|
|
|
232 |
} else {
|
|
|
233 |
$curpage = ($curpage - 1) * $pagelimit;
|
|
|
234 |
$getMe = "select distinct * from $table $whereClause $orderby limit $curpage, $pagelimit";
|
|
|
235 |
}
|
|
|
236 |
my ($totalcount) = $internalDBH->selectrow_array ("select distinct count(*) from $table $whereClause");
|
|
|
237 |
|
|
|
238 |
my $limhan = $internalDBH->prepare($getMe); # Get the tickets from the DB
|
|
|
239 |
$limhan->execute();
|
|
|
240 |
|
|
|
241 |
my @results = ();
|
|
|
242 |
while (my $P = $limhan->fetchrow_hashref)
|
|
|
243 |
{
|
|
|
244 |
push @results, $P;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
return (\@results, $totalcount);
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
sub getDBConnection {
|
|
|
252 |
use WebDB;
|
|
|
253 |
$dbh = WebDB::connect ();
|
|
|
254 |
return $dbh;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
sub inArray {
|
|
|
259 |
my $item = shift;
|
|
|
260 |
my $array = shift;
|
|
|
261 |
foreach (@{$array})
|
|
|
262 |
{
|
|
|
263 |
return 1 if $item eq $_;
|
|
|
264 |
}
|
|
|
265 |
return 0;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
sub notInArray {
|
|
|
269 |
return ! inArray (@_);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
sub uniq (@) {
|
|
|
274 |
# From CPAN List::MoreUtils, version 0.22
|
|
|
275 |
my %h;
|
|
|
276 |
map { $h{$_}++ == 0 ? $_ : () } @_;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
sub whereInArray {
|
|
|
281 |
my $item = shift;
|
|
|
282 |
my $array = shift;
|
|
|
283 |
my $i = 0;
|
|
|
284 |
foreach (@{$array})
|
|
|
285 |
{
|
|
|
286 |
return $i if $item eq $_;
|
|
|
287 |
$i++;
|
|
|
288 |
}
|
|
|
289 |
return -1;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
# Leave this alone, it's needed to compile correctly
|
|
|
295 |
return 1;
|