-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello_tickets.module
238 lines (214 loc) · 7.47 KB
/
trello_tickets.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
<?php
/**
* @file
* Tickets module altered slightly from Example Trello Module.
*
* Show the use of the API showing a list of boards in a block, and a form
* which posts to a list on a board.
*/
define('TRELLO_TICKETS_DEFAULT_LINK_TEXT', 'Request Support');
/**
* Implements hook_permission().
*/
function trello_tickets_permission() {
return array(
'create support ticket' => array(
'title' => t('Create support ticket.'),
'description' => t('Create a support ticket and submit it to the site admin for follow-up.'),
),
);
}
/**
* Implements hook_menu().
*/
function trello_tickets_menu() {
$items = array();
$items['issues'] = array(
'title' => 'Create support ticket',
'page callback' => 'drupal_get_form',
'page arguments' => array('trello_tickets_form'),
'access arguments' => array('create support ticket'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/issues'] = array(
'title' => 'Trello Tickets Configuration',
'description' => 'Configure your Support Queue list.',
'page callback' => 'drupal_get_form',
'page arguments' => array('trello_tickets_config_form'),
'access arguments' => array('administer trello'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* The example administration form.
*
* Let the user select a board and select or create a list in that board for
* use in the support ticket form.
*/
function trello_tickets_config_form() {
$t = trello_api('members/me/boards');
if ($t['meta_data']['count'] == 0 || $t['data'][0] == NULL) {
$form['noboards'] = array(
'#markup' => t('No boards found. Please confirm your Trello API configuration and auth token.'),
);
return $form;
}
$boards = array();
foreach ($t['data'] as $b) {
$boards[$b->id] = $b->name;
}
$list = variable_get('trello_tickets_queue');
if ($list) {
$t = trello_api(array('lists' => $list, 'board'));
$default_board = $t['data'][0]->id;
$t = trello_api(array('lists' => $list));
$default_list = $t['data'][0]->name;
}
$form = array();
$form['board'] = array(
'#type' => 'select',
'#title' => t('Board'),
'#options' => $boards,
'#required' => TRUE,
);
isset($default_board) AND $form['board']['#default_value'] = $default_board;
$form['list'] = array(
'#type' => 'textfield',
'#title' => t('List name'),
'#default_value' => (isset($default_list) ? $default_list : 'Support Queue'),
'#required' => TRUE,
);
$form['link_text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
'#default_value' => variable_get('trello_tickets_link_text', TRELLO_TICKETS_DEFAULT_LINK_TEXT),
'#required' => TRUE,
);
$form['notify'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('trello_tickets_notify', ''),
'#title' => t('Usernames to notify'),
'#required' => FALSE,
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Configuration'),
);
return $form;
}
/**
* Handle the support ticket postings.
*/
function trello_tickets_config_form_submit(&$form, &$form_state) {
$t = trello_api(array('boards' => $form_state['values']['board'], 'lists'));
foreach ($t['data'] as $list) {
if ($form_state['values']['list'] == $list->name) {
$list_id = $list->id;
}
}
if (empty($list_id)) {
// Create the list.
$args = array('name' => $form_state['values']['list'], 'idBoard' => $form_state['values']['board']);
$t = trello_api('lists', $args, 'POST');
$list_id = $t['data'][0]->id;
}
variable_set('trello_tickets_queue', $list_id);
$notify = isset($form_state['values']['notify']) ? $form_state['values']['notify'] : FALSE;
variable_set('trello_tickets_notify', $notify);
variable_set('trello_tickets_link_text', $form_state['values']['link_text']);
drupal_set_message(t('Your trello list configuration has been saved.'));
}
/**
* Present a simple form to the user for submitting a support request.
*/
function trello_tickets_form($form, &$form_state) {
// Check that we have a list to submit to.
if (!variable_get('trello_tickets_queue')) {
$markup = t('You need to configure a list first: !link',
array('!link' => l(t('Configuration page'), 'admin/config/services/trello/example')));
$form = array('notice' => array('#markup' => $markup));
return $form;
}
$form = array();
$form['help'] = array(
'#markup' => t('Please submit your support request here and it will be added to our queue.'),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#required' => TRUE,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Details'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Request'),
);
$form['referer'] = array(
'#type' => 'hidden',
'#default_value' => $_SERVER['HTTP_REFERER'],
);
$form['user_agent'] = array(
'#type' => 'hidden',
'#default_value' => $_SERVER['HTTP_USER_AGENT'],
);
$form['ip_address'] = array(
'#type' => 'hidden',
);
global $user;
$form['username'] = array(
'#type' => 'hidden',
'#default_value' => $user->uid == 0 ? 'Anonymous' : $user->name,
);
return $form;
}
/**
* Add the post as a card to the specified list.
*/
function trello_tickets_form_submit(&$form, &$form_state) {
$queue = variable_get('trello_tickets_queue');
$args = array('name' => $form_state['values']['title'], 'idList' => $queue);
empty($form_state['values']['description']) OR $args['desc'] = $form_state['values']['description'];
$referer = isset($form_state['values']['referer']) ? $form_state['values']['referer'] : '?';
$agent = isset($form_state['values']['user_agent']) ? $form_state['values']['user_agent'] : '?';
$user = isset($form_state['values']['username']) ? $form_state['values']['username'] : '?';
$args['desc'] .= <<<INFO
---
- Referer: $referer
- User Agent: $agent
- Username: $user
INFO;
$board = trello_api(array('lists' => $queue, 'board'), 'GET');
$members = trello_api(array('boards' => $board['data'][0]->id, 'members'), 'GET');
$members_to_notify = variable_get('trello_tickets_notify', FALSE);
$to_notify = $members_to_notify ? explode(',', $members_to_notify) : array();
$notify = array();
foreach($members['data'] as $member) {
if (in_array($member->username, $to_notify)) {
$notify[] = $member->id;
}
}
$args[] = array('idMembers' => $notify);
// need a due date formatted as 2017-06-15T04:00:00.000Z
$due = gmstrftime("%his link to track the progress: A %d-%b-%y %T %Z", time() + 86400);
$args['due'] = $due;
$cards = trello_api(array('lists' => $queue, 'cards'), 'GET');
$labels = trello_api(array('boards' => $board['data'][0]->id, 'labels'), 'GET');
// Use the object oriented version of the API.
$trello = new Trello();
$trello->post('cards', $args);
$cards = trello_api(array('lists' => $queue, 'cards'));
drupal_set_message(t('Thanks! Your ticket was submitted. Use this link to track the progress: <a href="@c">@c</a>',
array('@c' => url($trello->result['data'][0]->shortUrl))
));
}
function trello_tickets_block_view_alter(&$data, $block) {
if ($block->region == 'header_second' && $block->module == 'block') {
$data['content'] .= sprintf('<div class="support-ticket-link"><a href="https://askus.lib.lsu.edu/form?queue_id=5569">%s</a></div>', 'Submit Support Request');
}
}