-
Notifications
You must be signed in to change notification settings - Fork 1
/
macro.module
451 lines (381 loc) · 13.9 KB
/
macro.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* @file
* - allow administrators to record (export) form submissions
* - allow administrators to replay (import) form submissions
*/
/**
* Implementation of hook_help().
*/
function macro_help($section) {
switch ($section) {
case 'admin/help#macro':
$output = t('Todo: Add help text.');
case 'admin/macro/export' :
return t('This output can be saved to the profile`s .macro file, to be automatically played back upon completed install or used on an import on another site.');
case 'admin/macro/import' :
return t('Insert recorded macro here to be played into your site. All referenced modules needs to be enabled.');
case 'admin/macro':
return t('Configuration settings for the drupal macro engine. The export/import functions are currently not working and will be ported soon.');
}
}
/**
* Implementation of hook_perm().
*/
function macro_perm() {
return array('administer macro settings', 'macro access');
}
/**
* Implementation of hook_init().
*/
function macro_init() {
macro_delete();
if (empty($_POST) && variable_get('macro_enabled', FALSE)) {
$args = array(
'%d' => count(variable_get('macro_submissions', array())),
'!link' => l('End session', 'admin/macro/export/session/end')
);
drupal_set_message(t('[Active macros: %d | !link]', $args));
}
}
/**
* Implementation of hook_menu().
*/
function macro_menu() {
macro_delete();
$items = array();
$items['admin/macro'] = array(
'title' => 'Macro engine',
'description' => 'Configure the Drupal macro engine. Export recorded macros or import previously recorded macros.',
'page callback' => 'drupal_get_form',
'page arguments' => array('macro_admin_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer macro settings'),
);
// $items['admin/macro/export'] = array(
// 'title' => 'Export',
// 'page callback' => 'drupal_get_form',
// 'page arguments' => array('macro_export_macro'),
// 'access arguments' => array('macro access'),
// 'type' => MENU_LOCAL_TASK,
// );
// $items['admin/macro/import'] = array(
// 'title' => 'Import',
// 'page callback' => 'drupal_get_form',
// 'page arguments' => array('macro_import_macro'),
// 'access arguments' => array('macro access'),
// 'type' => MENU_LOCAL_TASK,
// );
$items['admin/macro/settings'] = array(
'title' => 'Configure',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/macro/export/session/end'] = array(
'page callback' => 'macro_end_macro_session',
'access arguments' => array('macro access'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Deletes Macros if variable delete_macro is set
*/
function macro_delete() {
// Clear the current sessions.
if (variable_get('macro_delete', FALSE)) {
variable_set('macro_submissions', array());
variable_set('macro_delete', FALSE);
}
}
/**
* Implementation of hook_form_alter().
*/
function macro_form_alter(&$form, $form_state, $form_id) {
// Clear the current sessions.
macro_delete();
switch ($form_id) {
case 'macro_export_macro':
/** TODO: eventually add file saving with foldering options.
$form['file-save'] = array('#type' => 'fieldset', '#title' => 'Save To File', '#description' => '');
$form['file-save']['filename'] = array('#type' => 'textfield', '#title' => 'Filename');
$form['file-save]['save'] = array('#type' => 'submit', '#value' => 'Save', '#submit' => array('macro_export_file_save_submit'));
*/
break;
// Forms to specifically ignore for macro.
case 'macro_admin_settings': case 'macro_import_macro': case 'macro_export_macro':
break;
default:
// Add import / export buttons to each form for simplified macro saving.
if (user_access('macro access') && variable_get('macro_display_actions', FALSE)) {
$form['macro-actions'] = array('#type' => 'fieldset',
'#title' => t('Macro actions'),
'#weight' => 5000,
'#collapsible' => TRUE,
'#collapsed' => TRUE);
$form['macro-actions']['import-data'] = array('#type' => 'submit',
'#name' => 'import',
'#value' => t('Import'),
'#submit' => array('macro_import_action_submit'));
$form['macro-actions']['export-data'] = array('#type' => 'submit',
'#name' => 'export',
'#value' => t('Export'),
'#submit' => array('macro_export_action_submit'));
if (!variable_get('macro_enabled', FALSE)) {
$form['macro-actions']['export-session-data'] = array('#type' => 'submit',
'#value' => t('Start session'),
'#submit' => array('macro_export_session_action_submit'));
}
}
// Add the record callback on submit.
if ($form_id != 'macro_import_macro' && variable_get('macro_enabled', FALSE)) {
$form['#submit'][] = 'macro_record_macro';
}
break;
}
}
/**
* Form submit handler to redirect to the import form.
*/
function macro_import_action_submit($form, &$form_state) {
drupal_goto('admin/macro/import', drupal_get_destination());
}
/**
* Form callback to handle macro export functionality.
*/
function macro_export_action_submit($form, &$form_state) {
// Start a fresh session.
variable_set('macro_submissions', array());
// Record the single macro.
macro_record_macro($form, $form_state);
// Send straight to the export form.
drupal_goto('admin/macro/export');
}
/**
* Form callback to handle macro export session functionality.
*/
function macro_export_session_action_submit($form, &$form_state) {
// Start recording submissions and clear the saved submissions for a fresh session.
variable_set('macro_enabled', TRUE);
variable_set('macro_submissions', array());
}
/**
* A form submission handler, that stores the form submissions into the variables table
*/
function macro_record_macro($form, &$form_state) {
$macros = variable_get('macro_submissions', array());
// Remove the $form_state as it will be rebuilt on import.
if (isset($form['#parameters']))
array_shift($form['#parameters']);
// TODO: Why is it when the record method is called through the $form['#submit'] when
// the action buttons are not displayed do these exception values not show up in the 'values' ?
$exceptions = array('export', 'export-data', 'export-session-data', 'import-data',
'form_id', 'submit', 'reset', 'form_build_id', 'form_token', 'delete');
// Remove the unneeded values that this module implements.
foreach ($exceptions as $exception) {
unset($form_state['values'][$exception]);
}
$macro = array(
'form_id' => $form['form_id']['#value'],
'path' => $_GET['q'],
'parameters' => isset($form['#parameters']) ? $form['#parameters'] : array(),
'values' => $form_state['values'],
);
// Support for multistep.
if (isset($form_state['storage'])) {
$macro['storage'] = $form_state['storage'];
}
$macros[] = $macro;
variable_set('macro_submissions', $macros);
return $macro;
}
/**
* This recursively runs thru an object and converts it into an array.
* This is to be called for form entries as we do not want varexport to treat any element
* as an object. If varexport sees an object, it will output stdClass::__set_state, which is
* not defined and we cannot define it either. So we recursively cast all objects to arrays.
*/
function _macro_recursively_convert_objects_to_arrays($entity) {
$converted = array();
foreach (((array) $entity) as $key => $value) {
if (is_array($value) || is_object($value)) {
$converted[$key] = _macro_recursively_convert_objects_to_arrays($value);
}
else {
$converted[$key] = $value;
}
}
return ($converted);
}
/**
* A form callback that displays the macro exported.
*
* The output of this callback should be saved to the profiles/$profile/macros.inc file, to be
* automatically played back upon completed install.
* @return a textarea containing the recorded macros
*/
function macro_export_macro() {
$form['code'] = array(
'#type' => 'textarea',
'#title' => 'macros exported',
'#default_value' => macro_get_macro(),
'#rows' => 20,
);
$form['save'] = array (
'#type' => 'submit',
'#value' => 'submit',
);
return $form;
}
/**
* Returns the captured form(s)
*
* @return Array the captured form(s)
*/
function macro_get_macro() {
return variable_get('macro_submissions', array());
}
/**
* The output of this callback should be saved to the profiles/$profile/macros.inc file, to be
* automatically played back upon completed install.
*
* @return a code representation of the recorded macro.
*/
function macro_get_macro_php() {
$subs = variable_get('macro_submissions', array());
$string = '';
foreach ($subs as $key => $form) {
$string .= "\$macro[$key]['form_id'] = " . var_export($form['form_id'], TRUE) . ";\n";
$string .= "\$macro[$key]['path'] = " . var_export($form['path'], TRUE) . ";\n";
$string .= "\$macro[$key]['values'] = " . var_export(_macro_recursively_convert_objects_to_arrays((array) $form['values']), TRUE) . ";\n";
// Add multistep support.
if (isset($form['storage'])) {
$string .= "\$macro[$key]['storage'] = " . var_export(_macro_recursively_convert_objects_to_arrays((array) $form['storage']), TRUE) . ";\n";
}
if (isset($form['parameters'])) {
// the form parameters are being used here.
array_shift($form['parameters']);
$string .= "\$macro[$key]['parameters'] = " . var_export(serialize($form['parameters']), TRUE) . ";\n\n";
}
}
return $string;
}
/**
* As macro_get_macro, but the output is a YAML file
*
* @return YAML string representation of the recorded macro.
*/
function macro_get_macro_yaml() {
$subs = variable_get('macro_submissions', array());
include_once libraries_get_path('spyc') . '/spyc.php';
$yaml = '';
$Spyc = new Spyc();
// Create a String from the YAML array
foreach ($subs as $sub) {
if (!empty($sub['values'])) {
$yaml .= $Spyc->dump($sub['values'], 2, 60);
$yaml .= '---';
}
}
return $yaml;
}
/**
* A form callback that displays the macro import form.
*
* @return a form for importing a previously recorded macro
*/
function macro_import_macro() {
$form['macro'] = array(
'#type' => 'textarea',
'#title' => 'macro to import',
'#rows' => 20,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('play macro'),
);
return $form;
}
/**
* Implementation of macro_import_macro hook_submit function.
*
* Plays back the submitted macro.
*/
function macro_import_macro_submit($form, &$form_state) {
include_once './includes/install.inc';
eval($form_state['values']['macro']);
drupal_execute_macro($macro);
}
/**
* Menu callback for the macro settings form.
*/
function macro_admin_settings() {
$form['settings_general'] = array(
'#type' => 'fieldset',
'#title' => t('Macro settings'),
'#collapsible' => TRUE,
);
$form['settings_general']['macro_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable macro recording'),
'#default_value' => variable_get('macro_enabled', FALSE),
'#description' => t('Set whether the macro engine will record form submissions.'),
);
$form['settings_general']['macro_display_actions'] = array(
'#type' => 'checkbox',
'#title' => 'Display Actions',
'#description' => 'Add "import / export" buttons at the bottom of each form that is displayed.',
'#default_value' => variable_get('macro_display_actions', FALSE),
);
$form['settings_general']['macro_delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete recorded macro'),
'#default_value' => variable_get('macro_delete', FALSE),
'#description' => t('Set whether to clear previously recorded macro.'),
);
return system_settings_form($form);
}
/**
* End a macro session from a page callback with a redirect to the export form.
*/
function macro_end_macro_session() {
variable_set('macro_enabled', FALSE);
drupal_goto('admin/macro/export');
}
/**
* Attempts to programmatically submit all the forms that have been specified in the $macros collection.
*
* @param array
* - a list of macros to execute
*
* @return array
* - a list of results based on the macros provided.
*/
function drupal_execute_macro($macro) {
foreach ($macro as $key => $data) {
$item = menu_get_item($data['path']);
if ($item && !empty($item['file'])) {
include_once $item['file'];
}
}
$results = array();
foreach ($macro as $key => $data) {
$param = unserialize($data['parameters']);
$form_values = array('values' => $data['values']);
// Support for multistep.
if (isset($data['storage'])) {
$form_values['storage'] = $data['storage'];
}
$args = array($data['form_id'], $form_values);
$args = array_merge($args, $param);
// TODO: fix bug
// Warning: Parameter 2 to drupal_form_submit() expected to be a reference,
// value given in drupal_execute_macro()
// (line 431 of /var/www/drupal-7.9/sites/all/modules/Macro/macro.module).
$results[] = call_user_func_array('drupal_form_submit', $args);
if (form_get_errors()) {
drupal_set_message(t("An error has occured with macro #%macro_number , form_id %form_id. Please check the errors displayed for more details.", array('%macro_number' => $key, '%form_id' => $data['form_id'])));
}
}
return $results;
}