Projet

Général

Profil

Paste
Télécharger (15,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panelizer / includes / admin.inc @ 651307cd

1
<?php
2
/**
3
 * @file
4
 * Contains administrative forms and settings.
5
 */
6

    
7
/**
8
 * Delegated hook_menu for admin
9
 *
10
 * Since most of hook_menu is delegated to plugins anyway, the admin menu
11
 * is delegated here to reduce code weight.
12
 */
13
function panelizer_admin_hook_menu(&$items) {
14
  // Configure settings pages.
15
  $settings_base = array(
16
    'access arguments' => array('administer panelizer'),
17
    'file' => 'includes/admin.inc',
18
  );
19

    
20
  $items['admin/structure/panelizer'] = array(
21
    'title' => 'Panelizer',
22
    'description' => 'Configure panelizer availability and defaults',
23
    'page callback' => 'drupal_get_form',
24
    'page arguments' => array('panelizer_settings_page_form'),
25
    'type' => MENU_NORMAL_ITEM,
26
  ) + $settings_base;
27

    
28
  $items['admin/structure/panelizer/%panelizer_handler/%/allowed'] = array(
29
    'title' => 'Allowed content',
30
    'page callback' => 'panelizer_allowed_content_page',
31
    'page arguments' => array(3, 4),
32
    'type' => MENU_CALLBACK,
33
    'weight' => -10,
34
  ) + $settings_base;
35

    
36
  $tabs_base = array(
37
    'access callback' => 'panelizer_has_no_choice_callback',
38
    'access arguments' => array(3, 4),
39
    'page arguments' => array(3, 4, 'default', ''),
40
    'type' => MENU_LOCAL_TASK,
41
    'file' => 'includes/admin.inc',
42
  );
43

    
44
  $items['admin/structure/panelizer/%panelizer_handler/%'] = array(
45
    'title callback' => 'panelizer_default_title_callback',
46
    'title arguments' => array(3, 4),
47
    'title' => 'Settings',
48
    'page callback' => 'panelizer_default_settings_page',
49
    'weight' => -5,
50
    'type' => MENU_SUGGESTED_ITEM,
51
  ) + $tabs_base;
52

    
53
  $index = 0;
54
  foreach (panelizer_operations() as $path => $operation) {
55
    $items['admin/structure/panelizer/%panelizer_handler/%/' . $path] = array(
56
      'title' => $operation['menu title'],
57
      'page callback' => $operation['admin callback'],
58
      'weight' => $index - 5,
59
      'type' => ($index === 0) ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
60
    ) + $tabs_base;
61
    if (isset($operation['file'])) {
62
      $items['admin/structure/panelizer/%panelizer_handler/%/' . $path]['file'] = $operation['file'];
63
    }
64
    if (isset($operation['file path'])) {
65
      $items['admin/structure/panelizer/%panelizer_handler/%/' . $path]['file path'] = $operation['file path'];
66
    }
67
    $index++;
68
  }
69

    
70
  $subtabs_base = array(
71
    'access callback' => 'panelizer_administer_panelizer_default',
72
    'access arguments' => array(3, 4, 6),
73
    'page arguments' => array(3, 4, 6, ''),
74
    'type' => MENU_LOCAL_TASK,
75
    'file' => 'includes/admin.inc',
76
  );
77

    
78
  $items['admin/structure/panelizer/%panelizer_handler/%/list/%'] = array(
79
    'title' => 'Settings',
80
    'page callback' => 'panelizer_default_settings_page',
81
    'title callback' => 'panelizer_default_name_title_callback',
82
    'type' => MENU_CALLBACK,
83
  ) + $subtabs_base;
84

    
85
  $index = 0;
86
  foreach (panelizer_operations() as $path => $operation) {
87
    $items['admin/structure/panelizer/%panelizer_handler/%/list/%/' . $path] = array(
88
      'title' => $operation['menu title'],
89
      'page callback' => $operation['admin callback'],
90
      'weight' => $index - 5,
91
    ) + $subtabs_base;
92
    if (isset($operation['file'])) {
93
      $items['admin/structure/panelizer/%panelizer_handler/%/list/%/' . $path]['file'] = $operation['file'];
94
    }
95
    if (isset($operation['file path'])) {
96
      $items['admin/structure/panelizer/%panelizer_handler/%/list/%/' . $path]['file path'] = $operation['file path'];
97
    }
98
    $index++;
99
  }
100

    
101
  $items['admin/structure/panelizer/%panelizer_handler/%/list/%/access'] = array(
102
    'title' => 'Access',
103
    'page callback' => 'panelizer_default_access_page',
104
    'weight' => -2,
105
  ) + $subtabs_base;
106

    
107
}
108

    
109
/**
110
 * Primary settings page.
111
 *
112
 * This settings page allows the administrator to select which node types
113
 * can be panelized, whether they have a default, and provides links to
114
 * edit those defaults.
115
 */
116
function panelizer_settings_page_form($form, &$form_state) {
117
  $form['entities'] = array('#tree' => TRUE);
118

    
119
  $plugins = panelizer_get_entity_plugins();
120
  foreach ($plugins as $entity_type => $plugin) {
121
    $entity_info = entity_get_info($entity_type);
122
    if ($entity_info) {
123
      if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
124
        $handler->settings_form($form, $form_state);
125
      }
126
    }
127
  }
128

    
129
  $form['submit'] = array(
130
    '#type' => 'submit',
131
    '#value' => t('Save'),
132
  );
133

    
134
  return $form;
135
}
136

    
137
/**
138
 * Format the output of the main settings form.
139
 *
140
 * We want our checkboxes to show up in a table.
141
 */
