summaryrefslogtreecommitdiffstats
path: root/panelizer.module
blob: 3d2323fa889626711bbac9f0f7df09819437818c (plain)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
<?php
/**
 * @file
 * The Panelizer module attaches panels to entities, providing default
 * panels and allowing each panel to be configured independently by
 * privileged users.
 */

// -----------------------------------------------------------------------
// Drupal core hooks

/**
 * Implements hook_permission().
 */
function panelizer_permission() {
  $items = array(
    'administer panelizer' => array(
      'title' => t('administer panelizer'),
      'description' => t('Fully administer panelizer and all panelizer settings.'),
    ),
  );

  // Delegate.
  foreach (panelizer_get_plugins_with_hook('permission') as $handler) {
    $handler->hook_permission($items);
  }

  return $items;
}

/**
 * Implements hook_theme().
 */
function panelizer_theme() {
  $items = array();

  $items['panelizer_settings_page_table'] = array(
    'render element' => 'element',
    'file' => 'includes/admin.inc',
  );

  // Delegate.
  foreach (panelizer_get_plugins_with_hook('theme') as $handler) {
    $handler->hook_theme($items);
  }

  return $items;
}

/**
 * Implements hook_menu().
 */
function panelizer_menu() {
  $items = array();

  // Delegate admin menu stuff to admin.inc
  ctools_include('admin', 'panelizer');
  panelizer_admin_hook_menu($items);

  // Delegate.
  foreach (panelizer_get_plugins_with_hook('menu') as $handler) {
    $handler->hook_menu($items);
  }

  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function panelizer_menu_alter(&$items) {
  // Delegate.
  foreach (panelizer_get_plugins_with_hook('menu_alter') as $handler) {
    $handler->hook_menu_alter($items);
  }
}

/**
 * Implements hook_admin_paths().
 */
function panelizer_admin_paths() {
  $items = array();

  // Delegate.
  foreach (panelizer_get_plugins_with_hook('admin_paths') as $handler) {
    $handler->hook_admin_paths($items);
  }

  return $items;
}

/**
 * Implements hook_form_alter().
 */
function panelizer_form_alter(&$form, &$form_state, $form_id) {
  // Delegate.
  foreach (panelizer_get_plugins_with_hook('form_alter') as $handler) {
    $handler->hook_form_alter($form, $form_state, $form_id);
  }
}

/**
 * Implements hook_page_alter().
 */
function panelizer_page_alter(&$page) {
  // Delegate.
  foreach (panelizer_get_plugins_with_hook('page_alter') as $handler) {
    $handler->hook_page_alter($page);
  }
}

/**
 * Implements hook_entity_load().
 */
function panelizer_entity_load(&$entities, $entity_type) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_entity_load($entities);
  }
}

/**
 * Implements hook_entity_update().
 */
function panelizer_entity_update($entity, $entity_type) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_entity_update($entity);
  }
}

/**
 * Implements hook_entity_insert().
 */
function panelizer_entity_insert($entity, $entity_type) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_entity_insert($entity);
  }
}

/**
 * Implements hook_entity_delete().
 */
function panelizer_entity_delete($entity, $entity_type) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_entity_delete($entity);
  }
}

/**
 * Implements hook_field_attach_delete_revision().
 */
function panelizer_field_attach_delete_revision($entity_type, $entity) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_field_attach_delete_revision($entity);
  }
}

function panelizer_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_field_attach_form($entity, $form, $form_state, $langcode);
  }
}

function panelizer_field_attach_submit($entity_type, $entity, &$form, &$form_state) {
  // Delegate to the handler.
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $handler->hook_field_attach_submit($entity, $form, $form_state);
  }
}

// -----------------------------------------------------------------------
// Panels and CTools hooks

/**
 * Implements hook_ctools_plugin_type()
 */
function panelizer_ctools_plugin_type() {
  $items['entity'] = array(
    'cache' => FALSE,
    'process' => array(
      'function' => 'panelizer_entity_plugin_process',
    ),
    'classes' => array('handler'),
  );

  return $items;
}

/**
 * Implements hook_ctools_plugin_directory()
 */
