View source
<?php
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.');
}
}
function macro_perm() {
return array(
'administer macro settings',
'macro access',
);
}
function macro_init() {
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));
}
}
function macro_menu() {
$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;
}
function macro_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'macro_export_macro':
break;
case 'macro_admin_settings':
case 'macro_import_macro':
case 'macro_export_macro':
break;
default:
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',
),
);
}
}
if ($form_id != 'macro_import_macro' && variable_get('macro_enabled', FALSE)) {
$form['#submit'][] = 'macro_record_macro';
}
if (variable_get('macro_delete', FALSE)) {
variable_set('macro_submissions', array());
variable_set('macro_delete', FALSE);
}
break;
}
}
function macro_import_action_submit($form, &$form_state) {
drupal_goto('admin/macro/import', drupal_get_destination());
}
function macro_export_action_submit($form, &$form_state) {
variable_set('macro_submissions', array());
macro_record_macro($form, $form_state);
drupal_goto('admin/macro/export');
}
function macro_export_session_action_submit($form, &$form_state) {
variable_set('macro_enabled', TRUE);
variable_set('macro_submissions', array());
}
function macro_record_macro($form, &$form_state) {
$macros = variable_get('macro_submissions', array());
if (isset($form['#parameters'])) {
array_shift($form['#parameters']);
}
$exceptions = array(
'export',
'export-data',
'export-session-data',
'import-data',
'form_id',
'submit',
'reset',
'form_build_id',
'form_token',
'delete',
);
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'],
);
if (isset($form_state['storage'])) {
$macro['storage'] = $form_state['storage'];
}
$macros[] = $macro;
variable_set('macro_submissions', $macros);
return $macro;
}
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;
}
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;
}
function macro_get_macro() {
$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";
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'])) {
array_shift($form['parameters']);
$string .= "\$macro[{$key}]['parameters'] = " . var_export(serialize($form['parameters']), TRUE) . ";\n\n";
}
}
return $string;
}
function macro_get_macro_yaml() {
$subs = variable_get('macro_submissions', array());
include_once libraries_get_path('spyc') . '/spyc.php';
$yaml = '';
$Spyc = new Spyc();
foreach ($subs as $sub) {
if (!empty($sub['values'])) {
$yaml .= $Spyc
->dump($sub['values'], 2, 60);
$yaml .= '---';
}
}
return $yaml;
}
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;
}
function macro_import_macro_submit($form, &$form_state) {
include_once './includes/install.inc';
eval($form_state['values']['macro']);
drupal_execute_macro($macro);
}
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);
}
function macro_end_macro_session() {
variable_set('macro_enabled', FALSE);
drupal_goto('admin/macro/export');
}
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'],
);
if (isset($data['storage'])) {
$form_values['storage'] = $data['storage'];
}
$args = array(
$data['form_id'],
$form_values,
);
$args = array_merge($args, $param);
$results[] = call_user_func_array('drupal_execute', $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;
}