Projet

Général

Profil

Paste
Télécharger (7,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / modal.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 * Implement a modal form using AJAX.
6
 *
7
 * The modal form is implemented primarily from mc.js; this contains the
8
 * Drupal specific stuff to use it. The modal is fairly generic and can
9
 * be activated mostly by setting up the right classes, but if you are
10
 * using the modal you must include links to the images in settings,
11
 * because the javascript does not inherently know where the images are.
12
 *
13
 * You can accomplish this with this PHP code:
14
 * @code {
15
 *   ctools_include('modal');
16
 *   ctools_modal_add_js();
17
 * }
18
 *
19
 * You can have links and buttons bound to use the modal by adding the
20
 * class ctools-use-modal.
21
 *
22
 * For links, the href of the link will be the destination, with any
23
 * appearance of /nojs/ converted to /ajax/.
24
 *
25
 * For submit buttons, however, the URL is found a different, slightly
26
 * more complex way. The ID of the item is taken and -url is appended to
27
 * it to derive a class name. Then, all form elements that contain that
28
 * class name are founded and their values put together to form a URL.
29
 *
30
 * For example, let's say you have an 'add' button, and it has a select
31
 * form item that tells your system what widget it is adding. If the id
32
 * of the add button is edit-add, you would place a hidden input with
33
 * the base of your URL in the form and give it a class of 'edit-add-url'.
34
 * You would then add 'edit-add-url' as a class to the select widget
35
 * allowing you to convert this value to the form without posting.
36
 *
37
 * If no URL is found, the action of the form will be used and the entire
38
 * form posted to it.
39
 */
40

    
41
function ctools_modal_add_js() {
42
  // Provide a gate so we only do this once.
43
  static $done = FALSE;
44
  if ($done) {
45
    return;
46
  }
47

    
48
  $settings = array(
49
    'CToolsModal' => array(
50
      'loadingText' => t('Loading...'),
51
      'closeText' => t('Close Window'),
52
      'closeImage' => theme('image', array(
53
        'path' => ctools_image_path('icon-close-window.png'),
54
        'title' => t('Close window'),
55
        'alt' => t('Close window'),
56
      )),
57
      'throbber' => theme('image', array(
58
          'path' => ctools_image_path('throbber.gif'),
59
          'title' => t('Loading...'),
60
          'alt' => t('Loading'),
61
      )),
62
    ),
63
  );
64

    
65
  drupal_add_js($settings, 'setting');
66
  drupal_add_library('system', 'jquery.form');
67
  drupal_add_library('system', 'drupal.progress');
68
  drupal_add_library('system', 'drupal.ajax');
69
  ctools_add_js('modal');
70

    
71
  ctools_add_css('modal');
72
  $done = TRUE;
73
}
74

    
75
/**
76
 * @todo this is deprecated
77
 */
78
function ctools_modal_add_plugin_js($plugins) {
79
  $css = array();
80
  $js = array(drupal_get_path('module', 'ctools') . '/js/dependent.js' => TRUE);
81
  foreach ($plugins as $subtype) {
82
    if (isset($subtype['js'])) {
83
      foreach ($subtype['js'] as $file) {
84
        if (file_exists($file)) {
85
          $js[$file] = TRUE;
86
        }
87
        else if (file(exists($subtype['path'] . '/' . $file))) {
88
          $js[$subtype['path'] . '/' . $file] = TRUE;
89
        }
90
      }
91
    }
92
    if (isset($subtype['css'])) {
93
      foreach ($subtype['css'] as $file) {
94
        if (file_exists($file)) {
95
          $css[$file] = TRUE;
96
        }
97
        else if (file(exists($subtype['path'] . '/' . $file))) {
98
          $css[$subtype['path'] . '/' . $file] = TRUE;
99
        }
100
      }
101
    }
102
  }
103

    
104
  foreach (array_keys($js) as $file) {
105
    drupal_add_js($file);
106
  }
107
  foreach (array_keys($css) as $file) {
108
    drupal_add_css($file);
109
  }
110
}
111

    
112
/**
113
 * Place HTML within the modal.
114
 *
115
 * @param $title
116
 *   The title of the modal.
117
 * @param $html
118
 *   The html to place within the modal.
119
 */
120
function ctools_modal_command_display($title, $html) {
121
  if (is_array($html)) {
122
    $html = drupal_render($html);
123
  }
124

    
125
  return array(
126
    'command' => 'modal_display',
127
    'title' => $title,
128
    'output' => $html,
129
  );
130
}
131

    
132
/**
133
 * Dismiss the modal.
134
 */
135
function ctools_modal_command_dismiss() {
136
  return array(
137
    'command' => 'modal_dismiss',
138
  );
139
}
140

    
141
/**
142
 * Display loading screen in the modal
143
 */
144
function ctools_modal_command_loading() {
145
  return array(
146
    'command' => 'modal_loading',
147
  );
148
}
149

    
150
/**
151
 * Render an image as a button link. This will automatically apply an AJAX class
152
 * to the link and add the appropriate javascript to make this happen.
153
 *
154
 * @param $image
155
 *   The path to an image to use that will be sent to theme('image') for rendering.
156
 * @param $dest
157
 *   The destination of the link.
158
 * @param $alt
159
 *   The alt text of the link.
160
 * @param $class
161
 *   Any class to apply to the link. @todo this should be a options array.
162
 */
163
function ctools_modal_image_button($image, $dest, $alt, $class = '') {
164
  return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class, 'ctools-use-modal');
165
}
166

    
167
/**
168
 * Render text as a link. This will automatically apply an AJAX class
169
 * to the link and add the appropriate javascript to make this happen.
170
 *
171
 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
172
 * not use user input so this is a very minor concern.
173
 *
174
 * @param $text
175
 *   The text that will be displayed as the link.
176
 * @param $dest
177
 *   The destination of the link.
178
 * @param $alt
179
 *   The alt text of the link.
180
 * @param $class
181
 *   Any class to apply to the link. @todo this should be a options array.
182
 */
