[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/dialog/ -> dialog.module (source)

   1  <?php
   2  // $Id: dialog.module,v 1.1.2.6 2011/01/31 21:03:40 drewish Exp $
   3  
   4  /**
   5   * Implementation of hook_theme().
   6   */
   7  function dialog_theme() {
   8    return array(
   9      'dialog_content' => array(
  10        'arguments' => array('content' => NULL),
  11        'template' => 'dialog-content',
  12      ),
  13    );
  14  }
  15  
  16  /**
  17   * Implementation of hook_menu().
  18   */
  19  function dialog_menu() {
  20    $items = array();
  21  
  22    $items['admin/settings/dialog'] = array(
  23      'title'            => 'Dialog API',
  24      'description'      => 'Set default attributes for dialogs',
  25      'page callback'    => 'drupal_get_form',
  26      'page arguments'   => array('dialog_admin_settings'),
  27      'access arguments' => array('administer site configuration'),
  28      'file'             => 'dialog.admin.inc',
  29    );
  30  
  31    return $items;
  32  }
  33  
  34  /**
  35   * Add all the necessary javascript (and css) to be able to display a dialog
  36   * on the current page.  This must be used on any page that could possibly
  37   * contain a dialog.  It is safe to call this function repeatedly.
  38   */
  39  function dialog_add_js() {
  40    // Provide a gate so we only do this once.
  41    static $done = FALSE;
  42    if ($done) {
  43      return;
  44    }
  45  
  46    $settings = array(
  47      'Dialog' => array(
  48        'throbber' => theme('image', ctools_image_path('throbber.gif'), t('Loading...'), t('Loading')),
  49        'height' => variable_get('dialog_default_height', 'auto'),
  50        'width' => variable_get('dialog_default_width', '600px'),
  51      )
  52    );
  53  
  54    drupal_add_js($settings, 'setting');
  55    drupal_add_js('misc/jquery.form.js');
  56    ctools_add_js('ajax-responder');
  57  
  58    // Add jquery_ui js and css.
  59    jquery_ui_add(array('ui.core', 'ui.resizable', 'ui.draggable', 'ui.dialog'));
  60  
  61    // Get the correct CSS path based on jQuery UI version.
  62    $version_16 = version_compare(jquery_ui_get_version(), '1.7.0', '<');
  63    $css_path = $version_16 ? 'default' : 'base';
  64    drupal_add_css(JQUERY_UI_PATH .'/themes/'. $css_path .'/ui.all.css');
  65  
  66    // And finally, the dialog js.
  67    drupal_add_js(drupal_get_path('module', 'dialog') .'/dialog.js');
  68  
  69    // Close the gate.
  70    $done = TRUE;
  71  }
  72  
  73  /**
  74   * Place HTML within the modal.
  75   *
  76   * @param $title
  77   *   The title of the modal.
  78   * @param $html
  79   *   The html to place within the modal.
  80   */
  81  function dialog_command_display($title, $html, $options = array()) {
  82    return array(
  83      'command' => 'dialog_display',
  84      'title' => $title,
  85      'output' => theme('dialog_content', $html),
  86      'options' => $options,
  87    );
  88  }
  89  
  90  /**
  91   * Dismiss the modal.
  92   */
  93  function dialog_command_dismiss() {
  94    return array(
  95      'command' => 'dialog_dismiss',
  96    );
  97  }
  98  
  99  /**
 100   * Display loading screen in the modal
 101   */
 102  function dialog_command_loading() {
 103    return array(
 104      'command' => 'dialog_loading',
 105    );
 106  }
 107  
 108  /**
 109   * Perform a simple modal render and immediately exit.
 110   *
 111   * This is primarily used for error displays, since usually modals will
 112   * contain forms.
 113   */
 114  function dialog_ajax_render($title, $output, $options = array()) {
 115    ctools_include('ajax');
 116    $commands = array();
 117    $commands[] = dialog_command_display($title, $output, $options);
 118    ctools_ajax_render($commands);
 119  }
 120  
 121  /**
 122   * Wrap a form so that we can use it properly with AJAX. Essentially if the
 123   * form wishes to render, it automatically does that, otherwise it returns
 124   * so we can see submission results.
 125   *
 126   * @return
 127   *   The output of the form, if it was rendered. If $form_state['ajax']
 128   *   is set, this will use ctools_modal_form_render so it will be
 129   *   a $command object suitable for ctools_ajax_render already.
 130   *
 131   *   The return will be NULL if the form was successfully submitted unless
 132   *   you specifically set re_render = TRUE. If ajax is set the
 133   *   form will never be redirected.
 134   */
 135  function dialog_form_wrapper($form_id, &$form_state) {
 136    ctools_include('form');
 137    // This won't override settings already in.
 138    $form_state += array(
 139      're_render' => FALSE,
 140      'no_redirect' => !empty($form_state['ajax']),
 141    );
 142  
 143    $output = ctools_build_form($form_id, $form_state);
 144    if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
 145      return dialog_form_render($form_state, $output);
 146    }
 147  
 148    return $output;
 149  }
 150  
 151  /**
 152   * Render a form into an AJAX display.
 153   */
 154  function dialog_form_render($form_state, $output) {
 155    $title = empty($form_state['title']) ? '' : $form_state['title'];
 156  
 157    // If there are messages for the form, render them.
 158    if ($messages = theme('status_messages')) {
 159      $output = $messages . $output;
 160    }
 161  
 162    $commands = array();
 163    if (isset($form_state['js settings'])) {
 164      $commands[] = ctools_ajax_command_settings($form_state['js settings']);
 165    }
 166  
 167    $commands[] = dialog_command_display($title, $output);
 168    return $commands;
 169  }
 170  
 171  
 172  /**
 173   * Generic dialog replacement for drupal_get_form().  Suitable for use as the
 174   * page callback in a menu item.
 175   *
 176   * This function introduces a new form callback function to handle the
 177   * post-submit dialog commands, in the ajax context.  This function takes the
 178   * form of form_id_dialog_success.  If this function is found, it will be
 179   * automatically called after a valid submission of the form has been detected.
 180   * If the function does not exist, a redirect will be issued based on the
 181   * redirect value in the form_state array.  As the final fallback, if the
 182   * redirect value is missing or empty, a client-side reload command is issued.
 183   *
 184   * @param $id
 185   *   The form_id that would normally be passed to drupal_get_form.
 186   * @param $js
 187   *   The %ctools_js wildcard parameter to specify when the call is being made
 188   *   in a javascript context.
 189   * @param ...
 190   *   Any additional parameters will be passed on to the form builder function.
 191   */
 192  function dialog_get_form($form_id, $js) {
 193    $args = func_get_args();
 194    $form_id = array_shift($args);
 195    $js = array_shift($args);
 196  
 197    if ($js) {
 198      ctools_include('ajax');
 199      $form_state = array(
 200        'ajax' => TRUE,
 201        'title' => drupal_get_title(),
 202        'args' => $args,
 203      );
 204      $output = dialog_form_wrapper($form_id, $form_state);
 205      if (empty($output)) {
 206        $func = $form_id .'_dialog_success';
 207        if (function_exists($func)) {
 208          $output = $func($form_state);
 209        }
 210        else if (!empty($form_state['redirect'])) {
 211          $output[] = ctools_ajax_command_redirect($form_state['redirect']);
 212        }
 213        else {
 214          $output[] = ctools_ajax_command_reload();
 215        }
 216      }
 217      ctools_ajax_render($output);
 218    }
 219    else {
 220      return drupal_get_form($form_id);
 221    }
 222  }
 223  
 224  /**
 225   * Process variables for dialog-content.tpl.php.
 226   */
 227  function template_preprocess_dialog_content(&$variables) {
 228    $variables['help'] = theme('help');
 229    $variables['messages'] = theme('status_messages');
 230  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7