#967734 by das-peter and intoxination: Upgrade node_get_types() in wizard.
[project/panels.git] / plugins / page_wizards / node_override.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Page wizard that can create a variant on the node_view to take over a node
7 * for a particular type.
8 *
9 * This wizard does a lot that's cut and pasted from exports. We can get away
10 * with this because we know exports tend to remain relatively backward
11 * compatible, and because we know that our context IDs are locked in the
12 * node_view page.
13 */
14 $plugin = array(
15 'title' => t('Node template'),
16 'page title' => t('Node template wizard'),
17 'description' => t('The node page wizard can help you override the node page for a type of node.'),
18
19 'type' => 'panels',
20
21 'form info' => array(
22 'order' => array(
23 'type' => t('Select node type'),
24 'content' => t('Content'),
25 ),
26
27 'forms' => array(
28 'type' => array(
29 'form id' => 'panels_node_override_basic',
30 ),
31 'content' => array(
32 'form id' => 'panels_node_override_content',
33 ),
34 ),
35 ),
36
37 'default cache' => 'panels_node_override_new_page',
38
39 'start' => 'panels_node_override_start',
40 'finish' => 'panels_node_override_finish',
41 );
42
43 /**
44 * Provide defaults for a new cache.
45 *
46 * The cache will store all our temporary data; it isn't really a page
47 * in itself, but it does contain everything we need to make one at the end.
48 */
49 function panels_node_override_new_page(&$cache) {
50 $cache->type = '';
51 $cache->display = panels_new_display();
52 $cache->display->layout = 'flexible';
53 }
54
55 /**
56 * Callback called prior to the wizard starting up on every page
57 * load.
58 */
59 function panels_node_override_start($form_info, $step, &$form_state) {
60 $form_state['page'] = page_manager_get_page_cache('node_view');
61 if (!empty($form_state['page']->locked)) {
62 $account = user_load($form_state['page']->locked->uid);
63 $name = theme('username', array('account' => $account));
64 $lock_age = format_interval(time() - $form_state['page']->locked->updated);
65 $break = url(page_manager_edit_url($form_state['page']->task_name, array('actions', 'break-lock')));
66
67 drupal_set_message(t('WARNING! The node_view is being edited by user !user, and is therefore locked from editing by others. This wizard cannot create a new node override while this page is locked. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => $name, '!age' => $lock_age, '!break' => $break)), 'warning');
68 }
69 }
70
71 /**
72 * First page of our page creator wizard.
73 */
74 function panels_node_override_basic($form, &$form_state) {
75 $types = node_type_get_types();
76 $form_state['types'] = $types;
77
78 $already_done = array();
79 // Figure out which types already have variants assigned to them.
80 foreach ($form_state['page']->handlers as $name => $handler) {
81 if ($handler->handler == 'panel_context' && !empty($handler->conf['access']['plugins'])) {
82 foreach ($handler->conf['access']['plugins'] as $plugin) {
83 if ($plugin['name'] == 'node_type') {
84 foreach ($plugin['settings']['type'] as $type) {
85 $already_done[$type] = $name;
86 }
87 }
88 }
89 }
90 }
91
92 if ($already_done) {
93 $items = array();
94 foreach ($already_done as $type => $handler_id) {
95 $items[] = check_plain($types[$type]->name) . ' ' . l(t('[Edit]'), page_manager_edit_url($form_state['page']->task_name, array('handlers', $handler_id, 'content')));
96 }
97
98 $form['already_done'] = array(
99 '#type' => 'item',
100 '#title' => t('Existing node templates'),
101 '#value' => theme('item_list', array('items' => $items)),
102 );
103 }
104
105 $options = array();
106 foreach ($types as $name => $type) {
107 if (empty($already_done[$name])) {
108 $options[$name] = $type->name;
109 }
110 }
111
112 $form['type'] = array(
113 '#type' => 'select',
114 '#title' => t('Node type'),
115 '#options' => $options,
116 '#default_value' => $form_state['cache']->type,
117 );
118
119 ctools_include('page-wizard', 'panels');
120 panels_page_wizard_add_layout($form, $form_state);
121
122 return $form;
123 }
124
125 /**
126 * Submit function to store the form data in our cache.
127 */
128 function panels_node_override_basic_submit(&$form, &$form_state) {
129 $cache = &$form_state['cache'];
130 $cache->display->layout = $form_state['values']['layout'];
131 $cache->type = $form_state['values']['type'];
132
133 // Create a new handler object and cache it; this way we can use the
134 // handler object for retrieving contexts properly.
135 // Create the the panel context variant configured with our display
136 $plugin = page_manager_get_task_handler('panel_context');
137
138 // Create a new handler.
139 $cache->handler = page_manager_new_task_handler($plugin);
140 $cache->handler->conf['title'] = $form_state['types'][$cache->type]->name;
141 $cache->handler->conf['pipeline'] = 'ipe';
142 $cache->handler->conf['access'] = array(
143 'plugins' => array(
144 0 => array(
145 'name' => 'node_type',
146 'settings' => array(
147 'type' => array(
148 $cache->type => $cache->type,
149 ),
150 ),
151 'context' => 'argument_nid_1',
152 'not' => FALSE,
153 ),
154 ),
155 'logic' => 'and',
156 );
157
158 // Find a region by trying some basic main content region IDs.
159 $layout = panels_get_layout($form_state['values']['layout']);
160 $regions = panels_get_regions($layout, $cache->display);
161 foreach (array('center', 'middle', 'content', 'main') as $candidate) {
162 if (!empty($regions[$candidate])) {
163 $region = $candidate;
164 break;
165 }
166 }
167
168 // If all of the above failed, use the first region.
169 if (empty($region)) {
170 $keys = array_keys($regions);
171 $region = reset($keys);
172 }
173
174 // Populate the layout with content. This is from an export, with minor
175 // changes to ensure defaults are correct and to add stuff to the proper region.
176 $pane = new stdClass;
177 $pane->pid = 'new-1';
178 $pane->panel = $region;
179 $pane->type = 'node_content';
180 $pane->subtype = 'node_content';
181 $pane->shown = TRUE;
182 $pane->access = array();
183 $pane->configuration = array(
184 'links' => 1,
185 'page' => 1,
186 'no_extras' => 0,
187 'override_title' => 0,
188 'override_title_text' => '',
189 'identifier' => '',
190 'link' => 0,
191 'leave_node_title' => 0,
192 'context' => 'argument_nid_1',
193 'build_mode' => 'full',
194 );
195 $pane->cache = array();
196 $pane->style = array(
197 'settings' => NULL,
198 );
199 $pane->css = array();
200 $pane->extras = array();
201 $pane->position = 0;
202 $cache->display->content['new-1'] = $pane;
203 $cache->display->panels[$region][0] = 'new-1';
204 $pane = new stdClass;
205 $pane->pid = 'new-2';
206 $pane->panel = $region;
207 $pane->type = 'node_comments';
208 $pane->subtype = 'node_comments';
209 $pane->shown = TRUE;
210 $pane->access = array();
211 $pane->configuration = array(
212 'mode' => variable_get('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED),
213 'order' => variable_get('comment_default_order', COMMENT_ORDER_NEWEST_FIRST),
214 'comments_per_page' => variable_get('comment_default_per_page', '50'),
215 'context' => 'argument_nid_1',
216 'override_title' => 0,
217 'override_title_text' => '',
218 );
219 $pane->cache = array();
220 $pane->style = array(
221 'settings' => NULL,
222 );
223 $pane->css = array();
224 $pane->extras = array();
225 $pane->position = 1;
226 $cache->display->content['new-2'] = $pane;
227 $cache->display->panels[$region][1] = 'new-2';
228 $pane = new stdClass;
229 $pane->pid = 'new-3';
230 $pane->panel = $region;
231 $pane->type = 'node_comment_form';
232 $pane->subtype = 'node_comment_form';
233 $pane->shown = TRUE;
234 $pane->access = array();
235 $pane->configuration = array(
236 'anon_links' => 1,
237 'context' => 'argument_nid_1',
238 'override_title' => 0,
239 'override_title_text' => '',
240 );
241 $pane->cache = array();
242 $pane->style = array(
243 'settings' => NULL,
244 );
245 $pane->css = array();
246 $pane->extras = array();
247 $pane->position = 2;
248 $cache->display->content['new-3'] = $pane;
249 $cache->display->panels[$region][2] = 'new-3';
250
251 $task = page_manager_get_task('node_view');
252 ctools_include('context');
253 ctools_include('context-task-handler');
254 $cache->context = ctools_context_handler_get_all_contexts($task, NULL, $cache->handler);
255
256 }
257
258 /**
259 * Second page of our wizard. This one provides a layout and lets the
260 * user add content.
261 */
262 function panels_node_override_content($form, &$form_state) {
263 ctools_include('page-wizard', 'panels');
264 panels_page_wizard_add_content($form, $form_state);
265
266 return $form;
267 }
268
269 /**
270 * Store changes to the display.
271 */
272 function panels_node_override_content_submit(&$form, &$form_state) {
273 panels_page_wizard_add_content_submit($form, $form_state);
274 }
275
276 /**
277 * Complete the wizard, create a new variant, and send them to the
278 * edit screen of that variant.
279 */
280 function panels_node_override_finish(&$form_state) {
281 $page = &$form_state['page'];
282 $cache = &$form_state['cache'];
283
284 // Add the new handler to the page
285 $cache->handler->conf['display'] = $cache->display;
286 page_manager_handler_add_to_page($page, $cache->handler);
287
288 // Save it
289 page_manager_save_page_cache($page);
290
291 // Send us to the page manager edit form for this.
292 $form_state['redirect'] = url(page_manager_edit_url('node_view', array('handlers', $cache->handler->name, 'content')));
293 drupal_set_message(t('Your node template has been created.'));
294 }