-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcm_drupal.module
More file actions
196 lines (182 loc) · 4.88 KB
/
cm_drupal.module
File metadata and controls
196 lines (182 loc) · 4.88 KB
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
<?php
/**
* @file
* Module to simplify interaction with
* the KualiCo CM api. Patterned loosely
* after the jQuery ajax functionality
* the basic usage is as follows:
* This module exposes five externally useful functions.
* Four of them are the basic types of requests
* that the CM API accepts namely:
* cm_drupal_get($url)
* cm_drupal_post($url [,$body = null])
* cm_drupal_put($url [,$body = null])
* cm_drupal_delete($url [,$body = null])
*
* The fifth is a basic ajax method where the request
* type is specified in the parameters:
* cm_drupal_ajax($url [, $method = 'GET', $body = null])
*
* Each function returns the JSON response from the
* server.
*/
/**
* Hit api based on provided config values
*
* @param url
* Route to hit. A list of available routes can be found in the API docs at
* {yourdomain}.kuali.co/cm/doc/index.html
* @param method
* HTTP method. Defaults to 'GET'
* @param body
* Payload to send with request. Default to none
*/
function cm_drupal_ajax($url, $method = 'GET', $body = null) {
$url = _cm_drupal_cleanse_url($url);
$context = array(
'http' => array(
'method' => $method,
'header' => 'Authorization: Bearer ' . variable_get('cm_drupal_api_key'),
'body' => $body
)
);
$context = stream_context_create($context);
$response = json_decode(
file_get_contents(
variable_get('cm_drupal_api_url') . $url,
false,
$context
),
TRUE
);
return $response;
}
/**
* Send a 'GET' request
*
* @param url
* Url to hit.
*/
function cm_drupal_get($url) {
return cm_drupal_ajax($url, 'GET');
}
/**
* Send a 'POST' request
*
* @param url
* Url to hit.
*/
function cm_drupal_post($url, $body) {
return cm_drupal_ajax($url, 'POST', $body);
}
/**
* Send a 'PUT' request
*
* @param url
* Url to hit.
*/
function cm_drupal_put($url, $body) {
return cm_drupal_ajax($url, 'PUT', $body);
}
/**
* Send a 'DELETE' request
*
* @param url
* Url to hit.
*/
function cm_drupal_delete($url, $body) {
return cm_drupal_ajax($url, 'DELETE', $body);
}
/**
* Prepares url for request
*
* @param url
* url to cleanse
*/
function _cm_drupal_cleanse_url($url) {
$url = substr($url, 1) == '/' ? substr($url, strlen($url) - 1) : $url;
return $url;
}
/**
* Implements hook_menu()
*
*/
function cm_drupal_menu() {
$items = array();
$items['admin/config/development/cm_drupal'] = array(
'title' => t('CM Drupal'),
'description' => t('Configuration of the cm drupal module'),
'page callback' => 'drupal_get_form',
'page arguments' => array('cm_drupal_config_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function cm_drupal_config_form($form, &$form_state) {
$form['cm_drupal_api_url'] = array(
'#type' => 'textfield',
'#title' => t('API Url'),
'#default_value' => variable_get(
'cm_drupal_api_url',
'example: http://monsters.kuali.co/api/cm'
),
'#size' => 64,
'#description' => t('Enter your API URL'),
'#required' => TRUE
);
$form['cm_drupal_api_key'] = array(
'#type' => 'textarea',
'#title' => t('API Key'),
'#default_value' => t('Manage your API-keys in your KualiCo Auth Profile'),
'#size' => 64,
'#maxlength' => 200,
'#description' => t('Enter your API key'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Implements form_validate()
* Displays hepl and module information
*
* @param form
* Which form is being validated
* @param form_state
* State of the form to be validated.
*/
function cm_drupal_config_form_validate($form, &$form_state) {
$api_key = $form_state['values']['cm_drupal_api_key'];
$api_url = $form_state['values']['cm_drupal_api_url'];
if($api_key === t('Manage your API-keys in your KualiCo Auth Profile')) {
form_set_value($form['cm_drupal_api_key'], variable_get('cm_drupal_api_key', '####NOT A REAL KEY####'), $form_state);
} else if (!_cm_drupal_validate_api_key($form_state)){
form_set_error(
'cm_drupal_api_key',
t('API Key must follow the format: [random characters].[random characters].[random characters]'));
}
if (substr($api_url, -1) !== '/') {
$api_url .= '/';
form_set_value($form['cm_drupal_api_url'], $api_url, $form_state);
}
}
function _cm_drupal_validate_api_key(&$form_state) {
$regex = '/^\S+[.]\S+[.]\S+$/';
return preg_match($regex, $form_state['values']['cm_drupal_api_key']);
}
/**
* Implements hook_help()
* Displays hepl and module information
*
* @param path
* Which path of the site we're using to display this help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function cm_drupal_permission() {
return array(
'acces cm_drupal content' => array(
'title' => t('Access content for the Kualico Student module'),
)
);
}