| 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++;
|
|
|
60 |
foreach $f (@displayFields)
|
|
|
61 |
{
|
|
|
62 |
$t->{$f} =~ s/<br>/\n/ig; $worksheet->write($row, $col++, "$t->{$f}");
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$workbook->close();
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
sub fetchColEntries {
|
|
|
72 |
my $colName = shift;
|
|
|
73 |
my $selection = shift;
|
|
|
74 |
my $table = $DBTABLE;
|
|
|
75 |
my $optionList = "";
|
|
|
76 |
|
|
|
77 |
if ($colName eq "derby_name" and $LVL < 2) {
|
|
|
78 |
# special case to anonymize assignees...
|
|
|
79 |
# my @opts = ("-blank-", $ORCUSER->{derby_name});
|
|
|
80 |
$optionList = join "", map { $selection eq $_ ? "<OPTION selected>$_</OPTION>" : "<OPTION>$_</OPTION>" } ("-blank-", $ORCUSER->{derby_name});
|
|
|
81 |
} else {
|
|
|
82 |
my $dept_where = $table eq "v_shift" ? "where dept != 'PER'" : "";
|
|
|
83 |
my $cathan = $internalDBH->prepare("select distinct $colName from $table $dept_where order by $colName");
|
|
|
84 |
|
|
|
85 |
$cathan->execute();
|
|
|
86 |
while (my ($cat) = $cathan->fetchrow) {
|
|
|
87 |
if ($cat eq "") { $cat = "-blank-"; }
|
|
|
88 |
if ($cat eq $selection) {
|
|
|
89 |
$optionList .= "<OPTION selected>$cat</OPTION>";
|
|
|
90 |
} else {
|
|
|
91 |
$optionList .= "<OPTION>$cat</OPTION>";
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
return $optionList;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
sub fetchDerbyNameWithRCid {
|
|
|
100 |
my $DEPT = shift // "";
|
|
|
101 |
my $optionList = "";
|
|
|
102 |
|
|
|
103 |
my $cathan;
|
|
|
104 |
if (!$DEPT) {
|
|
|
105 |
$cathan = $internalDBH->prepare("select RCid, derby_name from official where level > 0 order by derby_name");
|
|
|
106 |
$cathan->execute();
|
|
|
107 |
} else {
|
| 16 |
- |
108 |
$cathan = $internalDBH->prepare("select RCid, derby_name from official where level > 0 and (department like ? and department not like ?) order by derby_name");
|
|
|
109 |
$cathan->execute("%".$DEPT."%", "%".$DEPT."-0%");
|
| 7 |
- |
110 |
}
|
|
|
111 |
|
|
|
112 |
while (my ($id, $cat) = $cathan->fetchrow) {
|
|
|
113 |
$optionList .= "<OPTION value=$id>$cat</OPTION>";
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return $optionList;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
sub filter {
|
|
|
121 |
my $colName = shift;
|
|
|
122 |
my $filter = shift;
|
|
|
123 |
|
|
|
124 |
if (exists &{"filter_".$colName}) { return &{"filter_".$colName} ($colName, $filter); }
|
|
|
125 |
|
|
|
126 |
if ($colFilterTypeHash{$colName} eq 'select')
|
|
|
127 |
{
|
|
|
128 |
if (defined $filter) {
|
|
|
129 |
if ($filter eq "-blank-") {
|
|
|
130 |
return "($colName = '' or isNull($colName) = 1)";
|
|
|
131 |
}
|
|
|
132 |
# $filter = s/'/\'/g;
|
|
|
133 |
return "$colName = \"$filter\"";
|
|
|
134 |
}
|
|
|
135 |
else
|
|
|
136 |
{
|
|
|
137 |
my $thing = "filter-${colName}";
|
|
|
138 |
my $categories = &fetchColEntries($colName, $FORM{$thing});
|
|
|
139 |
my $Options = "<OPTION></OPTION>".$categories;
|
|
|
140 |
|
|
|
141 |
$Options =~ s/>($FORM{$thing})/ selected>$1/;
|
|
|
142 |
return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
|
|
|
143 |
}
|
|
|
144 |
} elsif ($colFilterTypeHash{$colName} eq 'text') {
|
|
|
145 |
if (defined $filter)
|
|
|
146 |
{
|
|
|
147 |
if ($filter =~ s/^=\s*//) { return "$colName = \"$filter\""; }
|
|
|
148 |
|
|
|
149 |
if ($filter !~ s/^\^\s*//) { $filter = "\%$filter"; }
|
|
|
150 |
if ($filter !~ s/\s*\$$//) { $filter = "$filter\%"; }
|
|
|
151 |
$filter =~ s/\*/%/g;
|
|
|
152 |
|
|
|
153 |
return "$colName like \"$filter\"";
|
|
|
154 |
}
|
|
|
155 |
else
|
|
|
156 |
{
|
|
|
157 |
my $thing = "filter-${colName}";
|
|
|
158 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
|
|
|
159 |
}
|
|
|
160 |
} elsif ($colFilterTypeHash{$colName} eq 'number') {
|
|
|
161 |
if (defined $filter)
|
|
|
162 |
{
|
|
|
163 |
if ($filter =~ /^[>=<]\s*/) { return "$colName $filter"; }
|
|
|
164 |
else { return "$colName = $filter"; }
|
|
|
165 |
}
|
|
|
166 |
else
|
|
|
167 |
{
|
|
|
168 |
my $thing = "filter-${colName}";
|
|
|
169 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=2 $onChange>";
|
|
|
170 |
}
|
|
|
171 |
} elsif ($colFilterTypeHash{$colName} eq 'date') {
|
|
|
172 |
if (defined $filter)
|
|
|
173 |
{
|
|
|
174 |
if ($filter =~ s/^<\s*//) { return "$colName < '$filter'"; }
|
|
|
175 |
if ($filter =~ s/^>\s*//) { return "$colName > '$filter'"; }
|
|
|
176 |
|
|
|
177 |
return "$colName = '$filter'";
|
|
|
178 |
}
|
|
|
179 |
else
|
|
|
180 |
{
|
|
|
181 |
my $thing = "filter-${colName}";
|
|
|
182 |
return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
|
|
|
183 |
}
|
|
|
184 |
} elsif ($colFilterTypeHash{$colName} eq 'ERROR') {
|
|
|
185 |
return "<center><strong><font color=red>ERROR!</font>";
|
|
|
186 |
} elsif ($colFilterTypeHash{$colName} eq 'none') {
|
|
|
187 |
return;
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
sub getData {
|
|
|
193 |
my $fields = shift;
|
|
|
194 |
my $whereClause = shift;
|
|
|
195 |
my $table = shift;
|
|
|
196 |
my $orderby = shift;
|
|
|
197 |
my $curpage = shift // 0;
|
|
|
198 |
my $pagelimit = shift // "All";
|
|
|
199 |
|
|
|
200 |
my $selected = '*';
|
|
|
201 |
$whereClause = scalar @{$whereClause} > 0 ? "where ".join " and ", @{$whereClause} : '';
|
|
|
202 |
|
|
|
203 |
if ($orderby eq "dayofweek") {
|
|
|
204 |
$orderby = "order by date, time";
|
|
|
205 |
} elsif ($orderby eq "eventid") { # only applicable to the log viewer...
|
|
|
206 |
$orderby = "order by eventid desc";
|
|
|
207 |
} else {
|
|
|
208 |
$orderby = $orderby eq "" ? "" : "order by $orderby";
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
my $getMe;
|
|
|
212 |
if ($pagelimit eq "All") {
|
|
|
213 |
$getMe = "select distinct * from $table $whereClause $orderby";
|
|
|
214 |
} else {
|
|
|
215 |
$curpage = ($curpage - 1) * $pagelimit;
|
|
|
216 |
$getMe = "select distinct * from $table $whereClause $orderby limit $curpage, $pagelimit";
|
|
|
217 |
}
|
|
|
218 |
my ($totalcount) = $internalDBH->selectrow_array ("select distinct count(*) from $table $whereClause");
|
|
|
219 |
|
|
|
220 |
my $limhan = $internalDBH->prepare($getMe); # Get the tickets from the DB
|
|
|
221 |
$limhan->execute();
|
|
|
222 |
|
|
|
223 |
my @results = ();
|
|
|
224 |
while (my $P = $limhan->fetchrow_hashref)
|
|
|
225 |
{
|
|
|
226 |
push @results, $P;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
return (\@results, $totalcount);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
sub getDBConnection {
|
|
|
234 |
use WebDB;
|
|
|
235 |
$dbh = WebDB::connect ();
|
|
|
236 |
return $dbh;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
sub inArray {
|
|
|
241 |
my $item = shift;
|
|
|
242 |
my $array = shift;
|
|
|
243 |
foreach (@{$array})
|
|
|
244 |
{
|
|
|
245 |
return 1 if $item eq $_;
|
|
|
246 |
}
|
|
|
247 |
return 0;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
sub notInArray {
|
|
|
251 |
return ! inArray (@_);
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
sub uniq (@) {
|
|
|
256 |
# From CPAN List::MoreUtils, version 0.22
|
|
|
257 |
my %h;
|
|
|
258 |
map { $h{$_}++ == 0 ? $_ : () } @_;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
sub whereInArray {
|
|
|
263 |
my $item = shift;
|
|
|
264 |
my $array = shift;
|
|
|
265 |
my $i = 0;
|
|
|
266 |
foreach (@{$array})
|
|
|
267 |
{
|
|
|
268 |
return $i if $item eq $_;
|
|
|
269 |
$i++;
|
|
|
270 |
}
|
|
|
271 |
return -1;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
# Leave this alone, it's needed to compile correctly
|
|
|
277 |
return 1;
|