-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhosting_saas_utils.module
executable file
·281 lines (242 loc) · 10.1 KB
/
hosting_saas_utils.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Implementation of hook_menu().
*/
function hosting_saas_utils_menu() {
$items['admin/hosting/saas_utils'] = array(
'title' => 'SaaS utilities',
'description' => 'Configure software-as-a-service utilities',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_saas_utils_settings'),
'access arguments' => array('administer sites'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Configuration form for hosting_saas_utils
*
* @see hosting_saas_utils_settings_submit
*/
function hosting_saas_utils_settings($form, &$form_state) {
/* New user / uid1 */
$user_options = array(1 => t('New user (recommended)'), 0 => t('Admin user (uid1, has all permissions)'));
$form['hosting_saas_utils_create_new_user'] = array(
'#type' => 'radios',
'#title' => t('Client user'),
'#default_value' => variable_get('hosting_saas_utils_create_new_user', 1),
'#options' => $user_options,
'#description' => t('The user to use on the new site.'),
);
$form['hosting_saas_utils_user_email'] = array(
'#type' => 'textfield',
'#title' => t('Client user email variable'),
'#description' => t("Enter the variable name that will contain the client user's email. Leave empty to not create or edit a user."),
'#default_value' => variable_get('hosting_saas_utils_user_email', ''),
'#required' => FALSE,
);
$form['hosting_saas_utils_user_name'] = array(
'#type' => 'textfield',
'#title' => t('Client user name variable'),
'#description' => t("Enter the variable name that will contain the client user's name. Will default to the email variable's value."),
'#default_value' => variable_get('hosting_saas_utils_user_name', ''),
'#required' => FALSE,
);
$form['hosting_saas_utils_user_role'] = array(
'#type' => 'textfield',
'#title' => t('Client user role to assign'),
'#description' => t("Enter the name of the role to assign to the client user. Leave empty for none."),
'#default_value' => variable_get('hosting_saas_utils_user_role', NULL),
'#required' => FALSE,
);
$form['hosting_saas_utils_send_email'] = array(
'#type' => 'checkbox',
'#title' => t('Send client user a registration email.'),
'#description' => t('This will use the default user registration message. You can set the message in your template site or enter it in the Variables tab of the site node.'),
'#default_value' => variable_get('hosting_saas_utils_send_email', 1),
);
_hosting_saas_utils_append_variables($form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
// Can't use system_settings_form() because of the dynamic variables array.
return $form;
}
/**
* Helper function to add dynamically generated variable fields.
*/
function _hosting_saas_utils_append_variables(&$form, &$form_state) {
$form['hosting_saas_utils_variables_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Variables'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t("Values to assign to site variables based on POST variables. This allows a 'bridge' from your subscription form to a site's variable. Exemple: whateveryouwant => site_name."),
'#prefix' => '<div id="hosting-saas-utils-fieldset-wrapper">',
'#suffix' => '</div>',
);
if (!isset($form_state['hosting_saas_utils_variables'])) {
$variables = variable_get('hosting_saas_utils_variables', array());
foreach ($variables as $key => $values) {
$form_state['hosting_saas_utils_variables'][] = array('key' => $values['key'], 'name' => $values['name']);
}
$form_state['hosting_saas_utils_variables'][] = array('key' => '', 'name' => '');
}
else {
foreach ($form_state['hosting_saas_utils_variables'] as $counter => $variable) {
if (isset($form_state['values']['hosting_saas_utils_variable_key_' . $counter])) {
if ($form_state['hosting_saas_utils_variables'][$counter]['key'] <> $form_state['values']['hosting_saas_utils_variable_key_' . $counter] || $form_state['hosting_saas_utils_variables'][$counter]['name'] <> $form_state['values']['hosting_saas_utils_variable_name_' . $counter]) {
$form_state['hosting_saas_utils_variables'][$counter]['key'] = $form_state['values']['hosting_saas_utils_variable_key_' . $counter];
}
if ($form_state['hosting_saas_utils_variables'][$counter]['name'] <> $form_state['values']['hosting_saas_utils_variable_name_' . $counter]) {
$form_state['hosting_saas_utils_variables'][$counter]['name'] = $form_state['values']['hosting_saas_utils_variable_name_' . $counter];
}
}
}
}
foreach ($form_state['hosting_saas_utils_variables'] as $counter => $variable) {
_hosting_saas_utils_append_variable_fieldset($form, $counter, $variable['key'], $variable['name']);
}
_hosting_saas_utils_append_variable_fieldset($form, $counter);
$form['hosting_saas_utils_variables_fieldset']['add_variable'] = array(
'#type' => 'submit',
'#value' => t('Add more'),
'#submit' => array('hosting_saas_utils_add_variable'),
'#ajax' => array(
'callback' => 'hosting_saas_utils_ajax_callback',
'wrapper' => 'hosting-saas-utils-fieldset-wrapper',
),
);
}
/**
* Helper function to add a variable set fieldset
*/
function _hosting_saas_utils_append_variable_fieldset(&$form, $index, $variable_key = '', $variable_name = '') {
$form['hosting_saas_utils_variables_fieldset'][$index] = array(
'#type' => 'fieldset',
'#title' => t('Variable set'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
// TODO: If key is supplied, value is mandatory.
$form['hosting_saas_utils_variables_fieldset'][$index]['hosting_saas_utils_variable_key_' . $index] = array(
'#type' => 'textfield',
'#title' => t('Variable POST key'),
'#description' => t("This is the arbitrary name of the POST argument you will give the variable."),
'#default_value' => $variable_key,
'#required' => FALSE,
);
$form['hosting_saas_utils_variables_fieldset'][$index]['hosting_saas_utils_variable_name_' . $index] = array(
'#type' => 'textfield',
'#title' => t('Drupal variable name'),
'#description' => t("This is the name of the variable that will be set in the new site using variable_set()."),
'#default_value' => $variable_name,
'#required' => FALSE,
);
}
function hosting_saas_utils_settings_submit(&$form, &$form_state) {
$variables = array();
foreach ($form_state['hosting_saas_utils_variables'] as $index => $val) {
// index is just the number of the child
$key = $form_state['values']['hosting_saas_utils_variable_key_' . $index];
$name = $form_state['values']['hosting_saas_utils_variable_name_' . $index];
if (!empty($key) && !empty($name)) {
$variables[$index] = array('key' => $key, 'name' => $name);
}
}
variable_set('hosting_saas_utils_variables', $variables);
$settings_to_save = array(
'create_new_user',
'user_email',
'user_name',
'user_role',
'send_email',
);
foreach ($settings_to_save as $setting) {
$variable_name = 'hosting_saas_utils_' . $setting;
variable_set($variable_name, $form_state['values'][$variable_name]);
}
}
/**
* Callback for both ajax-enabled buttons.
*
* Selects and returns the fieldset with the names in it.
*/
function hosting_saas_utils_ajax_callback($form, $form_state) {
return $form['hosting_saas_utils_variables_fieldset'];
}
/**
* Submit handler for the "add-more-variable" button.
*
* Add empty variable and causes a rebuild.
*/
function hosting_saas_utils_add_variable($form, &$form_state) {
$form_state['hosting_saas_utils_variables'][] = array('name' => '', 'key' => '');
$form_state['rebuild'] = TRUE;
}
/**
* Imlements hook_hosting_restapi_check_access() to store POST data.
*/
function hosting_saas_utils_hosting_restapi_task_added($task) {
$bridged_values = variable_get('hosting_saas_utils_bridged_values', array());
$variables = variable_get('hosting_saas_utils_variables', array());
$site_variables = array();
foreach ($variables as $values) {
if (!empty($_POST[$values['key']])) {
$site_variables[$values['name']] = $_POST[$values['key']];
}
}
if (!empty($site_variables)) {
$bridged_values[$task->task_args['new_uri']] = $site_variables;
variable_set('hosting_saas_utils_bridged_values', $bridged_values);
}
_hosting_task_utils_generate_todo($task->task_args['new_uri']);
}
/**
* Helper function to set variable array.
*/
function _hosting_task_utils_generate_todo($uri) {
$todo = variable_get('hosting_saas_utils_todo', array());
$client_user_email_key = variable_get('hosting_saas_utils_user_email', '');
$client_user_name_key = variable_get('hosting_saas_utils_user_name', '');
if (!empty($client_user_email_key) && !empty($_POST[$client_user_email_key])) {
if (!empty($client_user_name_key)) {
$client_user = array(
'name' => $_POST[$client_user_name_key],
'email' => $_POST[$client_user_email_key],
);
}
else {
$client_user = array(
'name' => $_POST[$client_user_email_key],
'email' => $_POST[$client_user_email_key],
);
}
if (variable_get('hosting_saas_utils_create_new_user', 1)) {
$todo[$uri]['create_new_user'] = $client_user;
}
else {
$todo[$uri]['change_admin_user'] = $client_user;
}
$role_name = variable_get('hosting_saas_utils_user_role', '');
if (!empty($role_name)) {
$todo[$uri]['set_user_role'] = $role_name;
}
$send_email = variable_get('hosting_saas_utils_send_email', FALSE);
if (!empty($send_email)) {
$todo[$uri]['send_email'] = TRUE;
}
}
variable_set('hosting_saas_utils_todo', $todo);
}
// hook_insert
function hosting_saas_utils_node_insert($node) {
$bridged_values = variable_get('hosting_saas_utils_bridged_values', array());
if ($node->type == 'site' && !empty($bridged_values[$node->title])) {
hosting_variables_set_site_variables($node->nid, $bridged_values[$node->title]);
// This variable is meant to be temporary
unset($bridged_values[$node->title]);
variable_set('hosting_saas_utils_bridged_values', $bridged_values);
}
}