This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
dru_claim.module
265 lines (236 loc) · 8.8 KB
/
dru_claim.module
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
<?php
/**
* @file
*/
/**
* Implements hook_permission().
*/
function dru_claim_permission() {
return array(
'create claim' => array(
'title' => t('Create Claim'),
'description' => t('Allow users to create claim'),
),
);
}
/**
* Implements hook_menu().
*/
function dru_claim_menu() {
$items['ticket/node/nojs/%node/%'] = array(
'page callback' => 'dru_claim_node',
'file' => 'dru_claim.pages.inc',
'page arguments' => array(2, 3, 4),
'access arguments' => array('create claim'),
'type' => MENU_CALLBACK,
);
$items['ticket/node/ajax/%node/%'] = array(
'page callback' => 'dru_claim_node',
'file' => 'dru_claim.pages.inc',
'page arguments' => array(2, 3, 4),
'access arguments' => array('create claim'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
$items['ticket/comment/nojs/%comment/%'] = array(
'page callback' => 'dru_claim_comment',
'file' => 'dru_claim.pages.inc',
'page arguments' => array(2, 3, 4),
'access arguments' => array('create claim'),
'type' => MENU_CALLBACK,
);
$items['ticket/comment/ajax/%comment/%'] = array(
'page callback' => 'dru_claim_comment',
'file' => 'dru_claim.pages.inc',
'page arguments' => array(2, 3, 4),
'access arguments' => array('create claim'),
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_preprocess_node().
*/
function dru_claim_preprocess_node(&$vars) {
if (!empty($vars['claim'])) {
$vars['classes_array'][] = 'dru-claim-process node-claim';
}
}
/**
* Implements hook_node_view().
*/
function dru_claim_node_view($node, $view_mode) {
drupal_add_css(drupal_get_path('module', 'dru_claim') . '/dru_claim.css');
if ($node->type == 'ticket') {
$claim = db_select('ticket_claim', 't')
->fields('t')
->condition('t.ticket_id', $node->nid, '=')
->execute()
->fetchObject();
if (!empty($claim)) {
$initiator = user_load($claim->init_uid);
$node->content['initiator'] = array(
'#markup' => '<div class="initiator">
<div class="label">' . t('Initiator') . ':</div>
<div class="value">' . l($initiator->name, 'user/' . $initiator->uid) . '</div>
</div>',
);
if ($claim->content_type == 'node') {
$original = node_load($claim->content_id);
$original_format = $original->body[LANGUAGE_NONE][0]['format'];
$original_text = $original->body[LANGUAGE_NONE][0]['value'];
$original_title = l($original->title, 'node/' . $claim->content_id);
}
else {
$original = comment_load($claim->content_id);
$original_format = $original->comment_body[LANGUAGE_NONE][0]['format'];
$original_text = $original->comment_body[LANGUAGE_NONE][0]['value'];
$original_title = l($original->subject, 'comment/' . $claim->content_id, array('fragment' => 'comment-' . $claim->content_id));
}
if ($view_mode == 'full') {
$node->content['entity'] = array(
'#markup' => '<div class="entity claimed-entity claimed-entity-full">
<div class="label claim-entity-label">' . t('Entity') . ':</div>
<div class="value claimed-entity-value">' . $original_title . check_markup($original_text, $original_format) . '</div>
</div>',
);
}
else {
$node->content['entity'] = array(
'#markup' => '<div class="entity claimed-entity claimed-entity-teaser">
<div class="label claim-entity-label">' . t('Entity') . ':</div>
<div class="value claimed-entity-value">' . $original_title . '</div>
</div>',
);
}
}
}
// Add node links.
if ($node->type != 'ticket' && user_access('create claim')) {
drupal_add_js(drupal_get_path('module', 'dru_tickets') . '/dru_tickets.js');
drupal_add_library('system', 'drupal.ajax');
$token = drupal_get_token('dru_claim');
if (isset($node->claim) && $node->claim == 0) {
$node->content['links']['comment']['#links']['dru-claim'] = array(
'title' => t('Claim'),
'href' => 'ticket/node/nojs/' . $node->nid . '/' . $token,
'query' => drupal_get_destination(),
'attributes' => array(
'class' => array('use-ajax', 'claim-node-' . $node->nid),
),
);
}
}
// View claim verdict.
if (user_is_logged_in()) {
if (isset($node->claim) && $node->claim && $ticket = node_load($node->claim)) {
$node->content['body'][0]['#markup'] = dru_claim_get_ticket_info($ticket, $node, 'node');
}
}
}
/**
* Get body with ticket info.
*/
function dru_claim_get_ticket_info($ticket, $entity, $entity_type) {
if (!$ticket->ticket->ticket_verdict) {
$verdict = t('Claim is under review');
$icon = '<i class="fa fa-cogs"></i>';
$class = 'claim_message_processed';
}
else {
$verdict = $ticket->ticket->ticket_verdict;
$icon = '<i class="fa fa-balance-scale"></i>';
$class = 'claim_message_done';
}
$ticket_author = user_load($ticket->uid);
$ticket_category = taxonomy_term_load($ticket->field_claim_category['und'][0]['tid']);
if (!empty($ticket->body['und'][0]['value'])) {
$ticket_body = '<i class="fa fa-commenting-o"></i>';
$ticket_body .= $ticket->body['und'][0]['value'];;
}
else {
$ticket_body = '';
}
$ticket_info = '<div class="ticket-body">';
$ticket_info .= $ticket_body;
$ticket_info .= '</div>';
$ticket_info .= '<div class="ticket-category">';
$ticket_info .= '<i class="fa fa-exclamation-triangle"></i>';
$ticket_info .= $ticket_category->name;
$ticket_info .= '</div>';
$ticket_info .= '<div class="ticket-created small-inline-text">';
$ticket_info .= '<i class="fa fa-calendar"></i>';
$ticket_info .= date('d.m.Y', $ticket->created);
$ticket_info .= '</div>';
$ticket_info .= '<div class="ticket-id small-inline-text">';
$ticket_info .= '<i class="fa fa-eye"></i>';
$ticket_info .= l(t('View ticket'), 'node/' . $ticket->nid);
$ticket_info .= '</div>';
$ticket_info .= '<div class="ticket-author small-inline-text">';
$ticket_info .= '<i class="fa fa-user"></i>';
$ticket_info .= l($ticket_author->name, 'user/' . $ticket_author->uid);
$ticket_info .= '</div>';
$claim_message = '<div class="claim_message ' . $class . '">';
$claim_message .= $icon;
$claim_message .= $verdict;
$claim_message .= '</div>';
$claim_message .= '<div class="ticket-info">';
$claim_message .= $ticket_info;
$claim_message .= '</div>';
if ($entity_type == 'node') {
$claim_message .= $entity->content['body'][0]['#markup'];
}
if ($entity_type == 'comment') {
$claim_message .= $entity->content['comment_body'][0]['#markup'];
}
return $claim_message;
}
/**
* Implements hook_preprocess_field().
*/
function dru_claim_preprocess_field(&$vars) {
if ($vars['element']['#field_name'] == 'body' && $vars['element']['#entity_type'] == 'node') {
$vars['classes_array'][] = 'body-' . $vars['element']['#object']->nid;
}
if ($vars['element']['#field_name'] == 'comment_body' && $vars['element']['#entity_type'] == 'comment') {
$vars['classes_array'][] = 'body-' . $vars['element']['#object']->cid;
}
}
/**
* Implements hook_preprocess_comment().
*/
function dru_claim_preprocess_comment(&$vars) {
if ($vars['comment']->claim <> 0) {
$vars['classes_array'][] = 'dru-claim comment-claim';
}
}
/**
* Implements hook_comment_view().
*/
function dru_claim_comment_view($comment, $view_mode) {
// Add comment links.
drupal_add_css(drupal_get_path('module', 'dru_claim') . '/dru_claim.css');
if (user_access('create claim')) {
drupal_add_js(drupal_get_path('module', 'dru_tickets') . '/dru_tickets.js');
drupal_add_library('system', 'drupal.ajax');
$token = drupal_get_token('dru_claim');
if ($comment->claim == 0 && $comment->node_type != 'comment_node_ticket') {
$comment->content['links']['comment']['#links']['dru-claim'] = array(
'title' => t('Claim'),
'href' => 'ticket/comment/nojs/' . $comment->cid . '/' . $token,
'query' => drupal_get_destination(),
'attributes' => array(
'class' => array('use-ajax', 'claim-comment-' . $comment->cid),
),
);
}
}
// View claim verdict.
if (user_is_logged_in()) {
if ($comment->claim <> 0) {
$ticket = node_load($comment->claim);
$comment->content['comment_body'][0]['#markup'] = dru_claim_get_ticket_info($ticket, $comment, 'comment');
}
}
}