View source
<?php
define('RULES_FORMS_ADMIN_PATH', 'admin/config/workflow/rules/forms');
function rules_forms_help($path, $arg) {
switch ($path) {
case 'admin/help#rules_forms':
return '<p>' . t('Rules Forms Support provides Rules events, conditions, and actions for any form built with the Drupal form API. To activate events for a form you must first visit <a href="!admin">Rules Forms settings</a> and enable activation messages.', array(
'!admin' => url(RULES_FORMS_ADMIN_PATH),
)) . '</p>';
case RULES_FORMS_ADMIN_PATH:
return '<p>' . t('Settings and overview of form events and active elements.') . '</p>';
}
}
function rules_forms_permission() {
return array(
'administer rules forms rules' => array(
'title' => t('Administer Rules Forms rules'),
'description' => t('Grants access to building rules for forms.'),
),
'administer rules forms' => array(
'title' => t('Administer Rules Forms'),
'description' => t('Grants access to Rules Forms settings and activating or deactivating rules for forms.'),
),
);
}
function rules_forms_menu() {
$items = array();
$items[RULES_FORMS_ADMIN_PATH] = array(
'title' => 'Forms Support',
'description' => 'Manage Rules Forms events, forms, and form element information.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'rules_forms_admin_events',
),
'access arguments' => array(
'administer rules forms',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'file' => 'includes/rules_forms.admin.inc',
);
$items[RULES_FORMS_ADMIN_PATH . '/%/activate/%'] = array(
'title' => 'Activate events for a form',
'type' => MENU_CALLBACK,
'page callback' => 'rules_forms_activate',
'page arguments' => array(
5,
7,
),
'access arguments' => array(
'administer rules forms',
),
'file' => 'includes/rules_forms.admin.inc',
);
return $items;
}
function rules_forms_form_alter(&$form, &$form_state, $form_id) {
$form_info = rules_forms_get_form_info($form_id);
if ($form_info !== FALSE) {
$entity_type = isset($form_state['entity_type']) ? $form_state['entity_type'] : NULL;
$entity = isset($entity_type) ? $form_state[$entity_type] : NULL;
rules_forms_event_build($form, $form_state, $form_id, $entity, $entity_type);
$form['#validate'][] = 'rules_forms_event_validate';
$form['#submit'][] = 'rules_forms_event_submit';
$form['#after_build'][] = 'rules_forms_after_build';
if ($form_info['buttons'] && isset($form_info['submit'])) {
foreach ($form_info['submit'] as $element_id => $label) {
$element =& _rules_forms_get_element($form, $element_id);
$element['#rules_forms_element_id'] = $element_id;
$element['#validate'][] = 'rules_forms_event_button_validate';
$element['#submit'][] = 'rules_forms_event_button_submit';
}
}
}
elseif (!empty($_SESSION['rules_forms_message'])) {
$uri = drupal_get_destination();
$link = l($form_id, RULES_FORMS_ADMIN_PATH . '/' . $form_id . '/activate/' . urlencode(trim($uri['destination'], '/')));
$msg = t('Activate events for !form.', array(
'!form' => $link,
));
drupal_set_message($msg, 'status', FALSE);
}
}
function rules_forms_event_build(&$form, &$form_state, $form_id, $entity, $entity_type) {
$form_state['rules_forms']['form_values'] = array();
$form_info = rules_forms_get_form_info($form_id);
if (isset($form_info['elements'])) {
$form_values = array();
foreach ($form_info['elements'] as $element_id => $info) {
$element = _rules_forms_get_element($form, $element_id);
if (isset($element['#value'])) {
$form_values[$element_id] = $element['#value'];
}
elseif (isset($element['#default_value'])) {
$form_values[$element_id] = $element['#default_value'];
}
else {
$form_values[$element_id] = NULL;
}
}
$form_state['rules_forms']['form_values'] = $form_values;
}
rules_forms_invoke_event('form_built', $form, $form_state, $form_id, $entity, $entity_type);
}
function rules_forms_event_validate(&$form, &$form_state) {
$entity_type = isset($form_state['entity_type']) ? $form_state['entity_type'] : NULL;
$entity = isset($entity_type) ? $form_state[$entity_type] : NULL;
rules_forms_invoke_event('form_validate', $form, $form_state, $entity, $entity_type);
}
function rules_forms_event_button_validate(&$form, &$form_state) {
if (isset($form_state['triggering_element'])) {
$form_id = $form['form_id']['#value'];
$form_info = rules_forms_get_form_info($form_id);
$button = $form_state['triggering_element'];
if (isset($button['#rules_forms_element_id']) && isset($form_info['submit'][$button['#rules_forms_element_id']])) {
$entity_type = isset($form_state['entity_type']) ? $form_state['entity_type'] : NULL;
$entity = isset($entity_type) ? $form_state[$entity_type] : NULL;
rules_forms_invoke_event('button_' . str_replace(':', '_', $button['#rules_forms_element_id']) . '_validate', $form, $form_state, $form_id, $entity, $entity_type);
}
}
}
function rules_forms_event_submit(&$form, &$form_state) {
$entity_type = isset($form_state['entity_type']) ? $form_state['entity_type'] : NULL;
$entity = isset($entity_type) ? $form_state[$entity_type] : NULL;
rules_forms_invoke_event('form_submit', $form, $form_state, $entity, $entity_type);
}
function rules_forms_event_button_submit(&$form, &$form_state) {
if (isset($form_state['triggering_element'])) {
$form_id = $form['form_id']['#value'];
$form_info = rules_forms_get_form_info($form_id);
$button = $form_state['triggering_element'];
if (isset($button['#rules_forms_element_id']) && isset($form_info['submit'][$button['#rules_forms_element_id']])) {
$entity_type = isset($form_state['entity_type']) ? $form_state['entity_type'] : NULL;
$entity = isset($entity_type) ? $form_state[$entity_type] : NULL;
rules_forms_invoke_event('button_' . str_replace(':', '_', $button['#rules_forms_element_id']) . '_submit', $form, $form_state, $form_id, $entity, $entity_type);
}
}
}
function rules_forms_invoke_event($event_type, &$form, &$form_state, $form_id = NULL, $entity = NULL, $entity_type = NULL) {
global $user;
if (empty($form_id)) {
$form_id = $form['form_id']['#value'];
}
$elements = array();
if ($element_ids = rules_forms_get_element_ids($form_id)) {
$elements = array_combine($element_ids, $element_ids);
}
$form_data = new ArrayObject((array) $form);
$form_state_data = new ArrayObject((array) $form_state);
$args = array(
"rules_forms_{$form_id}_{$event_type}",
$form_data,
$form_state_data,
$form_id,
entity_metadata_wrapper($entity_type, $entity),
$user,
) + $elements;
call_user_func_array('rules_invoke_event', $args);
$form = (array) $form_data;
$form_state = (array) $form_state_data;
if (strpos($event_type, 'submit', strlen($event_type) - 7) !== FALSE) {
unset($form_state['rules_forms']);
}
}
function rules_forms_after_build($form, &$form_state) {
$form_id = $form['form_id']['#value'];
$form_info = rules_forms_get_form_info($form_id);
if (isset($form_info) && $form_info !== FALSE && (isset($form_info['reset']) && $form_info['reset'] || (!isset($form_info['elements']) || empty($form_info['elements'])))) {
if (!empty($form_info['reset'])) {
$form_info['reset'] = FALSE;
}
$form_info['elements'] = array();
$form_info['validate'] = array();
$form_info['submit'] = array();
rules_forms_build_elements($form, $form_info);
rules_forms_save_form_info($form_id, $form_info);
drupal_set_message(t('Form elements for %form have been built.', array(
'%form' => $form_info['label'],
)));
}
if (!empty($_SESSION['rules_forms_element_info'])) {
rules_forms_add_popups($form, $form_info['elements']);
drupal_add_css(drupal_get_path('module', 'rules_forms') . '/rules_forms.css');
}
return $form;
}
function rules_forms_build_elements($form, &$form_info, $parent = '') {
foreach (element_children($form) as $key) {
if (isset($form[$key]['#type'])) {
if (isset($form[$key]['#title']) && !in_array($form[$key]['#type'], array(
'hidden',
'token',
)) || $form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button') {
$element_id = $form[$key]['#type'] . ':' . $parent . $key;
$element_key = isset($form[$key]['#title']) ? ucfirst($form[$key]['#title']) : $key;
$form_info['elements'][$element_id] = array(
'type' => $form[$key]['#type'],
'label' => $form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button' ? $form[$key]['#value'] : $element_key,
);
if ($form_info['buttons'] && ($form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button')) {
if (isset($form[$key]['#submit'])) {
$form_info['submit'][$element_id] = isset($form[$key]['#value']) ? $form[$key]['#value'] : $key;
}
}
if (isset($form[$key]['#options'])) {
$form_info['elements'][$element_id]['options'] = $form[$key]['#options'];
}
}
}
rules_forms_build_elements($form[$key], $form_info, $parent . $key . ':');
}
}
function rules_forms_add_popups(&$form, $elements) {
$help = t('Note: The circle symbol indicates an array (multiple value) property.');
foreach ($elements as $element_id => $info) {
$element =& _rules_forms_get_element($form, $element_id);
$attribute = isset($element['#type']) && ($element['#type'] == 'fieldset' || $element['#type'] == 'radio' || $element['#type'] == 'checkbox') ? '#prefix' : '#title';
if (isset($element['#title'])) {
if (isset($element['#type']) && $element['#type'] == 'checkbox') {
$element[$attribute] = '<a href="#" class="rules-forms-inspection-link" style="position:relative;top:22px;right:22px;"></a>';
}
elseif (isset($element['#type']) && $element['#type'] == 'radio') {
$element[$attribute] = '<a href="#" class="rules-forms-inspection-link" style="position:relative;top:28px;right:22px;"></a>';
}
else {
$element[$attribute] = $element['#title'] . '<a href="#" class="rules-forms-inspection-link"></a>';
}
}
else {
$element[$attribute] = ucfirst(str_replace('_', ' ', $element['#type'])) . '<a href="#" class="rules-forms-inspection-link"><span class="rules-forms-inspection-image"></span></a>';
}
$element[$attribute] .= '<div class="rules-forms-element-inspection">' . '<span class="rules-forms-element-label"><h3>' . $info['label'] . '</h3></span>' . '<table width="100%" border="0" cellpadding="2" class="rules-forms-inspection">' . '<thead><tr><th>Attribute</th><th>Value</th></tr></thead>';
$line = 'odd';
$reject_elements = array(
'#node',
'#entity',
);
foreach ($element as $key => $value) {
if ($key !== $attribute && element_property($key) && !in_array($key, $reject_elements)) {
$element[$attribute] .= '<tr class="' . $line . '"><td class="rules-forms-element-type">' . ucfirst(str_replace(array(
'#',
'_',
), array(
'',
' ',
), $key)) . ':</td><td class="rules-forms-element-value">';
_rules_forms_display_info($element[$attribute], $value);
$element[$attribute] .= '</td></tr>';
$line = $line == 'odd' ? 'even' : 'odd';
}
}
$element[$attribute] .= '</table><span class="rules-forms-info-help">' . $help . '</span></div>';
}
}
function _rules_forms_display_info(&$element, $data, $level = 1, $delta = NULL) {
if (is_object($data)) {
$data = (array) $data;
}
elseif (is_array($data) && $level != 3) {
$element .= $delta;
$element .= '<ul>';
foreach ($data as $key => $value) {
$element .= '<li>';
_rules_forms_display_info($element, $value, $level + 1, $key);
$element .= '</li>';
}
$element .= '</ul>';
}
elseif (is_array($data)) {
$element .= '(multiple values)';
}
else {
$value = !empty($delta) ? $delta . ': ' : '';
if ($data === TRUE) {
$data = 'TRUE';
}
elseif ($data === FALSE) {
$data = 'FALSE';
}
elseif ($data === '') {
$data = '(empty)';
}
$value .= $data;
$element .= $value;
}
}
function &_rules_forms_get_element(&$form, $element_id) {
$element_id = substr($element_id, strpos($element_id, ':') + 1);
$names = explode(':', $element_id);
$element =& $form;
foreach ($names as $name) {
if (isset($element[$name])) {
$element =& $element[$name];
}
}
return $element;
}
function &_rules_forms_get_elements(&$form) {
$elements = array();
if ($element_info = rules_forms_get_element_info($form)) {
$elements = array();
foreach ($element_info as $element_id => $info) {
$elements[$element_id] =& _rules_forms_get_element($form, $element_id);
}
return $elements;
}
return $elements;
}
function rules_forms_get_form_info($form_id = NULL) {
$form_info = variable_get('rules_forms_form_info', array());
if ($form_id === NULL) {
return $form_info;
}
if (!is_string($form_id)) {
if (isset($form_id['form_id'])) {
$form_id = $form_id['form_id']['#value'];
}
else {
return FALSE;
}
}
return isset($form_info[$form_id]) ? $form_info[$form_id] : FALSE;
}
function rules_forms_save_form_info($form_id, $form_info, $rebuild_cache = TRUE) {
$event_info = variable_get('rules_forms_form_info', array());
$event_info[$form_id] = $form_info;
variable_set('rules_forms_form_info', $event_info);
if ($rebuild_cache) {
rules_clear_cache();
}
}
function rules_forms_get_element_info($form_id) {
if ($form_info = rules_forms_get_form_info($form_id)) {
return isset($form_info['elements']) ? $form_info['elements'] : FALSE;
}
return FALSE;
}
function rules_forms_get_element_ids($form_id) {
if ($elements = rules_forms_get_element_info($form_id)) {
return array_keys($elements);
}
return FALSE;
}
function rules_forms_activate_form($form_id, $form_info) {
rules_forms_save_form_info($form_id, $form_info, FALSE);
}
function rules_forms_deactivate_form($form_id) {
$form_info = rules_forms_get_form_info();
if (isset($form_info[$form_id])) {
unset($form_info[$form_id]);
}
variable_set('rules_forms_form_info', $form_info);
}