View source
<?php
function mail_debugger_callback($form, &$form_state) {
$form = array(
'#tree' => TRUE,
'tab_group' => array(
'#type' => 'vertical_tabs',
'#default_tab' => user_variable_get('mail_debugger_default_tab'),
),
);
$default_values = user_variable_get('mail_debugger_default_values');
$pages = module_invoke_all('mail_debugger_info');
drupal_alter('mail_debugger_info', $pages);
foreach ($pages as $page => $info) {
if (!is_array($info)) {
continue;
}
$info = $info + array(
'include' => array(),
'title' => NULL,
'form' => NULL,
'validate' => NULL,
'submit' => NULL,
'weight' => 0,
);
if (empty($info['title']) || empty($info['form']) || empty($info['submit'])) {
continue;
}
if (is_array($info['include']) && count($info['include']) >= 3) {
list($type, $module, $name) = $info['include'];
module_load_include($type, $module, $name);
}
if (!function_exists($info['form'])) {
continue;
}
$page_form = call_user_func($info['form'], $default_values[$page]);
if (!$page_form) {
continue;
}
$form[$page] = array(
'#type' => 'fieldset',
'#group' => 'tab_group',
'#title' => $info['title'],
'#weight' => $info['weight'],
);
$form_state['mail_debugger']['submit_callback']["submit-{$page}"] = $info['submit'];
$form_state['mail_debugger']['validate_callback']["submit-{$page}"] = $info['validate'];
$form_state['mail_debugger']['active_element']["submit-{$page}"] = $page;
foreach ($page_form as $var => $data) {
$form[$page][$var] = $data;
$form_state['mail_debugger']['elements'][$page][$var] = "{$page}][{$var}";
}
$form[$page]['submit'] = array(
'#type' => 'submit',
'#name' => "submit-{$page}",
'#value' => t('Send'),
'#submit' => array(
'mail_debugger_callback_submit',
),
'#validate' => array(
'mail_debugger_callback_validate',
),
);
}
return $form;
}
function mail_debugger_callback_submit($form, &$form_state) {
$trigger = $form_state['triggering_element']['#name'];
$page = $form_state['mail_debugger']['active_element'][$trigger];
user_variable_set('mail_debugger_default_tab', "edit-{$page}");
user_variable_set('mail_debugger_default_values', $form_state['values']);
$function_name = $form_state['mail_debugger']['submit_callback'][$trigger];
if (function_exists($function_name)) {
call_user_func($function_name, $form_state['values'][$page], $form_state);
}
}
function mail_debugger_callback_validate($form, &$form_state) {
$trigger = $form_state['triggering_element']['#name'];
$page = $form_state['mail_debugger']['active_element'][$trigger];
if (!is_array($form_state['mail_debugger']['validate_callback'][$trigger])) {
$callbacks = array(
$form_state['mail_debugger']['validate_callback'][$trigger],
);
}
else {
$callbacks = $form_state['mail_debugger']['validate_callback'][$trigger];
}
foreach ($callbacks as $function_name) {
if (!function_exists($function_name)) {
continue;
}
$param = array(
$form_state['values'][$page],
$form_state['mail_debugger']['elements'][$page],
&$form_state,
);
call_user_func_array($function_name, $param);
}
}