142
function theme_panelizer_settings_page_table($variables) {
143
  $element = $variables['element'];
144

    
145
  // Render the table
146
  $header = $element['#header'];
147
  $columns = $element['#columns'];
148

    
149
  $rows = array();
150
  foreach (element_children($element) as $bundle) {
151
    foreach (element_children($element[$bundle]) as $view_mode) {
152
      $row = array();
153
      foreach ($columns as $column) {
154
        $row[] = drupal_render($element[$bundle][$view_mode][$column]);
155
      }
156

    
157
      $rows[] = $row;
158
    }
159
  }
160

    
161
  $output = theme('table', array('header' => $header, 'rows' => $rows));
162

    
163
  // Render everything else
164
  $output .= drupal_render_children($element);
165
  return $output;
166
}
167

    
168
function panelizer_settings_page_form_validate($form, &$form_state) {
169
  $plugins = panelizer_get_entity_plugins();
170
  foreach ($plugins as $entity_type => $plugin) {
171
    $entity_info = entity_get_info($entity_type);
172
    if ($entity_info) {
173
      if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
174
        $handler->settings_form_validate($form, $form_state);
175
      }
176
    }
177
  }
178
}
179

    
180
function panelizer_settings_page_form_submit($form, &$form_state) {
181
  $plugins = panelizer_get_entity_plugins();
182
  foreach ($plugins as $entity_type => $plugin) {
183
    $entity_info = entity_get_info($entity_type);
184
    if ($entity_info) {
185
      if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
186
        $handler->settings_form_submit($form, $form_state);
187
      }
188
    }
189
  }
190

    
191
  // Ensure that defaults get cached to recognize new settings immediately.
192
  drupal_flush_all_caches();
193
}
194

    
195
/**
196
 * Page to configure what content is available for a given node type.
197
 */
198
function panelizer_allowed_content_page($handler, $bundle) {
199
  // Old URLs would include a view mode, but allowed content does not
200
  // change for view modes, so make that version of the page not found.
201
  if (strpos($bundle, '.') !== FALSE) {
202
    return MENU_NOT_FOUND;
203
  }
204

    
205
  if (is_string($handler)) {
206
    $handler = panelizer_entity_plugin_get_handler($handler);
207
  }
208

    
209
  if (!$handler->is_panelized($bundle)) {
210
    return MENU_NOT_FOUND;
211
  }
212

    
213
  ctools_include('common', 'panels');
214
  return drupal_get_form('panels_common_settings', 'panelizer_' . $handler->entity_type . ':' . $bundle);
215
}
216

    
217
/**
218
 * Page to configure basic settings for a panelizer default.
219
 */
220
function panelizer_default_settings_page($handler, $bundle, $name, $view_mode) {
221
  if (is_string($handler)) {
222
    $handler = panelizer_entity_plugin_get_handler($handler);
223
  }
224

    
225
  if ($view_mode) {
226
    $bundle .= '.' . $view_mode;
227
    // We use a $mode copy of $view_mode because the presence of $view_mode
228
    // tells us later if we want fake tabs, but we still need to know the
229
    // actual view mode regardless of that.
230
    $mode = $view_mode;
231
  }
232
  else {
233
    // We are NOT testing for the . here because if this is missing
234
    // that is a sign of a bug and we want the notice.
235
    list(, $mode) = explode('.', $bundle);
236
  }
237

    
238
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
239
  if (empty($panelizer)) {
240
    return t('No default display has been configured for this view mode.');
241
  }
242

    
243
  $form_state = array(
244
    'panelizer' => &$panelizer,
245
    'no_redirect' => TRUE,
246
    'view_mode' => $mode,
247
  );
248

    
249
  if ($handler->has_panel_choice($bundle)) {
250
    $form_state['has title'] = TRUE;
251
  }
252

    
253
  ctools_include('common', 'panelizer');
254
  $output = drupal_build_form('panelizer_settings_form', $form_state);
255
  if (!empty($form_state['executed'])) {
256
    ctools_include('export');
257
    ctools_export_crud_save('panelizer_defaults', $panelizer);
258
    drupal_set_message(t('The settings have been updated.'));
259
    drupal_goto($_GET['q']);
260
  }
261

    
262
  if ($view_mode && !$handler->has_panel_choice($bundle)) {
263
    $output = $handler->wrap_default_panelizer_pages($bundle, $output);
264
  }
265

    
266
  return $output;
267
}
268

    
269
/**
270
 * Delete a panelizer node panel from the database.
271
 */
