Révision 7e72b748
Ajouté par Assos Assos il y a plus de 6 ans
drupal7/sites/all/modules/ctools/ctools_ajax_sample/ctools_ajax_sample.module | ||
---|---|---|
8 | 8 |
|
9 | 9 |
// --------------------------------------------------------------------------- |
10 | 10 |
// Drupal hooks. |
11 |
|
|
12 | 11 |
/** |
13 |
* Implementation of hook_menu()
|
|
12 |
* Implementation of hook_menu() |
|
14 | 13 |
*/ |
14 |
|
|
15 | 15 |
function ctools_ajax_sample_menu() { |
16 | 16 |
$items['ctools_ajax_sample'] = array( |
17 |
'title' => 'Chaos Tools AJAX Demo',
|
|
18 |
'page callback' => 'ctools_ajax_sample_page',
|
|
19 |
'access callback' => TRUE,
|
|
20 |
'type' => MENU_NORMAL_ITEM,
|
|
17 |
'title' => 'Chaos Tools AJAX Demo', |
|
18 |
'page callback' => 'ctools_ajax_sample_page', |
|
19 |
'access callback' => TRUE, |
|
20 |
'type' => MENU_NORMAL_ITEM, |
|
21 | 21 |
); |
22 | 22 |
$items['ctools_ajax_sample/simple_form'] = array( |
23 | 23 |
'title' => 'Simple Form', |
... | ... | |
26 | 26 |
'type' => MENU_CALLBACK, |
27 | 27 |
); |
28 | 28 |
$items['ctools_ajax_sample/%ctools_js/hello'] = array( |
29 |
'title' => 'Hello World',
|
|
30 |
'page callback' => 'ctools_ajax_sample_hello',
|
|
31 |
'page arguments' => array(1),
|
|
32 |
'access callback' => TRUE,
|
|
33 |
'type' => MENU_CALLBACK,
|
|
29 |
'title' => 'Hello World', |
|
30 |
'page callback' => 'ctools_ajax_sample_hello', |
|
31 |
'page arguments' => array(1), |
|
32 |
'access callback' => TRUE, |
|
33 |
'type' => MENU_CALLBACK, |
|
34 | 34 |
); |
35 | 35 |
$items['ctools_ajax_sample/%ctools_js/tablenix/%'] = array( |
36 |
'title' => 'Hello World',
|
|
37 |
'page callback' => 'ctools_ajax_sample_tablenix',
|
|
38 |
'page arguments' => array(1, 3),
|
|
39 |
'access callback' => TRUE,
|
|
40 |
'type' => MENU_CALLBACK,
|
|
36 |
'title' => 'Hello World', |
|
37 |
'page callback' => 'ctools_ajax_sample_tablenix', |
|
38 |
'page arguments' => array(1, 3), |
|
39 |
'access callback' => TRUE, |
|
40 |
'type' => MENU_CALLBACK, |
|
41 | 41 |
); |
42 | 42 |
$items['ctools_ajax_sample/%ctools_js/login'] = array( |
43 |
'title' => 'Login',
|
|
44 |
'page callback' => 'ctools_ajax_sample_login',
|
|
45 |
'page arguments' => array(1),
|
|
46 |
'access callback' => TRUE,
|
|
47 |
'type' => MENU_CALLBACK,
|
|
43 |
'title' => 'Login', |
|
44 |
'page callback' => 'ctools_ajax_sample_login', |
|
45 |
'page arguments' => array(1), |
|
46 |
'access callback' => TRUE, |
|
47 |
'type' => MENU_CALLBACK, |
|
48 | 48 |
); |
49 | 49 |
$items['ctools_ajax_sample/%ctools_js/animal'] = array( |
50 |
'title' => 'Animal',
|
|
51 |
'page callback' => 'ctools_ajax_sample_animal',
|
|
52 |
'page arguments' => array(1),
|
|
53 |
'access callback' => TRUE,
|
|
54 |
'type' => MENU_CALLBACK,
|
|
50 |
'title' => 'Animal', |
|
51 |
'page callback' => 'ctools_ajax_sample_animal', |
|
52 |
'page arguments' => array(1), |
|
53 |
'access callback' => TRUE, |
|
54 |
'type' => MENU_CALLBACK, |
|
55 | 55 |
); |
56 | 56 |
$items['ctools_ajax_sample/%ctools_js/login/%'] = array( |
57 |
'title' => 'Post-Login Action',
|
|
58 |
'page callback' => 'ctools_ajax_sample_login_success',
|
|
59 |
'page arguments' => array(1, 3),
|
|
60 |
'access callback' => TRUE,
|
|
61 |
'type' => MENU_CALLBACK,
|
|
57 |
'title' => 'Post-Login Action', |
|
58 |
'page callback' => 'ctools_ajax_sample_login_success', |
|
59 |
'page arguments' => array(1, 3), |
|
60 |
'access callback' => TRUE, |
|
61 |
'type' => MENU_CALLBACK, |
|
62 | 62 |
); |
63 | 63 |
$items['ctools_ajax_sample/jumped'] = array( |
64 | 64 |
'title' => 'Successful Jumping', |
... | ... | |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
// --------------------------------------------------------------------------- |
107 |
// Page callbacks |
|
108 |
|
|
107 |
// Page callbacks. |
|
109 | 108 |
/** |
110 | 109 |
* Page callback to display links and render a container for AJAX stuff. |
111 | 110 |
*/ |
111 |
|
|
112 | 112 |
function ctools_ajax_sample_page() { |
113 | 113 |
global $user; |
114 | 114 |
|
... | ... | |
156 | 156 |
|
157 | 157 |
// The extra class points to the info in ctools-sample-style which we added |
158 | 158 |
// to the settings, prefixed with 'ctools-modal'. |
159 |
$links[] = ctools_modal_text_button(t('Modal Login (custom style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'), 'ctools-modal-ctools-sample-style');
|
|
159 |
$links[] = ctools_modal_text_button(t('Modal Login (custom style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'), 'ctools-modal-ctools-sample-style'); |
|
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
// Four ways to do our animal picking wizard. |
163 | 163 |
$button_form = ctools_ajax_sample_ajax_button_form(); |
164 | 164 |
$links[] = l(t('Wizard (no modal)'), 'ctools_ajax_sample/nojs/animal'); |
165 | 165 |
$links[] = ctools_modal_text_button(t('Wizard (default modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal')); |
166 |
$links[] = ctools_modal_text_button(t('Wizard (custom modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'), 'ctools-modal-ctools-sample-style');
|
|
166 |
$links[] = ctools_modal_text_button(t('Wizard (custom modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'), 'ctools-modal-ctools-sample-style'); |
|
167 | 167 |
$links[] = drupal_render($button_form); |
168 | 168 |
|
169 | 169 |
$links[] = ctools_ajax_text_button(t('Hello world!'), "ctools_ajax_sample/nojs/hello", t('Replace text with "hello world"')); |
... | ... | |
176 | 176 |
// Create a table that we can have data removed from via AJAX. |
177 | 177 |
$header = array(t('Row'), t('Content'), t('Actions')); |
178 | 178 |
$rows = array(); |
179 |
for($i = 1; $i < 11; $i++) { |
|
179 |
for ($i = 1; $i < 11; $i++) {
|
|
180 | 180 |
$rows[] = array( |
181 |
'class' => array('ajax-sample-row-'. $i), |
|
181 |
'class' => array('ajax-sample-row-' . $i),
|
|
182 | 182 |
'data' => array( |
183 | 183 |
$i, |
184 | 184 |
md5($i), |
... | ... | |
189 | 189 |
|
190 | 190 |
$output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('ajax-sample-table')))); |
191 | 191 |
|
192 |
// Show examples of ctools javascript widgets |
|
193 |
$output .= '<h2>'. t('CTools Javascript Widgets') .'</h2>';
|
|
192 |
// Show examples of ctools javascript widgets.
|
|
193 |
$output .= '<h2>' . t('CTools Javascript Widgets') . '</h2>';
|
|
194 | 194 |
|
195 |
// Create a drop down menu |
|
195 |
// Create a drop down menu.
|
|
196 | 196 |
$links = array(); |
197 | 197 |
$links[] = array('title' => t('Link 1'), 'href' => $_GET['q']); |
198 | 198 |
$links[] = array('title' => t('Link 2'), 'href' => $_GET['q']); |
... | ... | |
201 | 201 |
$output .= '<h3>' . t('Drop Down Menu') . '</h3>'; |
202 | 202 |
$output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links)); |
203 | 203 |
|
204 |
// Create a collapsible div |
|
204 |
// Create a collapsible div.
|
|
205 | 205 |
$handle = t('Click to Collapse'); |
206 | 206 |
$content = 'Nulla ligula ante, aliquam at adipiscing egestas, varius vel arcu. Etiam laoreet elementum mi vel consequat. Etiam scelerisque lorem vel neque consequat quis bibendum libero congue. Nulla facilisi. Mauris a elit a leo feugiat porta. Phasellus placerat cursus est vitae elementum.'; |
207 |
$output .= '<h3>'. t('Collapsible Div') .'</h3>';
|
|
207 |
$output .= '<h3>' . t('Collapsible Div') . '</h3>';
|
|
208 | 208 |
$output .= theme('ctools_collapsible', array('handle' => $handle, 'content' => $content, 'collapsed' => FALSE)); |
209 | 209 |
|
210 |
// Create a jump menu |
|
210 |
// Create a jump menu.
|
|
211 | 211 |
ctools_include('jump-menu'); |
212 | 212 |
$form = drupal_get_form('ctools_ajax_sample_jump_menu_form'); |
213 |
$output .= '<h3>'. t('Jump Menu') .'</h3>';
|
|
213 |
$output .= '<h3>' . t('Jump Menu') . '</h3>';
|
|
214 | 214 |
$output .= drupal_render($form); |
215 | 215 |
|
216 | 216 |
return array('markup' => array('#markup' => $output)); |
... | ... | |
225 | 225 |
ctools_include('ajax'); |
226 | 226 |
$commands = array(); |
227 | 227 |
$commands[] = ajax_command_html('#ctools-sample', $output); |
228 |
print ajax_render($commands); // this function exits. |
|
228 |
// This function exits. |
|
229 |
print ajax_render($commands); |
|
229 | 230 |
exit; |
230 | 231 |
} |
231 | 232 |
else { |
... | ... | |
234 | 235 |
} |
235 | 236 |
|
236 | 237 |
/** |
237 |
* Nix a row from a table and restripe.
|
|
238 |
* Nix a row from a table and restripe. |
|
238 | 239 |
*/ |
239 | 240 |
function ctools_ajax_sample_tablenix($js, $row) { |
240 | 241 |
if (!$js) { |
... | ... | |
272 | 273 |
$output = array(); |
273 | 274 |
$inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account')); |
274 | 275 |
$account = ctools_ajax_text_button(t('your account'), 'ctools_ajax_sample/nojs/login/user', t('Go to your account')); |
275 |
$output[] = ctools_modal_command_display(t('Login Success'), '<div class="modal-message">Login successful. You can now choose whether to '. $inplace .', or go to '. $account.'.</div>');
|
|
276 |
$output[] = ctools_modal_command_display(t('Login Success'), '<div class="modal-message">Login successful. You can now choose whether to ' . $inplace . ', or go to ' . $account . '.</div>');
|
|
276 | 277 |
} |
277 | 278 |
print ajax_render($output); |
278 | 279 |
exit; |
... | ... | |
283 | 284 |
*/ |
284 | 285 |
function ctools_ajax_sample_login_success($js, $action) { |
285 | 286 |
if (!$js) { |
286 |
// we should never be here out of ajax context
|
|
287 |
// We should never be here out of ajax context.
|
|
287 | 288 |
return MENU_NOT_FOUND; |
288 | 289 |
} |
289 | 290 |
|
... | ... | |
291 | 292 |
ctools_add_js('ajax-responder'); |
292 | 293 |
$commands = array(); |
293 | 294 |
if ($action == 'inplace') { |
294 |
// stay here
|
|
295 |
// Stay here.
|
|
295 | 296 |
$commands[] = ctools_ajax_command_reload(); |
296 | 297 |
} |
297 | 298 |
else { |
298 |
// bounce bounce
|
|
299 |
// Bounce bounce.
|
|
299 | 300 |
$commands[] = ctools_ajax_command_redirect('user'); |
300 | 301 |
} |
301 | 302 |
print ajax_render($commands); |
... | ... | |
318 | 319 |
'show back' => TRUE, |
319 | 320 |
'show cancel' => TRUE, |
320 | 321 |
'show return' => FALSE, |
321 |
'next callback' => 'ctools_ajax_sample_wizard_next',
|
|
322 |
'next callback' => 'ctools_ajax_sample_wizard_next', |
|
322 | 323 |
'finish callback' => 'ctools_ajax_sample_wizard_finish', |
323 | 324 |
'cancel callback' => 'ctools_ajax_sample_wizard_cancel', |
324 |
// this controls order, as well as form labels
|
|
325 |
// This controls order, as well as form labels.
|
|
325 | 326 |
'order' => array( |
326 | 327 |
'start' => t('Choose animal'), |
327 | 328 |
), |
328 |
// here we map a step to a form id.
|
|
329 |
// Here we map a step to a form id.
|
|
329 | 330 |
'forms' => array( |
330 |
// e.g. this for the step at wombat/create |
|
331 |
// e.g. this for the step at wombat/create.
|
|
331 | 332 |
'start' => array( |
332 |
'form id' => 'ctools_ajax_sample_start' |
|
333 |
'form id' => 'ctools_ajax_sample_start',
|
|
333 | 334 |
), |
334 | 335 |
), |
335 | 336 |
); |
... | ... | |
341 | 342 |
// in creation. |
342 | 343 |
// |
343 | 344 |
// We skip all this here by just using an id of 1. |
344 |
|
|
345 | 345 |
$object_id = 1; |
346 | 346 |
|
347 | 347 |
if (empty($step)) { |
... | ... | |
406 | 406 |
$commands[] = ajax_command_html('#ctools-sample', $animal); |
407 | 407 |
$commands[] = ctools_modal_command_dismiss(); |
408 | 408 |
} |
409 |
else if (!empty($form_state['cancel'])) {
|
|
409 |
elseif (!empty($form_state['cancel'])) { |
|
410 | 410 |
// If cancelling, return to the activity. |
411 | 411 |
$commands[] = ctools_modal_command_dismiss(); |
412 | 412 |
} |
... | ... | |
420 | 420 |
if ($output === FALSE || !empty($form_state['complete'])) { |
421 | 421 |
return $animal; |
422 | 422 |
} |
423 |
else if (!empty($form_state['cancel'])) {
|
|
423 |
elseif (!empty($form_state['cancel'])) { |
|
424 | 424 |
drupal_goto('ctools_ajax_sample'); |
425 | 425 |
} |
426 | 426 |
else { |
... | ... | |
430 | 430 |
} |
431 | 431 |
|
432 | 432 |
// --------------------------------------------------------------------------- |
433 |
// Themes |
|
434 |
|
|
433 |
// Themes. |
|
435 | 434 |
/** |
436 | 435 |
* Theme function for main rendered output. |
437 | 436 |
*/ |
437 |
|
|
438 | 438 |
function theme_ctools_ajax_sample_container($vars) { |
439 | 439 |
$output = '<div id="ctools-sample">'; |
440 | 440 |
$output .= $vars['content']; |
... | ... | |
445 | 445 |
|
446 | 446 |
// --------------------------------------------------------------------------- |
447 | 447 |
// Stuff needed for our little wizard. |
448 |
|
|
449 | 448 |
/** |
450 | 449 |
* Get a list of our animals and associated forms. |
451 | 450 |
* |
... | ... | |
453 | 452 |
* which is often how it will work in the real world. If using CTools, what |
454 | 453 |
* you would probably really have, here, is a set of plugins for each animal. |
455 | 454 |
*/ |
455 |
|
|
456 | 456 |
function ctools_ajax_sample_animals() { |
457 | 457 |
return array( |
458 | 458 |
'sheep' => array( |
... | ... | |
478 | 478 |
|
479 | 479 |
// --------------------------------------------------------------------------- |
480 | 480 |
// Wizard caching helpers. |
481 |
|
|
482 | 481 |
/** |
483 | 482 |
* Store our little cache so that we can retain data from form to form. |
484 | 483 |
*/ |
484 |
|
|
485 | 485 |
function ctools_ajax_sample_cache_set($id, $object) { |
486 | 486 |
ctools_include('object-cache'); |
487 | 487 |
ctools_object_cache_set('ctools_ajax_sample', $id, $object); |
... | ... | |
495 | 495 |
$object = ctools_object_cache_get('ctools_ajax_sample', $id); |
496 | 496 |
if (!$object) { |
497 | 497 |
// Create a default object. |
498 |
$object = new stdClass; |
|
498 |
$object = new stdClass();
|
|
499 | 499 |
$object->type = 'unknown'; |
500 | 500 |
$object->name = ''; |
501 | 501 |
} |
... | ... | |
513 | 513 |
|
514 | 514 |
// --------------------------------------------------------------------------- |
515 | 515 |
// Wizard in-between helpers; what to do between or after forms. |
516 |
|
|
517 | 516 |
/** |
518 | 517 |
* Handle the 'next' click on the add/edit pane form wizard. |
519 | 518 |
* |
520 | 519 |
* All we need to do is store the updated pane in the cache. |
521 | 520 |
*/ |
521 |
|
|
522 | 522 |
function ctools_ajax_sample_wizard_next(&$form_state) { |
523 | 523 |
ctools_ajax_sample_cache_set($form_state['object_id'], $form_state['object']); |
524 | 524 |
} |
... | ... | |
542 | 542 |
|
543 | 543 |
// --------------------------------------------------------------------------- |
544 | 544 |
// Wizard forms for our simple info collection wizard. |
545 |
|
|
546 | 545 |
/** |
547 | 546 |
* Wizard start form. Choose an animal. |
548 | 547 |
*/ |
548 |
|
|
549 | 549 |
function ctools_ajax_sample_start($form, &$form_state) { |
550 | 550 |
$form_state['title'] = t('Choose animal'); |
551 | 551 |
|
... | ... | |
713 | 713 |
} |
714 | 714 |
|
715 | 715 |
/** |
716 |
* Helper function to provide a sample jump menu form |
|
716 |
* Helper function to provide a sample jump menu form.
|
|
717 | 717 |
*/ |
718 | 718 |
function ctools_ajax_sample_jump_menu_form() { |
719 | 719 |
$url = url('ctools_ajax_sample/jumped'); |
... | ... | |
723 | 723 |
} |
724 | 724 |
|
725 | 725 |
/** |
726 |
* Provide a message to the user that the jump menu worked |
|
726 |
* Provide a message to the user that the jump menu worked.
|
|
727 | 727 |
*/ |
728 | 728 |
function ctools_ajax_sample_jump_menu_page() { |
729 | 729 |
$return_link = l(t('Return to the examples page.'), 'ctools_ajax_sample'); |
... | ... | |
732 | 732 |
} |
733 | 733 |
|
734 | 734 |
/** |
735 |
* Provide a form for an example ajax modal button |
|
735 |
* Provide a form for an example ajax modal button.
|
|
736 | 736 |
*/ |
737 | 737 |
function ctools_ajax_sample_ajax_button_form() { |
738 | 738 |
$form = array(); |
Formats disponibles : Unified diff
Weekly update of contrib modules