Subversion Repositories VORC

Rev

Rev 129 | Rev 196 | 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";
153 - 21
my $internalDBH = WebDB::connect () || die "Unable to connect to Database\n\n";
7 - 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...
122 - 89
    my @opts = ("-blank-", $ORCUSER->{derby_name});
90
    push @opts, map { @{$_} } @{ $internalDBH->selectall_arrayref ("select derby_name from official where showme = 1 and derby_name <> ? order by derby_name", undef, $ORCUSER->{derby_name}) };
91
    $optionList = join "", map { $selection eq $_ ? "<OPTION selected>$_</OPTION>" : "<OPTION>$_</OPTION>" } @opts;
7 - 92
  } else {
129 - 93
    my $dept_where = $table eq "v_shift" ? "where dept != 'PER' and year(date) = year(now())" : "";
46 - 94
    my $orderby = $colName eq "dayofweek" ? "field(dayofweek, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')" : $colName;
95
	  my $cathan = $internalDBH->prepare("select distinct $colName from $table $dept_where order by $orderby");
7 - 96
 
97
  	$cathan->execute();
98
  	while (my ($cat) = $cathan->fetchrow) {
99
  	  if ($cat eq "") { $cat = "-blank-"; }
100
  	  if ($cat eq $selection) {
101
  	    $optionList .= "<OPTION selected>$cat</OPTION>";
102
  	  } else {
103
  		  $optionList .= "<OPTION>$cat</OPTION>";
104
  		}
105
  	}
106
  }
107
 
108
	return $optionList;
109
}
110
 
111
sub fetchDerbyNameWithRCid {
97 bgadell 112
  my $ATTRIBUTES;
113
  if (ref $_[0] eq "HASH") {
114
    $ATTRIBUTES = shift;
115
  }
7 - 116
  my $DEPT = shift // "";
57 bgadell 117
  my $selected = shift // "";
7 - 118
	my $optionList = "";
119
 
120
	my $cathan;
29 - 121
	if (!$DEPT or $DEPT eq "CMP") {
97 bgadell 122
	  $cathan = $internalDBH->prepare("select RCid, derby_name from official where access > 0 order by derby_name");
7 - 123
  	$cathan->execute();
57 bgadell 124
  } elsif ($DEPT eq "CLA") {
97 bgadell 125
    $cathan = $internalDBH->prepare("select RCid, derby_name from v_official where access > 0 and isnull(MVPid) = false and last_login > CURRENT_DATE - INTERVAL 365 DAY order by derby_name");
57 bgadell 126
    $cathan->execute;
7 - 127
	} else {
97 bgadell 128
    $cathan = $internalDBH->prepare("select RCid, derby_name from official where access > 0 and (department like ? and department not like ?) and last_login > CURRENT_DATE - INTERVAL 365 DAY order by derby_name");
16 - 129
    $cathan->execute("%".$DEPT."%", "%".$DEPT."-0%");
7 - 130
	}
97 bgadell 131
 
132
  my $valuelabel = $ATTRIBUTES->{DATALIST} ? "data-value" : "value";
7 - 133
	while (my ($id, $cat) = $cathan->fetchrow) {
57 bgadell 134
	  if ($id == $selected) {
97 bgadell 135
	    $optionList .= "<OPTION $valuelabel=$id selected>$cat</OPTION>";
57 bgadell 136
	  } else {
97 bgadell 137
	    $optionList .= "<OPTION $valuelabel=$id>$cat</OPTION>";
57 bgadell 138
	  }
7 - 139
	}
140
 
141
	return $optionList;
142
}
143
 
144
 