272
function panelizer_delete_panelizer_defaults($panelizer) {
273
  // Allow modules to react on a default Panelizer object before deletion.
274
  // Triggers hook_panelizer_delete_default().
275
  module_invoke_all('panelizer_delete_default', $panelizer);
276

    
277
  if (!empty($panelizer->pnid)) {
278
    if (!empty($panelizer->did)) {
279
      panels_delete_display($panelizer->did);
280
    }
281
    db_delete('panelizer_defaults')
282
      ->condition('pnid', $panelizer->pnid)
283
      ->execute();
284
  }
285
}
286

    
287
/**
288
 * Page to configure what content is available for a given node type.
289
 */
290
function panelizer_default_context_page($handler, $bundle, $name, $view_mode) {
291
  if (is_string($handler)) {
292
    $handler = panelizer_entity_plugin_get_handler($handler);
293
  }
294

    
295
  if ($view_mode) {
296
    $bundle .= '.' . $view_mode;
297
  }
298

    
299
  // Verify there's a Panelizer display available.
300
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
301
  if (empty($panelizer)) {
302
    return t('No default display has been configured for this view mode.');
303
  }
304

    
305
  $cache_key = implode(':', array($handler->entity_type, $bundle, $name));
306
  $panelizer = panelizer_context_cache_get('default', $cache_key);
307
  if (empty($panelizer)) {
308
    return t('No default display has been configured for this view mode.');
309
  }
310

    
311
  $form_state = array(
312
    'panelizer' => &$panelizer,
313
    'panelizer type' => 'default',
314
    'cache key' => $cache_key,
315
    'no_redirect' => TRUE,
316
  );
317

    
318
  ctools_include('common', 'panelizer');
319
  $output = drupal_build_form('panelizer_default_context_form', $form_state);
320
  if (!empty($form_state['executed'])) {
321
    if (!empty($form_state['clicked_button']['#write'])) {
322
      drupal_set_message(t('The settings have been updated.'));
323
      ctools_include('export');
324
      ctools_export_crud_save('panelizer_defaults', $panelizer);
325
    }
326
    else {
327
      drupal_set_message(t('Changes have been discarded.'));
328
    }
329

    
330
    panelizer_context_cache_clear('default', $cache_key);
331
    drupal_goto($_GET['q']);
332
  }
333

    
334
  if ($view_mode && !$handler->has_panel_choice($bundle)) {
335
    $output = $handler->wrap_default_panelizer_pages($bundle, $output);
336
  }
337

    
338
  return $output;
339
}
340

    
341
/**
342
 * Pass through to the panels layout editor.
343
 */
344
function panelizer_default_layout_page($handler, $bundle, $name, $view_mode, $step = NULL, $layout = NULL) {
345
  if (is_string($handler)) {
346
    $handler = panelizer_entity_plugin_get_handler($handler);
347
  }
348

    
349
  $original_bundle = $bundle;
350
  if ($view_mode) {
351
    $bundle .= '.' . $view_mode;
352
  }
353

    
354
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
355
  if (empty($panelizer)) {
356
    return t('No default display has been configured for this view mode.');
357
  }
358

    
359
  $display = $panelizer->display;
360
  $display->context = $handler->get_contexts($panelizer);
361

    
362
  if ($name == 'default' && $handler->has_default_panel($bundle)) {
363
    $path = 'admin/structure/panelizer/' . $handler->entity_type . '/' . $bundle;
364
  }
365
  else {
366
    $path = 'admin/structure/panelizer/' . $handler->entity_type . '/' . $bundle . '/list/' . $name ;
367
  }
368

    
369
  $form_state = array(
370
    'display' => $display,
371
    'wizard path' => $path . '/layout/%step',
372
    'allowed_layouts' => panelizer_get_allowed_layouts_option($handler->entity_type, $original_bundle),
373
  );
374

    
375
  ctools_include('common', 'panelizer');
376
  $output = panelizer_change_layout_wizard($form_state, $step, $layout);
377
  if (!empty($form_state['complete'])) {
378
    $panelizer->display = $form_state['display'];
379
    ctools_export_crud_save('panelizer_defaults', $panelizer);
380
    drupal_set_message(t('The layout has been changed.'));
381
    drupal_goto($path . '/content');
382
  }
383

    
384
  if ($view_mode && !$handler->has_panel_choice($bundle)) {
385
    $output = $handler->wrap_default_panelizer_pages($bundle, $output);
386
  }
387

    
388
  return $output;
389
}
390

    
391
/**
392
 * Determine edit access for this panelizer default.
393
 */
