export-ui.inc
16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* @file
* Provide a tool for creating UIs for exportable objects.
*
* See Advanced Help for documentation.
*/
/**
* Process an export-ui plugin to provide it with defaults.
*/
function ctools_export_ui_process(&$plugin, $info) {
ctools_include('export');
$plugin += array(
'has menu' => TRUE,
'title' => $plugin['name'],
'export' => array(),
'allowed operations' => array(),
'menu' => array(),
'redirect' => array(),
'form' => array(),
'strings' => array(),
'list' => NULL,
'access' => 'administer site configuration',
);
// Provide CRUD access defaults based on the base 'access' setting:
$plugin += array(
'create access' => $plugin['access'],
'delete access' => $plugin['access'],
);
if (empty($plugin['has menu'])) {
return;
}
// The following keys are required and the plugin cannot be processed
// without them.
$keys = array(
'title singular',
'title plural',
'title singular proper',
'title plural proper',
'schema',
);
foreach ($keys as $key) {
if (empty($plugin[$key])) {
drupal_set_message(t('The plugin definition of @plugin is missing the %key key.', array('%key' => $key, '@plugin' => $plugin['name'])), 'error');
}
}
// If we're on the modules page and building a menu, there is a design flaw
// in Drupal core that causes modules to be installed but the schema does
// not become available until AFTER menu rebuild. This helps smooth that
// out. This is a HACK but it should work:
$schema = ctools_export_get_schema($plugin['schema']);
if (empty($schema)) {
// If we're updating the schema may not have been read yet, so don't report this error in that case.
if (!defined('MAINTENANCE_MODE')) {
drupal_set_message(t('The plugin definition of @plugin cannot locate schema %schema.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error');
}
return;
}
if (empty($schema['export'])) {
drupal_set_message(t('The plugin definition of @plugin uses %schema, but it has no export section.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error');
return;
}
$plugin['export'] += $schema['export'];
$plugin['export'] += array(
// Add the identifier key from the schema so we don't have to call
// ctools_export_get_schema() just for that.
'key' => $schema['export']['key'],
);
// Add some default fields that appear often in exports
// If these use different keys they can easily be specified in the
// $plugin.
if (empty($plugin['export']['admin_title']) && !empty($schema['fields']['admin_title'])) {
$plugin['export']['admin_title'] = 'admin_title';
}
if (empty($plugin['export']['admin_description']) && !empty($schema['fields']['admin_description'])) {
$plugin['export']['admin_description'] = 'admin_description';
}
// Define allowed operations, and the name of the operations.
$plugin['allowed operations'] += array(
'edit' => array('title' => t('Edit')),
'enable' => array('title' => t('Enable'), 'ajax' => TRUE, 'token' => TRUE),
'disable' => array('title' => t('Disable'), 'ajax' => TRUE, 'token' => TRUE),
'revert' => array('title' => t('Revert')),
'delete' => array('title' => t('Delete')),
'clone' => array('title' => t('Clone')),
'import' => array('title' => t('Import')),
'export' => array('title' => t('Export')),
);
$plugin['menu'] += array(
'menu item' => str_replace(' ', '-', $plugin['name']),
'menu prefix' => 'admin/structure',
'menu title' => $plugin['title'],
'menu description' => '',
);
$base_path = ctools_export_ui_plugin_base_path($plugin);
$prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
$plugin['menu'] += array(
// Default menu items that should be declared.
'items' => array(),
);
$plugin['menu']['items'] += array(
'list callback' => array(),
'list' => array(),
'add' => array(),
'edit callback' => array(),
'edit' => array(),
);
$plugin['menu']['items']['list callback'] += array(
'path' => '',
// Menu items are translated by the menu system.
// TODO: We need more flexibility in title. The title of the admin page
// is not necessarily the title of the object, plus we need
// plural, singular, proper, not proper, etc.
'title' => $plugin['menu']['menu title'],
'description' => $plugin['menu']['menu description'],
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'list'),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'list'),
'type' => MENU_NORMAL_ITEM,
);
$plugin['menu']['items']['list'] += array(
'path' => 'list',
'title' => 'List',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'list'),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'list'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$plugin['menu']['items']['add'] += array(
'path' => 'add',
'title' => 'Add',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'add'),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'add'),
'type' => MENU_LOCAL_ACTION,
);
$plugin['menu']['items']['edit callback'] += array(
'path' => 'list/%ctools_export_ui',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
$plugin['menu']['items']['edit'] += array(
'path' => 'list/%ctools_export_ui/edit',
'title' => 'Edit',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
if ($plugin['allowed operations']['import']) {
$plugin['menu']['items'] += array('import' => array());
$plugin['menu']['items']['import'] += array(
'path' => 'import',
'title' => 'Import',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'import'),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'import'),
'type' => MENU_LOCAL_ACTION,
);
}
if ($plugin['allowed operations']['export']) {
$plugin['menu']['items'] += array('export' => array());
$plugin['menu']['items']['export'] += array(
'path' => 'list/%ctools_export_ui/export',
'title' => 'Export',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'export', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'export', $prefix_count + 2),
'type' => MENU_LOCAL_TASK,
);
}
if ($plugin['allowed operations']['revert']) {
$plugin['menu']['items'] += array('revert' => array());
$plugin['menu']['items']['revert'] += array(
'path' => 'list/%ctools_export_ui/revert',
'title' => 'Revert',
'page callback' => 'ctools_export_ui_switcher_page',
// Note: Yes, 'delete' op is correct.
'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'revert', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
}
if ($plugin['allowed operations']['delete']) {
$plugin['menu']['items'] += array('delete' => array());
$plugin['menu']['items']['delete'] += array(
'path' => 'list/%ctools_export_ui/delete',
'title' => 'Delete',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'delete', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
}
if ($plugin['allowed operations']['clone']) {
$plugin['menu']['items'] += array('clone' => array());
$plugin['menu']['items']['clone'] += array(
'path' => 'list/%ctools_export_ui/clone',
'title' => 'Clone',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'clone', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'clone', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
}
if ($plugin['allowed operations']['enable']) {
$plugin['menu']['items'] += array('enable' => array());
$plugin['menu']['items']['enable'] += array(
'path' => 'list/%ctools_export_ui/enable',
'title' => 'Enable',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'enable', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'enable', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
}
if ($plugin['allowed operations']['disable']) {
$plugin['menu']['items'] += array('disable' => array());
$plugin['menu']['items']['disable'] += array(
'path' => 'list/%ctools_export_ui/disable',
'title' => 'Disable',
'page callback' => 'ctools_export_ui_switcher_page',
'page arguments' => array($plugin['name'], 'disable', $prefix_count + 2),
'load arguments' => array($plugin['name']),
'access callback' => 'ctools_export_ui_task_access',
'access arguments' => array($plugin['name'], 'disable', $prefix_count + 2),
'type' => MENU_CALLBACK,
);
}
// Define some redirects that should happen after edit/add/clone/delete operations.
$plugin['redirect'] += array(
'add' => $base_path,
'clone' => $base_path,
'edit' => $base_path,
'delete' => $base_path,
'revert' => $base_path,
'import' => $base_path,
);
// Define form elements.
$plugin['form'] += array(
'settings' => function_exists($plugin['name'] . '_form') ? $plugin['name'] . '_form' : '',
'validate' => function_exists($plugin['name'] . '_form_validate') ? $plugin['name'] . '_form_validate' : '',
'submit' => function_exists($plugin['name'] . '_form_submit') ? $plugin['name'] . '_form_submit' : '',
);
// Define strings.
// For all strings, %title may be filled in at a later time via str_replace
// since we do not know the title now.
$plugin['strings'] += array(
'title' => array(),
'confirmation' => array(),
'help' => array(),
'message' => array(),
);
// Strings used in drupal_set_title().
$plugin['strings']['title'] += array(
'add' => t('Add a new @plugin', array('@plugin' => $plugin['title singular'])),
// The "%title" will be replaced in ctools_export_ui_form(), as in this
// stage we dont have the specific exportable object.
'edit' => t('Edit @plugin %title', array('@plugin' => $plugin['title singular'])),
'clone' => t('Clone @plugin %title', array('@plugin' => $plugin['title singular'])),
'import' => t('Import @plugin', array('@plugin' => $plugin['title singular'])),
'export' => t('Export @plugin %title', array('@plugin' => $plugin['title singular'])),
);
// Strings used in confirmation pages.
$plugin['strings']['confirmation'] += array(
'revert' => array(),
'delete' => array(),
'add' => array(),
'edit' => array(),
);
$plugin['strings']['confirmation']['revert'] += array(
'question' => t('Are you sure you want to revert %title?'),
'information' => t('This action will permanently remove any customizations made to this item.'),
'success' => t('The item has been reverted.'),
);
$plugin['strings']['confirmation']['delete'] += array(
'question' => t('Are you sure you want to delete %title?'),
'information' => t('This action will permanently remove this item from your database..'),
'success' => t('The item has been deleted.'),
);
$plugin['strings']['confirmation']['add'] += array(
'success' => t('%title has been created.'),
'fail' => t('%title could not be created.'),
);
$plugin['strings']['confirmation']['edit'] += array(
'success' => t('%title has been updated.'),
'fail' => t('%title could not be updated.'),
);
// Strings used in $forms.
$plugin['strings']['help'] += array(
'import' => t('You can import an exported definition by pasting the exported object code into the field below.'),
);
// Strings used in drupal_set_message().
$plugin['strings']['message'] += array(
'enable' => t('@plugin %title was enabled.', array('@plugin' => $plugin['title singular proper'])),
'disable' => t('@plugin %title was disabled.', array('@plugin' => $plugin['title singular proper'])),
'no items' => t('There are no @titles to display.', array('@titles' => $plugin['title plural'])),
);
}
/**
* Get the class to handle creating a list of exportable items.
*
* If a plugin does not define a lister class at all, then the default
* lister class will be used.
*
* @return
* Either the lister class or FALSE if one could not be had.
*/
function ctools_export_ui_get_handler($plugin) {
$cache = &drupal_static(__FUNCTION__, array());
if (empty($cache[$plugin['name']])) {
// If a list class is not specified by the plugin, fall back to the
// default ctools_export_ui plugin instead.
if (empty($plugin['handler'])) {
$default = ctools_get_export_ui('ctools_export_ui');
$class = ctools_plugin_get_class($default, 'handler');
}
else {
$class = ctools_plugin_get_class($plugin, 'handler');
}
if ($class) {
$cache[$plugin['name']] = new $class();
$cache[$plugin['name']]->init($plugin);
}
}
return !empty($cache[$plugin['name']]) ? $cache[$plugin['name']] : FALSE;
}
/**
* Get the base path from a plugin.
*
* @param $plugin
* The plugin.
*
* @return
* The menu path to the plugin's list.
*/
function ctools_export_ui_plugin_base_path($plugin) {
return $plugin['menu']['menu prefix'] . '/' . $plugin['menu']['menu item'];
}
/**
* Get the path to a specific menu item from a plugin.
*
* @param $plugin
* The plugin name.
* @param $item_id
* The id in the menu items from the plugin.
* @param $export_key
* The export key of the item being edited, if it exists.
* @return
* The menu path to the plugin's list.
*/
function ctools_export_ui_plugin_menu_path($plugin, $item_id, $export_key = NULL) {
$path = $plugin['menu']['items'][$item_id]['path'];
if ($export_key) {
$path = str_replace('%ctools_export_ui', $export_key, $path);
}
return ctools_export_ui_plugin_base_path($plugin) . '/' . $path;
}
/**
* Helper function to include CTools plugins and get an export-ui exportable.
*
* @param $plugin_name
* The plugin that should be laoded.
*/
function ctools_get_export_ui($plugin_name) {
ctools_include('plugins');
return ctools_get_plugins('ctools', 'export_ui', $plugin_name);
}
/**
* Helper function to include CTools plugins and get all export-ui exportables.
*/
function ctools_get_export_uis() {
ctools_include('plugins');
return ctools_get_plugins('ctools', 'export_ui');
}
/**
* Main page callback to manipulate exportables.
*
* This simply loads the object defined in the plugin and hands it off to
* a method based upon the name of the operation in use. This can easily
* be used to add more ops.
*/
function ctools_export_ui_switcher_page($plugin_name, $op) {
$args = func_get_args();
$js = !empty($_REQUEST['js']);
// Load the $plugin information
$plugin = ctools_get_export_ui($plugin_name);
$handler = ctools_export_ui_get_handler($plugin);
if ($handler) {
$method = $op . '_page';
if (method_exists($handler, $method)) {
// replace the first two arguments:
$args[0] = $js;
$args[1] = $_POST;
return call_user_func_array(array($handler, $method), $args);
}
}
else {
return t('Configuration error. No handler found.');
}
}