function panelizer_ctools_plugin_directory($module, $plugin) {
  if (in_array($module, array('panelizer', 'ctools', 'page_manager'))) {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_ctools_plugin_api().
 */
function panelizer_ctools_plugin_api($module, $api) {
  if (($module == 'page_manager' && $api == 'pages_default') || $module == 'panelizer') {
    return array(
      'version' => 1,
      'path' => drupal_get_path('module', 'panelizer') . '/includes',
    );
  }
}

/**
 * Implements hook_features_api().
 */
function panelizer_features_api() {
  $api = array();
  $api['panelizer_defaults'] = _ctools_features_get_info('panelizer_defaults');
  $api['panelizer_defaults']['alter_type'] = FEATURES_ALTER_TYPE_NONE;

  return $api;
}

/**
 * Implementation of hook_views_api().
 */
function panelizer_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'panelizer') . '/plugins/views',
  );
}

/**
 * Implements hook_panelizer_defaults_alter().
 */
function panelizer_panelizer_defaults_alter(&$items) {
  // Delegate.
  foreach (panelizer_get_plugins_with_hook('panelizer_defaults') as $handler) {
    $handler->hook_panelizer_defaults($items);
  }
}

/**
 * Implements hook_default_page_manager_handlers().
 */
function panelizer_default_page_manager_handlers() {
  $items = array();
  // Delegate.
  foreach (panelizer_get_plugins_with_hook('default_page_manager_handlers') as $handler) {
    $handler->hook_default_page_manager_handlers($items);
  }

  return $items;
}

/**
 * Implement CTools access form caching callback: get.
 */
function panelizer_ctools_access_get($argument) {
  list($entity_type, $bundle, $name) = explode(':', $argument);
  $handler = panelizer_entity_plugin_get_handler($entity_type);
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);

  if (empty($panelizer)) {
    return;
  }

  if (!$handler->access_default_panelizer_object($panelizer)) {
    return;
  }

  // First, see if there's a cache
  ctools_include('object-cache');
  $access = ctools_object_cache_get('panelizer_access', $argument);
  if (!$access) {
    $access = $panelizer->access;
  }

  $context = $handler->get_contexts($panelizer);

  return array($access, $context);
}

/**
 * Implement CTools access form caching callback: set.
 */
function panelizer_ctools_access_set($argument, $access) {
  list($entity_type, $bundle, $name) = explode(':', $argument);
  $handler = panelizer_entity_plugin_get_handler($entity_type);
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);

  if (empty($panelizer)) {
    return;
  }

  if (!$handler->access_default_panelizer_object($panelizer)) {
    return;
  }

  ctools_include('object-cache');
  ctools_object_cache_set('panelizer_access', $argument, $access);
}

/**
 * Implement CTools access form caching callback: get.
 */
function panelizer_ctools_access_clear($argument) {
  list($entity_type, $bundle, $name) = explode(':', $argument);
  $handler = panelizer_entity_plugin_get_handler($entity_type);
  $panelizer = $handler->get_default_panelizer_object($bundle, $name);

  if (empty($panelizer)) {
    return;
  }

  if (!$handler->access_default_panelizer_object($panelizer)) {
    return;
  }

  ctools_include('object-cache');
  ctools_object_cache_clear('panelizer', $argument);
}

// -----------------------------------------------------------------------
// CTools entity plugin support code

/**
 * CTools process callback for an entity plugin.
 *
 * This adds configuration data to the plugin so that we know what
 * bundles it is enabled for.
 */
function panelizer_entity_plugin_process(&$plugin, $info) {
  $entity_type = $plugin['name'];
  $entity_info = entity_get_info($entity_type);
  $plugin['bundles'] = array();
  if ($entity_info) {
    foreach ($entity_info['bundles'] as $bundle => $label) {
      if ($settings = variable_get('panelizer_defaults_' . $entity_type . '_' . $bundle, array())) {
        $plugin['bundles'][$bundle] = $settings;
      }
    }
  }

  drupal_alter('panelizer_entity_plugin_process', $plugin, $info);
}

/**
 * Fetch a single entity plugin.
 */
function panelizer_get_entity_plugin($entity_type) {
  ctools_include('plugins');
  return ctools_get_plugins('panelizer', 'entity', $entity_type);
}

