forked from abztrakt/WP-UWConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-page-template.php
350 lines (321 loc) · 19.9 KB
/
request-page-template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
define( 'DONOTCACHEPAGE', True );
if ( isset( $_SERVER['REMOTE_USER'] ) ) {
$user = $_SERVER['REMOTE_USER'];
} else if ( isset($_SERVER['REDIRECT_REMOTE_USER'] ) ) {
$user = $_SERVER['REDIRECT_REMOTE_USER'];
} else if ( isset( $_SERVER['PHP_AUTH_USER'] ) ) {
$user = $_SERVER['PHP_AUTH_USER'];
}
$error_flag = False;
$sn_num = get_query_var( 'ticketID' );
if ( $sn_num == '' ) {
$new_url = site_url() . '/myrequests/';
wp_redirect( $new_url );
}
//Handle submitting comments
if ( isset( $_POST['submitted'] ) && isset( $_POST['comments'] ) ) {
$comments = $_POST['comments'];
$comments_json = array(
'actor' => $user,
'record' => $sn_num,
'comment' => $comments,
);
$comments_json = json_encode( $comments_json );
$comments_url = get_option( 'uwc_SN_URL' ) . '/comment.do';
// If a POST and have comments - create a comment in SN
if ( get_option( 'uwc_SN_USER' ) && get_option( 'uwc_SN_PASS' ) && get_option( 'uwc_SN_URL' ) ) {
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( get_option( 'uwc_SN_USER' ) . ':' . get_option( 'uwc_SN_PASS' ) ),
'Content-Type' => 'application/json',
),
'body' => $comments_json,
);
}
$response = wp_remote_post( $comments_url, $args );
wp_redirect( $_SERVER['REQUEST_URI'] ); exit;
}
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'header', 'image' ); ?>
<div class="container uw-body">
<div class="row">
<div class="col-md-<?php echo (($sidebar[0]!='on') ? '8' : '12' ) ?>
uw-content" role='main'>
<?php uw_site_title(); ?><span class="udub-slant"><span></span></span><h3 class="uw-site-tagline" >Information technology tools and resources at the UW</h3>
<?php uw_mobile_front_page_menu(); ?>
<?php service_breadcrumbs(); ?>
<div id='main_content' tabindex="-1" style="margin-right: 1em;" >
<?php the_content();
if ( isset( $user ) ) {
if ( isset( $response ) ) {
$status = json_decode($response['body'], true);
if( $status['Error']['Status'] !== '200' ) {
echo '<div class="alert alert-warning" style="margin-top:2em;">';
echo 'Attention! Your comment could not be posted: ' . $status['Error']['Text'] . ' (' . $status['Error']['Status'] . ')';
echo '</div>';
$error_flag = True;
}
}
?>
<div class="user-logout row">
<span style="float:right;"><span class="glyphicon glyphicon-user"></span> <?php echo $user; ?> <a href="<?php echo home_url('/user_logout'); ?>" class="buttonesque">LOGOUT</a></span>
</div>
<?php
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( get_option('uwc_SN_USER') . ':' . get_option('uwc_SN_PASS') ),
)
);
//Get table record as JSON associated with NETID logged in user
$user_url = '/sys_user_list.do?JSONv2&sysparm_query=user_name%3D' . $user;
$user_json = get_SN($user_url, $args);
//SN sys_id of user
$user_id = $user_json->records[0]->sys_id;
$firstname = $user_json->records[0]->first_name;
$lastname = $user_json->records[0]->last_name;
//fullname of user for use in comparing to watchlist
$name = $firstname . " " . $lastname;
//Set full ticekt type, build display value url, and sys_id url (for use with watch list) based on the type of ticket
$sn_type = substr($sn_num, 0, 3);
if( $sn_type == 'REQ' ) {
$url = '/u_simple_requests_list.do?JSONv2&displayvalue=true&sysparm_query=number=' . $sn_num . '^u_caller.user_name=' . $user . '^ORwatch_listLIKE' . $user_id;
$sn_type = 'request (REQ)';
$urlwl = '/u_simple_requests_list.do?JSONv2&sysparm_query=number=' . $sn_num . '^u_caller.user_name=' . $user . '^ORwatch_listLIKE' . $user_id;
} else if( $sn_type == 'INC' ) {
$url = '/incident.do?JSONv2&displayvalue=true&sysparm_query=number=' . $sn_num . '^caller_id.user_name=' . $user . '^ORwatch_listLIKE' . $user_id;
$sn_type = 'incident (INC)';
$urlwl = '/incident.do?JSONv2&sysparm_query=number='. $sn_num . '^caller_id.user_name=' . $user . '^ORwatch_listLIKE' . $user_id;
} else if ($sn_type == 'RIT') {
$sn_type='item (RITM)';
$url = '/sc_req_item.do?JSONv2&displayvalue=true&sysparm_query=number='.$sn_num.'^sys_created_by=' . $user . '^ORwatch_listLIKE' . $user_id. '^ORrequest.requested_for.user_name=' . $user;
$urlwl = '/sc_req_item.do?JSONv2&sysparm_query=number='.$sn_num.'^sys_created_by=' . $user . '^ORwatch_listLIKE' . $user_id. '^ORrequest.requested_for.user_name=' . $user;
} else {
echo "Unrecognized type";
$error_flag = True;
}
$ticket_json = get_SN($url, $args);
$record = $ticket_json->records[0];
$ticket_jsonwl = get_SN($urlwl, $args);
$recordwl = $ticket_jsonwl->records[0];
//array of sys_id's of users in watch list
$watch_list = explode(',', $recordwl->watch_list);
//We already have the logged in user's netid - is the logged in user the caller?
if( $record->u_caller == $name || $record->caller_id == $name || $record->sys_opened_by == $user) {
$caller_nid = $user;
} else {
if ($sn_type == 'request (REQ)') {
$caller_url = '/sys_user_list.do?JSONv2&sysparm_query=name%3D' . urlencode($record->u_caller);
} else if ($sn_type == 'incident (INC)') {
$caller_url = '/sys_user_list.do?JSONv2&sysparm_query=name%3D' . urlencode($record->caller_id);
} else if ($sn_type == 'item (RITM)') {
$caller_url = '/sys_user_list.do?JSONv2&sysparm_query=name%3D' . urlencode($record->opened_by);
}
$caller_json = get_SN($caller_url, $args);
$caller_nid = $caller_json->records[0]->user_name;
}
// Get the comments
$comment_url = '/sys_journal_field.do?displayvalue=true&JSONv2&sysparm_cation=getRecords&sysparm_query=active=true^element=comments^element_id=' . $record->sys_id;
$comment_json = get_SN($comment_url, $args);
$comments = $comment_json->records;
if ($sn_num !== $record->number) {
echo "<div class='alert alert-danger'>$sn_num is not one of your current requests.</div>";
$error_flag = True;
} else {
echo "<h2 style='margin-top:0;'>$record->short_description <span style='color:#999;'>($record->number)</span></h2>";
echo "<h3 class='assistive-text'>Details:</h3>";
echo "<table class='table'>";
if( !empty( $record->caller_id ) ) {
$caller = $record->caller_id;
} else if( !empty( $record->u_caller ) ) {
$caller = $record->u_caller;
} else {
$caller = 'UNKNOWN';
}
// Array of record states and their corresponding classes
$states = array(
"New" => 'class="label label-success"',
"Active" => 'class="label label-success"',
"Awaiting User Info" => 'class="label label-warning"',
"Awaiting Tier 2 Info" => 'class="label label-success"',
"Awaiting Vendor Info" => 'class="label label-success"',
"Internal Review" => 'class="label label-success"',
"Stalled" => 'class="label label-success"',
"Delivered" => 'class="label label-success"',
"Resolved" => 'class="label label-default"',
"Closed" => 'class="label label-default"',
);
$item_stages = array(
"Approval" => 'class="label label-success"',
"Internal Review" => 'class="label label-success"',
"Work in Progress" => 'class="label label-success"',
"Fulfillment" => 'class="label label-success"',
"Complete" => 'class="label label-default"',
"Completed" => 'class="label label-default"',
"Request Review" => 'class="label label-success"',
"Waiting for Approval" => 'class="label label-success"',
"waiting_for_approval" => 'class="label label-success"',
"Configuration" => 'class="label label-success"',
"CIO Approval" => 'class="label label-success"',
"Backordered" => 'class="label label-warning"',
"Request Cancelled" => 'class="label label-default"',
"Delivery" => 'class="label label-success"',
"Dept. Head Approval" => 'label label-success"',
"Catalog item removed" => 'class="label label-warning"',
"Gift Requested" => 'class="label label-success"',
"Procurement" => 'class="label label-success"',
"Awaiting Delivery" => 'class="label label-success"',
"Completed" => 'class="label label-default"',
"Event Planning" => 'class="label label-success"',
"Deployment" => 'class="label label-success"',
);
if ($record->state != "Resolved" && $record->state != "Awaiting User Info" && $record->state != "Closed") {
$record->state = "Active";
}
echo "<tr><td>Status:</td><td class='request_status'>";
if ($sn_type !== 'item (RITM)') {
if (array_key_exists($record->state, $states)) {
$class = $states[$record->state];
echo "<span $class>$record->state</span>";
}
}
else {
if (array_key_exists($record->stage, $item_stages)) {
$class = $item_stages[$record->stage];
echo "<span $class>$record->stage</span>";
}
}
if ( in_array($user_id, $watch_list) && $user_id != $recordwl->u_caller) {
echo " <span class='label label-warning'>Watching</span>";
}
echo "</td></tr>";
echo "<tr><td>Service:</td> <td>$record->cmdb_ci</td></tr>";
echo "<tr><td>Opened on:</td> <td>$record->opened_at</td></tr>";
echo "<tr><td>Last Updated:</td> <td>$record->sys_updated_on</td></tr>";
//Get attachments
/*
$att_url = '/sys_attachment.do?JSONv2&sysparm_query=table_sys_id=' . $record->sys_id;
$attach_json = get_SN($att_url, $args);
if (count($attach_json->records) > 0) {
echo "<tr><td>Attachments:</td> <td>";
echo "<div class='row'>";
foreach( $attach_json->records as $attachment ) {
$attID = $attachment->sys_id;
$attName = $attachment->file_name;
$content_type = $attachment->content_type;
//attachment download link
$url = 'https://uweval.service-now.com/sys_attachment.do?sys_id=' . $attID;
echo "<div class='col-lg-6'>";
//Check for mimetype and display related icon
if (strstr($content_type, "/", true) == "image") {
?>
<a href=<?= $url; ?> title="<?= $attName ?>"><div class="att_wrap"><i class="fa fa-file-image-o fa-2x"></i><p><?= $attName ?></p></div></a>
<?php
} else if (strstr($content_type, "/") == "/pdf" ) {
?>
<a href=<?= $url; ?> title="<?= $attName ?>"><div class="att_wrap"><i class="fa fa-file-pdf-o fa-2x"></i><p><?= $attName ?></p></div></a>
<?php
} else if ( strpos( strstr($content_type, "/"), "zip") ) {
?>
<a href=<?= $url; ?> title="<?= $attName ?>"><div class="att_wrap"><i class="fa fa-file-zip-o fa-2x"></i><p><?= $attName ?></p></div></a>
<?php
} else {
?>
<a href=<?= $url; ?> title="<?= $attName ?>"><div class="att_wrap"><i class="fa fa-file-o fa-2x"></i><p><?= $attName ?></p></div></a>
<?php
}
echo "</div>";
}
?>
<?php
}
*/
echo "</div>";
echo "</td></tr>";
echo "</table>";
if ($sn_type !== 'item (RITM)') {
echo "<h3 style='margin-top:2em;'>Description:</h3><div><pre>" . stripslashes($record->description) . " </pre></div>";
}
//Set up comment box
if(!$error_flag && $record->state != "Closed" ) {
$submit_url = site_url() . '/myrequest/' . $sn_num . '/'; ?>
<form class='request-comment-form' role='form' action="<?php $submit_url; ?>" method='post'>
<div class='form-group' style='margin-bottom:1em;'>
<label for='exampleInputPassword1'>Respond to Support Staff:</label>
<textarea name='comments' class='form-control' rows='3' style='resize:vertical;'></textarea>
</div>
<button type='submit' class='btn btn-primary'>Submit</button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php
} else if ($record->state == "Closed" || $record->active==FALSE) {
echo "<p class='alert alert-error'>This record has been closed. If you wish to revisit this issue, you can reference the issue number above in a new request to <a href='mailto:help@uw.edu'>help@uw.edu</a>.";
} else {
echo "<h3>Status 403: Unauthorized</h3>";
echo "<p>Please log in to your UW NETID in order to view your Requests and Incidents</p>";
}
if (sizeof($comments) > 0) {
echo "<h3 style='margin-top:2em;'>Additional comments:</h3>";
usort( $comments, 'sortByCreatedOnDesc' ); //comments sorted chronologically descending
echo "<ol style='margin-left:0;'>";
$prevwatch = array();
foreach( $comments as $comment ) {
$watcher = False;
$comment_user = $comment->sys_created_by;
//is this user the logged in user or do we know they're in the watch list already? if not get their SN sys_id
if ( !in_array($comment_user, $prevwatch) && $comment_user != $user) {
$user_url = '/sys_user_list.do?JSONv2&sysparm_query=user_name%3D' . $comment_user;
$user_json = get_SN($user_url, $args);
$comment_user_id = $user_json->records[0]->sys_id;
//Are they a watcher?
if ( in_array($comment_user_id, $watch_list) ) {
$watcher = True;
//We've seen them now
array_push($prevwatch, $comment_user);
}
} else if ( in_array($comment_user, $prevwatch) ) {
$watcher = True;
} else {
}
echo "<li class='media'>";
//Check who the commenter is, following roles win in order listed if a user is more than one
//logged in user?
if ($comment->sys_created_by == $user) {
echo "<div class='media-body caller-comments'>";
$display_user = $user;
//caller?
} elseif ($comment->sys_created_by == $caller_nid) {
echo "<div class='media-body support-comments'>";
$display_user = "Caller";
//watcher?
} elseif ($watcher) {
echo "<div class='media-body support-comments'>";
$display_user = "Watcher";
//support staff
} else {
echo "<div class='media-body support-comments'>";
$display_user = "SUPPORT STAFF";
}
echo "<div class='comment-timestamp'><strong class='user_name'>$display_user</strong> <span class='create-date'>$comment->sys_created_on</span></div>";
echo "<pre style='white-space:pre-wrap'>";
echo stripslashes($comment->value);
echo "</pre>";
echo "</div>";
echo "</li>";
}
echo "</ol>";
}
} //end if else to see if incident/request number doesn't match
}
?>
</div> <!-- #main-content -->
</div> <!-- uw-content -->
<div id='sidebar' role='navigation' aria-label='Sidebar Menu'>
<?php dynamic_sidebar('Service-Catalog-Sidebar'); ?>
</div> <!-- #sidebar -->
</div> <!-- row -->
</div> <!-- uw-body -->
<?php
endwhile;
get_footer(); ?>