Stripping CVS keywords
[project/panels.git] / plugins / task_handlers / panel_context.inc
CommitLineData
172dca0a 1<?php
172dca0a
EM
2
3/**
4 * @file
5 *
6 * This is the task handler plugin to handle attaching a panel to any
7 * task that advertises itself as a 'context' type, which all of the
6a04891d 8 * basic page tasks provided by page_manager.module do by default.
172dca0a
EM
9 */
10
ffb9381e
EM
11// Plugin definition
12$plugin = array(
13 // is a 'context' handler type, meaning it supports the API of the
14 // context handlers provided by ctools context plugins.
15 'handler type' => 'context',
16 'visible' => TRUE, // may be added up front.
6a04891d 17
ffb9381e
EM
18 // Administrative fields.
19 'title' => t('Panel'),
20 'admin summary' =>'panels_panel_context_admin_summary',
21 'admin title' => 'panels_panel_context_title',
ffb9381e
EM
22 'operations' => array(
23 'settings' => array(
24 'title' => t('General'),
25 'description' => t('Change general settings about this variant.'),
26 'form' => 'panels_panel_context_edit_settings',
6a04891d 27 ),
ffb9381e
EM
28 'criteria' => array(
29 'title' => t('Selection rules'),
30 'description' => t('Control the criteria used to decide whether or not this variant is used.'),
c243f983 31 'ajax' => FALSE,
ffb9381e
EM
32 'form' => array(
33 'order' => array(
34 'form' => t('Selection rules'),
35 ),
36 'forms' => array(
37 'form' => array(
38 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
39 'form id' => 'ctools_context_handler_edit_criteria',
6a04891d
EM
40 ),
41 ),
42 ),
ffb9381e
EM
43 ),
44 'context' => array(
45 'title' => t('Contexts'),
2a2d6206 46 'ajax' => FALSE,
ffb9381e
EM
47 'description' => t('Add additional context objects to this variant that can be used by the content.'),
48 'form' => array(
49 'order' => array(
50 'form' => t('Context'),
51 ),
52 'forms' => array(
53 'form' => array(
54 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
55 'form id' => 'ctools_context_handler_edit_context',
6a04891d
EM
56 ),
57 ),
58 ),
ffb9381e
EM
59 ),
60 'layout' => array(
61 'title' => t('Layout'),
62 'description' => t('Change the layout of this panel.'),
63 // No AJAX so we get our CSS loaded.
64 'ajax' => FALSE,
65 'form' => array(
66 'order' => array(
67 'choose' => t('Change layout'),
68 'move' => t('Move content from old layout'),
69 ),
70 'forms' => array(
71 'choose' => array(
72 'form id' => 'panels_panel_context_edit_layout',
6a04891d 73 ),
ffb9381e
EM
74 'move' => array(
75 'include' => array(
76 drupal_get_path('module', 'panels') . '/includes/display-layout.inc',
6a04891d 77 ),
ffb9381e
EM
78 'form id' => 'panels_panel_context_edit_move',
79 'submit' => 'panels_change_layout_submit',
6a04891d
EM
80 ),
81 ),
82 ),
ffb9381e
EM
83 ),
84 'content' => array(
85 'title' => t('Content'),
86 'description' => t('Add content items and change their location with a drag and drop interface.'),
87 'ajax' => FALSE,
88 'form' => array(
89 'order' => array(
90 'form' => t('Content'),
91 ),
92 'forms' => array(
93 'form' => array(
94 'include' => array(
95 drupal_get_path('module', 'panels') . '/includes/display-edit.inc',
6a04891d 96 ),
ffb9381e
EM
97 'form id' => 'panels_panel_context_edit_content',
98 'no blocks' => TRUE,
6a04891d
EM
99 ),
100 ),
101 ),
102 ),
ffb9381e
EM
103 'preview' => array(
104 'title' => t('Preview'),
105 'description' => t('Get a preview of what this variant will look like.'),
106 'form' => 'panels_panel_context_edit_preview',
107 'ajax' => FALSE,
108 'silent' => TRUE,
109 'form info' => array('finish text' => t('Preview')),
110 ),
111 ),
6a04891d 112
bd8cdec1
EM
113 'tab operation' => 'panels_panel_context_tab_operation',
114
ffb9381e
EM
115 // Callback to render the data.
116 'render' => 'panels_panel_context_render',
6a04891d 117
ffb9381e
EM
118 // Various callbacks for operations performed on the handler to ensure
119 // related data is updated properly.
120 'save' => 'panels_panel_context_save',
121 'delete' => 'panels_panel_context_delete',
122 'export' => 'panels_panel_context_export',
123 'clone' => 'panels_panel_context_clone',
172dca0a 124
ffb9381e
EM
125 'add features' => array(
126 'criteria' => t('Selection rules'),
127 'context' => t('Contexts'),
128 ),
129 // Where to go when finished.
130 'add finish' => 'content',
6a04891d 131
ffb9381e
EM
132 'required forms' => array(
133 'choose' => t('Choose layout'),
84918fe2 134 'settings' => t('Panel settings'),
ffb9381e
EM
135 'content' => t('Panel content'),
136 ),
6a04891d 137
ffb9381e
EM
138 'edit forms' => array(
139 'content' => t('Panel content'),
140 'criteria' => t('Selection rules'),
141 'settings' => t('General'),
142 'context' => t('Contexts'),
143 'layout' => t('Change layout'),
144 'move' => '', // no title makes it a 'hidden' edit form.
145 ),
146 'forms' => array(
147 'settings' => array(
148 'form id' => 'panels_panel_context_edit_settings',
ae06d315 149 ),
ffb9381e
EM
150 'choose' => array(
151 'form id' => 'panels_panel_context_edit_choose',
152 'no back validate' => TRUE,
153 ),
154 'layout' => array(
155 'no return' => TRUE,
156 'form id' => 'panels_panel_context_edit_layout',
157 ),
158 'move' => array(
159 'include' => array(
160 drupal_get_path('module', 'panels') . '/includes/display-layout.inc',
172dca0a 161 ),
ffb9381e
EM
162 'form id' => 'panels_panel_context_edit_move',
163 'submit' => 'panels_change_layout_submit',
164 ),
165 'content' => array(
166 'include' => array(
167 drupal_get_path('module', 'panels') . '/includes/display-edit.inc',
ae06d315 168 ),
ffb9381e
EM
169 'form id' => 'panels_panel_context_edit_content',
170 'no blocks' => TRUE,
ae06d315 171 ),
ffb9381e
EM
172 'context' => array(
173 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
174 'form id' => 'ctools_context_handler_edit_context',
172dca0a 175 ),
ffb9381e
EM
176 'criteria' => array(
177 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
178 'form id' => 'ctools_context_handler_edit_criteria',
179 ),
180 ),
181 'default conf' => array(
182 'title' => t('Panel'),
183 'no_blocks' => FALSE,
70395f2d 184 'pipeline' => 'standard',
ffb9381e
EM
185 'css_id' => '',
186 'css' => '',
187 'contexts' => array(),
188 'relationships' => array(),
189 ),
190);
172dca0a
EM
191
192/**
bd8cdec1
EM
193 * Provide the operation trail for the 'Edit panel' link.
194 *
195 * When editing the panel, go directly to the content tab.
196 */
197function panels_panel_context_tab_operation($handler, $contexts, $args) {
198 return array('handlers', $handler->name, 'content');
199}
200
201/**
b263dd82
EM
202 * Get the display for a task handler.
203 *
204 * There are three methods that the display can be found.
205 * - In the database. $handler->conf['did'] will be set in this case,
206 * and $handler->conf['display'] won't be.
207 * - In $handler->conf['display'], with $handler->conf['did'] empty. This
208 * will be true for a default/imported task handler as well as a handler
209 * that has just been created but has not yet been saved.
210 * - in $handler->conf['display'] with $handler->conf['did' populated. This
211 * simply means that the display has been modified and is awaiting
212 * save. The modified one should always be used for editing purposes.
213 * - If none of the above is true, then a new display needs to be created
214 * for the handler and pla
215 */
216function &panels_panel_context_get_display(&$handler) {
217 if (isset($handler->conf['display'])) {
218 return $handler->conf['display'];
219 }
220
221 if (isset($handler->conf['did'])) {
222 $handler->conf['display'] = panels_load_display($handler->conf['did']);
223
224 // Check for a valid display. If no valid display can be loaded, something
225 // is wrong and we'll create a new one.
226 if (!empty($handler->conf['display'])) {
227 return $handler->conf['display'];
228 }
229 }
230
231 $handler->conf['display'] = panels_new_display();
232
233 return $handler->conf['display'];
234}
235
236/**
80688d7e 237 * Check selection rules and, if passed, render the contexts.
172dca0a 238 */
ab4648f3 239function panels_panel_context_render($handler, $base_contexts, $args, $test = TRUE) {
172dca0a 240 // Go through arguments and see if they match.
2d5c8606 241 ctools_include('context');
172dca0a 242 ctools_include('context-task-handler');
70395f2d
EM
243 ctools_include('plugins', 'panels');
244
b04dcb3a
EM
245 // Add my contexts
246 $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
247
248 // Test.
d84ff3ec 249 if ($test && !ctools_context_handler_select($handler, $contexts)) {
172dca0a
EM
250 return;
251 }
252
73da3435
EM
253 if (isset($handler->handler)) {
254 ctools_context_handler_pre_render($handler, $contexts, $args);
255 }
b6c2e4e7 256
172dca0a 257 // Load the display
b263dd82
EM
258 $display = panels_panel_context_get_display($handler);
259
172dca0a 260 $display->context = $contexts;
ab4648f3 261 $display->args = $args;
172dca0a 262 $display->css_id = $handler->conf['css_id'];
66ce3318
EM
263 $task_name = page_manager_make_task_name($handler->task, $handler->subtask);
264
265 $display->cache_key = 'panel_context:' . $task_name . ':' . $handler->name;
172dca0a
EM
266
267 // Check to see if there is any CSS.
8ae66079
EM
268 if (!empty($handler->conf['css'])) {
269 ctools_include('css');
270 $css_id = 'panel_context:' . $handler->name;
271 $filename = ctools_css_retrieve($css_id);
272 if (!$filename) {
273 $filename = ctools_css_store($css_id, $handler->conf['css']);
172dca0a 274 }
5d86d06c 275 ctools_css_add_css($filename);
172dca0a
EM
276 }
277
2505a50c
EM
278 // With an argument, this actually sets the display.
279 panels_get_current_page_display($display);
e68ca0b2 280
70395f2d
EM
281 // Handle backward compatibility with the IPE checkbox.
282 if (empty($handler->conf['pipeline'])) {
283 $handler->conf['pipeline'] = !empty($handler->conf['use_ipe']) ? 'ipe' : 'standard';
517fd2f1
SB
284 }
285
70395f2d
EM
286 $renderer = panels_get_renderer($handler->conf['pipeline'], $display);
287
81df9ec2 288 $info = array(
517fd2f1 289 'content' => panels_render_display($display, $renderer),
81df9ec2
EM
290 'no_blocks' => !empty($handler->conf['no_blocks']),
291 );
292
6f68e54d 293 $info['title'] = $display->get_title();
81df9ec2
EM
294
295 return $info;
172dca0a
EM
296}
297
298/**
172dca0a
EM
299 * Callback to allow the handler to react to being saved.
300 *
301 * When a handler with a display is saved, two things have to happen.
e3d69f60
EM
302 * First, we have to save the display so that it becomes a real display,
303 * not the fake one we started with. Second, we have to cache
172dca0a
EM
304 * any CSS that the display is using. This CSS can get re-cached
305 * later if the file disappears, but it's imperative that we do it here
306 * to make sure that old, dirty CSS cache gets removed.
307 */
308function panels_panel_context_save(&$handler, $update) {
b263dd82
EM
309 // Only save the display if we believe it has been modified.
310 if (isset($handler->conf['display'])) {
311 panels_save_display($handler->conf['display']);
312 $handler->conf['did'] = $handler->conf['display']->did;
313 unset($handler->conf['display']);
172dca0a
EM
314 }
315
8ae66079
EM
316 // Delete any previous CSS cache file.
317 ctools_include('css');
318 ctools_css_clear('panel_context:' . $handler->name);
e3d69f60
EM
319
320 if (isset($page->conf['temp_layout'])) {
321 unset($page->conf['temp_layout']);
322 }
172dca0a
EM
323}
324
325/**
326 * Special handling for exporting a panel task handler.
327 *
328 * When a panel is exported, we need to export the display separately
329 * rather than just letting its object be unpacked, which does not work
330 * very well.
331 */
332function panels_panel_context_export(&$handler, $indent) {
b263dd82 333 $display = panels_panel_context_get_display($handler);
e3d69f60
EM
334 foreach (array('display', 'did', 'css_cache', 'temp_layout') as $item) {
335 if (isset($handler->conf[$item])) {
336 unset($handler->conf[$item]);
337 }
b263dd82 338 }
172dca0a
EM
339
340 $output = panels_export_display($display, $indent);
341 $output .= $indent . '$handler->conf[\'display\'] = $display' . ";\n";
342 return $output;
343}
344
345/**
bee68a97
EM
346 * When a handler is cloned, we have to clone the display.
347 */
348 function panels_panel_context_clone(&$handler) {
349 $old_display = panels_panel_context_get_display($handler);
350 $code = panels_export_display($old_display);
351 eval($code);
352 foreach (array('display', 'did', 'css_cache', 'temp_layout') as $item) {
353 if (isset($handler->conf[$item])) {
354 unset($handler->conf[$item]);
355 }
356 }
357 $display->did = 'new';
358 $handler->conf['display'] = $display;
359}
360
361/**
e3d69f60
EM
362 * Callback to delete the display when a handler is deleted.
363 */
364function panels_panel_context_delete(&$handler) {
365 if (!empty($handler->conf['did'])) {
366 panels_delete_display($handler->conf['did']);
367 }
368}
369
370/**
361470d2
EM
371 * Set up a title for the panel based upon the selection rules.
372 */
373function panels_panel_context_title($handler, $task, $subtask) {
374 if (isset($handler->conf['title'])) {
375 return check_plain($handler->conf['title']);
376 }
377 else {
378 return t('Panel');
379 }
380}
381
382/**
172dca0a
EM
383 * Provide a nice little summary of what's in a panel.
384 *
385 * The task handler manager provides a summary of a given handler in a
386 * collapsible div. This callback provides that. For a Panel, we
387 * provide a summary of the layout type and content on one side, and
388 * a summary of the contexts in use on the other.
389 */
b6c2e4e7 390function panels_panel_context_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) {
361470d2 391 $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
172dca0a 392 $output = '';
385b9f41 393
b263dd82 394 $display = panels_panel_context_get_display($handler);
172dca0a 395
0fc23cd0 396 ctools_include('plugins', 'panels');
172dca0a 397 ctools_include('context');
b04dcb3a
EM
398 ctools_include('context-task-handler');
399
b6c2e4e7
EM
400 // Get the operations
401 $operations = page_manager_get_operations($page);
402
403 // Get operations for just this handler.
404 $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children'];
998c3b6a
EM
405 $args = array('handlers', $handler->name, 'actions');
406 $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => 'actions'), 'actions', $args);
b6c2e4e7 407
172dca0a 408 $layout = panels_get_layout($display->layout);
172dca0a 409
6a04891d 410 $plugin = page_manager_get_task_handler($handler->handler);
172dca0a 411
6a04891d 412 $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
b04dcb3a 413 $display->context = ctools_context_load_contexts($object, TRUE);
172dca0a 414
361470d2
EM
415 $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $display->context);
416 if ($access) {
417 $access = t('This panel will be selected if @conditions.', array('@conditions' => $access));
418 }
419 else {
420 $access = t('This panel will always be selected.');
421 }
422
423 $rows = array();
424
5f2e6e5a
EM
425 $type = $handler->type == t('Default') ? t('In code') : $handler->type;
426 $rows[] = array(
427 array('class' => t('page-summary-label'), 'data' => t('Storage')),
428 array('class' => t('page-summary-data'), 'data' => $type),
429 array('class' => t('page-summary-operation'), 'data' => ''),
430 );
431
432 if (!empty($handler->disabled)) {
433 $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'enable')));
434 $text = t('Disabled');
435 }
436 else {
437 $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'disable')));
438 $text = t('Enabled');
439 }
440
441 $rows[] = array(
442 array('class' => t('page-summary-label'), 'data' => t('Status')),
443 array('class' => t('page-summary-data'), 'data' => $text),
444 array('class' => t('page-summary-operation'), 'data' => $link),
445 );
446
361470d2
EM
447 $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'criteria')));
448 $rows[] = array(
449 array('class' => t('page-summary-label'), 'data' => t('Selection rule')),
450 array('class' => t('page-summary-data'), 'data' => $access),
451 array('class' => t('page-summary-operation'), 'data' => $link),
452 );
453
51ddcbf4
EM
454 $link = l(t('Change layout'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'layout')));
455 $link .= '<br />' . l(t('Edit content'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'content')));
456 $link .= '<br />' . l(t('Preview'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'preview')));
361470d2
EM
457 $rows[] = array(
458 array('class' => t('page-summary-label'), 'data' => t('Layout')),
459 array('class' => t('page-summary-data'), 'data' => check_plain($layout['title'])),
460 array('class' => t('page-summary-operation'), 'data' => $link),
461 );
462
463 $content_link = ' [' . l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'content'))) . ']';
464 $context_link = ' [' . l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'context'))) . ']';
b6c2e4e7 465
361470d2
EM
466 $info = theme('table', array(), $rows, array('class' => 'page-manager-handler-summary'));
467
540ebafe 468/*
172dca0a 469 $content = theme('panels_common_content_list', $display);
361470d2
EM
470 if (empty($contents)) {
471 $contents = t('This panel has no content.');
472 }
b04dcb3a 473 $contexts = theme('ctools_context_list_no_table', $object);
361470d2
EM
474 if (empty($contexts)) {
475 $contexts = t('This panel has no contexts.');
476 }
540ebafe 477*/
172dca0a 478
361470d2
EM
479 $title = $handler->conf['title'];
480 if ($title != t('Panel')) {
481 $title = t('Panel: @title', array('@title' => $title));
482 }
5f2e6e5a 483
361470d2 484 $output .= '<div class="clear-block">';
b6c2e4e7 485 if ($show_title) {
c01f833f 486 $output .= '<div class="handler-title clear-block">';
b6c2e4e7
EM
487 $output .= '<div class="actions handler-actions">' . $rendered_operations['actions'] . '</div>';
488 $output .= '<span class="title-label">' . $title . '</span>';
489 }
490
491 $output .= '</div>';
361470d2
EM
492 $output .= $info;
493 $output .= '</div>';
494/*
172dca0a 495 $output .= '<div class="right-container">';
361470d2 496 $output .= '<h3 class="context-title">' . t('Contexts') . $context_link . '</h3>';
172dca0a
EM
497 $output .= $contexts;
498 $output .= '</div>';
499
500 $output .= '<div class="left-container">';
361470d2
EM
501// $output .= $icon;
502 $output .= '<h3 class="handler-title">' . t('Content') . $content_link . '</h3>';
503 $output .= $content;
172dca0a 504 $output .= '</div>';
361470d2 505*/
172dca0a
EM
506 return $output;
507}
508
172dca0a
EM
509// --------------------------------------------------------------------------
510// Forms
511
512/**
513 * General notes about forms: The handler is automatically cached by the form
514 * wizard, so anything we store on $form_state['handler'] anywhere will get
515 * saved and appear on the next form. The cache is a 'working' cache and
516 * if the user hits cancel on any page of the multi-page wizard, all
517 * changes since the last 'update/finish' click will be flushed away.
518 *
519 * Many of the Panels forms call through to the real Panels cousins. These
520 * forms are smart enough to know that they're being wrapped in another
521 * form and act appropriately. Some of them are so smart that we just let
522 * their submit and validate handlers do the work rather than writing
523 * additional ones here.
524 */
525
526/**
527 * Choose a layout for this panel.
528 *
529 * This is only called during 'add', when we know that there isn't a
530 * previous layout to choose from. a different, only slightly different
531 * variant is called to change a pre-existing layout.
532 */
533function panels_panel_context_edit_choose(&$form, &$form_state) {
0fc23cd0
SB
534 ctools_include('common', 'panels');
535 ctools_include('display-layout', 'panels');
536 ctools_include('plugins', 'panels');
172dca0a
EM
537
538 // @todo -- figure out where/how to deal with this.
b2313b92 539 $form_state['allowed_layouts'] = 'panels_page';
172dca0a 540
b263dd82 541 $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
172dca0a
EM
542
543 // Tell the Panels form not to display buttons.
544 $form_state['no buttons'] = TRUE;
545
546 // Change the #id of the form so the CSS applies properly.
547 $form['#id'] = 'panels-choose-layout';
548 $form = array_merge($form, panels_choose_layout($form_state));
549}
550
551/**
552 * Validate that a layout was chosen.
553 */
554function panels_panel_context_edit_choose_validate(&$form, &$form_state) {
555 if (empty($form_state['values']['layout'])) {
556 form_error($form['layout'], t('You must select a layout.'));
557 }
558}
559
560/**
561 * A layout has been selected, set it up.
562 */
563function panels_panel_context_edit_choose_submit(&$form, &$form_state) {
564 $form_state['display']->layout = $form_state['values']['layout'];
565 $form_state['handler']->conf['display'] = $form_state['display'];
51ddcbf4
EM
566 if (isset($form_state['page']->display_cache[$form_state['handler_id']])) {
567 $form_state['page']->display_cache[$form_state['handler_id']]->display = $form_state['display'];
568 }
172dca0a
EM
569}
570
172dca0a
EM
571/**
572 * Change the layout for this panel.
573 *
574 * This form is only used if a layout already exists and the user wants
575 * to change to a different one. The submit handler changes the next form
576 * to the move content form, which is 'hidden' so it won't be accessed
577 * directly.
578 */
579function panels_panel_context_edit_layout(&$form, &$form_state) {
0fc23cd0
SB
580 ctools_include('common', 'panels');
581 ctools_include('display-layout', 'panels');
582 ctools_include('plugins', 'panels');
172dca0a
EM
583
584 // @todo -- figure out where/how to deal with this.
b2313b92 585 $form_state['allowed_layouts'] = 'panels_page';
172dca0a 586
b263dd82 587 $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
172dca0a
EM
588
589 // Tell the Panels form not to display buttons.
590 $form_state['no buttons'] = TRUE;
591
592 // Change the #id of the form so the CSS applies properly.
593 $form['#id'] = 'panels-choose-layout';
594 $form = array_merge($form, panels_choose_layout($form_state));
595}
596
597/**
598 * Validate that a layout was chosen.
599 */
600function panels_panel_context_edit_layout_validate(&$form, &$form_state) {
4bec7e7f
EM
601 $display = &panels_panel_context_get_display($form_state['handler']);
602
172dca0a
EM
603 if (empty($form_state['values']['layout'])) {
604 form_error($form['layout'], t('You must select a layout.'));
605 }
4bec7e7f
EM
606 if ($form_state['values']['layout'] == $display->layout) {
607 form_error($form['layout'], t('You must select a different layout if you wish to change layouts.'));
608 }
172dca0a
EM
609}
610
611/**
612 * A layout has been selected, set it up.
613 */
614function panels_panel_context_edit_layout_submit(&$form, &$form_state) {
b263dd82
EM
615 $display = &panels_panel_context_get_display($form_state['handler']);
616
617 if ($form_state['values']['layout'] != $display->layout) {
172dca0a 618 $form_state['handler']->conf['temp_layout'] = $form_state['values']['layout'];
172dca0a
EM
619 }
620}
621
622/**
623 * When a layout is changed, the user is given the opportunity to move content.
624 */
625function panels_panel_context_edit_move(&$form, &$form_state) {
b263dd82 626 $form_state['display'] = &panels_panel_context_get_display($form_state['handler']);
172dca0a
EM
627 $form_state['layout'] = $form_state['handler']->conf['temp_layout'];
628
0fc23cd0
SB
629 ctools_include('common', 'panels');
630 ctools_include('display-layout', 'panels');
631 ctools_include('plugins', 'panels');
172dca0a
EM
632
633 // Tell the Panels form not to display buttons.
634 $form_state['no buttons'] = TRUE;
635
636 // Change the #id of the form so the CSS applies properly.
637 $form = array_merge($form, panels_change_layout($form_state));
638
639 // Change the 'back' button to just go directly to the previous form
80688d7e
EM
640// $task_id = $form_state['task']['name'];
641// $handler_id = $form_state['handler']->handler;
642// $name = $form_state['handler']->name;
172dca0a
EM
643
644 // This form is outside the normal wizard list, so we need to specify the
645 // previous/next forms.
646 $form['buttons']['previous']['#next'] = 'layout';
efcf7fcf 647 $form['buttons']['next']['#next'] = 'content';
6a04891d 648
dce7d3e4 649 $form_state['form_info']['return path'] = page_manager_edit_url($form_state['page']->task_name, array('handlers', $form_state['handler_id'], 'content'));
172dca0a
EM
650}
651
652/**
653 * Present the panels drag & drop editor to edit the display attached
654 * to the task handler.
655 */
656function panels_panel_context_edit_content(&$form, &$form_state) {
95f6686a 657 ctools_include('ajax');
0fc23cd0
SB
658 ctools_include('plugins', 'panels');
659 ctools_include('common', 'panels');
b04dcb3a
EM
660 ctools_include('context');
661 ctools_include('context-task-handler');
172dca0a 662
026b6377 663 $cache = panels_edit_cache_get('panel_context:' . $form_state['task_name'] . ':' . $form_state['handler_id']);
172dca0a 664
b95ec55e
EM
665 $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
666 $form_state['renderer']->cache = &$cache;
667
026b6377
EM
668 $form_state['display'] = &$cache->display;
669 $form_state['content_types'] = $cache->content_types;
172dca0a
EM
670 // Tell the Panels form not to display buttons.
671 $form_state['no buttons'] = TRUE;
8650be77 672 $form_state['display_title'] = !empty($cache->display_title);
aa263be0 673 $form_state['no preview'] = TRUE;
51ddcbf4 674 $form_state['page']->display_cache[$form_state['handler_id']] = $cache;
172dca0a
EM
675
676 $form = array_merge($form, panels_edit_display_form($form_state));
677 // Make sure the theme will work since our form id is different.
678 $form['#theme'] = 'panels_edit_display_form';
c6884d42 679
fb5fc4a1 680 if (!isset($form_state['type']) || $form_state['type'] != 'add' && !empty($form_state['handler_id']) && !empty($form['buttons'])) {
f41a3964
EM
681 $form['buttons']['preview'] = $form['buttons']['return'];
682 $form['buttons']['preview']['#value'] = t('Update and preview');
683 }
c6884d42
EM
684}
685
686function panels_panel_context_edit_content_submit(&$form, &$form_state) {
687 panels_edit_display_form_submit($form, $form_state);
026b6377 688 $handler = &$form_state['handler'];
51ddcbf4
EM
689
690 // update the cached display:
026b6377
EM
691 $display = $form_state['page']->display_cache[$form_state['handler_id']]->display;
692 $handler->conf['display'] = $display;
026b6377
EM
693 unset($form_state['page']->display_cache[$form_state['handler_id']]);
694
c6884d42
EM
695 if ($form_state['clicked_button']['#value'] == t('Update and preview')) {
696 $form_state['new trail'] = array('handlers', $form_state['handler_id'], 'preview');
697 }
172dca0a
EM
698}
699
700/**
701 * General settings for the panel
702 */
703function panels_panel_context_edit_settings(&$form, &$form_state) {
704 $conf = $form_state['handler']->conf;
b04dcb3a
EM
705 $form['conf']['title'] = array(
706 '#type' => 'textfield',
707 '#default_value' => $conf['title'],
708 '#title' => t('Administrative title'),
6a04891d 709 '#description' => t('Administrative title of this variant.'),
b04dcb3a
EM
710 );
711
172dca0a
EM
712 $form['conf']['no_blocks'] = array(
713 '#type' => 'checkbox',
714 '#default_value' => $conf['no_blocks'],
715 '#title' => t('Disable Drupal blocks/regions'),
6a04891d 716 '#description' => t('Check this to have the page disable all regions displayed in the theme. Note that some themes support this setting better than others. If in doubt, try with stock themes to see.'),
172dca0a
EM
717 );
718
70395f2d
EM
719 ctools_include('plugins', 'panels');
720 $pipelines = panels_get_renderer_pipelines();
721
722 // Handle backward compatibility with the IPE checkbox.
723 if (empty($conf['pipeline'])) {
724 $conf['pipeline'] = !empty($conf['use_ipe']) ? 'ipe' : 'standard';
725 }
726
727 // If there are no pipelines, that probably means we're operating in
728 // legacy mode.
729 if (empty($pipelines)) {
730 // We retain the original pipeline so we don't wreck things by installing
731 // old modules.
732 $form['conf']['pipeline'] = array(
733 '#type' => 'value',
734 '#value' => $conf['pipeline'],
517fd2f1 735 );
70395f2d
EM
736 }
737 else {
738 $options = array();
739 foreach ($pipelines as $name => $pipeline) {
740 $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
ba5b9665 741 }
70395f2d
EM
742
743 $form['conf']['pipeline'] = array(
744 '#type' => 'radios',
745 '#options' => $options,
746 '#title' => t('Renderer'),
747 '#default_value' => $conf['pipeline'],
748 );
517fd2f1
SB
749 }
750
172dca0a
EM
751 $form['conf']['css_id'] = array(
752 '#type' => 'textfield',
753 '#size' => 35,
754 '#default_value' => $conf['css_id'],
755 '#title' => t('CSS ID'),
756 '#description' => t('The CSS ID to apply to this page'),
757 );
758
759 $form['conf']['css'] = array(
760 '#type' => 'textarea',
761 '#title' => t('CSS code'),
762 '#description' => t('Enter well-formed CSS code here; this code will be embedded into the page, and should only be used for minor adjustments; it is usually better to try to put CSS for the page into the theme if possible. This CSS will be filtered for safety so some CSS may not work.'),
763 '#default_value' => $conf['css'],
764 );
765}
766
767/**
768 * Submit handler for general settings form.
769 */
770function panels_panel_context_edit_settings_submit(&$form, &$form_state) {
771 $form_state['handler']->conf['no_blocks'] = $form_state['values']['no_blocks'];
70395f2d 772 $form_state['handler']->conf['pipeline'] = $form_state['values']['pipeline'];
172dca0a
EM
773 $form_state['handler']->conf['css_id'] = $form_state['values']['css_id'];
774 $form_state['handler']->conf['css'] = $form_state['values']['css'];
6a04891d 775 $form_state['handler']->conf['title'] = $form_state['values']['title'];
70395f2d
EM
776
777 // Unset the old checkbox so we don't store needless data.
778 if (isset($form_state['handler']->conf['use_ipe'])) {
779 unset($form_state['handler']->conf['use_ipe']);
780 }
172dca0a
EM
781}
782
aa263be0
EM
783/**
784 * Form to show a nice preview.
785 */
786function panels_panel_context_edit_preview(&$form, &$form_state) {
787 ctools_include('context');
788 ctools_include('context-task-handler');
789
790 $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']);
791 $form['preview'] = array();
792 ctools_context_replace_form($form['preview'], $contexts);
793
794 // automatically preview if there are no argument placeholders.
795 if (empty($form['preview'])) {
796 $display = panels_panel_context_get_display($form_state['handler']);
797 $display->context = $contexts;
7ac4d314 798 $display->skip_cache = TRUE;
aa263be0 799 $output = panels_render_display($display);
fb5fc4a1
EM
800 if (isset($form['buttons'])) {
801 unset($form['buttons']);
802 }
aa263be0
EM
803 }
804 else {
805 $form['preview']['#tree'] = TRUE;
806 $form_state['contexts'] = $contexts;
807 }
808
809 if (!empty($output)) {
810 $form['output'] = array(
811 '#value' => $output,
812 );
813 }
814
815 $form_state['do not cache'] = TRUE;
816}
817
818/**
819 * Display a preview upon submit if arguments were needed.
820 */
821function panels_panel_context_edit_preview_submit(&$form, &$form_state) {
822 $display = panels_panel_context_get_display($form_state['handler']);
823 $display->context = ctools_context_replace_placeholders($form_state['contexts'], $form_state['values']['preview']);
824
825 $form_state['content'] = panels_render_display($display);
826 $form_state['redirect'] = FALSE;
827 $form_state['rerender'] = TRUE;
828}