1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Sample AJAX functionality so people can see some of the CTools AJAX
|
6 |
|
|
* features in use.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
// ---------------------------------------------------------------------------
|
10 |
|
|
// Drupal hooks.
|
11 |
|
|
|
12 |
|
|
/**
|
13 |
|
|
* Implementation of hook_menu()
|
14 |
|
|
*/
|
15 |
|
|
function ctools_ajax_sample_menu() {
|
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,
|
21 |
|
|
);
|
22 |
|
|
$items['ctools_ajax_sample/simple_form'] = array(
|
23 |
|
|
'title' => 'Simple Form',
|
24 |
|
|
'page callback' => 'ctools_ajax_simple_form',
|
25 |
|
|
'access callback' => TRUE,
|
26 |
|
|
'type' => MENU_CALLBACK,
|
27 |
|
|
);
|
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,
|
34 |
|
|
);
|
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,
|
41 |
|
|
);
|
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,
|
48 |
|
|
);
|
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,
|
55 |
|
|
);
|
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,
|
62 |
|
|
);
|
63 |
|
|
$items['ctools_ajax_sample/jumped'] = array(
|
64 |
|
|
'title' => 'Successful Jumping',
|
65 |
|
|
'page callback' => 'ctools_ajax_sample_jump_menu_page',
|
66 |
|
|
'access callback' => TRUE,
|
67 |
|
|
'type' => MENU_NORMAL_ITEM,
|
68 |
|
|
);
|
69 |
|
|
|
70 |
|
|
return $items;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
function ctools_ajax_simple_form() {
|
74 |
|
|
ctools_include('content');
|
75 |
|
|
ctools_include('context');
|
76 |
|
|
$node = node_load(1);
|
77 |
|
|
$context = ctools_context_create('node', $node);
|
78 |
|
|
$context = array('context_node_1' => $context);
|
79 |
|
|
return ctools_content_render('node_comment_form', 'node_comment_form', ctools_ajax_simple_form_pane(), array(), array(), $context);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
function ctools_ajax_simple_form_pane() {
|
83 |
|
|
$configuration = array(
|
84 |
|
|
'anon_links' => 0,
|
85 |
|
|
'context' => 'context_node_1',
|
86 |
|
|
'override_title' => 0,
|
87 |
|
|
'override_title_text' => '',
|
88 |
|
|
);
|
89 |
|
|
return $configuration;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
/**
|
93 |
|
|
* Implementation of hook_theme()
|
94 |
|
|
*
|
95 |
|
|
* Render some basic output for this module.
|
96 |
|
|
*/
|
97 |
|
|
function ctools_ajax_sample_theme() {
|
98 |
|
|
return array(
|
99 |
|
|
// Sample theme functions.
|
100 |
|
|
'ctools_ajax_sample_container' => array(
|
101 |
|
|
'arguments' => array('content' => NULL),
|
102 |
|
|
),
|
103 |
|
|
);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
// ---------------------------------------------------------------------------
|
107 |
|
|
// Page callbacks
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Page callback to display links and render a container for AJAX stuff.
|
111 |
|
|
*/
|
112 |
|
|
function ctools_ajax_sample_page() {
|
113 |
|
|
global $user;
|
114 |
|
|
|
115 |
|
|
// Include the CTools tools that we need.
|
116 |
|
|
ctools_include('ajax');
|
117 |
|
|
ctools_include('modal');
|
118 |
|
|
|
119 |
|
|
// Add CTools' javascript to the page.
|
120 |
|
|
ctools_modal_add_js();
|
121 |
|
|
|
122 |
|
|
// Create our own javascript that will be used to theme a modal.
|
123 |
|
|
$sample_style = array(
|
124 |
|
|
'ctools-sample-style' => array(
|
125 |
|
|
'modalSize' => array(
|
126 |
|
|
'type' => 'fixed',
|
127 |
|
|
'width' => 500,
|
128 |
|
|
'height' => 300,
|
129 |
|
|
'addWidth' => 20,
|
130 |
|
|
'addHeight' => 15,
|
131 |
|
|
),
|
132 |
|
|
'modalOptions' => array(
|
133 |
|
|
'opacity' => .5,
|
134 |
|
|
'background-color' => '#000',
|
135 |
|
|
),
|
136 |
|
|
'animation' => 'fadeIn',
|
137 |
|
|
'modalTheme' => 'CToolsSampleModal',
|
138 |
|
|
'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'), 'alt' => t('Loading...'), 'title' => t('Loading'))),
|
139 |
|
|
),
|
140 |
|
|
);
|
141 |
|
|
|
142 |
|
|
drupal_add_js($sample_style, 'setting');
|
143 |
|
|
|
144 |
|
|
// Since we have our js, css and images in well-known named directories,
|
145 |
|
|
// CTools makes it easy for us to just use them without worrying about
|
146 |
|
|
// using drupal_get_path() and all that ugliness.
|
147 |
|
|
ctools_add_js('ctools-ajax-sample', 'ctools_ajax_sample');
|
148 |
|
|
ctools_add_css('ctools-ajax-sample', 'ctools_ajax_sample');
|
149 |
|
|
|
150 |
|
|
// Create a list of clickable links.
|
151 |
|
|
$links = array();
|
152 |
|
|
|
153 |
|
|
// Only show login links to the anonymous user.
|
154 |
|
|
if ($user->uid == 0) {
|
155 |
|
|
$links[] = ctools_modal_text_button(t('Modal Login (default style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'));
|
156 |
|
|
|
157 |
|
|
// The extra class points to the info in ctools-sample-style which we added
|
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');
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
// Four ways to do our animal picking wizard.
|
163 |
|
|
$button_form = ctools_ajax_sample_ajax_button_form();
|
164 |
|
|
$links[] = l(t('Wizard (no modal)'), 'ctools_ajax_sample/nojs/animal');
|
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');
|
167 |
|
|
$links[] = drupal_render($button_form);
|
168 |
|
|
|
169 |
|
|
$links[] = ctools_ajax_text_button(t('Hello world!'), "ctools_ajax_sample/nojs/hello", t('Replace text with "hello world"'));
|
170 |
|
|
|
171 |
|
|
$output = theme('item_list', array('items' => $links, 'title' => t('Actions')));
|
172 |
|
|
|
173 |
|
|
// This container will have data AJAXed into it.
|
174 |
|
|
$output .= theme('ctools_ajax_sample_container', array('content' => '<h1>' . t('Sample Content') . '</h1>'));
|
175 |
|
|
|
176 |
|
|
// Create a table that we can have data removed from via AJAX.
|
177 |
|
|
$header = array(t('Row'), t('Content'), t('Actions'));
|
178 |
|
|
$rows = array();
|
179 |
|
|
for($i = 1; $i < 11; $i++) {
|
180 |
|
|
$rows[] = array(
|
181 |
|
|
'class' => array('ajax-sample-row-'. $i),
|
182 |
|
|
'data' => array(
|
183 |
|
|
$i,
|
184 |
|
|
md5($i),
|
185 |
|
|
ctools_ajax_text_button("remove", "ctools_ajax_sample/nojs/tablenix/$i", t('Delete this row')),
|
186 |
|
|
),
|
187 |
|
|
);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
$output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('ajax-sample-table'))));
|
191 |
|
|
|
192 |
|
|
// Show examples of ctools javascript widgets
|
193 |
|
|
$output .= '<h2>'. t('CTools Javascript Widgets') .'</h2>';
|
194 |
|
|
|
195 |
|
|
// Create a drop down menu
|
196 |
|
|
$links = array();
|
197 |
|
|
$links[] = array('title' => t('Link 1'), 'href' => $_GET['q']);
|
198 |
|
|
$links[] = array('title' => t('Link 2'), 'href' => $_GET['q']);
|
199 |
|
|
$links[] = array('title' => t('Link 3'), 'href' => $_GET['q']);
|
200 |
|
|
|
201 |
|
|
$output .= '<h3>' . t('Drop Down Menu') . '</h3>';
|
202 |
|
|
$output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links));
|
203 |
|
|
|
204 |
|
|
// Create a collapsible div
|
205 |
|
|
$handle = t('Click to Collapse');
|
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>';
|
208 |
|
|
$output .= theme('ctools_collapsible', array('handle' => $handle, 'content' => $content, 'collapsed' => FALSE));
|
209 |
|
|
|
210 |
|
|
// Create a jump menu
|
211 |
|
|
ctools_include('jump-menu');
|
212 |
|
|
$form = drupal_get_form('ctools_ajax_sample_jump_menu_form');
|
213 |
|
|
$output .= '<h3>'. t('Jump Menu') .'</h3>';
|
214 |
|
|
$output .= drupal_render($form);
|
215 |
|
|
|
216 |
|
|
return array('markup' => array('#markup' => $output));
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Returns a "take it all over" hello world style request.
|
221 |
|
|
*/
|
222 |
|
|
function ctools_ajax_sample_hello($js = NULL) {
|
223 |
|
|
$output = '<h1>' . t('Hello World') . '</h1>';
|
224 |
|
|
if ($js) {
|
225 |
|
|
ctools_include('ajax');
|
226 |
|
|
$commands = array();
|
227 |
|
|
$commands[] = ajax_command_html('#ctools-sample', $output);
|
228 |
|
|
print ajax_render($commands); // this function exits.
|
229 |
|
|
exit;
|
230 |
|
|
}
|
231 |
|
|
else {
|
232 |
|
|
return $output;
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
/**
|
237 |
|
|
* Nix a row from a table and restripe.
|
238 |
|
|
*/
|
239 |
|
|
function ctools_ajax_sample_tablenix($js, $row) {
|
240 |
|
|
if (!$js) {
|
241 |
|
|
// We don't support degrading this from js because we're not
|
242 |
|
|
// using the server to remember the state of the table.
|
243 |
|
|
return MENU_ACCESS_DENIED;
|
244 |
|
|
}
|
245 |
|
|
ctools_include('ajax');
|
246 |
|
|
|
247 |
|
|
$commands = array();
|
248 |
|
|
$commands[] = ajax_command_remove("tr.ajax-sample-row-$row");
|
249 |
|
|
$commands[] = ajax_command_restripe("table.ajax-sample-table");
|
250 |
|
|
print ajax_render($commands);
|
251 |
|
|
exit;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
/**
|
255 |
|
|
* A modal login callback.
|
256 |
|
|
*/
|
257 |
|
|
function ctools_ajax_sample_login($js = NULL) {
|
258 |
|
|
// Fall back if $js is not set.
|
259 |
|
|
if (!$js) {
|
260 |
|
|
return drupal_get_form('user_login');
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
ctools_include('modal');
|
264 |
|
|
ctools_include('ajax');
|
265 |
|
|
$form_state = array(
|
266 |
|
|
'title' => t('Login'),
|
267 |
|
|
'ajax' => TRUE,
|
268 |
|
|
);
|
269 |
|
|
$output = ctools_modal_form_wrapper('user_login', $form_state);
|
270 |
|
|
if (!empty($form_state['executed'])) {
|
271 |
|
|
// We'll just overwrite the form output if it was successful.
|
272 |
|
|
$output = array();
|
273 |
|
|
$inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account'));
|
274 |
|
|
$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 |
|
|
}
|
277 |
|
|
print ajax_render($output);
|
278 |
|
|
exit;
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
/**
|
282 |
|
|
* Post-login processor: should we go to the user account or stay in place?
|
283 |
|
|
*/
|
284 |
|
|
function ctools_ajax_sample_login_success($js, $action) {
|
285 |
|
|
if (!$js) {
|
286 |
|
|
// we should never be here out of ajax context
|
287 |
|
|
return MENU_NOT_FOUND;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
ctools_include('ajax');
|
291 |
|
|
ctools_add_js('ajax-responder');
|
292 |
|
|
$commands = array();
|
293 |
|
|
if ($action == 'inplace') {
|
294 |
|
|
// stay here
|
295 |
|
|
$commands[] = ctools_ajax_command_reload();
|
296 |
|
|
}
|
297 |
|
|
else {
|
298 |
|
|
// bounce bounce
|
299 |
|
|
$commands[] = ctools_ajax_command_redirect('user');
|
300 |
|
|
}
|
301 |
|
|
print ajax_render($commands);
|
302 |
|
|
exit;
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
/**
|
306 |
|
|
* A modal login callback.
|
307 |
|
|
*/
|
308 |
|
|
function ctools_ajax_sample_animal($js = NULL, $step = NULL) {
|
309 |
|
|
if ($js) {
|
310 |
|
|
ctools_include('modal');
|
311 |
|
|
ctools_include('ajax');
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
$form_info = array(
|
315 |
|
|
'id' => 'animals',
|
316 |
|
|
'path' => "ctools_ajax_sample/" . ($js ? 'ajax' : 'nojs') . "/animal/%step",
|
317 |
|
|
'show trail' => TRUE,
|
318 |
|
|
'show back' => TRUE,
|
319 |
|
|
'show cancel' => TRUE,
|
320 |
|
|
'show return' => FALSE,
|
321 |
|
|
'next callback' => 'ctools_ajax_sample_wizard_next',
|
322 |
|
|
'finish callback' => 'ctools_ajax_sample_wizard_finish',
|
323 |
|
|
'cancel callback' => 'ctools_ajax_sample_wizard_cancel',
|
324 |
|
|
// this controls order, as well as form labels
|
325 |
|
|
'order' => array(
|
326 |
|
|
'start' => t('Choose animal'),
|
327 |
|
|
),
|
328 |
|
|
// here we map a step to a form id.
|
329 |
|
|
'forms' => array(
|
330 |
|
|
// e.g. this for the step at wombat/create
|
331 |
|
|
'start' => array(
|
332 |
|
|
'form id' => 'ctools_ajax_sample_start'
|
333 |
|
|
),
|
334 |
|
|
),
|
335 |
|
|
);
|
336 |
|
|
|
337 |
|
|
// We're not using any real storage here, so we're going to set our
|
338 |
|
|
// object_id to 1. When using wizard forms, id management turns
|
339 |
|
|
// out to be one of the hardest parts. Editing an object with an id
|
340 |
|
|
// is easy, but new objects don't usually have ids until somewhere
|
341 |
|
|
// in creation.
|
342 |
|
|
//
|
343 |
|
|
// We skip all this here by just using an id of 1.
|
344 |
|
|
|
345 |
|
|
$object_id = 1;
|
346 |
|
|
|
347 |
|
|
if (empty($step)) {
|
348 |
|
|
// We reset the form when $step is NULL because that means they have
|
349 |
|
|
// for whatever reason started over.
|
350 |
|
|
ctools_ajax_sample_cache_clear($object_id);
|
351 |
|
|
$step = 'start';
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
// This automatically gets defaults if there wasn't anything saved.
|
355 |
|
|
$object = ctools_ajax_sample_cache_get($object_id);
|
356 |
|
|
|
357 |
|
|
$animals = ctools_ajax_sample_animals();
|
358 |
|
|
|
359 |
|
|
// Make sure we can't somehow accidentally go to an invalid animal.
|
360 |
|
|
if (empty($animals[$object->type])) {
|
361 |
|
|
$object->type = 'unknown';
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
// Now that we have our object, dynamically add the animal's form.
|
365 |
|
|
if ($object->type == 'unknown') {
|
366 |
|
|
// If they haven't selected a type, add a form that doesn't exist yet.
|
367 |
|
|
$form_info['order']['unknown'] = t('Configure animal');
|
368 |
|
|
$form_info['forms']['unknown'] = array('form id' => 'nothing');
|
369 |
|
|
}
|
370 |
|
|
else {
|
371 |
|
|
// Add the selected animal to the order so that it shows up properly in the trail.
|
372 |
|
|
$form_info['order'][$object->type] = $animals[$object->type]['config title'];
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
// Make sure all animals forms are represented so that the next stuff can
|
376 |
|
|
// work correctly:
|
377 |
|
|
foreach ($animals as $id => $animal) {
|
378 |
|
|
$form_info['forms'][$id] = array('form id' => $animals[$id]['form']);
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
$form_state = array(
|
382 |
|
|
'ajax' => $js,
|
383 |
|
|
// Put our object and ID into the form state cache so we can easily find
|
384 |
|
|
// it.
|
385 |
|
|
'object_id' => $object_id,
|
386 |
|
|
'object' => &$object,
|
387 |
|
|
);
|
388 |
|
|
|
389 |
|
|
// Send this all off to our form. This is like drupal_get_form only wizardy.
|
390 |
|
|
ctools_include('wizard');
|
391 |
|
|
$form = ctools_wizard_multistep_form($form_info, $step, $form_state);
|
392 |
|
|
$output = drupal_render($form);
|
393 |
|
|
|
394 |
|
|
if ($output === FALSE || !empty($form_state['complete'])) {
|
395 |
|
|
// This creates a string based upon the animal and its setting using
|
396 |
|
|
// function indirection.
|
397 |
|
|
$animal = $animals[$object->type]['output']($object);
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
// If $output is FALSE, there was no actual form.
|
401 |
|
|
if ($js) {
|
402 |
|
|
// If javascript is active, we have to use a render array.
|
403 |
|
|
$commands = array();
|
404 |
|
|
if ($output === FALSE || !empty($form_state['complete'])) {
|
405 |
|
|
// Dismiss the modal.
|
406 |
|
|
$commands[] = ajax_command_html('#ctools-sample', $animal);
|
407 |
|
|
$commands[] = ctools_modal_command_dismiss();
|
408 |
|
|
}
|
409 |
|
|
else if (!empty($form_state['cancel'])) {
|
410 |
|
|
// If cancelling, return to the activity.
|
411 |
|
|
$commands[] = ctools_modal_command_dismiss();
|
412 |
|
|
}
|
413 |
|
|
else {
|
414 |
|
|
$commands = ctools_modal_form_render($form_state, $output);
|
415 |
|
|
}
|
416 |
|
|
print ajax_render($commands);
|
417 |
|
|
exit;
|
418 |
|
|
}
|
419 |
|
|
else {
|
420 |
|
|
if ($output === FALSE || !empty($form_state['complete'])) {
|
421 |
|
|
return $animal;
|
422 |
|
|
}
|
423 |
|
|
else if (!empty($form_state['cancel'])) {
|
424 |
|
|
drupal_goto('ctools_ajax_sample');
|
425 |
|
|
}
|
426 |
|
|
else {
|
427 |
|
|
return $output;
|
428 |
|
|
}
|
429 |
|
|
}
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
// ---------------------------------------------------------------------------
|
433 |
|
|
// Themes
|
434 |
|
|
|
435 |
|
|
/**
|
436 |
|
|
* Theme function for main rendered output.
|
437 |
|
|
*/
|
438 |
|
|
function theme_ctools_ajax_sample_container($vars) {
|
439 |
|
|
$output = '<div id="ctools-sample">';
|
440 |
|
|
$output .= $vars['content'];
|
441 |
|
|
$output .= '</div>';
|
442 |
|
|
|
443 |
|
|
return $output;
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
// ---------------------------------------------------------------------------
|
447 |
|
|
// Stuff needed for our little wizard.
|
448 |
|
|
|
449 |
|
|
/**
|
450 |
|
|
* Get a list of our animals and associated forms.
|
451 |
|
|
*
|
452 |
|
|
* What we're doing is making it easy to add more animals in just one place,
|
453 |
|
|
* which is often how it will work in the real world. If using CTools, what
|
454 |
|
|
* you would probably really have, here, is a set of plugins for each animal.
|
455 |
|
|
*/
|
456 |
|
|
function ctools_ajax_sample_animals() {
|
457 |
|
|
return array(
|
458 |
|
|
'sheep' => array(
|
459 |
|
|
'title' => t('Sheep'),
|
460 |
|
|
'config title' => t('Configure sheep'),
|
461 |
|
|
'form' => 'ctools_ajax_sample_configure_sheep',
|
462 |
|
|
'output' => 'ctools_ajax_sample_show_sheep',
|
463 |
|
|
),
|
464 |
|
|
'lizard' => array(
|
465 |
|
|
'title' => t('Lizard'),
|
466 |
|
|
'config title' => t('Configure lizard'),
|
467 |
|
|
'form' => 'ctools_ajax_sample_configure_lizard',
|
468 |
|
|
'output' => 'ctools_ajax_sample_show_lizard',
|
469 |
|
|
),
|
470 |
|
|
'raptor' => array(
|
471 |
|
|
'title' => t('Raptor'),
|
472 |
|
|
'config title' => t('Configure raptor'),
|
473 |
|
|
'form' => 'ctools_ajax_sample_configure_raptor',
|
474 |
|
|
'output' => 'ctools_ajax_sample_show_raptor',
|
475 |
|
|
),
|
476 |
|
|
);
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
// ---------------------------------------------------------------------------
|
480 |
|
|
// Wizard caching helpers.
|
481 |
|
|
|
482 |
|
|
/**
|
483 |
|
|
* Store our little cache so that we can retain data from form to form.
|
484 |
|
|
*/
|
485 |
|
|
function ctools_ajax_sample_cache_set($id, $object) {
|
486 |
|
|
ctools_include('object-cache');
|
487 |
|
|
ctools_object_cache_set('ctools_ajax_sample', $id, $object);
|
488 |
|
|
}
|
489 |
|
|
|
490 |
|
|
/**
|
491 |
|
|
* Get the current object from the cache, or default.
|
492 |
|
|
*/
|
493 |
|
|
function ctools_ajax_sample_cache_get($id) {
|
494 |
|
|
ctools_include('object-cache');
|
495 |
|
|
$object = ctools_object_cache_get('ctools_ajax_sample', $id);
|
496 |
|
|
if (!$object) {
|
497 |
|
|
// Create a default object.
|
498 |
|
|
$object = new stdClass;
|
499 |
|
|
$object->type = 'unknown';
|
500 |
|
|
$object->name = '';
|
501 |
|
|
}
|
502 |
|
|
|
503 |
|
|
return $object;
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
/**
|
507 |
|
|
* Clear the wizard cache.
|
508 |
|
|
*/
|
509 |
|
|
function ctools_ajax_sample_cache_clear($id) {
|
510 |
|
|
ctools_include('object-cache');
|
511 |
|
|
ctools_object_cache_clear('ctools_ajax_sample', $id);
|
512 |
|
|
}
|
513 |
|
|
|
514 |
|
|
// ---------------------------------------------------------------------------
|
515 |
|
|
// Wizard in-between helpers; what to do between or after forms.
|
516 |
|
|
|
517 |
|
|
/**
|
518 |
|
|
* Handle the 'next' click on the add/edit pane form wizard.
|
519 |
|
|
*
|
520 |
|
|
* All we need to do is store the updated pane in the cache.
|
521 |
|
|
*/
|
522 |
|
|
function ctools_ajax_sample_wizard_next(&$form_state) {
|
523 |
|
|
ctools_ajax_sample_cache_set($form_state['object_id'], $form_state['object']);
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
/**
|
527 |
560c3060
|
Julien Enselme
|
* Handle the 'finish' click on the add/edit pane form wizard.
|
528 |
85ad3d82
|
Assos Assos
|
*
|
529 |
|
|
* All we need to do is set a flag so the return can handle adding
|
530 |
|
|
* the pane.
|
531 |
|
|
*/
|
532 |
|
|
function ctools_ajax_sample_wizard_finish(&$form_state) {
|
533 |
|
|
$form_state['complete'] = TRUE;
|
534 |
|
|
}
|
535 |
|
|
|
536 |
|
|
/**
|
537 |
|
|
* Handle the 'cancel' click on the add/edit pane form wizard.
|
538 |
|
|
*/
|
539 |
|
|
function ctools_ajax_sample_wizard_cancel(&$form_state) {
|
540 |
|
|
$form_state['cancel'] = TRUE;
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
// ---------------------------------------------------------------------------
|
544 |
|
|
// Wizard forms for our simple info collection wizard.
|
545 |
|
|
|
546 |
|
|
/**
|
547 |
|
|
* Wizard start form. Choose an animal.
|
548 |
|
|
*/
|
549 |
|
|
function ctools_ajax_sample_start($form, &$form_state) {
|
550 |
|
|
$form_state['title'] = t('Choose animal');
|
551 |
|
|
|
552 |
|
|
$animals = ctools_ajax_sample_animals();
|
553 |
|
|
foreach ($animals as $id => $animal) {
|
554 |
|
|
$options[$id] = $animal['title'];
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
$form['type'] = array(
|
558 |
|
|
'#title' => t('Choose your animal'),
|
559 |
|
|
'#type' => 'radios',
|
560 |
|
|
'#options' => $options,
|
561 |
|
|
'#default_value' => $form_state['object']->type,
|
562 |
|
|
'#required' => TRUE,
|
563 |
|
|
);
|
564 |
|
|
|
565 |
|
|
return $form;
|
566 |
|
|
}
|
567 |
|
|
|
568 |
|
|
/**
|
569 |
|
|
* They have selected a sheep. Set it.
|
570 |
|
|
*/
|
571 |
|
|
function ctools_ajax_sample_start_submit(&$form, &$form_state) {
|
572 |
|
|
$form_state['object']->type = $form_state['values']['type'];
|
573 |
|
|
// Override where to go next based on the animal selected.
|
574 |
|
|
$form_state['clicked_button']['#next'] = $form_state['values']['type'];
|
575 |
|
|
}
|
576 |
|
|
|
577 |
|
|
/**
|
578 |
|
|
* Wizard form to configure your sheep.
|
579 |
|
|
*/
|
580 |
|
|
function ctools_ajax_sample_configure_sheep($form, &$form_state) {
|
581 |
|
|
$form_state['title'] = t('Configure sheep');
|
582 |
|
|
|
583 |
|
|
$form['name'] = array(
|
584 |
|
|
'#type' => 'textfield',
|
585 |
|
|
'#title' => t('Name your sheep'),
|
586 |
|
|
'#default_value' => $form_state['object']->name,
|
587 |
|
|
'#required' => TRUE,
|
588 |
|
|
);
|
589 |
|
|
|
590 |
|
|
$form['sheep'] = array(
|
591 |
|
|
'#title' => t('What kind of sheep'),
|
592 |
|
|
'#type' => 'radios',
|
593 |
|
|
'#options' => array(
|
594 |
|
|
t('Wensleydale') => t('Wensleydale'),
|
595 |
|
|
t('Merino') => t('Merino'),
|
596 |
|
|
t('Corriedale') => t('Coriedale'),
|
597 |
|
|
),
|
598 |
|
|
'#default_value' => !empty($form_state['object']->sheep) ? $form_state['object']->sheep : '',
|
599 |
|
|
'#required' => TRUE,
|
600 |
|
|
);
|
601 |
|
|
return $form;
|
602 |
|
|
}
|
603 |
|
|
|
604 |
|
|
/**
|
605 |
|
|
* Submit the sheep and store the values from the form.
|
606 |
|
|
*/
|
607 |
|
|
function ctools_ajax_sample_configure_sheep_submit(&$form, &$form_state) {
|
608 |
|
|
$form_state['object']->name = $form_state['values']['name'];
|
609 |
|
|
$form_state['object']->sheep = $form_state['values']['sheep'];
|
610 |
|
|
}
|
611 |
|
|
|
612 |
|
|
/**
|
613 |
|
|
* Provide some output for our sheep.
|
614 |
|
|
*/
|
615 |
|
|
function ctools_ajax_sample_show_sheep($object) {
|
616 |
|
|
return t('You have a @type sheep named "@name".', array(
|
617 |
|
|
'@type' => $object->sheep,
|
618 |
|
|
'@name' => $object->name,
|
619 |
|
|
));
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
/**
|
623 |
|
|
* Wizard form to configure your lizard.
|
624 |
|
|
*/
|
625 |
|
|
function ctools_ajax_sample_configure_lizard($form, &$form_state) {
|
626 |
|
|
$form_state['title'] = t('Configure lizard');
|
627 |
|
|
|
628 |
|
|
$form['name'] = array(
|
629 |
|
|
'#type' => 'textfield',
|
630 |
|
|
'#title' => t('Name your lizard'),
|
631 |
|
|
'#default_value' => $form_state['object']->name,
|
632 |
|
|
'#required' => TRUE,
|
633 |
|
|
);
|
634 |
|
|
|
635 |
|
|
$form['lizard'] = array(
|
636 |
|
|
'#title' => t('Venomous'),
|
637 |
|
|
'#type' => 'checkbox',
|
638 |
|
|
'#default_value' => !empty($form_state['object']->lizard),
|
639 |
|
|
);
|
640 |
|
|
return $form;
|
641 |
|
|
}
|
642 |
|
|
|
643 |
|
|
/**
|
644 |
|
|
* Submit the lizard and store the values from the form.
|
645 |
|
|
*/
|
646 |
|
|
function ctools_ajax_sample_configure_lizard_submit(&$form, &$form_state) {
|
647 |
|
|
$form_state['object']->name = $form_state['values']['name'];
|
648 |
|
|
$form_state['object']->lizard = $form_state['values']['lizard'];
|
649 |
|
|
}
|
650 |
|
|
|
651 |
|
|
/**
|
652 |
|
|
* Provide some output for our raptor.
|
653 |
|
|
*/
|
654 |
|
|
function ctools_ajax_sample_show_lizard($object) {
|
655 |
|
|
return t('You have a @type lizard named "@name".', array(
|
656 |
|
|
'@type' => empty($object->lizard) ? t('non-venomous') : t('venomous'),
|
657 |
|
|
'@name' => $object->name,
|
658 |
|
|
));
|
659 |
|
|
}
|
660 |
|
|
|
661 |
|
|
/**
|
662 |
|
|
* Wizard form to configure your raptor.
|
663 |
|
|
*/
|
664 |
|
|
function ctools_ajax_sample_configure_raptor($form, &$form_state) {
|
665 |
|
|
$form_state['title'] = t('Configure raptor');
|
666 |
|
|
|
667 |
|
|
$form['name'] = array(
|
668 |
|
|
'#type' => 'textfield',
|
669 |
|
|
'#title' => t('Name your raptor'),
|
670 |
|
|
'#default_value' => $form_state['object']->name,
|
671 |
|
|
'#required' => TRUE,
|
672 |
|
|
);
|
673 |
|
|
|
674 |
|
|
$form['raptor'] = array(
|
675 |
|
|
'#title' => t('What kind of raptor'),
|
676 |
|
|
'#type' => 'radios',
|
677 |
|
|
'#options' => array(
|
678 |
|
|
t('Eagle') => t('Eagle'),
|
679 |
|
|
t('Hawk') => t('Hawk'),
|
680 |
|
|
t('Owl') => t('Owl'),
|
681 |
|
|
t('Buzzard') => t('Buzzard'),
|
682 |
|
|
),
|
683 |
|
|
'#default_value' => !empty($form_state['object']->raptor) ? $form_state['object']->raptor : '',
|
684 |
|
|
'#required' => TRUE,
|
685 |
|
|
);
|
686 |
|
|
|
687 |
|
|
$form['domesticated'] = array(
|
688 |
|
|
'#title' => t('Domesticated'),
|
689 |
|
|
'#type' => 'checkbox',
|
690 |
|
|
'#default_value' => !empty($form_state['object']->domesticated),
|
691 |
|
|
);
|
692 |
|
|
return $form;
|
693 |
|
|
}
|
694 |
|
|
|
695 |
|
|
/**
|
696 |
|
|
* Submit the raptor and store the values from the form.
|
697 |
|
|
*/
|
698 |
|
|
function ctools_ajax_sample_configure_raptor_submit(&$form, &$form_state) {
|
699 |
|
|
$form_state['object']->name = $form_state['values']['name'];
|
700 |
|
|
$form_state['object']->raptor = $form_state['values']['raptor'];
|
701 |
|
|
$form_state['object']->domesticated = $form_state['values']['domesticated'];
|
702 |
|
|
}
|
703 |
|
|
|
704 |
|
|
/**
|
705 |
|
|
* Provide some output for our raptor.
|
706 |
|
|
*/
|
707 |
|
|
function ctools_ajax_sample_show_raptor($object) {
|
708 |
|
|
return t('You have a @type @raptor named "@name".', array(
|
709 |
|
|
'@type' => empty($object->domesticated) ? t('wild') : t('domesticated'),
|
710 |
|
|
'@raptor' => $object->raptor,
|
711 |
|
|
'@name' => $object->name,
|
712 |
|
|
));
|
713 |
|
|
}
|
714 |
|
|
|
715 |
|
|
/**
|
716 |
|
|
* Helper function to provide a sample jump menu form
|
717 |
|
|
*/
|
718 |
|
|
function ctools_ajax_sample_jump_menu_form() {
|
719 |
|
|
$url = url('ctools_ajax_sample/jumped');
|
720 |
|
|
$form_state = array();
|
721 |
|
|
$form = ctools_jump_menu(array(), $form_state, array($url => t('Jump!')), array());
|
722 |
|
|
return $form;
|
723 |
|
|
}
|
724 |
|
|
|
725 |
|
|
/**
|
726 |
|
|
* Provide a message to the user that the jump menu worked
|
727 |
|
|
*/
|
728 |
|
|
function ctools_ajax_sample_jump_menu_page() {
|
729 |
|
|
$return_link = l(t('Return to the examples page.'), 'ctools_ajax_sample');
|
730 |
|
|
$output = t('You successfully jumped! !return_link', array('!return_link' => $return_link));
|
731 |
|
|
return $output;
|
732 |
|
|
}
|
733 |
|
|
|
734 |
|
|
/**
|
735 |
|
|
* Provide a form for an example ajax modal button
|
736 |
|
|
*/
|
737 |
|
|
function ctools_ajax_sample_ajax_button_form() {
|
738 |
|
|
$form = array();
|
739 |
|
|
|
740 |
|
|
$form['url'] = array(
|
741 |
|
|
'#type' => 'hidden',
|
742 |
|
|
// The name of the class is the #id of $form['ajax_button'] with "-url"
|
743 |
|
|
// suffix.
|
744 |
|
|
'#attributes' => array('class' => array('ctools-ajax-sample-button-url')),
|
745 |
|
|
'#value' => url('ctools_ajax_sample/nojs/animal'),
|
746 |
|
|
);
|
747 |
|
|
|
748 |
|
|
$form['ajax_button'] = array(
|
749 |
|
|
'#type' => 'button',
|
750 |
|
|
'#value' => 'Wizard (button modal)',
|
751 |
|
|
'#attributes' => array('class' => array('ctools-use-modal')),
|
752 |
|
|
'#id' => 'ctools-ajax-sample-button',
|
753 |
|
|
);
|
754 |
|
|
|
755 |
|
|
return $form;
|
756 |
|
|
} |