t('Ajax Forms Settings'), 'description' => t('Controls which forms should use AJAX submissions.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('ajax_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * Get content types * * @return Assoc */ function ajax_get_types() { $types = array_merge( ajax_types_node(), ajax_types_user()); ajax_invoke('ajax_types', $types); return $types; } /** * Get node types * * @return Assoc */ function ajax_types_node() { $out = array(); $types = node_get_types(); foreach ($types as $k => $v) { $out[$k . '_node_form'] = ucwords('Content Type: ' . $v->name); } return $out; } /** * Get module types. * This function currently unused. * * @return Assoc */ function ajax_types_module() { $out = array(); $funcs = get_defined_functions(); foreach ($funcs['user'] as $f) { if (preg_match("/^([a-zA-Z][a-zA-Z0-9_]*?)_submit$/", $f, $func_name)) { $out[$func_name[1]] = ucwords(str_replace('_', ' ', $func_name[1])); } } return $out; } /** * Get other default types * * @return Assoc */ function ajax_types_user() { return array( 'user_login' => t('User Login'), 'user_edit' => t('User Edit'), 'user_register' => t('User Register') ); } /** * Display admin form * * @return Assoc */ function ajax_admin() { $form = array( 'container_default' => array( '#type' => 'fieldset', '#title' => t('Default Forms'), '#collapsible' => TRUE, '#collapsed' => TRUE, 'ajax_types_default' => array( '#type' => 'select', '#multiple' => TRUE, '#options' => ajax_get_types(), '#default_value' => variable_get('ajax_types_default', array()), '#size' => 20, '#description' => t( "Select the default forms you wish to use AJAX submissions.") ) ), 'container_custom' => array( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Custom Forms (Advanced)'), 'ajax_types_custom' => array( '#type' => 'textarea', '#default_value' => variable_get('ajax_types_custom', ''), '#description' => t( 'Enter the form IDs of the custom forms you wish to ' . 'use AJAX Submissions. Enter one ID per line. Only alpha-numeric ' . 'characters should be used. ') ) ) ); return system_settings_form($form); } /** * hook_init * * @return Bool */ function ajax_init() { drupal_add_js(drupal_get_path('module', 'ajax') . '/Ajax.js', 'module'); return TRUE; } /** * Checks if form is a specified type * * @param $form Assoc * @param $form_id String * @return Bool */ function ajax_is_type($form, $form_id) { // Module defined forms if (array_key_exists('#ajax', $form) && $form['#ajax']) { return TRUE; } // User defined forms else { $exists = array_key_exists($form_id, variable_get('ajax_types_default', array())); if ($exists) { return TRUE; } else { $custom = variable_get('ajax_types_custom', FALSE); if ($custom !== FALSE) { $parts = preg_split("/[\r\n]+/", $custom, -1, PREG_SPLIT_NO_EMPTY); if ($parts) { foreach ($parts as $v) { if (trim($v) === $form_id) { return TRUE; } } } } return FALSE; } } } /** * hook_form_alter * * @param $form Assoc * @param $form_state Assoc * @param $form_id String * @return Bool */ function ajax_form_alter(&$form, $form_state, $form_id) { //watchdog('AJAX Forms', 'Form ID: %form_id', array('%form_id'=>$form_id)); if (ajax_is_type($form, $form_id)) { $found = FALSE; ajax_invoke('ajax_alter', $form, $form_id); ajax_validator_set($form); ajax_submitter_find($form, $found); ajax_submitter_set($form, $found); } return TRUE; } /** * Gets internal path from actual url path * * @param $val String * @return String */ function ajax_drupal_path($val) { $b = base_path(); $bs = sizeof($b); if (substr($val, 0, $bs) === $b) { return substr($val, $bs); } else { return $val; } } /** * Collects path info * * @param $val String * @return Assoc */ function ajax_path_info($val) { $out = array( 'path' => NULL, 'query' => array(), 'fragment' => NULL ); $u = parse_url($val); if (array_key_exists('query', $u)) { parse_str($u, $out['query']); } if (array_key_exists('path', $u)) { $out['path'] = $u['path']; } if (array_key_exists('fragment', $u)) { $out['fragment'] = $u['fragment']; } return $out; } /** * Sets the validator * * @param $form Assoc * @return Bool */ function ajax_validator_set(&$form) { $form['#validate'][] = 'ajax_validator'; return TRUE; } /** * Sets the submitter * * @param $form Assoc * @param $found Bool * @return Bool */ function ajax_submitter_set(&$form, $found) { if (!$found) { $form['#submit'][] = 'ajax_submitter'; } return TRUE; } /** * Finds the submitter * * @param $form Assoc * @param $found Bool * @return Bool */ function ajax_submitter_find(&$form, &$found) { foreach ($form as $form_key => $form_val) { if (is_array($form[$form_key])) { //submit or preview button if ( (array_key_exists('#type', $form[$form_key]) && ($form[$form_key]['#type'] === 'submit' || $form[$form_key]['#type'] === 'button')) && ($form_key === 'submit' || $form_key === 'preview')) { $form[$form_key]['#attributes'] = array('onclick' => 'return Ajax.go(this);'); if (array_key_exists('#submit', $form[$form_key]) && !empty($form[$form_key]['#submit'])) { $form[$form_key]['#submit'][] = 'ajax_submitter'; $found = TRUE; } } //nested else { ajax_submitter_find($form[$form_key], $found); } } } return TRUE; } /** * Gets redirect * Sometimes the redirect can be an array in the form of * 0 => path * 1 => query * 2 => fragment * * @param $redirect String|Array * @return String */ function ajax_get_redirect($redirect) { //watchdog('AJAX Forms', 'Redirect: !redirect', // array('!redirect'=>gettype($redirect[1]))); $args = array(); $args['absolute'] = TRUE; if (is_array($redirect)) { if ($redirect[1] !== NULL) { $args['query'] = $redirect[1]; } if ($redirect[2] !== NULL) { $args['fragment'] = $redirect[2]; } $path = $redirect[0]; } else { $path = $redirect; } return url($path, $args); } /** * Submission handler callback * * @param $form Assoc * @param $form_state Assoc * @return Bool */ function ajax_submitter(&$form, &$form_state) { $data = array(); // Node Preview if (array_key_exists('node_preview', $form_state) && $form_state['node_preview'] !== NULL) { $data['preview'] = $form_state['node_preview']; } // Go to redirection page if (array_key_exists('redirect', $form_state) && $form_state['redirect'] !== NULL) { $data['redirect'] = $form_state['redirect']; } // Display messages internally without redirect else { $messages = drupal_get_messages(NULL, TRUE); if (array_key_exists('status', $messages)) { $data['messages_status'] = $messages['status']; } if (array_key_exists('warning', $messages)) { $data['messages_warning'] = $messages['warning']; } } $out = ajax_build($data); ajax_out($out); return TRUE; } /** * Invokes hooks * * @param unknown_type $view_name * @param unknown_type $display_id * @param unknown_type $out * @return unknown */ function ajax_invoke($hook, &$p1=NULL, &$p2=NULL, &$p3=NULL, &$p4=NULL) { foreach (module_implements($hook) as $name) { $function = $name . '_' . $hook; $result = $function($p1, $p2, $p3, $p4); } return TRUE; } /** * Validation handler callback * * @param $form Assoc * @param $form_state Assoc * @return Bool */ function ajax_validator(&$form, &$form_state) { if (array_key_exists('ajax', $_REQUEST)) { drupal_get_messages(NULL, TRUE); $data = ajax_build(array( 'messages_error' => form_get_errors() )); // FAIL if (!$data['status']) { ajax_invoke('ajax_validate_fail', $form, $form_state, $data); ajax_out($data); } // PASS else { $pass = TRUE; ajax_invoke('ajax_validate_pass', $form, $form_state, $data, $pass); if (!$pass) { ajax_out($data); } } } return TRUE; } /** * Outputs data * * @param $data String * @return Exit */ function ajax_out($data) { header('HTTP/1.1 200 OK', TRUE); header("Content-Type: application/json; charset=UTF-8", TRUE); print drupal_to_js($data); exit; } /** * Gets a clean element id * taken from form_clean_id * * @param $field_id String * @return String */ function ajax_clean_id($field_id) { return str_replace(array('][', '_', ' '), '-', $field_id); } /** * Builds the output object * * @param $data Assoc * @return Assoc */ function ajax_build($data) { $out = array( 'status' => true, 'updaters' => array(), 'debug' => array(), 'messages_error' => array(), 'messages_status' => array(), 'messages_warning' => array(), 'redirect' => NULL, 'preview' => NULL ); // MESSAGE:ERROR if (array_key_exists('messages_error', $data) && $data['messages_error'] !== NULL) { $out['status'] = FALSE; foreach ($data['messages_error'] as $k => $v) { $out['messages_error'][] = array( 'id' => ajax_clean_id("edit-" . $k), 'value' => $v ); } } // MESSAGE:STATUS if (array_key_exists('messages_status', $data) && $data['messages_status'] !== NULL) { foreach ($data['messages_status'] as $k => $v) { $out['messages_status'][] = array( 'id' => (int)$k, 'value' => $v ); } } // MESSAGE:WARNING if (array_key_exists('messages_warning', $data) && $data['messages_warning'] !== null) { foreach ($data['messages_warning'] as $k => $v) { $out['messages_warning'][] = array( 'id' => (int)$k, 'value' => $v ); } } // Redirect: Destination Parameter if (array_key_exists('destination', $_GET)) { $out['redirect'] = ajax_get_redirect($_GET['destination']); } // Redirect: Explicit elseif (array_key_exists('redirect', $data) && $data['redirect'] !== NULL) { $out['redirect'] = ajax_get_redirect($data['redirect']); } // Preview if (array_key_exists('preview', $data) && $data['preview'] !== NULL) { $out['preview'] = rawurlencode($data['preview']); } // Debug if (array_key_exists('debug', $data) && $data['debug'] !== NULL) { $out['debug'] = $data['debug']; } return $out; }