Subversion Repositories ORC

Rev

Rev 9 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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