145
sub filter {
146
	my $colName = shift;
147
	my $filter = shift;
148
 
149
	if (exists &{"filter_".$colName}) { return &{"filter_".$colName} ($colName, $filter); }
150
 
151
	if ($colFilterTypeHash{$colName} eq 'select')
152
	{
153
		if (defined $filter)	{
154
			if ($filter eq "-blank-") {
155
				return "($colName = '' or isNull($colName) = 1)";
156
			}
157
#			$filter = s/'/\'/g;
158
			return "$colName = \"$filter\"";
159
		}
160
		else
161
		{
162
			my $thing = "filter-${colName}";
163
			my $categories = &fetchColEntries($colName, $FORM{$thing});
164
			my $Options = "<OPTION></OPTION>".$categories;
165
 
166
			$Options =~ s/>($FORM{$thing})/ selected>$1/;
167
			return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
168
		}
169
	} elsif ($colFilterTypeHash{$colName} eq 'text') {
170
		if (defined $filter)
171
		{
50 bgadell 172
			if ($filter =~ /\d{1,2}:\d{2}/ and $ORCUSER->{timeformat} eq "ampm") {
173
			  my ($h, $m) = split /:/, $filter;
174
			  my $caret;
175
			  if ($h =~ s/^\^\s*//) { $caret = "^"; }
176
			  if ($h < 8) { $h += 12; }
177
			  $filter = $caret.join ":", $h, $m;
178
			}
179
 
7 - 180
			if ($filter =~ s/^=\s*//) { return "$colName = \"$filter\""; }
181
 
182
			if ($filter !~ s/^\^\s*//) { $filter = "\%$filter"; }
183
			if ($filter !~ s/\s*\$$//) { $filter = "$filter\%"; }
184
			$filter =~ s/\*/%/g;
185
 
186
			return "$colName like \"$filter\"";
187
		}
188
		else
189
		{
190
			my $thing = "filter-${colName}";
191
			return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
192
		}
193
	} elsif ($colFilterTypeHash{$colName} eq 'number') {
194
		if (defined $filter)
195
		{
196
			if ($filter =~ /^[>=<]\s*/) { return "$colName $filter"; }
197
			else { return "$colName = $filter"; }
198
		}
199
		else
200
		{
201
			my $thing = "filter-${colName}";
202
			return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=2 $onChange>";
203
		}
204
	} elsif ($colFilterTypeHash{$colName} eq 'date') {
205
		if (defined $filter)
206
		{
207
			if ($filter =~ s/^<\s*//) { return "$colName < '$filter'"; }
208
			if ($filter =~ s/^>\s*//) { return "$colName > '$filter'"; }
209
 
210
			return "$colName = '$filter'";
211
		}
212
		else
213
		{
214
			my $thing = "filter-${colName}";
215
			return "<INPUT type=text name=$thing value=\"$FORM{$thing}\" size=15 $onChange>";
216
		}
29 - 217
	} elsif ($colFilterTypeHash{$colName} eq 'boolean') {
218
		if (defined $filter)
219
		{
220
			return "$colName = $filter";
221
		}
222
		else
223
		{
224
			my $thing = "filter-${colName}";
225
			my $Options = "<OPTION></OPTION><OPTION>True</OPTION><OPTION>False</OPTION>";
226
 
227
			$Options =~ s/>($FORM{$thing})/ selected>$1/;
228
			return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
229
		}
7 - 230
	} elsif ($colFilterTypeHash{$colName} eq 'ERROR') {
231
		return "<center><strong><font color=red>ERROR!</font>";
232
	} elsif ($colFilterTypeHash{$colName} eq 'none') {
233
		return;
234
	}
235
}
236
 
237
 
238
sub getData {
239
	my $fields = shift;
240
	my $whereClause = shift;
241
	my $table = shift;
242
	my $orderby = shift;
33 - 243
	my $curpage = shift; $curpage = 1 unless $curpage;
7 - 244
	my $pagelimit = shift // "All";
245
 
246
	my $selected = '*';
247
	$whereClause = scalar @{$whereClause} > 0 ? "where ".join " and ", @{$whereClause} : '';
248
 
249
  if ($orderby eq "dayofweek") {
250
   	$orderby = "order by date, time";
251
  } elsif ($orderby eq "eventid") { # only applicable to the log viewer...
252
   	$orderby = "order by eventid desc";
253
  } else {
254
	  $orderby = $orderby eq "" ? "" : "order by $orderby";
255
  }
256
 
257
  my $getMe;
258
  if ($pagelimit eq "All") {
259
  	$getMe = "select distinct * from $table $whereClause $orderby";
260
  } else {
261
    $curpage = ($curpage - 1) * $pagelimit;
262
  	$getMe = "select distinct * from $table $whereClause $orderby limit $curpage, $pagelimit";
263
  }
65 bgadell 264
	my ($totalcount) = @{$internalDBH->selectrow_arrayref ("select distinct count(*) from $table $whereClause")};
7 - 265
 
266
	my $limhan = $internalDBH->prepare($getMe);			# Get the tickets from the DB
267
	$limhan->execute();
268
 
269
	my @results = ();
270
	while (my $P = $limhan->fetchrow_hashref)
271
	{
272
		push @results, $P;
273
	}
274
 
275
	return (\@results, $totalcount);
276
}
277
 
278
 
279
sub getDBConnection {
280
  use WebDB;
281
  $dbh = WebDB::connect ();
282
  return $dbh;
283
}
284
 
285
 
286
sub inArray {
287
	my $item = shift;
288
	my $array = shift;
289
	foreach (@{$array})
290
	{
291
		return 1 if $item eq $_;
292
	}
293
	return 0;
294
}
295
 
296
sub notInArray {
297
	return ! inArray (@_);
298
}
299
 
300
 
301
sub uniq (@) {
302
    # From CPAN List::MoreUtils, version 0.22
303
    my %h;
304
    map { $h{$_}++ == 0 ? $_ : () } @_;
305
}
306
 
307
 
308
sub whereInArray {
309
	my $item = shift;
310
	my $array = shift;
311
	my $i = 0;
312
	foreach (@{$array})
313
	{
314
		return $i if $item eq $_;
315
		$i++;
316
	}
317
	return -1;
318
}
319
 
320
 
321
 
322
# Leave this alone, it's needed to compile correctly
323
return 1;