Issue #1093852: Complete upgrade of reusable layouts.
[project/panels.git] / plugins / export_ui / panels_layouts_ui.class.php
1 <?php
2
3 /**
4 * @file
5 * Contains the administrative UI for reusable layouts.
6 */
7
8 class panels_layouts_ui extends ctools_export_ui {
9 var $lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam egestas congue nibh, vel dictum ante posuere vitae. Cras gravida massa tempor metus eleifend sed elementum tortor scelerisque. Vivamus egestas, tortor quis luctus tristique, sem velit adipiscing risus, et tempus enim felis in massa. Morbi viverra, nisl quis rhoncus imperdiet, turpis massa vestibulum turpis, egestas faucibus nibh metus vel nunc. In hac habitasse platea dictumst. Nunc sit amet nisi quis ipsum tincidunt semper. Donec ac urna enim, et placerat arcu. Morbi eu laoreet justo. Nullam nec velit eu neque mattis pulvinar sed non libero. Sed sed vulputate erat. Fusce sit amet dui nibh.";
10
11 function hook_menu(&$items) {
12 // During updates, this can run before our schema is set up, so our
13 // plugin can be empty.
14 if (empty($this->plugin['menu']['items']['add'])) {
15 return;
16 }
17
18 // Change the item to a tab on the Panels page.
19 $this->plugin['menu']['items']['list callback']['type'] = MENU_LOCAL_TASK;
20
21 // Establish a base for adding plugins
22 $base = $this->plugin['menu']['items']['add'];
23 // Remove the default 'add' menu item.
24 unset($this->plugin['menu']['items']['add']);
25
26 ctools_include('plugins', 'panels');
27 $this->builders = panels_get_layout_builders();
28 asort($this->builders);
29 foreach ($this->builders as $name => $builder) {
30 // Create a new menu item for the builder
31 $item = $base;
32 $item['title'] = !empty($builder['builder tab title']) ? $builder['builder tab title'] : 'Add ' . $builder['title'];
33 $item['page arguments'][] = $name;
34 $item['path'] = 'add-' . $name;
35 $this->plugin['menu']['items']['add ' . $name] = $item;
36 }
37
38 parent::hook_menu($items);
39 }
40
41 function edit_form(&$form, &$form_state) {
42 ctools_include('plugins', 'panels');
43 // If the plugin is not set, then it should be provided as an argument:
44 if (!isset($form_state['item']->plugin)) {
45 $form_state['item']->plugin = $form_state['function args'][2];
46 }
47
48 parent::edit_form($form, $form_state);
49
50 $form['category'] = array(
51 '#type' => 'textfield',
52 '#title' => t('Category'),
53 '#description' => t('What category this layout should appear in. If left blank the category will be "Miscellaneous".'),
54 '#default_value' => $form_state['item']->category,
55 );
56
57 ctools_include('context');
58 ctools_include('display-edit', 'panels');
59 ctools_include('content');
60
61 // Provide actual layout admin UI here.
62 // Create a display for editing:
63 $cache_key = 'builder-' . $form_state['item']->name;
64
65 // Load the display being edited from cache, if possible.
66 if (!empty($_POST) && is_object($cache = panels_edit_cache_get($cache_key))) {
67 $display = &$cache->display;
68 }
69 else {
70 $content_types = ctools_content_get_available_types();
71
72 $display->cache_key = $cache_key;
73 panels_cache_clear('display', $cache_key);
74 $cache = new stdClass();
75
76 $display = panels_new_display();
77 $display->did = $form_state['item']->name;
78 $display->layout = $form_state['item']->plugin;
79 $display->layout_settings = $form_state['item']->settings;
80 $display->cache_key = $cache_key;
81 $display->editing_layout = TRUE;
82
83 $cache->display = $display;
84 $cache->content_types = $content_types;
85 $cache->display_title = FALSE;
86 panels_edit_cache_set($cache);
87 }
88
89 // Set up lipsum content in all of the existing panel regions:
90 $display->content = array();
91 $display->panels = array();
92 $custom = ctools_get_content_type('custom');
93 $layout = panels_get_layout($display->layout);
94
95 $regions = panels_get_regions($layout, $display);
96 foreach ($regions as $id => $title) {
97 $pane = panels_new_pane('custom', 'custom');
98 $pane->pid = $id;
99 $pane->panel = $id;
100 $pane->configuration = ctools_content_get_defaults($custom, 'custom');
101 $pane->configuration['title'] = 'Lorem Ipsum';
102 $pane->configuration['body'] = $this->lipsum;
103 $display->content[$id] = $pane;
104 $display->panels[$id] = array($id);
105 }
106
107 $form_state['display'] = &$display;
108 // Tell the Panels form not to display buttons.
109 $form_state['no buttons'] = TRUE;
110 $form_state['no display settings'] = TRUE;
111
112 $form_state['cache_key'] = $cache_key;
113 $form_state['content_types'] = $cache->content_types;
114 $form_state['display_title'] = FALSE;
115
116 $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
117 $form_state['renderer']->cache = &$cache;
118
119 $form = panels_edit_display_form($form, $form_state);
120 // Make sure the theme will work since our form id is different.
121 $form['#theme'] = 'panels_edit_display_form';
122
123 // If we leave the standard submit handler, it'll try to reconcile
124 // content from the input, but we've not exposed that to the user. This
125 // makes previews work with the content we forced in.
126 $form['preview']['button']['#submit'] = array('panels_edit_display_form_preview');
127 }
128
129 function edit_form_submit(&$form, &$form_state) {
130 parent::edit_form_submit($form, $form_state);
131 $form_state['item']->settings = $form_state['display']->layout_settings;
132 }
133
134 function list_form(&$form, &$form_state) {
135 ctools_include('plugins', 'panels');
136 $this->builders = panels_get_layout_builders();
137 parent::list_form($form, $form_state);
138
139 $categories = $plugins = array('all' => t('- All -'));
140 foreach ($this->items as $item) {
141 $categories[$item->category] = $item->category ? $item->category : t('Miscellaneous');
142 }
143
144 $form['top row']['category'] = array(
145 '#type' => 'select',
146 '#title' => t('Category'),
147 '#options' => $categories,
148 '#default_value' => 'all',
149 '#weight' => -10,
150 );
151
152 foreach ($this->builders as $name => $plugin) {
153 $plugins[$name] = $plugin['title'];
154 }
155
156 $form['top row']['plugin'] = array(
157 '#type' => 'select',
158 '#title' => t('Type'),
159 '#options' => $plugins,
160 '#default_value' => 'all',
161 '#weight' => -9,
162 );
163 }
164
165 function list_filter($form_state, $item) {
166 if ($form_state['values']['category'] != 'all' && $form_state['values']['category'] != $item->category) {
167 return TRUE;
168 }
169
170 if ($form_state['values']['plugin'] != 'all' && $form_state['values']['plugin'] != $item->plugin) {
171 return TRUE;
172 }
173
174 return parent::list_filter($form_state, $item);
175 }
176
177 function list_sort_options() {
178 return array(
179 'disabled' => t('Enabled, title'),
180 'title' => t('Title'),
181 'name' => t('Name'),
182 'category' => t('Category'),
183 'storage' => t('Storage'),
184 'plugin' => t('Type'),
185 );
186 }
187
188 function list_build_row($item, &$form_state, $operations) {
189 // Set up sorting
190 switch ($form_state['values']['order']) {
191 case 'disabled':
192 $this->sorts[$item->name] = empty($item->disabled) . $item->admin_title;
193 break;
194 case 'title':
195 $this->sorts[$item->name] = $item->admin_title;
196 break;
197 case 'name':
198 $this->sorts[$item->name] = $item->name;
199 break;
200 case 'category':
201 $this->sorts[$item->name] = ($item->category ? $item->category : t('Miscellaneous')) . $item->admin_title;
202 break;
203 case 'plugin':
204 $this->sorts[$item->name] = $item->plugin;
205 break;
206 case 'storage':
207 $this->sorts[$item->name] = $item->type . $item->admin_title;
208 break;
209 }
210
211 $type = !empty($this->builders[$item->plugin]) ? $this->builders[$item->plugin]['title'] : t('Broken/missing plugin');
212 $category = $item->category ? check_plain($item->category) : t('Miscellaneous');
213 $this->rows[$item->name] = array(
214 'data' => array(
215 array('data' => check_plain($type), 'class' => array('ctools-export-ui-type')),
216 array('data' => check_plain($item->name), 'class' => array('ctools-export-ui-name')),
217 array('data' => check_plain($item->admin_title), 'class' => array('ctools-export-ui-title')),
218 array('data' => $category, 'class' => array('ctools-export-ui-category')),
219 array('data' => theme('links', array('links' => $operations)), 'class' => array('ctools-export-ui-operations')),
220 ),
221 'title' => check_plain($item->admin_description),
222 'class' => array(!empty($item->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'),
223 );
224 }
225
226 function list_table_header() {
227 return array(
228 array('data' => t('Type'), 'class' => array('ctools-export-ui-type')),
229 array('data' => t('Name'), 'class' => array('ctools-export-ui-name')),
230 array('data' => t('Title'), 'class' => array('ctools-export-ui-title')),
231 array('data' => t('Category'), 'class' => array('ctools-export-ui-category')),
232 array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations')),
233 );
234 }
235 }