/**
 * Fetch all entity plugin.
 */
function panelizer_get_entity_plugins() {
  ctools_include('plugins');
  return ctools_get_plugins('panelizer', 'entity');
}

/**
 * Get the class to handle custom code for a given entity type plugin.
 *
 * If a plugin does not define a class at all, then the default class
 *
 * @return
 *   Either the instantiated handler or FALSE if one could not be had.
 */
function panelizer_entity_plugin_get_handler($plugin) {
  // The default plugin handler is abstract and cannot be loaded.
  if ($plugin == 'default') {
    return;
  }

  $cache = &drupal_static(__FUNCTION__, array());

  // If a string was passed, turn it into a plugin.
  if (is_string($plugin)) {
    $plugin = panelizer_get_entity_plugin($plugin);
    if (!$plugin) {
      return FALSE;
    }
  }

  // Get the class name from the 'handler' property if we have not already
  // cached a handler.
  if (is_object($plugin)) {
    vpr_trace();
    exit;
  }
  if (empty($cache[$plugin['name']]) && ($class = ctools_plugin_get_class($plugin, 'handler'))) {
    // @todo is there a good reason to use ->init instead of __construct?
    $cache[$plugin['name']] = new $class();
    $cache[$plugin['name']]->init($plugin);
  }
  return !empty($cache[$plugin['name']]) ? $cache[$plugin['name']] : FALSE;
}

/**
 * Load handler to get a plugin as a menu callback.
 */
function panelizer_handler_load($entity_type) {
  return panelizer_entity_plugin_get_handler($entity_type);
}

/**
 * Fetch handler objects for all plugins that implement the named hook.
 *
 * These plugins must set $plugin['hooks'][$hook] = TRUE in order to
 * be instantiated.
 *
 * This is only called for system wide hooks such as hook_menu and
 * hook_menu_alter; entity specific hooks will always be called.
 */
function panelizer_get_plugins_with_hook($hook) {
  $objects = array();
  $plugins = panelizer_get_entity_plugins();
  foreach ($plugins as $entity_type => $plugin) {
    if (!empty($plugin['hooks'][$hook])) {
      if ($handler = panelizer_entity_plugin_get_handler($plugin)) {
        $objects[$entity_type] = $handler;
      }
    }
  }

  return $objects;
}

/**
 * Page callback for entity menu callbacks.
 *
 * This function is to be used as a menu callback for menu items that
 * are to be handled by a method on the handler object. It loads the object
 * defined in the plugin and hands it off to a method based upon the name
 * of the operation in use.
 *
 * For example, if the 'op' is 'revision' then the callback method will be
 * 'page_revisions', with all of the arguments *except* the $op and the
 * plugin name.
 */
function panelizer_entity_plugin_switcher_page($entity_type, $op) {
  $args = func_get_args();
  $js = !empty($_REQUEST['js']);

  // Load the $plugin information
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $method = 'page_' . $op;
    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.');
  }
}

/**
 * Callback used for switching callbacks into the proper plugin.
 */
