123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317 |
- <?php
- function ajax_render($commands = array()) {
-
-
- ajax_set_verification_header();
-
-
-
-
-
- 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_add_css($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(drupal_array_merge_deep_array($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();
- }
-
-
-
-
- $commands = array();
- if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
-
- $commands[] = ajax_command_update_build_id($form);
- $form_build_id = $form['#build_id'];
- }
-
- $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, $commands);
- }
- function ajax_form_callback() {
- list($form, $form_state, $form_id, $form_build_id, $commands) = 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) && is_callable($callback)) {
- $result = $callback($form, $form_state);
- if (!(is_array($result) && isset($result['#type']) && $result['#type'] == 'ajax')) {
-
- $result = array(
- '#type' => 'ajax',
- '#commands' => ajax_prepare_response($result),
- );
- }
- $result['#commands'] = array_merge($commands, $result['#commands']);
- return $result;
- }
- }
- 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');
- }
- }
-
- ajax_set_verification_header();
-
- $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_set_verification_header() {
- $added = &drupal_static(__FUNCTION__);
-
-
-
-
-
- if (empty($added)) {
- drupal_add_http_header('X-Drupal-Ajax-Token', '1');
-
- $added = TRUE;
- }
- }
- 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),
- 'urlIsAjaxTrusted' => array(
- $settings['url'] => TRUE,
- ),
- ),
- );
-
- $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,
- );
- }
- function ajax_command_update_build_id($form) {
- return array(
- 'command' => 'updateBuildId',
- 'old' => $form['#build_id_old'],
- 'new' => $form['#build_id'],
- );
- }
- function ajax_command_add_css($styles) {
- return array(
- 'command' => 'add_css',
- 'data' => $styles,
- );
- }
|