Subversion Repositories VORC

Rev

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