Subversion Repositories VORC

Rev

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

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