/[drupal]/contributions/modules/casetracker_services/casetracker_client.module
ViewVC logotype

Contents of /contributions/modules/casetracker_services/casetracker_client.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Mon Feb 25 10:57:03 2008 UTC (21 months ago) by sime
Branch: MAIN
CVS Tags: DRUPAL-5--0-1-ALPHA, HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +1 -0 lines
File MIME type: text/x-php
Added Id tags
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function casetracker_client_perm() {
8 return array(
9 'access case tracker client',
10 );
11 }
12
13 /**
14 * Implementation of hook_menu().
15 */
16 function casetracker_client_menu($may_cache) {
17 if ($may_cache) {
18 $items[] = array(
19 'path' => 'admin/settings/casetracker_client',
20 'access' => user_access('administer site configuration'),
21 'title' => 'Casetracker Support',
22 'callback' => 'drupal_get_form',
23 'callback arguments' => array('casetracker_client_settings'),
24 );
25 $items[] = array(
26 'path' => 'admin/casetracker_client',
27 'access' => user_access('access casetracker support'),
28 'callback' => 'casetracker_client_cases',
29 'title' => 'Support',
30 );
31 $items[] = array(
32 'path' => 'admin/casetracker_client/report',
33 'callback' => 'drupal_get_form',
34 'callback arguments' => array('casetracker_client_create'),
35 'title' => 'New issue',
36 'type' => MENU_LOCAL_TASK,
37 );
38 $items[] = array(
39 'path' => 'admin/casetracker_client/overview',
40 'callback' => 'casetracker_client_cases',
41 'title' => 'Support queue',
42 'type' => MENU_DEFAULT_LOCAL_TASK,
43 'weight' => -1,
44 );
45 }
46 else {
47 if (is_numeric(arg(3))) {
48 $items[] = array(
49 'path' => 'admin/casetracker_client/case/'. arg(3),
50 'callback' => 'casetracker_client_node',
51 'title' => 'Case details',
52 'weight' => 1,
53 'type' => MENU_LOCAL_TASK,
54 );
55 }
56 }
57 return $items;
58 }
59
60 /**
61 * Case Tracker Client options.
62 */
63 function casetracker_client_settings() {
64 $form = array();
65
66 $form['casetracker_general'] = array(
67 '#type' => 'fieldset',
68 '#title' => t('Connection settings'),
69 '#collapsible' => TRUE,
70 '#collapsed' => FALSE,
71 );
72 $form['casetracker_general']['casetracker_client_server_url'] = array(
73 '#type' => 'textfield',
74 '#title' => t('Remote server'),
75 '#required' => TRUE,
76 '#default_value' => variable_get('casetracker_client_server_url', 'https://example.com'),
77 '#description' => t("URL of the remote server running casetracker. Could be something like http://example.com/drupalroot"),
78 );
79 $form['casetracker_general']['casetracker_client_server_key'] = array(
80 '#type' => 'textfield',
81 '#title' => t('Remote API key'),
82 '#required' => TRUE,
83 '#default_value' => variable_get('casetracker_client_server_key', ''),
84 '#description' => t("API Key generated by Services module on the remote server."),
85 );
86
87 return system_settings_form($form);
88 }
89
90 /**
91 * Get display a node from the Case Tracker server site.
92 */
93 function casetracker_client_node() {
94 drupal_add_css(drupal_get_path('module', 'casetracker_client') .'/casetracker_client.css', 'module', 'all', false);
95
96 $url = variable_get('casetracker_client_server_url', '');
97 $key = variable_get('casetracker_client_server_key', '');
98
99 $nid = arg(3);
100 if ($nid && is_numeric($nid)) {
101 // Ask the server for a node.
102 $node = xmlrpc($url .'/services/xmlrpc/'. $key, 'casetracker.node', $key, $nid);
103 if (is_array($node) && isset($node['nid'])) {
104 $node['called_path'] = 'services/xmlrpc/'. $key;
105 $node['called_url'] = $url;
106 // Simple theme of the returned node.
107 $output = theme('casetracker_client_node', $node);
108 }
109 else {
110 $output = theme('casetracker_client_node', array(), 'Unknown Case ID');
111 }
112 }
113 else {
114 $output = theme('casetracker_client_node', array(), 'Bad Case ID');
115 }
116 return $output;
117 }
118
119 /**
120 * Theme a node returned from the CT server - it's an array, not an object.
121 */
122 function theme_casetracker_client_node($node = array(), $error = false) {
123 if ($error) {
124 drupal_set_title('Casetracker connection error');
125 return t($error);
126 }
127
128 drupal_set_title($node['title']);
129 // Dirty hack to clear up support site urls.
130 $summary = str_replace('href="/', 'href="'. $node['called_url'] .'/', $node['content']['casetracker_case_summary']['#value']);
131
132 $comments = str_replace('href="/'. $node['called_path'], 'href="'. $node['called_url'] .'/node/'. $node['nid'], $node['comments']);
133
134 $output = '<div class="casetracker-client-case">'."\n";
135 $output .= $summary;
136 $output .= '<h3>'. t('Issue by ') . '<em>'. $node['name'] .'</em></h3>';
137 $output .= $node['content']['body']['#value'];
138 $output .= $comments;
139 $output .= drupal_get_form('casetracker_client_update', $node);
140 $output .= '</div>';
141 return $output;
142 }
143
144 /**
145 * Get a list of cases from the CT server.
146 */
147 function casetracker_client_cases() {
148 drupal_add_css(drupal_get_path('module', 'casetracker_client') .'/casetracker_client.css');
149
150 $url = variable_get('casetracker_client_server_url', '');
151 $key = variable_get('casetracker_client_server_key', '');
152
153 // The 'all' stands for filter I haven't coded in the service yet.
154 // We add $key to the call url, it will be used by casetracker_services to restrict a project nid.
155 $overview = xmlrpc($url .'/services/xmlrpc/'. $key, 'casetracker.cases', $key, 'all');
156
157 return theme('casetracker_client_cases', $overview);
158 }
159
160 /**
161 * Theme our list of cases from the CT server.
162 */
163 function theme_casetracker_client_cases($overview) {
164 foreach ($overview['data'] as $case) {
165 $row = array();
166 $status = $overview['lookup'][$case['case_status_id']];
167 $type = $overview['lookup'][$case['case_type_id']];
168 $priority = $overview['lookup'][$case['case_priority_id']];
169 $row = array(
170 'class' => 'state-'. $case['case_status_id'] .' type-'. $case['case_type_id'],
171 'data' => array(
172 $case['case_number'],
173 l($case['title'], 'admin/casetracker_client/case/'. $case['nid']),
174 format_date($case['last_comment_timestamp'], 'short'),
175 $type,
176 $status,
177 $priority,
178 ),
179 );
180 $display[] = $row;
181 }
182 $output = '';
183
184 $output .= theme('table', array('Case', 'Title', 'Updated', 'Type', 'Status', 'Prioirity'), $display);
185 return $output;
186 }
187
188 /**
189 * Display a new case form
190 */
191 function casetracker_client_create() {
192 // get the case creation form.
193 $form = casetracker_client_update_form();
194 return $form;
195 }
196
197 /**
198 * Display a form for the bottom of an existing case.
199 */
200 function casetracker_client_update($node) {
201 // use the same form for case creation and alter it for the context of displaying a node.
202 $form = casetracker_client_update_form($node);
203 $form['nid'] = array(
204 '#type' => 'hidden',
205 '#default_value' => $node['nid'],
206 );
207 $form['title']['#title'] = 'Subject';
208 $form['attributes']['#collapsed'] = true;
209 $form['form_title'] = array(
210 '#value' => '<h2>'. t('Update this case') .'</h2>',
211 '#weight' => -2,
212 );
213 return $form;
214 }
215
216 /**
217 * A generic case form used for new cases and updating cases.
218 */
219 function casetracker_client_update_form($node = false) {
220
221 $url = variable_get('casetracker_client_server_url', '');
222 $key = variable_get('casetracker_client_server_key', '');
223
224 // Ask the CT server for all the codes we need for the select boxes.
225 $codes = xmlrpc($url .'/services/xmlrpc/'. $key, 'casetracker.codes', $key);
226
227 $form['attributes'] = array(
228 '#type' => 'fieldset',
229 '#title' => 'Case attributes',
230 '#collapsible' => true,
231 '#collapsed' => false,
232 '#weight' => -1,
233 );
234 $form['attributes']['case_status_id'] = array(
235 '#type' => 'select',
236 '#options' => $codes['status'],
237 '#default_value' => ($node) ? $node['case_status_id'] : 0,
238 '#title' => 'Status',
239 );
240 $form['attributes']['case_type_id'] = array(
241 '#type' => 'select',
242 '#options' => $codes['type'],
243 '#default_value' => ($node) ? $node['case_type_id'] : 0,
244 '#title' => 'Type',
245 );
246 $form['attributes']['case_priority_id'] = array(
247 '#type' => 'select',
248 '#options' => $codes['priority'],
249 '#default_value' => ($node) ? $node['case_priority_id'] : 0,
250 '#title' => 'Priority',
251 );
252 $form['title'] = array(
253 '#type' => 'textfield',
254 '#title' => t('Title'),
255 );
256 $form['body'] = array(
257 '#type' => 'textarea',
258 '#title' => 'Issue',
259 '#rows' => 5,
260 );
261 $form['submit'] = array(
262 '#type' => 'submit',
263 '#weight' => 1,
264 '#value' => t('Submit'),
265 );
266 return $form;
267 }
268
269 /**
270 * Submit the creation form, pass the values to the CT server.
271 */
272 function casetracker_client_create_submit($form_id, $form_values) {
273 $url = variable_get('casetracker_client_server_url', '');
274 $key = variable_get('casetracker_client_server_key', '');
275
276 $form_values['client_url'] = $url;
277 $nid = xmlrpc($url .'/services/xmlrpc/'. $key, 'casetracker.log', $key, 'new', $form_values);
278 return 'admin/casetracker_client/case/'. $nid;
279 }
280
281 /**
282 * Submit the update form, pass the values to the CT server.
283 */
284 function casetracker_client_update_submit($form_id, $form_values) {
285 $url = variable_get('casetracker_client_server_url', '');
286 $key = variable_get('casetracker_client_server_key', '');
287
288 $nid = $form_values['nid'];
289 unset($form_values['nid']);
290
291 $form_values['client_url'] = $url;
292 $nid = xmlrpc($url .'/services/xmlrpc/'. $key, 'casetracker.log', $key, $nid, $form_values);
293 return 'admin/casetracker_client/case/'. $nid;
294 }

  ViewVC Help
Powered by ViewVC 1.1.2