Subversion Repositories ORC

Rev

Rev 25 | Rev 33 | 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 {
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;
29 - 104
	if (!$DEPT or $DEPT eq "CMP") {
7 - 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
		}
29 - 184
	} elsif ($colFilterTypeHash{$colName} eq 'boolean') {
185
		if (defined $filter)
186
		{
187
			return "$colName = $filter";
188
		}
189
		else
190
		{
191
			my $thing = "filter-${colName}";
192
			my $Options = "<OPTION></OPTION><OPTION>True</OPTION><OPTION>False</OPTION>";
193
 
194
			$Options =~ s/>($FORM{$thing})/ selected>$1/;
195
			return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
196
		}
7 - 197
	} elsif ($colFilterTypeHash{$colName} eq 'ERROR') {
198
		return "<center><strong><font color=red>ERROR!</font>";
199
	} elsif ($colFilterTypeHash{$colName} eq 'none') {
200
		return;
201
	}
202
}
203
 
204
 
205
sub getData {
206
	my $fields = shift;
207
	my $whereClause = shift;
208
	my $table = shift;
209
	my $orderby = shift;
210
	my $curpage = shift // 0;
211
	my $pagelimit = shift // "All";
212
 
213
	my $selected = '*';
214
	$whereClause = scalar @{$whereClause} > 0 ? "where ".join " and ", @{$whereClause} : '';
215
 
216
  if ($orderby eq "dayofweek") {
217
   	$orderby = "order by date, time";
218
  } elsif ($orderby eq "eventid") { # only applicable to the log viewer...
219
   	$orderby = "order by eventid desc";
220
  } else {
221
	  $orderby = $orderby eq "" ? "" : "order by $orderby";
222
  }
223
 
224
  my $getMe;
225
  if ($pagelimit eq "All") {
226
  	$getMe = "select distinct * from $table $whereClause $orderby";
227
  } else {
228
    $curpage = ($curpage - 1) * $pagelimit;
229
  	$getMe = "select distinct * from $table $whereClause $orderby limit $curpage, $pagelimit";
230
  }
231
	my ($totalcount) = $internalDBH->selectrow_array ("select distinct count(*) from $table $whereClause");
232
 
233
	my $limhan = $internalDBH->prepare($getMe);			# Get the tickets from the DB
234
	$limhan->execute();
235
 
236
	my @results = ();
237
	while (my $P = $limhan->fetchrow_hashref)
238
	{
239
		push @results, $P;
240
	}
241
 
242
	return (\@results, $totalcount);
243
}
244
 
245
 
246
sub getDBConnection {
247
  use WebDB;
248
  $dbh = WebDB::connect ();
249
  return $dbh;
250
}
251
 
252
 
253
sub inArray {
254
	my $item = shift;
255
	my $array = shift;
256
	foreach (@{$array})
257
	{
258
		return 1 if $item eq $_;
259
	}
260
	return 0;
261
}
262
 
263
sub notInArray {
264
	return ! inArray (@_);
265
}
266
 
267
 
268
sub uniq (@) {
269
    # From CPAN List::MoreUtils, version 0.22
270
    my %h;
271
    map { $h{$_}++ == 0 ? $_ : () } @_;
272
}
273
 
274
 
275
sub whereInArray {
276
	my $item = shift;
277
	my $array = shift;
278
	my $i = 0;
279
	foreach (@{$array})
280
	{
281
		return $i if $item eq $_;
282
		$i++;
283
	}
284
	return -1;
285
}
286
 
287
 
288
 
289
# Leave this alone, it's needed to compile correctly
290
return 1;