function panelizer_entity_plugin_callback_switcher($entity_type, $switcher_type, $op) {
  $args = func_get_args();
  if (count($args) < 3) {
    return FALSE;
  }
  $entity_type = array_shift($args);
  $switcher_type = array_shift($args);
  $op = array_shift($args);

  // Load the $plugin information
  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    $method = $switcher_type . '_' . $op;
    if (method_exists($handler, $method)) {
      return call_user_func_array(array($handler, $method), $args);
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Specialized version of ctools_export_ui_switcher_page()
 *
 * This one is designed to set our entity handler and bundle on the
 * object so we can refer to it later without having to override
 * all of the entry points.
 */
function panelizer_export_ui_switcher_page($entity_handler, $bundle, $plugin_name, $op) {
  $args = func_get_args();

  // Remove the handler and the bundle.
  array_shift($args);
  array_shift($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) {
    if (is_string($entity_handler)) {
      $entity_handler = panelizer_entity_plugin_get_handler($entity_handler);
    }

    $handler->entity_handler = $entity_handler;
    $handler->entity_bundle = $bundle;
    if (empty($entity_handler->entity_admin_root) || substr($_GET['q'], 30) == 'admin/config/content/panelizer') {
      $handler->plugin['menu']['menu prefix'] = 'admin/config/content/panelizer/' . $entity_handler->entity_type;
      $handler->plugin['menu']['menu item'] = $bundle;
    }
    else {
      $base_path = $entity_handler->entity_admin_root . '/panelizer';
      if (is_numeric($entity_handler->entity_admin_bundle)) {
        $bits = explode('/', $base_path);
        $bits[$entity_handler->entity_admin_bundle] = $bundle;
        $base_path = implode('/', $bits);
      }
      $handler->plugin['menu']['menu prefix'] = dirname($base_path);
      $handler->plugin['menu']['menu item'] = basename($base_path);
    }

    $path = $handler->plugin['menu']['menu prefix'] . '/' . $handler->plugin['menu']['menu item'];

    foreach ($handler->plugin['redirect'] as $key => $old_path) {
      if ($key == 'add') {
        $handler->plugin['redirect'][$key] = $path . '/list/%ctools_export_ui/settings';
      }
      else {
        $handler->plugin['redirect'][$key] = $path . '/list';
      }
    }

    $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.');
  }
}

// ---------------------------------------------------------------------------
// Menu callbacks

/**
 * Title callback to properly set the tile when editing panelizer defaults.
 */
function panelizer_default_title_callback($handler, $bundle) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  if (!$handler) {
    return '';
  }

  $title = $handler->get_bundle_title($bundle);
  return $title;
}

/**
 * Menu callback to determine if a type has a choice of defaults.
 *
 * We use this to make sure the right tabs appear.
 */
function panelizer_has_choice_callback($handler, $bundle, $name = NULL) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  if (!$handler) {
    return FALSE;
  }

  if (!panelizer_administer_entity_bundle($handler, $bundle)) {
    return FALSE;
  }

  // Check to see if $name is valid
  if ($name && !$handler->get_default_panelizer_object($bundle, $name)) {
    return FALSE;
  }

  return $handler->has_panel_choice($bundle);
}

/**
 * Menu callback to determine if a type has a choice of defaults.
 *
 * We use this to make sure the right tabs appear.
 */
function panelizer_has_no_choice_callback($handler, $bundle) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  if (!$handler) {
    return FALSE;
  }

  if (!panelizer_administer_entity_bundle($handler, $bundle)) {
    return FALSE;
  }

  return $handler->has_default_panel($bundle) && !$handler->has_panel_choice($bundle);
}

/**
 * Menu callback to determine if a type has a choice of defaults.
 *
 * We use this to make sure the right tabs appear.
 */
function panelizer_is_panelized($handler, $bundle) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  if (!$handler) {
    return FALSE;
  }

  if (!panelizer_administer_entity_bundle($handler, $bundle)) {
    return FALSE;
  }

  return $handler->is_panelized($bundle);
}

/**
 * Access callback to see if a user can administer a particular bundle.
 */
function panelizer_administer_entity_bundle($handler, $bundle) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  return user_access('administer panelizer') || user_access("administer panelizer $handler->entity_type $bundle defaults");
}

/**
 * Access callback to see if a user can administer a particular panelizer default.
 */
function panelizer_administer_panelizer_default($handler, $bundle, $name) {
  if (is_string($handler)) {
    $handler = panelizer_entity_plugin_get_handler($handler);
  }

  $panelizer = $handler->get_default_panelizer_object($bundle, $name);
  if (!$panelizer) {
    return FALSE;
  }

  return $handler->access_default_panelizer_object($panelizer);
}

/**
 * Menu load callback to scrub a node bundle from the URL safe equivalent.
 */
function panelizer_node_type_load($name) {
  if ($type = node_type_get_type(strtr($name, array('-' => '_')))) {
    return $type->type;
  }
}

// ---------------------------------------------------------------------------
// export.inc callbacks to handle proper in/out of our defaults

/**
 * export.inc callback to properly save a panelizer default.
 */