183
function ctools_modal_text_button($text, $dest, $alt, $class = '') {
184
  return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
185
}
186

    
187
/**
188
 * Wrap a form so that we can use it properly with AJAX. Essentially if the
189
 * form wishes to render, it automatically does that, otherwise it returns
190
 * the render array so we can see submission results.
191

    
192
 * @param array $form
193
 *   An associative array containing the structure of the form.
194
 * @param array $form_state
195
 *   An associative array containing the current state of the form.
196
 *   If the 'reset_html_ids' key is set to TRUE, it will prevent HTML IDs in
197
 *   forms from being incremented.
198
 *
199
 * @return mixed
200
 *   The output of the form, if it was rendered. If $form_state['ajax']
201
 *   is set, this will use ctools_modal_form_render so it will be
202
 *   a $command object suitable for ajax_render already.
203
 *
204
 *   If the form was not rendered, the raw render array will be returned.
205
 *
206
 *   If ajax is set the form will never be redirected.
207
 */
208
function ctools_modal_form_wrapper($form_id, &$form_state) {
209
  // Since this will run again on form rebuild while still in the modal, prevent
210
  // form IDs from being incremented.
211
  // @todo https://drupal.org/node/1305882
212
  if (!empty($form_state['reset_html_ids']) && !empty($_POST['ajax_html_ids'])) {
213
    unset($_POST['ajax_html_ids']);
214
  }
215

    
216
  // This won't override settings already in.
217
  $form_state += array(
218
    're_render' => FALSE,
219
    'no_redirect' => !empty($form_state['ajax']),
220
  );
221

    
222
  $output = drupal_build_form($form_id, $form_state);
223
  if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
224
     return ctools_modal_form_render($form_state, $output);
225
  }
226

    
227
  return $output;
228
}
229

    
230
/**
231
 * Render a form into an AJAX display.
232
 */
233
function ctools_modal_form_render($form_state, $output) {
234
  if (is_array($output)) {
235
    $output = drupal_render($output);
236
  }
237

    
238
  $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title'];
239

    
240
  // If there are messages for the form, render them.
241
  if ($messages = theme('status_messages')) {
242
    $output = $messages . $output;
243
  }
244

    
245
  $commands = array();
246
  // If the form has not yet been rendered, render it.
247
  $commands[] = ctools_modal_command_display($title, $output);
248
  return $commands;
249
}
250

    
251
/**
252
 * Perform a simple modal render and immediately exit.
253
 *
254
 * This is primarily used for error displays, since usually modals will
255
 * contain forms.
256
 */
257
function ctools_modal_render($title, $output) {
258
  $commands = array();
259
  $commands[] = ctools_modal_command_display($title, $output);
260
  print ajax_render($commands);
261
}