forked from stevem/Connect
/
connect_admin.php
364 lines (322 loc) · 12.2 KB
/
connect_admin.php
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
// $Id: connect_admin.php,v 1.3.2.2 2010/01/27 20:07:14 stevem Exp $
/**** FORM: select campaign functions ****/
function connect_node_functions_form() {
drupal_add_css(drupal_get_path('module', 'connect') .'/connect.css');
$child = array();
$options = array();
$p_nid = arg(1);
$parent = node_load($p_nid);
// determine possible and enabled actions
$requirements_OK = TRUE;
$actions = connect_get_actions($parent->nid);
$action_list = module_invoke_all('connect');
unset($action_list['connect_action_basic']);
foreach ($action_list as $function=>$action) {
$status = '';
if (in_array($function, $actions)) {
if (TRUE === _connect_hook_check_requirements($parent, $child, $function, 'parent')) {
$status = ' ' . theme_image(drupal_get_path('module', 'connect') . '/images/accept.png', '(ACTIVE)', 'This function is active.');
}
else {
$requirements_OK = FALSE;
$status = ' ' . theme_image(drupal_get_path('module', 'connect') . '/images/exclamation.png', '(INACTIVE)', 'This function is not active. Please check the settings tab.');
}
}
$options[$function] = $action['title'] . $status . '<p class="connect-comment">' . $action['desc'] . '</p>';
}
if (!$requirements_OK) {
drupal_set_message('One or more of your selected functions requires additional settings to be configured.<br />Please check the settings tab for details.' , 'error');
}
$form = array();
$form['parent_id'] = array(
'#type' => 'value',
'#value' => $parent->nid,
);
$form['connect_actions'] = array(
'#type' => 'fieldset',
'#title' => 'Choose functions to apply to this campaign',
);
$form['connect_actions']['actions'] = array(
'#type' => 'checkboxes',
'#title' => '',
'#options' => $options,
'#default_value' => $actions,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function connect_node_functions_form_submit($form, &$form_state) {
// save action options
$options = array();
foreach( $form_state['values']['actions'] as $key=>$value ) {
if ("$key" == "$value") {
$options[] = $value;
}
}
// restore mandatory action
$options[] = 'connect_action_basic';
connect_node_options( $form_state['values']['parent_id'], 'connect_actions', $options );
drupal_set_message('Your selected functions have been updated.');
}
/**** FORM: settings ****/
function connect_node_settings_form() {
drupal_add_css(drupal_get_path('module', 'connect') .'/connect.css');
$form = array();
$child = NULL;
$p_nid = arg(1);
$parent = node_load($p_nid);
$actions = connect_get_actions($parent->nid);
//var_dump($actions);
// test for requirements
$requirements_message = '';
foreach ($actions as $function) {
$this_OK = _connect_hook_check_requirements($parent, $child, $function, 'parent');
if ($this_OK !== TRUE) $requirements_message .= $this_OK;
}
if (!empty($requirements_message)) {
$requirements_message = 'The following items need to to be configured for your actions to work properly:<br />'. $requirements_message;
drupal_set_message($requirements_message);
}
// store the parent nid
$form['parent_id'] = array(
'#type' => 'value',
'#value' => $parent->nid,
);
// required variables
$map = connect_get_map($parent->nid);
$required = connect_get_required_vars($parent, $child);
//var_dump($map);
//var_dump($required);
if (!empty($required['variables'])) {
$form['campaign_variables'] = array('#tree' => TRUE);
foreach ( $required['variables'] as $action=>$vars ) {
$description = $action_list[$action];
$form['campaign_variables']["variables_$action"] = array(
'#type' => 'fieldset',
'#title' => $description['title'],
);
foreach( $vars as $key=>$formitem ) {
$form['campaign_variables']["variables_$action"][$key] = $formitem;
}
if ($action == 'connect_action_basic') {
$form['campaign_variables']["variables_$action"]['#weight'] = -1;
}
}
}
// parent node fields
//var_dump($required['parent']);
if ( !empty($required['parent']) ) {
$options = connect_get_node_fields($parent->type);
$form['variables_parent'] = array(
'#type' => 'fieldset',
'#title' => 'Required fields in parent/campaign node',
'#tree' => TRUE,
);
$form['variables_parent']['message'] = array(
'#value' => '<em>'. t('The selected functions store or use information from the campaign/parent node. Please identify which fields in the parent node correspond to the function settings below.') .'</em>',
);
foreach ( $required['parent'] as $key=>$desc ) {
$form['variables_parent'][$key] = array(
'#type' => 'select',
'#title' => $desc,
'#options' => $options,
'#default_value' => isset($map[$key]) ? $map[$key]: '',
'#required' => TRUE,
);
}
}
// child node fields
if (connect_node_options($parent->nid, 'participant_type')) {
if (!empty($required['child'])) {
//var_dump($required['child']);
$form['variables_child'] = array(
'#type' => 'fieldset',
'#title' => 'Required fields in child/participant node',
'#tree' => TRUE,
);
$form['variables_child']['message'] = array(
'#value' => '<em>'. t('The selected functions store or use information from the participant/child node. Please identify which fields in the child node correspond to the function settings below.') .'</em>',
);
$child_type = connect_node_options( $parent->nid, 'participant_type' );
$options = connect_get_node_fields($child_type);
foreach ( $required['child'] as $key=>$desc ) {
$form['variables_child'][$key] = array(
'#type' => 'select',
'#title' => $desc,
'#options' => $options,
'#default_value' => isset($map[$key]) ? $map[$key]: '',
'#required' => TRUE,
);
}
}
}
else {
$form['variables_child']['message'] = array(
'#value' => t('Please select a participant type and return here to set up your variables.'),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
//var_dump($form);
return $form;
}
function connect_node_settings_form_validate($form, &$form_state) {
$child = NULL;
$parent->nid = $form_state['values']['parent_id']; // required by connect_call_hooks
$parent->data = $form_state['values'];
connect_call_hooks($parent, $child, 'admin-validate', 'parent');
}
// handle nested arrays
function _connect_set_values_from_form($item, $parent_id) {
foreach ($item as $key=>$value) {
if (is_array($value)) {
_connect_set_values_from_form($value, $parent_id);
}
else {
connect_node_options($parent_id, $key, $value);
}
}
}
function connect_node_settings_form_submit($form, &$form_state) {
// save overall settings
foreach ($form_state['values']['campaign_variables'] as $function=>$vars) {
_connect_set_values_from_form($vars, $form_state['values']['parent_id']);
}
// save variable->field mapping
$options = array();
foreach ( array('variables_parent','variables_child') as $formitem) {
if (isset($form_state['values'][$formitem])) {
foreach ($form_state['values'][$formitem] as $key=>$value) {
if ( !empty($value) ) {
$options[$key] = $value;
}
}
}
}
connect_node_options( $form_state['values']['parent_id'], 'connect_map', $options );
$null = drupal_get_messages(NULL); // clear bogus _connect_hook_check_requirements errors
drupal_set_message('The campaign configuration has been updated.');
}
/**** FORM: connect module administration ****/
function connect_admin_form() {
// grab all node types, format for use in form
$sql = "SELECT type FROM {node_type};";
$result = db_query($sql);
while ($row = db_fetch_object($result)) {
$type_options[$row->type] = $row->type;
}
// remove some standard node types
$remove = array('blog', 'forum', 'page', 'story');
$type_options = array_diff($type_options, $remove);
$form = array();
if (!empty($type_options)) {
// participant node types
$form['connect_participant_nodes'] = array(
'#type' => 'checkboxes',
'#title' => t('Which node types hold participant/child information?'),
'#options' => $type_options,
'#required' => TRUE,
'#default_value' => variable_get('connect_participant_nodes', array()),
);
// parent node types
$form['connect_parent_nodes'] = array(
'#type' => 'checkboxes',
'#title' => t('Which node types can be used as campaign/parent nodes?'),
'#options' => $type_options,
'#required' => TRUE,
'#default_value' => variable_get('connect_parent_nodes', array()),
);
// captcha
$form['connect_captcha_required'] = array(
'#type' => 'radios',
'#title' => t('Should connect require a CAPTCHA?'),
'#options' => array( 'yes' => 'Yes', 'no' => 'No' ),
'#default_value' => variable_get('connect_captcha_required', 'yes'),
);
// cache settings
$form['connect_cache'] = array(
'#value' => 'Define the timeout value for your cached data using the format "999 X", where "999" is an integer and "X" is one of mhd, for minutes, hours, days. Leave blank, or set the number to zero (0) to set an unlimited cache lifetime.',
);
require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
$cache_names = _connect_get_cache_names();
foreach ($cache_names as $key=>$title) {
$form['connect_cache']["connect_cache_$key"] = array(
'#type' => 'textfield',
'#title' => $title,
'#size' => 10,
'#default_value' => variable_get("connect_cache_$key", ''),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
}
else {
$form['message'] = array(
'#value' => t('No node types have been defined yet. Please set up some content types and return here to set up Connect.'),
);
}
return $form;
}
function connect_admin_form_validate($form, &$form_state) {
// parent node type cannot be a child node type as well
$test = array_intersect_assoc($form_state['values']['connect_participant_nodes'], $form_state['values']['connect_parent_nodes']);
foreach ($test as $key=>$val) {
if (empty($val)) {
unset($test[$key]);
}
}
if (!empty($test)) {
form_set_error('', 'A node type cannot be both a parent and a child node.');
}
// validate cache timeouts
require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
$cache_names = _connect_get_cache_names();
$regex = '/^[0-9]{1,}\ [mhd]$/';
foreach ($cache_names as $key=>$title) {
$field_value = $form_state['values']["connect_cache_$key"];
if (!empty($field_value) && !preg_match($regex,$field_value)) {
form_set_error("connect_cache_$key", 'Please specify cache lifetime in the "999 X" format.');
}
}
}
function connect_admin_form_submit($form, &$form_state) {
// save participant node info
$data = array();
foreach ($form_state['values']['connect_participant_nodes'] as $key=>$value) {
if ($key === $value) {
$data[] = $value;
}
}
if (! empty($data)) {
variable_set('connect_participant_nodes', $data);
}
// save parent node info
$data = array();
foreach ($form_state['values']['connect_parent_nodes'] as $key=>$value) {
if ($key === $value) {
$data[] = $value;
}
}
if (! empty($data)) {
variable_set('connect_parent_nodes', $data);
}
// captcha
$captcha = $form_state['values']['connect_captcha_required'] == 'no' ? 'no' : 'yes';
variable_set('connect_captcha_required', $captcha);
// cache timeouts
require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
$cache_names = _connect_get_cache_names();
foreach ($cache_names as $key=>$title) {
$interval = empty($form_state['values']["connect_cache_$key"]) ? 0 : $form_state['values']["connect_cache_$key"];
variable_set("connect_cache_$key", $interval);
}
drupal_set_message(t('The connect settings have been updated.'));
}