function panelizer_export_save_callback(&$object) {
  if (!empty($object->display)) {
    // First write the display
    panels_save_display($object->display);

    // Make sure we have the did.
    $object->did = $object->display->did;
  }

  // Then write the default
  if ($object->export_type & EXPORT_IN_DATABASE) {
    // Existing record.
    $update = array('pnid');
  }
  else {
    // New record.
    $update = array();
    $object->export_type = EXPORT_IN_DATABASE;
  }

  return drupal_write_record('panelizer_defaults', $object, $update);
}

/**
 * export.inc callback to properly export a panelizer default.
 */
function panelizer_export_export_callback($object, $indent) {
  $object->did = NULL;
  $output = ctools_export_object('panelizer_defaults', $object, $indent);
  $output .= panels_export_display($object->display, $indent);
  $output .= $indent . '$panelizer->display = $display;' . "\n";

  return $output;
}

/**
 * export.inc callback to properly delete a panelizer default.
 */
function panelizer_export_delete_callback($object) {
  if (!empty($object->did)) {
    panels_delete_display($object->did);
  }

  db_delete('panelizer_defaults')
    ->condition('name', $object->name)
    ->execute();
}

/**
 * export.inc callback to load sub records for an object.
 */
function panelizer_export_delete_callback_subrecords($objects) {
  $dids = array();
  foreach ($objects as $panelizer) {
    if (!empty($panelizer->did)) {
      $dids[$panelizer->did] = $panelizer->did;
    }
  }

  if ($dids) {
    $displays = panels_load_displays($dids);
    foreach ($objects as $panelizer) {
      if (!empty($panelizer->did) && !empty($displays[$panelizer->did])) {
        $panelizer->display = $displays[$panelizer->did];
      }
    }
  }
}

// ---------------------------------------------------------------------------
// Context cache callbacks -- this really needs a less lame system someday.

/**
 * Fetch the panelizer object from the object cache.
 *
 * CTools clumsy context editing system requires caching. This lets us
 * do it reasonably.
 *
 * @param $entity_type
 *   Can be something like 'node' or 'user' or 'default'.
 * @param $key
 *   Depends on the $entity_type. Can be a nid, a uid or a default key.
 */
function panelizer_context_cache_get($entity_type, $key) {
  ctools_include('object-cache');
  $cache = ctools_object_cache_get('panelizer_context_cache', $entity_type . ':' . $key);
  if (!empty($cache)) {
    $cache->cached = TRUE;
    return $cache;
  }

  if ($entity_type == 'default') {
    list($entity_type, $bundle, $name) = @explode(':', $key, 3);
    $get_default = TRUE;
  }

  if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
    if (!empty($get_default)) {
      $panelizer = $handler->get_default_panelizer_object($bundle, $name);
      $panelizer->base_contexts = $handler->get_base_contexts();
      return $panelizer;
    }
    else {
      $entities = entity_load($entity_type, array($key));
      if ($entities[$key] && $entities[$key]->panelizer) {
        $panelizer = $entities[$key]->panelizer;
        $panelizer->base_contexts = $handler->get_base_contexts($entities[$key]);
        return $panelizer;
      }
    }
  }
}

/**
 * Store the panelizer object in the object cache.
 *
 * CTools clumsy context editing system requires caching. This lets us
 * do it reasonably.
 *
 * @param $entity_type
 *   Can be something like 'node' or 'user' or 'default'.
 * @param $key
 *   Either the node type or the nid.
 * @param $object
 *   The cached object.
 */
function panelizer_context_cache_set($entity_type, $key, $object) {
  ctools_include('object-cache');
  ctools_object_cache_set('panelizer_context_cache', $entity_type . ':' . $key, $object);
}

/**
 * Clear the panelizer object in the object cache.
 *
 * CTools clumsy context editing system requires caching. This lets us
 * do it reasonably.
 *
 * @param $entity_type
 *   Can be something like 'node' or 'user' or 'default'.
 * @param $key
 *   Either the node type or the nid.
 */
function panelizer_context_cache_clear($entity_type, $key) {
  ctools_include('object-cache');
  ctools_object_cache_clear('panelizer_context_cache', $entity_type . ':' . $key);
}

// --------------------------------------------------------------------------
// Panels edit cache contexts.

/**
 * Get display edit cache for a panel being edited.
 *
 * The key is the second half of the key in this form:
 * panelizer:TYPE:KEY;
 */
