Projet

Général

Profil

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

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

1 85ad3d82 Assos Assos
<?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 560c3060 Julien Enselme
  drupal_add_library('system', 'ui');
70 85ad3d82 Assos Assos
  ctools_add_js('modal');
71
72
  ctools_add_css('modal');
73
  $done = TRUE;
74
}
75
76
/**
77
 * @todo this is deprecated
78
 */
79
function ctools_modal_add_plugin_js($plugins) {
80
  $css = array();
81
  $js = array(drupal_get_path('module', 'ctools') . '/js/dependent.js' => TRUE);
82
  foreach ($plugins as $subtype) {
83
    if (isset($subtype['js'])) {
84
      foreach ($subtype['js'] as $file) {
85
        if (file_exists($file)) {
86
          $js[$file] = TRUE;
87
        }
88
        else if (file(exists($subtype['path'] . '/' . $file))) {
89
          $js[$subtype['path'] . '/' . $file] = TRUE;
90
        }
91
      }
92
    }
93
    if (isset($subtype['css'])) {
94
      foreach ($subtype['css'] as $file) {
95
        if (file_exists($file)) {
96
          $css[$file] = TRUE;
97
        }
98
        else if (file(exists($subtype['path'] . '/' . $file))) {
99
          $css[$subtype['path'] . '/' . $file] = TRUE;
100
        }
101
      }
102
    }
103
  }
104
105
  foreach (array_keys($js) as $file) {
106
    drupal_add_js($file);
107
  }
108
  foreach (array_keys($css) as $file) {
109
    drupal_add_css($file);
110
  }
111
}
112
113
/**
114
 * Place HTML within the modal.
115
 *
116
 * @param $title
117
 *   The title of the modal.
118
 * @param $html
119
 *   The html to place within the modal.
120
 */
121
function ctools_modal_command_display($title, $html) {
122
  if (is_array($html)) {
123
    $html = drupal_render($html);
124
  }
125
126
  return array(
127
    'command' => 'modal_display',
128
    'title' => $title,
129
    'output' => $html,
130
  );
131
}
132
133
/**
134
 * Dismiss the modal.
135
 */
136
function ctools_modal_command_dismiss() {
137
  return array(
138
    'command' => 'modal_dismiss',
139
  );
140
}
141
142
/**
143
 * Display loading screen in the modal
144
 */
145
function ctools_modal_command_loading() {
146
  return array(
147
    'command' => 'modal_loading',
148
  );
149
}
150
151
/**
152
 * Render an image as a button link. This will automatically apply an AJAX class
153
 * to the link and add the appropriate javascript to make this happen.
154
 *
155
 * @param $image
156
 *   The path to an image to use that will be sent to theme('image') for rendering.
157
 * @param $dest
158
 *   The destination of the link.
159
 * @param $alt
160
 *   The alt text of the link.
161
 * @param $class
162
 *   Any class to apply to the link. @todo this should be a options array.
163
 */
164
function ctools_modal_image_button($image, $dest, $alt, $class = '') {
165
  return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class, 'ctools-use-modal');
166
}
167
168
/**
169
 * Render text as a link. This will automatically apply an AJAX class
170
 * to the link and add the appropriate javascript to make this happen.
171
 *
172
 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
173
 * not use user input so this is a very minor concern.
174
 *
175
 * @param $text
176
 *   The text that will be displayed as the link.
177
 * @param $dest
178
 *   The destination of the link.
179
 * @param $alt
180
 *   The alt text of the link.
181
 * @param $class
182
 *   Any class to apply to the link. @todo this should be a options array.
183
 */
184
function ctools_modal_text_button($text, $dest, $alt, $class = '') {
185
  return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
186
}
187
188
/**
189
 * Wrap a form so that we can use it properly with AJAX. Essentially if the
190
 * form wishes to render, it automatically does that, otherwise it returns
191 e4c061ad Assos Assos
 * the render array so we can see submission results.
192 96a203dd Assos Assos
193
 * @param array $form
194
 *   An associative array containing the structure of the form.
195
 * @param array $form_state
196
 *   An associative array containing the current state of the form.
197
 *   If the 'reset_html_ids' key is set to TRUE, it will prevent HTML IDs in
198
 *   forms from being incremented.
199 85ad3d82 Assos Assos
 *
200 96a203dd Assos Assos
 * @return mixed
201 85ad3d82 Assos Assos
 *   The output of the form, if it was rendered. If $form_state['ajax']
202
 *   is set, this will use ctools_modal_form_render so it will be
203
 *   a $command object suitable for ajax_render already.
204
 *
205 e4c061ad Assos Assos
 *   If the form was not rendered, the raw render array will be returned.
206
 *
207
 *   If ajax is set the form will never be redirected.
208 85ad3d82 Assos Assos
 */
209
function ctools_modal_form_wrapper($form_id, &$form_state) {
210 96a203dd Assos Assos
  // Since this will run again on form rebuild while still in the modal, prevent
211
  // form IDs from being incremented.
212
  // @todo https://drupal.org/node/1305882
213
  if (!empty($form_state['reset_html_ids']) && !empty($_POST['ajax_html_ids'])) {
214
    unset($_POST['ajax_html_ids']);
215
  }
216
217 85ad3d82 Assos Assos
  // This won't override settings already in.
218
  $form_state += array(
219
    're_render' => FALSE,
220
    'no_redirect' => !empty($form_state['ajax']),
221
  );
222
223
  $output = drupal_build_form($form_id, $form_state);
224 e4c061ad Assos Assos
  if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
225 85ad3d82 Assos Assos
     return ctools_modal_form_render($form_state, $output);
226
  }
227
228
  return $output;
229
}
230
231
/**
232
 * Render a form into an AJAX display.
233
 */
234
function ctools_modal_form_render($form_state, $output) {
235
  if (is_array($output)) {
236
    $output = drupal_render($output);
237
  }
238
239
  $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title'];
240
241
  // If there are messages for the form, render them.
242
  if ($messages = theme('status_messages')) {
243
    $output = $messages . $output;
244
  }
245
246
  $commands = array();
247
  // If the form has not yet been rendered, render it.
248
  $commands[] = ctools_modal_command_display($title, $output);
249
  return $commands;
250
}
251
252
/**
253
 * Perform a simple modal render and immediately exit.
254
 *
255
 * This is primarily used for error displays, since usually modals will
256
 * contain forms.
257
 */
258
function ctools_modal_render($title, $output) {
259
  $commands = array();
260
  $commands[] = ctools_modal_command_display($title, $output);
261
  print ajax_render($commands);
262
}