12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- <?php
- function ajax_render($commands = array()) {
-
-
-
-
-
- foreach (array('css', 'js') as $type) {
-
-
-
-
- if (empty($_POST['ajax_page_state'][$type])) {
- $items[$type] = array();
- }
- else {
- $function = 'drupal_add_' . $type;
- $items[$type] = $function();
- drupal_alter($type, $items[$type]);
-
-
-
-
-
-
- foreach ($items[$type] as $key => $item) {
- if (is_numeric($key)) {
- unset($items[$type][$key]);
- }
- }
-
- $items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);
- }
- }
-
-
-
-
- if (isset($items['js']['settings'])) {
- unset($items['js']['settings']);
- }
- $styles = drupal_get_css($items['css'], TRUE);
- $scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
- $scripts_header = drupal_get_js('header', $items['js'], TRUE);
- $extra_commands = array();
- if (!empty($styles)) {
- $extra_commands[] = ajax_command_prepend('head', $styles);
- }
- if (!empty($scripts_header)) {
- $extra_commands[] = ajax_command_prepend('head', $scripts_header);
- }
- if (!empty($scripts_footer)) {
- $extra_commands[] = ajax_command_append('body', $scripts_footer);
- }
- if (!empty($extra_commands)) {
- $commands = array_merge($extra_commands, $commands);
- }
-
- $scripts = drupal_add_js();
- if (!empty($scripts['settings'])) {
- $settings = $scripts['settings'];
- array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));
- }
-
- drupal_alter('ajax_render', $commands);
- return drupal_json_encode($commands);
- }
- function ajax_get_form() {
- $form_state = form_state_defaults();
- $form_build_id = $_POST['form_build_id'];
-
- $form = form_get_cache($form_build_id, $form_state);
- if (!$form) {
-
-
-
-
-
- watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING);
- drupal_exit();
- }
-
- $form_state['no_redirect'] = TRUE;
-
-
-
- $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
- $form_state['rebuild_info']['copy']['#action'] = TRUE;
-
-
- $form_state['input'] = $_POST;
- $form_id = $form['#form_id'];
- return array($form, $form_state, $form_id, $form_build_id);
- }
- function ajax_form_callback() {
- list($form, $form_state) = ajax_get_form();
- drupal_process_form($form['#form_id'], $form, $form_state);
-
-
-
-
-
-
- if (!empty($form_state['triggering_element'])) {
- $callback = $form_state['triggering_element']['#ajax']['callback'];
- }
- if (!empty($callback) && function_exists($callback)) {
- return $callback($form, $form_state);
- }
- }
- function ajax_base_page_theme() {
- if (!empty($_POST['ajax_page_state']['theme']) && !empty($_POST['ajax_page_state']['theme_token'])) {
- $theme = $_POST['ajax_page_state']['theme'];
- $token = $_POST['ajax_page_state']['theme_token'];
-
-
-
-
-
- if ($theme === variable_get('theme_default', 'bartik') || drupal_valid_token($token, $theme)) {
- return $theme;
- }
- }
- }
- function ajax_deliver($page_callback_result) {
-
-
-
-
-
-
-
- $iframe_upload = !empty($_POST['ajax_iframe_upload']);
-
-
- if (is_null(drupal_get_http_header('Content-Type'))) {
- if (!$iframe_upload) {
-
-
-
- drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
- }
- else {
-
-
- drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
- }
- }
-
- $commands = ajax_prepare_response($page_callback_result);
- $json = ajax_render($commands);
- if (!$iframe_upload) {
-
-
- print $json;
- }
- else {
-
-
-
-
-
-
- print '<textarea>' . $json . '</textarea>';
- }
-
- ajax_footer();
- }
- function ajax_prepare_response($page_callback_result) {
- $commands = array();
- if (!isset($page_callback_result)) {
-
-
- }
- elseif (is_int($page_callback_result)) {
- switch ($page_callback_result) {
- case MENU_NOT_FOUND:
- $commands[] = ajax_command_alert(t('The requested page could not be found.'));
- break;
- case MENU_ACCESS_DENIED:
- $commands[] = ajax_command_alert(t('You are not authorized to access this page.'));
- break;
- case MENU_SITE_OFFLINE:
- $commands[] = ajax_command_alert(filter_xss_admin(variable_get('maintenance_mode_message',
- t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal'))))));
- break;
- }
- }
- elseif (is_array($page_callback_result) && isset($page_callback_result['#type']) && ($page_callback_result['#type'] == 'ajax')) {
-
-
- $page_callback_result += element_info('ajax');
- $error = $page_callback_result['#error'];
- if (isset($error) && $error !== FALSE) {
- if ((empty($error) || $error === TRUE)) {
- $error = t('An error occurred while handling the request: The server received invalid input.');
- }
- $commands[] = ajax_command_alert($error);
- }
- else {
- $commands = $page_callback_result['#commands'];
- }
- }
- else {
-
-
-
-
-
-
- $html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);
- $commands[] = ajax_command_insert(NULL, $html);
-
-
- $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
- }
- return $commands;
- }
- function ajax_footer() {
-
-
-
- if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
- module_invoke_all('exit');
- }
-
-
- if (function_exists('drupal_session_commit')) {
- drupal_session_commit();
- }
- }
- function ajax_process_form($element, &$form_state) {
- $element = ajax_pre_render_element($element);
- if (!empty($element['#ajax_processed'])) {
- $form_state['cache'] = TRUE;
- }
- return $element;
- }
- function ajax_pre_render_element($element) {
-
- if (isset($element['#ajax_processed'])) {
- return $element;
- }
-
- $element['#ajax_processed'] = FALSE;
-
- if (!(isset($element['#ajax']['callback']) || isset($element['#ajax']['path']))) {
- return $element;
- }
-
- if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {
- switch ($element['#type']) {
- case 'submit':
- case 'button':
- case 'image_button':
-
-
-
-
-
- $element['#ajax']['event'] = 'mousedown';
-
-
-
- $element['#ajax']['keypress'] = TRUE;
-
-
-
-
-
-
-
- if (!isset($element['#ajax']['prevent'])) {
- $element['#ajax']['prevent'] = 'click';
- }
- break;
- case 'password':
- case 'textfield':
- case 'textarea':
- $element['#ajax']['event'] = 'blur';
- break;
- case 'radio':
- case 'checkbox':
- case 'select':
- $element['#ajax']['event'] = 'change';
- break;
- case 'link':
- $element['#ajax']['event'] = 'click';
- break;
- default:
- return $element;
- }
- }
-
- if (isset($element['#ajax']['event'])) {
- $element['#attached']['library'][] = array('system', 'jquery.form');
- $element['#attached']['library'][] = array('system', 'drupal.ajax');
- $settings = $element['#ajax'];
-
- $settings += array(
- 'path' => 'system/ajax',
- 'options' => array(),
- );
-
- if (isset($settings['method']) && $settings['method'] == 'replace') {
- $settings['method'] = 'replaceWith';
- }
-
- $settings['url'] = url($settings['path'], $settings['options']);
- unset($settings['path'], $settings['options']);
-
-
-
-
- if (isset($settings['trigger_as'])) {
-
-
-
-
-
-
- $settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
- if (isset($settings['trigger_as']['value'])) {
- $settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
- }
- unset($settings['trigger_as']);
- }
- elseif (isset($element['#name'])) {
-
-
- $settings['submit']['_triggering_element_name'] = $element['#name'];
-
-
-
- if (isset($element['#button_type']) && empty($element['#has_garbage_value'])) {
- $settings['submit']['_triggering_element_value'] = $element['#value'];
- }
- }
-
- if (isset($settings['progress']) && is_string($settings['progress'])) {
- $settings['progress'] = array('type' => $settings['progress']);
- }
-
- if (isset($settings['progress']['path'])) {
- $settings['progress']['url'] = url($settings['progress']['path']);
- unset($settings['progress']['path']);
- }
- $element['#attached']['js'][] = array(
- 'type' => 'setting',
- 'data' => array('ajax' => array($element['#id'] => $settings)),
- );
-
- $element['#ajax_processed'] = TRUE;
- }
- return $element;
- }
- function ajax_command_alert($text) {
- return array(
- 'command' => 'alert',
- 'text' => $text,
- );
- }
- function ajax_command_insert($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => NULL,
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_replace($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'replaceWith',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_html($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'html',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_prepend($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'prepend',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_append($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'append',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_after($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'after',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_before($selector, $html, $settings = NULL) {
- return array(
- 'command' => 'insert',
- 'method' => 'before',
- 'selector' => $selector,
- 'data' => $html,
- 'settings' => $settings,
- );
- }
- function ajax_command_remove($selector) {
- return array(
- 'command' => 'remove',
- 'selector' => $selector,
- );
- }
- function ajax_command_changed($selector, $asterisk = '') {
- return array(
- 'command' => 'changed',
- 'selector' => $selector,
- 'asterisk' => $asterisk,
- );
- }
- function ajax_command_css($selector, $argument) {
- return array(
- 'command' => 'css',
- 'selector' => $selector,
- 'argument' => $argument,
- );
- }
- function ajax_command_settings($argument, $merge = FALSE) {
- return array(
- 'command' => 'settings',
- 'settings' => $argument,
- 'merge' => $merge,
- );
- }
- function ajax_command_data($selector, $name, $value) {
- return array(
- 'command' => 'data',
- 'selector' => $selector,
- 'name' => $name,
- 'value' => $value,
- );
- }
- function ajax_command_invoke($selector, $method, array $arguments = array()) {
- return array(
- 'command' => 'invoke',
- 'selector' => $selector,
- 'method' => $method,
- 'arguments' => $arguments,
- );
- }
- function ajax_command_restripe($selector) {
- return array(
- 'command' => 'restripe',
- 'selector' => $selector,
- );
- }
|