function panelizer_panels_cache_get($argument) {
  ctools_include('object-cache');
  list($entity_type, $key) = explode(':', $argument, 2);
  $cache = ctools_object_cache_get('panelizer_display_cache', $entity_type . ':' . $key);

  // Keep $type because $entity_type can be 'default' which is not actually an
  // entity type in that case.
  $type = $entity_type;
  if ($entity_type == 'default') {
    list($entity_type, $bundle, $name) = @explode(':', $key, 3);
    $get_default = TRUE;
  }

  $handler = panelizer_entity_plugin_get_handler($entity_type);
  if (!$handler) {
    return;
  }

  // If it's already cached, we still need to restore our contexts.
  if (!empty($cache)) {
    $cache->cached = TRUE;
    if (!empty($get_default)) {
      $panelizer = $handler->get_default_panelizer_object($bundle, $name);
      $cache->display->context = $handler->get_contexts($panelizer);
    }
    else {
      $entities = entity_load($entity_type, array($key));
      if ($entities[$key] && $entities[$key]->panelizer) {
        $panelizer = $entities[$key]->panelizer;
        $cache->display->context = $handler->get_contexts($panelizer, $entities[$key]);
      }
    }

    return $cache;
  }

  $cache = new stdClass();

  // If it wasn't cached, create a new cache.
  if (!empty($get_default)) {
    $panelizer = $handler->get_default_panelizer_object($bundle, $name);
    $cache->display = $panelizer->display;
    $cache->display->context = $handler->get_contexts($panelizer);
  }
  else {
    $entities = entity_load($entity_type, array($key));
    if (empty($entities[$key]) || empty($entities[$key]->panelizer)) {
      return $cache;
    }

    list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entities[$key]);
    $panelizer = $entities[$key]->panelizer;
    $cache->display = $panelizer->display;
    $cache->display->context = $handler->get_contexts($panelizer, $entities[$key]);
  }

  ctools_include('common', 'panels');
  $cache->display->cache_key = "panelizer:$type:$key";
  $cache->content_types = panels_common_get_allowed_types('panelizer_' . $type . ':' . $bundle, $cache->display->context);

  return $cache;
}

/**
 * Store a display edit in progress in the page cache.
 */
function panelizer_panels_cache_set($argument, $cache) {
  list($type, $key) = explode(':', $argument, 2);
  ctools_include('object-cache');
  ctools_object_cache_set('panelizer_display_cache', $type . ':' . $key, $cache);
}

/**
 * Save all changes made to a display using the Page Manager page cache.
 */
function panelizer_panels_cache_clear($argument, $cache) {
  list($type, $key) = explode(':', $argument, 2);
  ctools_include('object-cache');
  ctools_object_cache_clear('panelizer_display_cache', $type . ':' . $key);
}

/**
 * Save all changes made to a display using the Page Manager page cache.
 */
function panelizer_panels_cache_save($argument, $cache) {
  list($entity_type, $key) = explode(':', $argument, 2);
  $type = $entity_type;
  if ($entity_type == 'default') {
    list($entity_type, $bundle, $name) = @explode(':', $key, 3);
    $get_default = TRUE;
  }

  $handler = panelizer_entity_plugin_get_handler($entity_type);
  if (!$handler) {
    return;
  }

  if (!empty($get_default)) {
    $panelizer = $handler->get_default_panelizer_object($bundle, $name);
    $panelizer->display = $cache->display;
    ctools_include('export');
    ctools_export_crud_save('panelizer_defaults', $panelizer);
  }
  else {
    $entities = entity_load($entity_type, array($key));
    if ($entities[$key] && $entities[$key]->panelizer) {
      $entities[$key]->panelizer->display = $cache->display;
      $entities[$key]->panelizer->display_is_modified = TRUE;
      $handler->entity_save($entities[$key]);
    }
  }
  panelizer_panels_cache_clear($argument, $cache);

}

// ---------------------------------------------------------------------------
// Contrib module hooks to provide needed functionality.


/**
 * Implements hook_export_node_alter().
 *
 * Integrate with export.module for saving panel_nodes into code.
 */
