You are here

function _content_lock_add_unload_js in Content locking (anti-concurrent editing) 7.2

Same name and namespace in other branches
  1. 6.2 content_lock.module \_content_lock_add_unload_js()
  2. 6 content_lock.module \_content_lock_add_unload_js()
  3. 7 content_lock.module \_content_lock_add_unload_js()

Add unload js.

Parameters

array $form: Form.

array $form_state: Form state.

Return value

mixed Form array

1 call to _content_lock_add_unload_js()
content_lock_node_ajax_callback in ./content_lock.module
AJAX callback to lock a node manually.
1 string reference to '_content_lock_add_unload_js'
content_lock_form_alter in ./content_lock.module
Implements hook_form_alter().

File

./content_lock.module, line 461
Allows users to lock documents for modification.

Code

function _content_lock_add_unload_js(&$form, $form_state) {
  $m = drupal_get_path('module', 'content_lock');
  drupal_add_js("{$m}/js/jquery.url.packed.js", array(
    'group' => JS_LIBRARY,
  ));
  drupal_add_js("{$m}/js/onUserExit.js", array(
    'group' => JS_LIBRARY,
  ));
  drupal_add_js("{$m}/js/content_lock_init.js");
  $nid = empty($form['nid']['#value']) ? NULL : $form['nid']['#value'];
  $internal_urls = array();
  $internal_form_selectors = array();

  // We're on a locked revision reversion page.
  if (!empty($form['#node_revision']->nid)) {
    $nid = $form['#node_revision']->nid;

    // Don't ask the user if he wants to leave the page when
    // cancelling a reversion.
    $internal_urls[] = $form['actions']['cancel']['#href'];
    $internal_form_selectors[] = '.confirmation';
  }
  $internal_urls[] = 'node/' . $nid . '/edit';
  $internal_form_selectors[] = 'form.node-form';

  // Check lock status.
  $lock = content_lock_fetch_lock($nid);
  if ($lock) {
    $lock_ajax_key = $lock->ajax_key;
  }
  else {
    $lock_ajax_key = FALSE;
  }

  // Get tokens.
  $token = content_lock_get_release_token($nid);

  // Prepare settings.
  $settings = array(
    'nid' => $nid,
    'ajax_key' => $lock_ajax_key,
    'token' => $token,
    'unload_js_message_enable' => variable_get('content_lock_unload_js_message_enable', TRUE),
    'internal_urls' => implode('|', $internal_urls),
    'internal_forms' => implode(', ', $internal_form_selectors),
  );
  if ($settings['unload_js_message_enable']) {
    $settings['unload_js_message'] = variable_get('content_lock_unload_js_message', 'If you proceed, ALL of your changes will be lost.');
  }

  /*
   * Workaround for http://drupal.org/node/1525784 where this function
   * is called multiple times when doing a file field AJAX upload and
   * array_merge_recursive() is used instead of
   * drupal_array_merge_deep_array() to construct the Drupal.settings
   * value. Not calling drupal_add_js() multiple times deprives
   * file_ajax_upload() of the ability to mess up here ;-).
   */
  $called =& drupal_static(__FUNCTION__ . '__called');
  if (!empty($called)) {
    $called++;
    return $form;
  }
  $called = 1;
  drupal_add_js(array(
    'content_lock' => $settings,
  ), 'setting');
  return $form;
}