summaryrefslogtreecommitdiffstats
path: root/plugins/content_types/panels_ajax_tab_tabs.inc
blob: 646eb96c544dfdc64f727be5d07e119088332397 (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
<?php

$plugin = array(
  'title'            => t('AJAX Tab - Tabs'),
  'description'      => t('List of Tabs to control a container'),
  'single'           => TRUE,
  'category'         => t('AJAX Tabs'),
  'required context' => array(
    new ctools_context_optional(t('Entity Context'), 'entity'),
  ),
  'render callback'  => 'panels_ajax_tab_tabs_render',
  'edit form'        => 'panels_ajax_tab_tabs_edit_form'
);

function panels_ajax_tab_tabs_render($subtype, $conf, $panel_args, &$contexts) {
  static $default_tab_container_id = 0;
  ctools_include('context');
  ctools_include('plugins', 'panels');
  $block = new stdClass();
  $block->title = '';
  $block->content = '';

  // Add required JavaScript and CSS
  drupal_add_js(drupal_get_path('module', 'panels_ajax_tab') . '/js/panels_ajax_tab.js');

  // Generate the context string
  if (!$contexts[0]->data) {
    $context_string = 'none';
  }
  else {
    $entity_type = $contexts[0]->type[2];
    $entity_ids = entity_extract_ids($entity_type, $contexts[0]->data);
    $entity_id = array_shift($entity_ids);
    $context_string = $entity_type . ':' . $entity_id;
  }

  // sort and load the mini-panels
  $minipanels = array();
  uasort($conf['mini_panels'], function($a,$b) { return $a['weight'] - $b['weight']; });
  foreach ($conf['mini_panels'] as $panel_name => $panel_conf) {
    if ($panel_conf['selected']) {
      $minipanels[] = panels_mini_load($panel_name);
    }
  }

  // Render the tabs
  $block->content .= theme('panels_ajax_tab_tabs', array('minipanels' => $minipanels, 'tab_container_id' => $conf['container_id'], 'context_string' => $context_string));

  return $block;
}

function panels_ajax_tab_tabs_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];
  $panels = panels_mini_load_all();
  foreach($panels as $panel_name => $panel) {
    $panels[$panel_name] = (array) $panel;
    $panels[$panel_name] = empty($conf['mini_panels'][$panel_name]) ? $panels[$panel_name] : array_merge($panels[$panel_name],$conf['mini_panels'][$panel_name]);
  }
  if(!empty($conf['mini_panels'][$panel_name])) {
    uasort($panels, function($a,$b) { return $a['weight'] - $b['weight'];});
  }

  $form['mini_panels']['#tree'] = TRUE;
  foreach ($panels as $mini_panel) {
    $form['mini_panels'][$mini_panel['name']] = array(

      'selected' => array(
        '#type' => 'checkbox',
        '#default_value' => $mini_panel['selected'],
      ),

      'name' => array(
        '#markup' => $mini_panel['admin_title'],
      ),

      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#default_value' => $mini_panel['weight'],
        '#delta' => 10,
        '#title-display' => 'invisible',
      ),
    );
  }

  $form['container_id'] = array(
    '#title' => t('Container ID'),
    '#type' => 'textfield',
    '#description' => 'If you are using tabs in seperate pane, you must supply a unique container id string.',
    '#default_value' => $conf['container_id'],
    '#required' => TRUE,
  );
  $form['#theme'] = array('panels_ajax_tab_tabs_edit_form');

  return $form;
}

function panels_ajax_tab_tabs_edit_form_submit(&$form, &$form_state) {
  $form_state['conf']['mini_panels'] = $form_state['values']['mini_panels'];
  $form_state['conf']['container_id'] = $form_state['values']['container_id'];
}