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

Contents of /contributions/modules/activecollab/activecollab.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Dec 3 01:46:26 2008 UTC (11 months, 3 weeks ago) by greenskin
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1, DRUPAL-6--2
File MIME type: text/x-php
Initial commit of ActiveCollab.
1 <?php
2 // $Id$
3
4 /**
5 * activecollab.module@file
6 * Allows modules to use the activeCollab API.
7 *
8 * This module allows other modules to use the activeCollab API. Each user can
9 * set their own API Key in their account profile, and each activeCollab user
10 * has their own API status meaning if the API is enabled for them and if they
11 * have 'read-only' or 'read and write' privileges.
12 *
13 * Written by greenSkin. Development sponsored by the It Is Written.
14 * http://www.itiswritten.org
15 */
16
17 /*******************************************************************************
18 * Hook Functions (Drupal)
19 ******************************************************************************/
20
21 /**
22 * Implementation of hook_perm().
23 */
24 function activecollab_perm() {
25 return array(
26 'use activeCollab API',
27 'administer activeCollab'
28 );
29 }
30
31 /**
32 * Implementation of hook_menu().
33 */
34 function activecollab_menu() {
35 $items = array();
36
37 $items['admin/settings/activecollab'] = array(
38 'title' => t('activeCollab settings'),
39 'page callback' => 'drupal_get_form',
40 'page arguments' => array('activecollab_admin_settings'),
41 'access arguments' => array('administer activeCollab'),
42 'type' => MENU_NORMAL_ITEM
43 );
44 $items['admin/settings/activecollab/general'] = array(
45 'title' => t('General'),
46 'description' => t('General activeCollab settings.'),
47 'type' => MENU_DEFAULT_LOCAL_TASK,
48 'weight' => -10
49 );
50 $items['admin/content/activecollab'] = array(
51 'title' => t('activeCollab'),
52 'page callback' => 'activecollab_overview',
53 'access arguments' => array('administer activeCollab'),
54 'type' => MENU_NORMAL_ITEM
55 );
56 $items['admin/content/activecollab/overview'] = array(
57 'title' => t('Overview'),
58 'description' => t('An overview of activeCollab'),
59 'type' => MENU_DEFAULT_LOCAL_TASK
60 );
61 $items['activecollab/ajax/call'] = array(
62 'page callback' => 'activecollab_ajax_call',
63 'access arguments' => array('use activeCollab API'),
64 'type' => MENU_CALLBACK
65 );
66
67 return $items;
68 }
69
70 /**
71 * Implementation of hook_user().
72 */
73 function activecollab_user($op, &$edit, &$account, $category = NULL) {
74 if ($url = variable_get('activecollab_url', '') && (user_access('use activeCollab API') || user_access('administer activeCollab'))) {
75 if ($op == 'load') {
76 if ($activecollab = db_fetch_object(db_query("SELECT * FROM {activecollab} WHERE uid = %d", $account->uid))) {
77 $account->activeCollab = $activecollab;
78 } else if (variable_get('activecollab_public', '')) {
79 $account->activeCollab->api_key = variable_get('activecollab_public', '');
80 }
81 }
82
83 if ($op == 'form' && $category == 'activecollab') {
84 $form['api_key_fieldset'] = array(
85 '#type' => 'fieldset',
86 '#title' => t('API Key'),
87 '#collapsible' => ($account->activeCollab->api_key) ? TRUE : FALSE,
88 '#collapsed' => TRUE
89 );
90 $form['api_key_fieldset']['api_key'] = array(
91 '#type' => 'textfield',
92 '#title' => t('activeCollab API Key'),
93 '#description' => t('Enter your API Key for activeCollab. To find your API Key view your profile !here.', array('!here' => variable_get('activecollab_url', ''))),
94 '#default_value' => $account->activeCollab->api_key,
95 '#required' => TRUE
96 );
97 return $form;
98 }
99
100 if ($op == 'validate' && $category == 'activecollab') {
101
102 }
103
104 if ($op == 'submit' && $category == 'activecollab') {
105 $edit['api_key'] = trim($edit['api_key']);
106 }
107
108 if ($op == 'update' && $category == 'activecollab') {
109 db_query("UPDATE {activecollab} SET api_key = '%s' WHERE uid = %d", $edit['api_key'], $account->uid);
110 if (db_affected_rows() <= 0) {
111 db_query("INSERT INTO {activecollab} (uid, api_key) VALUES (%d, '%s')", $account->uid, $edit['api_key']);
112 }
113 unset($edit['api_key']);
114 }
115
116 if ($op == 'categories') {
117 return array(
118 array(
119 'name' => 'activecollab',
120 'title' => t('activeCollab'),
121 'weight' => 2
122 )
123 );
124 }
125 }
126 }
127
128 /**
129 * Implementation of hook_init();
130 */
131 function activecollab_init() {
132 global $user;
133
134 if (!$user->activeCollab) {
135 $user = user_load(array('uid' => $user->uid));
136 }
137 $url = variable_get('activecollab_url', '');
138 $api_key = $user->activeCollab->api_key;
139 if ($url && $api_key) {
140 $settings = array(
141 'url' => base_path() .'activecollab/ajax/call'
142 );
143 drupal_add_js(array('activeCollab' => $settings), 'setting');
144 drupal_add_js(drupal_get_path('module', 'activecollab') .'/js/global.js', 'module');
145 }
146 }
147
148 /*******************************************************************************
149 * Callback Functions, Forms, and Tables
150 ******************************************************************************/
151
152 function activecollab_admin_settings() {
153 $form['activecollab_url'] = array(
154 '#type' => 'textfield',
155 '#title' => t('activeCollab URL'),
156 '#description' => t('Specify the URL which points to index.php for activeCollab.'),
157 '#default_value' => variable_get('activecollab_url', ''),
158 '#required' => TRUE
159 );
160 $form['activecollab_public'] = array(
161 '#type' => 'textfield',
162 '#title' => t('Public API key'),
163 '#description' => t('Specify the API key for users without an API key set.'),
164 '#default_value' => variable_get('activecollab_public', '')
165 );
166 return system_settings_form($form);
167 }
168
169 function activecollab_admin_settings_validate($form, &$form_state) {
170 $url = preg_replace('/\/$/', '', $form_state['values']['activecollab_url']);
171 form_set_value($form['activecollab_url'], $url, $form_state);
172 }
173
174 function activecollab_overview() {
175 return 'overview';
176 }
177
178 function activecollab_ajax_call() {
179 $call = $_POST['call'];
180 $vars = array();
181 foreach ($_POST as $key => $value) {
182 if ($key != 'call') {
183 if ($key == 'type') {
184 $type = $value;
185 } else {
186 $vars[$key] = $value;
187 }
188 }
189 }
190
191 if (!empty($vars)) {
192 $post = array();
193 foreach ($vars as $key => $value) {
194 $cur_key = $type .'['. $key .']';
195 $post[$cur_key] = $value;
196 }
197 } else {
198 $post = FALSE;
199 }
200
201 $result = activecollab_call($call, 'json', $post);
202 if ($result) {
203 echo $result;
204 exit();
205 }
206 echo $result;
207 exit();
208 }
209
210 /*******************************************************************************
211 * Module and Helper Functions
212 ******************************************************************************/
213
214 function activecollab_call($call, $format, $post = FALSE) {
215 static $activecollab = array();
216
217 if (!empty($activecollab[$format][$call]) && !$post) {
218 return $activecollab[$format][$call];
219 }
220
221 if ($url = variable_get('activecollab_url', '')) {
222 global $user;
223
224 if (!$user->activeCollab->api_key) {
225 $user = user_load(array('uid' => $user->uid));
226 }
227
228 $headers = array(
229 'Accept' => 'application/'. $format,
230 'Content-Type' => 'application/x-www-form-urlencoded'
231 );
232 $method = 'GET';
233 $variables = array();
234 $data = NULL;
235 if ($post) {
236 $variables['submitted'] = 'submitted';
237 $variables+= $post;
238 // $data = implode('&', $variables);
239 $data = http_build_query($variables, '', '&');
240 $method = 'POST';
241 }
242
243 $full_call = $url . $call .'?token='. $user->activeCollab->api_key;
244 if ($result = drupal_http_request($full_call, $headers, $method, $data)) {
245 if ($result->headers['Content-Type'] == 'text/html; charset=utf-8' && $result->error) {
246 watchdog('activeCollab', 'There was an error making the request to activeCollab: @error', array('@error' => $result->error), WATCHDOG_ERROR);
247 return FALSE;
248 }
249 switch ($format) {
250 case 'xml':
251 if ($result->headers['Content-Type'] == 'application/xml; charset=utf-8') {
252 $xml = $result->data;
253 $activecollab[$format][$call] = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
254 return $activecollab[$format][$call];
255 }
256 break;
257 case 'json':
258 if ($result->headers['Content-Type'] == 'application/json; charset=utf-8') {
259 $json = $result->data;
260 $activecollab[$format][$call] = $json;
261 return $activecollab[$format][$call];
262 }
263 break;
264 }
265 return FALSE;
266 }
267 watchdog('activeCollab', 'There was a problem calling activeCollab using: @url', array('@url' => $full_call), WATCHDOG_ERROR);
268 return FALSE;
269 }
270 watchdog('activeCollab', 'Call to activeCollab was attempted without a URL set.', array(), WATCHDOG_WARNING, l(t('Set activeCollab URL'), 'admin/settings/activecollab'));
271 return FALSE;
272 }

  ViewVC Help
Powered by ViewVC 1.1.2