Stripping CVS keywords
[project/panels.git] / panels_ipe / plugins / display_renderers / panels_renderer_ipe.class.php
CommitLineData
a96d0939 1<?php
95346dfd
SB
2
3/**
b95ec55e 4 * Renderer class for all In-Place Editor (IPE) behavior.
95346dfd 5 */
b95ec55e
EM
6class panels_renderer_ipe extends panels_renderer_editor {
7 // The IPE operates in normal render mode, not admin mode.
8 var $admin = FALSE;
9
a96d0939 10 function render() {
94826bad 11 $output = parent::render();
66ce3318 12 return "<div id='panels-ipe-display-{$this->clean_key}' class='panels-ipe-display-container'>$output</div>";
94826bad
SB
13 }
14
15 function add_meta() {
b95ec55e
EM
16 ctools_include('display-edit', 'panels');
17 ctools_include('content');
18
66ce3318
EM
19 if (empty($this->display->cache_key)) {
20 $this->cache = panels_edit_cache_get_default($this->display);
21 }
22 // @todo we may need an else to load the cache, but I am not sure we
23 // actually need to load it if we already have our cache key, and doing
24 // so is a waste of resources.
b95ec55e 25
66ce3318
EM
26 ctools_include('cleanstring');
27 $this->clean_key = ctools_cleanstring($this->display->cache_key);
28 panels_ipe_get_cache_key($this->clean_key);
b95ec55e 29
c90672b4
SB
30 ctools_include('ajax');
31 ctools_include('modal');
32 ctools_modal_add_js();
b95ec55e
EM
33
34 ctools_add_css('panels_dnd', 'panels');
35 ctools_add_css('panels_admin', 'panels');
36 ctools_add_js('panels_ipe', 'panels_ipe');
37 ctools_add_css('panels_ipe', 'panels_ipe');
38
16382314 39 $settings = array(
b95ec55e 40 'formPath' => url($this->get_url('save-form')),
16382314 41 );
66ce3318
EM
42 drupal_add_js(array('PanelsIPECacheKeys' => array($this->clean_key)), 'setting');
43 drupal_add_js(array('PanelsIPESettings' => array($this->clean_key => $settings)), 'setting');
b95ec55e 44
a96d0939 45 jquery_ui_add(array('ui.draggable', 'ui.droppable', 'ui.sortable'));
94826bad 46 parent::add_meta();
a96d0939
SB
47 }
48
49 /**
fac4d041
SB
50 * Override & call the parent, then pass output through to the dnd wrapper
51 * theme function.
52 *
a96d0939
SB
53 * @param $pane
54 */
1e951eef 55 function render_pane(&$pane) {
fac4d041 56 $output = parent::render_pane($pane);
93b38577
SB
57 if (empty($output)) {
58 return;
59 }
b95ec55e 60
3d244c95
EM
61 if (empty($pane->IPE_empty)) {
62 // Add an inner layer wrapper to the pane content before placing it into
63 // draggable portlet
64 $output = "<div class=\"panels-ipe-portlet-content\">$output</div>";
65 }
66 else {
67 $output = "<div class=\"panels-ipe-portlet-content panels-ipe-empty-pane\">$output</div>";
68 }
76b6a395 69 // Hand it off to the plugin/theme for placing draggers/buttons
b95ec55e 70 $output = theme('panels_ipe_pane_wrapper', $output, $pane, $this->display, $this);
3d244c95
EM
71 return "<div id=\"panels-ipe-paneid-{$pane->pid}\" class=\"panels-ipe-portlet-wrapper panels-ipe-portlet-marker\">" . $output . "</div>";
72 }
73
74 function render_pane_content(&$pane) {
75 $content = parent::render_pane_content($pane);
76 // Ensure that empty panes have some content.
77 if (empty($content->content)) {
78 // Get the administrative title.
79 $content_type = ctools_get_content_type($pane->type);
80 $title = ctools_content_admin_title($content_type, $pane->subtype, $pane->configuration, $this->display->context);
81
82 $content->content = t('Placeholder for empty "@title"', array('@title' => $title));
83 $pane->IPE_empty = TRUE;
84 }
85
86 return $content;
fac4d041 87 }
a96d0939 88
1474c116 89 /**
0a5ef449
SB
90 * Add an 'empty' pane placeholder above all the normal panes.
91 *
5423dfb5 92 * @param $region_id
1474c116
SB
93 * @param $panes
94 */
5423dfb5 95 function render_region($region_id, $panes) {
1474c116 96 // Generate this region's 'empty' placeholder pane from the IPE plugin.
5423dfb5 97 $empty_ph = theme('panels_ipe_placeholder_pane', $region_id, $this->plugins['layout']['panels'][$region_id]);
b95ec55e 98
1474c116 99 // Wrap the placeholder in some guaranteed markup.
3dbbebde 100 $panes['empty_placeholder'] = '<div class="panels-ipe-placeholder panels-ipe-on panels-ipe-portlet-marker panels-ipe-portlet-static">' . $empty_ph . "</div>";
b95ec55e 101
1474c116 102 // Generate this region's add new pane button. FIXME waaaaay too hardcoded
b95ec55e 103 $panes['add_button'] = theme('panels_ipe_add_pane_button', $region_id, $this->display, $this);
1474c116 104
5423dfb5
SB
105 $output = parent::render_region($region_id, $panes);
106 $output = theme('panels_ipe_region_wrapper', $output, $region_id, $this->display);
1474c116 107 $classes = 'panels-ipe-region';
68a6ef5d
SB
108
109 ctools_include('cleanstring');
cbcd7f71
SB
110 $region_id = ctools_cleanstring($region_id);
111 return "<div id='panels-ipe-regionid-$region_id' class='panels-ipe-region'>$output</div>";
a96d0939 112 }
b95ec55e
EM
113
114 /**
115 * AJAX entry point to create the controller form for an IPE.
116 */
66ce3318 117 function ajax_save_form($break = NULL) {
b95ec55e 118 ctools_include('form');
66ce3318
EM
119 if (!empty($this->cache->locked)) {
120 if ($break != 'break') {
121 $account = user_load($this->cache->locked->uid);
122 $name = theme('username', $account);
123 $lock_age = format_interval(time() - $this->cache->locked->updated);
124
125 $message = t("This panel is being edited by user !user, and is therefore locked from editing by others. This lock is !age old.\n\nClick OK to break this lock and discard any changes made by !user.", array('!user' => $name, '!age' => $lock_age));
126
127 $this->commands[] = array(
128 'command' => 'unlockIPE',
129 'message' => $message,
130 'break_path' => url($this->get_url('save-form', 'break'))
131 );
132 return;
133 }
134
135 // Break the lock.
136 panels_edit_cache_break_lock($this->cache);
137 }
b95ec55e
EM
138
139 $form_state = array(
140 'display' => &$this->display,
141 'content_types' => $this->cache->content_types,
142 'rerender' => FALSE,
143 'no_redirect' => TRUE,
144 // Panels needs this to make sure that the layout gets callbacks
145 'layout' => $this->plugins['layout'],
146 );
147
148 $output = ctools_build_form('panels_ipe_edit_control_form', $form_state);
149 if ($output) {
66ce3318
EM
150 // At this point, we want to save the cache to ensure that we have a lock.
151 panels_edit_cache_set($this->cache);
b95ec55e
EM
152 $this->commands[] = array(
153 'command' => 'initIPE',
66ce3318 154 'key' => $this->clean_key,
b95ec55e
EM
155 'data' => $output,
156 );
157 return;
158 }
159
160 // no output == submit
161 if (!empty($form_state['clicked_button']['#save-display'])) {
3657ef4a 162 // Saved. Save the cache.
66ce3318 163 panels_edit_cache_save($this->cache);
b95ec55e
EM
164 }
165 else {
3657ef4a 166 // Cancelled. Clear the cache.
66ce3318 167 panels_edit_cache_clear($this->cache);
b95ec55e
EM
168 }
169
170 $this->commands[] = array(
171 'command' => 'endIPE',
66ce3318 172 'key' => $this->clean_key,
b95ec55e
EM
173 'data' => $output,
174 );
175 }
176
177 /**
178 * Create a command array to redraw a pane.
179 */
180 function command_update_pane($pid) {
181 if (is_object($pid)) {
182 $pane = $pid;
183 }
184 else {
185 $pane = $this->display->content[$pid];
186 }
187
188 $this->commands[] = ctools_ajax_command_replace("#panels-ipe-paneid-$pane->pid", $this->render_pane($pane));
66ce3318 189 $this->commands[] = ctools_ajax_command_changed("#panels-ipe-display-{$this->clean_key}");
b95ec55e
EM
190 }
191
192 /**
193 * Create a command array to add a new pane.
194 */
195 function command_add_pane($pid) {
196 if (is_object($pid)) {
197 $pane = $pid;
198 }
199 else {
200 $pane = $this->display->content[$pid];
201 }
68a6ef5d
SB
202
203 ctools_include('cleanstring');
cbcd7f71
SB
204 $region_id = ctools_cleanstring($pane->panel);
205 $this->commands[] = ctools_ajax_command_append("#panels-ipe-regionid-$region_id div.panels-ipe-sort-container", $this->render_pane($pane));
66ce3318 206 $this->commands[] = ctools_ajax_command_changed("#panels-ipe-display-{$this->clean_key}");
b95ec55e
EM
207 }
208}
209
210/**
211 * FAPI callback to create the Save/Cancel form for the IPE.
212 */
213function panels_ipe_edit_control_form(&$form_state) {
214 $display = &$form_state['display'];
66ce3318
EM
215 // @todo -- this should be unnecessary as we ensure cache_key is set in add_meta()
216// $display->cache_key = isset($display->cache_key) ? $display->cache_key : $display->did;
b95ec55e
EM
217
218 // Annoyingly, theme doesn't have access to form_state so we have to do this.
219 $form['#display'] = $display;
220
221 $layout = panels_get_layout($display->layout);
222 $layout_panels = panels_get_regions($layout, $display);
223
224 $form['panel'] = array('#tree' => TRUE);
225 $form['panel']['pane'] = array('#tree' => TRUE);
226
227 foreach ($layout_panels as $panel_id => $title) {
228 // Make sure we at least have an empty array for all possible locations.
229 if (!isset($display->panels[$panel_id])) {
230 $display->panels[$panel_id] = array();
231 }
232
233 $form['panel']['pane'][$panel_id] = array(
234 // Use 'hidden' instead of 'value' so the js can access it.
235 '#type' => 'hidden',
236 '#default_value' => implode(',', (array) $display->panels[$panel_id]),
237 );
238 }
239
240 $form['buttons']['submit'] = array(
241 '#type' => 'submit',
242 '#value' => t('Save'),
243 '#id' => 'panels-ipe-save',
244 '#submit' => array('panels_edit_display_form_submit'),
245 '#save-display' => TRUE,
246 );
247 $form['buttons']['cancel'] = array(
248 '#type' => 'submit',
249 '#value' => t('Cancel'),
19aa0683 250 '#id' => 'panels-ipe-cancel',
b95ec55e
EM
251 );
252 return $form;
a96d0939 253}