394
function panelizer_default_access_page($handler, $bundle, $name, $view_mode) {
395
  if (is_string($handler)) {
396
    $handler = panelizer_entity_plugin_get_handler($handler);
397
  }
398

    
399
  if ($view_mode) {
400
    $bundle .= '.' . $view_mode;
401
  }
402

    
403
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
404
  if (empty($panelizer)) {
405
    return t('No default display has been configured for this view mode.');
406
  }
407
  $argument = $name;
408

    
409
  ctools_include('context-access-admin');
410
  ctools_include('object-cache');
411

    
412
  // Ensure that if they visit this page fresh, any cached data is removed:
413
  if (empty($_POST)) {
414
    ctools_object_cache_clear('panelizer_access', $argument);
415
  }
416
  else {
417
    $access = ctools_object_cache_get('panelizer_access', $argument);
418
  }
419

    
420
  if (empty($access)) {
421
    $access = $panelizer->access;
422
  }
423

    
424
  if (!is_array($access)) {
425
    $access = array();
426
  }
427

    
428
  $form_state = array(
429
    'access' => $access,
430
    'module' => 'panelizer',
431
    'callback argument' => $argument,
432
    // A bug in context-access-admin requires this until it's fixed.
433
    'argument' => $argument,
434
    'contexts' => $handler->get_contexts($panelizer),
435
    'no_redirect' => TRUE,
436
  );
437

    
438
  $output = drupal_build_form('ctools_access_admin_form', $form_state);
439

    
440
  if (!empty($form_state['executed'])) {
441
    ctools_object_cache_clear('panelizer_access', $argument);
442
    $panelizer->access = $form_state['access'];
443
    ctools_export_crud_save('panelizer_defaults', $panelizer);
444

    
445
    drupal_set_message(t('The access settings have been updated.'));
446
    drupal_goto($_GET['q']);
447
  }
448

    
449
  return $output;
450
}
451

    
452
/**
453
 * Pass through to the panels content editor.
454
 */
455
function panelizer_default_content_page($handler, $bundle, $name, $view_mode) {
456
  if (is_string($handler)) {
457
    $handler = panelizer_entity_plugin_get_handler($handler);
458
  }
459

    
460
  if ($view_mode) {
461
    $bundle .= '.' . $view_mode;
462
  }
463

    
464
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
465
  if (empty($panelizer)) {
466
    return t('No default display has been configured for this view mode.');
467
  }
468

    
469
  $cache_key = implode(':', array('panelizer', 'default', $handler->entity_type, $bundle, $name));
470

    
471
  $form_state = array(
472
    'display cache' => panels_edit_cache_get($cache_key),
473
    'no_redirect' => TRUE,
474
  );
475

    
476
  ctools_include('common', 'panelizer');
477
  $output = drupal_build_form('panelizer_edit_content_form', $form_state);
478
  if (!empty($form_state['executed'])) {
479
    if (!empty($form_state['clicked_button']['#save-display'])) {
480
      drupal_set_message(t('The settings have been updated.'));
481
      $panelizer->display = $form_state['display'];
482
      ctools_export_crud_save('panelizer_defaults', $panelizer);
483
    }
484
    else {
485
      drupal_set_message(t('Changes have been discarded.'));
486
    }
487

    
488
    panels_edit_cache_clear($form_state['display cache']);
489
    drupal_goto($_GET['q']);
490
  }
491
  elseif ($form_state['display cache']->display->did === 'new') {
492
    panels_edit_cache_set($form_state['display cache']);
493
  }
494

    
495
  ctools_set_no_blocks(FALSE);
496

    
497
  if ($view_mode && !$handler->has_panel_choice($bundle)) {
498
    $output = $handler->wrap_default_panelizer_pages($bundle, $output);
499
  }
500

    
501
  drupal_set_page_content($output);
502
  $page = element_info('page');
503

    
504
  return $page;
505
}