function panelizer_export_node_alter(&$node, $original_node, $method) {
  // @todo
}

/**
 * Implements hook_panelizer_defaults_alter().
 *
 * Remove the panels node because there is no point to panelizing it.
 */
function panelizer_panelizer_default_types_alter(&$bundles, $entity_type) {
  switch ($entity_type) {
    case 'node':
      // Disallow the panel node type, since it's already a panel.
      if (module_exists('panels_node') && !empty($bundles['panel'])) {
        unset($bundles['panel']);
      }
      break;
    case 'user':
  }
}

/**
 * Implements hook_features_export_alter().
 */
function panelizer_features_export_alter(&$export, $module_name) {
  if (!empty($export['features']['panelizer_defaults'])) {
    foreach ($export['features']['panelizer_defaults'] as $machine_name) {
      list ($entity_type, $bundle) = explode(':', $machine_name);

      $variables = array(
        'panelizer_defaults_' . $entity_type . '_' . $bundle,
        'panelizer_' . $entity_type . ':' . $bundle . '_allowed_layouts',
        'panelizer_' . $entity_type . ':' . $bundle . '_allowed_types',
        'panelizer_' . $entity_type . ':' . $bundle . '_default'
      );

      if (module_exists('strongarm')) {
        foreach ($variables as $key => $variable) {
          if (variable_get($variable) === NULL) {
            unset($variables[$key]);
          }
        }
        $variables = array_diff($variables, array_keys(strongarm_vars_load()));
      }

      foreach ($variables as $variable) {
        $export['features']['variable'][$variable] = $variable;
      }
    }
  }

  return array();
}

// -----------------------------------------------------------------------
// Drupal actions integration for VBO

/**
 * Implements hook_action_info().
 */
function panelizer_action_info() {
  return array(
    'panelizer_set_status_action' => array(
      'type' => 'entity',
      'label' => t('Set panelizer status'),
      'vbo_configurable' => TRUE,
      'behavior' => array('changes_property'),
      'configurable' => TRUE,
    )
  );
}

/**
 * Executes the panelizer_set_status action.
 */
function panelizer_set_status_action($entity, $context) {
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($context['entity_type'], $entity);
  if (isset($context['panelizer_default'])) {
    $entity->panelizer = $context['panelizer_default'];
    $entity->panelizer->did = NULL;

    // Ensure original values are maintained:
    $entity->panelizer->entity_id = $entity_id;
    $entity->panelizer->revision_id = $revision_id;
  }
  else {
    $entity->panelizer->name = NULL;
    $entity->panelizer->did = NULL;
  }
}

/**
 * Provides the panelizer_set_status_action form.
 */
function panelizer_set_status_action_form($context, &$form_state) {
  $form = array();
  $entity_info = entity_get_info($context['entity_type']);
  $entities = entity_load($context['entity_type'], $form_state['selection']);
  $bundles = array();

  // Collect our list of bundles.
  foreach ($entities as $entity) {
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($context['entity_type'], $entity);
    $bundles[$bundle] = $bundle;
  }

  $conditions = array(
    'panelizer_type' => $context['entity_type'],
    'panelizer_key' => $bundles,
  );

  ctools_include('export');
  $defaults = ctools_export_load_object('panelizer_defaults', 'conditions', $conditions);

  foreach ($defaults as $name => $default) {
    if (empty($default->title)) {
      $default->title = t('Default');
    }
    $options[$name] = t('@bundle: @title', array('@bundle' => $entity_info['bundles'][$default->panelizer_key]['label'], '@title' => $default->title));
  }

  natcasesort($options);
  $options = array(
    'not' => t('Not panelized'),
  ) + $options;

  $form['panelizer'] = array(
    '#type' => 'select',
    '#title' => t('Panelizer status'),
    '#options' => $options,
  );

  $form['#panelizer_defaults'] = $defaults;
  return $form;
}

function panelizer_set_status_action_submit($form, $form_state) {
  $retval = array(
    'panelizer' => $form_state['values']['panelizer'],
  );

  if ($form_state['values']['panelizer'] != 'not') {
    $retval['panelizer_default'] = $form['#panelizer_defaults'][$form_state['values']['panelizer']];
  }

  return $retval;
}