Subversion Repositories VORC

Rev

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