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

Contents of /contributions/modules/casetracker_services/casetracker_services.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_menu().
6 */
7 function casetracker_services_menu($may_cache) {
8 if ($may_cache) {
9 $items[] = array(
10 'path' => 'admin/settings/casetracker_services',
11 'access' => user_access('administer case tracker'),
12 'title' => 'Case Tracker services',
13 'callback' => 'drupal_get_form',
14 'callback arguments' => array('casetracker_services_settings'),
15 );
16 }
17 return $items;
18 }
19
20
21 /**
22 * Configures the various Case Tracker Server options.
23 *
24 * The main settings simply take each api key generate in the services
25 * module and ask the user to allocate a valid user.
26 *
27 * This way, when the request comes in, the user has already been validated
28 * by the services module and we protect the system from changes outside
29 * the validated project.
30 *
31 */
32 function casetracker_services_settings() {
33 $form = array();
34
35 $form['casetracker_services'] = array(
36 '#type' => 'fieldset',
37 '#title' => t('Project access'),
38 '#collapsible' => TRUE,
39 '#collapsed' => FALSE,
40 '#description' => t('Assign projects to all the remote websites set up in the Services admin'),
41 );
42
43 $mappings = casetracker_services_project_mapping_load();
44
45 $projects = db_query("SELECT nid, title FROM {node} WHERE type = 'casetracker_basic_project'");
46 $project_choices[0] = 'Blocked';
47 while ($project = db_fetch_array($projects)) {
48 $project_choices[$project['nid']] = $project['title'];
49 }
50 foreach ($mappings as $key => $data) {
51 $form['casetracker_services'][$key] = array(
52 '#type' => 'select',
53 '#title' => $data['domain'],
54 '#description' => $data['title'],
55 '#options' => $project_choices,
56 '#default_value' => $data['project'],
57 );
58 }
59
60 $form['submit'] = array(
61 '#type' => 'submit',
62 '#value' => t('Submit'), // this text is an easter egg.
63 );
64
65 return $form;
66 }
67
68 /**
69 * Find out the key that the user is connecting by.
70 * Now this only works because we add the key to the url call.
71 * There should be a way to get this key from the services module but
72 * I don't know a way
73 */
74 function casetracker_services_get_key() {
75 // Assume the key is arg 2...
76 if (!arg(2)) {
77 watchdog('A call for Case Tracker Server from is missing the required api key in the third part of the url.');
78 return false;
79 }
80
81 return arg(2);
82 }
83
84 function casetracker_services_settings_submit($form_id, $form_values) {
85 casetracker_services_project_mapping_save($form_values);
86 }
87
88
89 function casetracker_services_get_project() {
90 $key = casetracker_services_get_key();
91 if (!$key) {
92 return;
93 }
94 return casetracker_services_project_mapping_load($key);
95 }
96
97 function casetracker_services_project_mapping_load($key = false) {
98 $mappings = unserialize(variable_get('casetracker_services_project_mapping', ''));
99
100 $keys = services_get_keys();
101 if ($key) {
102 // A service call, just return the allowed project.
103 return $mappings[$key];
104 }
105 foreach ($keys as $kid => $data) {
106 $mappings[$kid] = array('project' => (isset($mappings[$kid]) ? $mappings[$kid] : 0));
107 $mappings[$kid]['title'] = $data->title;
108 $mappings[$kid]['domain'] = $data->domain;
109 }
110 return $mappings;
111 }
112
113 /**
114 * Save the project id/services key mapping. Called by settings page.
115 */
116 function casetracker_services_project_mapping_save($form_values = array()) {
117
118 $keys = services_get_keys();
119 $mappings = array();
120 foreach ($keys as $kid => $data) {
121 if (isset($form_values[$kid])) {
122 $mappings[$kid] = $form_values[$kid];
123 }
124 }
125
126 variable_set('casetracker_services_project_mapping', serialize($mappings));
127 }
128
129 /**
130 * Implementation of hook_service()
131 */
132 function casetracker_services_service() {
133 return array(
134 // casetracker.load
135 array(
136 '#method' => 'casetracker.node',
137 '#callback' => 'casetracker_services_node',
138 '#args' => array(
139 array(
140 '#name' => 'case',
141 '#type' => 'string',
142 '#description' => t('A case nid.'))
143 ),
144 '#return' => 'struct',
145 '#help' => t('Returns a Casetracker Case.'),
146 ),
147 array(
148 '#method' => 'casetracker.cases',
149 '#callback' => 'casetracker_services_cases',
150 '#args' => array(
151 array(
152 '#name' => 'filter',
153 '#type' => 'string',
154 '#description' => t("Valid case tracker filter, project will be applied either way."),
155 ),
156 ),
157 '#return' => 'struct',
158 '#help' => t('Returns a list of casetracker cases.'),
159 ),
160 array(
161 '#method' => 'casetracker.log',
162 '#callback' => 'casetracker_services_log',
163 '#args' => array(
164 array(
165 '#name' => 'nid',
166 '#type' => 'array',
167 '#description' => t("'new' for a new case, otherwise a nid."),
168 ),
169 array(
170 '#name' => 'data',
171 '#type' => 'array',
172 '#description' => t("The values required are: 'body', 'title', 'status', 'case_priority_id', 'case_status_id', 'case_type_id'"),
173 ),
174 ),
175 '#return' => 'struct',
176 '#help' => t('Creates a new case.'),
177 ),
178 array(
179 '#method' => 'casetracker.codes',
180 '#callback' => 'casetracker_services_codes',
181 '#return' => 'struct',
182 '#help' => t('Returns status and type codes.'),
183 ),
184 );
185 }
186
187 /**
188 * Returns a specified node.
189 */
190 function casetracker_services_node($nid) {
191 $node = node_load($nid);
192 if ($node->pid != casetracker_services_get_project()) {
193 return t("Accessed denied");
194 }
195
196 $comments = comment_render($node);
197 $node = (array)node_build_content($node, $teaser, $page);
198 $node['comments'] = $comments;
199 return $node;
200 }
201
202 /**
203 * Returns a list of cases.
204 */
205 function casetracker_services_cases($filter) {
206 $project = casetracker_services_get_project();
207
208 $return = array();
209 $rows = array();
210
211 // Would be nice to be able to get the result set from casetracker_cases_overview(),
212 // instead we completely ignore that monster and just use some similar SQL.
213 $query = "SELECT DISTINCT(n.nid), n.title, ncs.last_comment_timestamp, cc.case_number, cc.case_priority_id, cc.case_status_id, cc.case_type_id, cc.assign_to, cp.project_number FROM node n LEFT JOIN casetracker_case cc ON (n.vid = cc.vid) LEFT JOIN casetracker_project cp ON (cp.nid = cc.pid) LEFT JOIN node_comment_statistics ncs ON (n.nid = ncs.nid) WHERE n.status = 1 AND n.type IN ('casetracker_basic_case') AND cc.pid IN (%d) ORDER BY ncs.last_comment_timestamp DESC LIMIT 0, 100";
214 $result = db_query($query, $project);
215 while ($row = db_fetch_array($result)) {
216 $rows[] = $row;
217 }
218 $output['data'] = $rows;
219
220 $lookup = array();
221 $query = "SELECT csid, case_state_name FROM {casetracker_case_states}";
222
223 $result = db_query($query);
224 while ($row = db_fetch_array($result)) {
225 $lookup[$row['csid']] = $row['case_state_name'];
226 }
227 $output['lookup'] = $lookup;
228
229 return $output;
230 }
231
232 /**
233 * Returns a list of CT codes
234 */
235 function casetracker_services_codes() {
236 $project = casetracker_services_get_project();
237
238 $lookup = array();
239 $query = "SELECT csid, case_state_realm, case_state_name FROM {casetracker_case_states}";
240
241 $result = db_query($query);
242 while ($row = db_fetch_array($result)) {
243 $lookup[$row['case_state_realm']][$row['csid']] = $row['case_state_name'];
244 }
245
246 return $lookup;
247 }
248
249
250 /**
251 * Create a case, or add a comment, depending on the presense of a nid.
252 */
253 function casetracker_services_log($nid, $data) {
254 $project = casetracker_services_get_project();
255 if (!$project){
256 return;
257 }
258 $project = node_load($project);
259 $uid = $project->uid;
260
261
262 if ($nid == 'new') {
263 $data = (object)$data;
264 $data->type = 'casetracker_basic_case';
265 $data->uid = $project->uid;
266 $data->pid = $project->nid;
267 $data->status = 1;
268 $data->comment = 2;
269 $data->promote = 0;
270 node_save($data);
271 return $data->nid;
272 }
273 elseif (is_numeric($nid)) {
274 $node = node_load($nid);
275 $account = user_load(array('uid' => $uid));
276 global $user;
277 $user = $account;
278
279 if ($node->pid == $project->nid) {
280 $comment = array();
281 $comment['nid'] = $nid;
282 $comment['revision_id'] = $node->vid;
283 $comment['prid'] = $project->nid;
284 $comment['author'] = $user->name;
285 $comment['case_type_id'] = $data['case_type_id'];
286 $comment['case_status_id'] = $data['case_status_id'];
287 $comment['case_priority_id'] = $data['case_priority_id'];
288 $comment['subject'] = $data['title'];
289 $comment['comment'] = $data['body'];
290 comment_form_validate('comment_form', $comment);
291 if ($errors = form_get_errors()) {
292 watchdog('ctserver', "Errors were generated updating a case from ". $data->url .": ". print_r($errors, true));
293 }
294 $comment['case_title'] = $node->title; // We're not currently doing remote case title changes.
295 comment_form_submit('comment_form', $comment);
296 return $nid;
297 }
298 }
299
300 return false;
301 }
302
303 function db_queryct($query) {
304 $args = func_get_args();
305 array_shift($args);
306 $query = db_prefix_tables($query);
307 if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
308 $args = $args[0];
309 }
310 _db_query_callback($args, TRUE);
311 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
312 watchdog('ct', $query);
313 }

  ViewVC Help
Powered by ViewVC 1.1.2