#1824978 fix for condition when is not set
[project/panels_ajax_tab.git] / panels_ajax_tab.module
1 <?php
2
3 /**
4 * Implements hook_ctools_plugin_api().
5 */
6 function panels_ajax_tab_ctools_plugin_api() {
7 return array("version" => "1");
8 }
9
10 /**
11 * Implementation of hook_ctools_plugin_dierctory() to let the system know
12 * we implement plugins.
13 */
14 function panels_ajax_tab_ctools_plugin_directory($module, $plugin) {
15 if ($plugin == 'content_types') {
16 return 'plugins/' . $plugin;
17 }
18 }
19
20 /**
21 * Implementation of hook_menu()
22 */
23 function panels_ajax_tab_menu() {
24 $items = array();
25
26 $items['panels_ajax_tab/%/%'] = array(
27 'title' => 'panels-ajax tab AJAX callback',
28 'description' => 'AHAJ callback for panels tabs',
29 'page callback' => 'panels_ajax_tab_ajax',
30 'page arguments' => array(1,2),
31 'access arguments' => array('access content'),
32 'type' => MENU_CALLBACK,
33 );
34
35 return $items;
36 }
37
38 /**
39 * Implementation of hook_menu()
40 */
41 function panels_ajax_tab_theme($existing, $type, $theme, $path) {
42 return array(
43 'panels_ajax_tab_tabs' => array(
44 'variables' => array('minipanels' => NULL, 'tab_container_id' => NULL, 'context_string' => NULL),
45 ),
46 'panels_ajax_tab_container' => array(
47 'variables' => array('tab_container_id' => NULL, 'context_string' => NULL),
48 ),
49 'panels_ajax_tab_ajax' => array(
50 'variables' => array('minipanel' => NULL),
51 ),
52 'panels_ajax_tab_tabs_edit_form' => array(
53 'render element' => 'form',
54 ),
55 );
56 }
57
58 /**
59 * AHAH call-back for preview
60 */
61 function panels_ajax_tab_ajax($panel_name, $context_string) {
62 ctools_include('context');
63 ctools_include('plugins', 'panels');
64
65 $mini = panels_mini_load($panel_name);
66
67 if ($context_string != 'none') {
68 $parts = explode(':', $context_string);
69 $entity_type = strtolower($parts[0]);
70 $entity_id = (int) $parts[1];
71 $entity = array_pop(entity_load($entity_type, array($entity_id)));
72
73 // If it's a node, check node-view permissions
74 if ($entity_type == 'node') {
75 if (!node_access('view', $entity)) {
76 drupal_access_denied();
77 exit();
78 }
79 }
80
81 $context_plugin = ctools_get_plugins('ctools', 'contexts', 'entity');
82 $context_plugin['keyword'] = $entity_type;
83 $context = ctools_context_create_entity(FALSE, $entity, FALSE, $context_plugin);
84
85 // Load up any contexts we might be using.
86 $context = ctools_context_match_required_contexts($mini->requiredcontexts, array($context));
87 $mini->context = $mini->display->context = ctools_context_load_contexts($mini, FALSE, $context);
88 }
89
90 if (empty($mini) || !empty($mini->disabled)) {
91 return;
92 }
93
94 $mini->display->owner = $mini;
95 // unique ID of this mini.
96 $mini->display->owner->id = $mini->name;
97
98 // Clear the JavaScript and CSS that are not related to the panels being rendered - they already exists
99 drupal_static_reset('drupal_add_js');
100 drupal_static_reset('drupal_add_css');
101
102 print theme('panels_ajax_tab_ajax', array('minipanel' => $mini));
103
104 exit;
105 }
106
107
108 function theme_panels_ajax_tab_tabs($vars) {
109 foreach ($vars['minipanels'] as $mini) {
110 $tabs[] = '<a href="#" class="panels-ajax-tab-tab" data-panel-name="'.$mini->name.'" data-target-id="' . $vars['tab_container_id'] . '" data-entity-context="' . $vars['context_string'] . '">' . $mini->admin_title . '</a>';
111 }
112 return theme('item_list', array('items' => $tabs, 'attributes' => array('class' => array('tabs', 'inline', 'panels-ajax-tab'))));
113 }
114
115 function theme_panels_ajax_tab_container($vars) {
116 return '<div id="panels-ajax-tab-container-' . $vars['tab_container_id'] . '" class="panels-ajax-tab-container"></div>';
117 }
118
119 function theme_panels_ajax_tab_ajax($vars) {
120 $mini = $vars['minipanel'];
121 $layout = panels_get_layout($mini->display->layout);
122 $panels_output = panels_render_display($mini->display);
123
124 $output = '';
125 $output .= '<?xml version="1.0" encoding="UTF-8" ?>';
126 $output .= '
127 <html version="HTML+RDFa+MathML 1.1"
128 xmlns:content="http://purl.org/rss/1.0/modules/content/"
129 xmlns:dc="http://purl.org/dc/terms/"
130 xmlns:foaf="http://xmlns.com/foaf/0.1/"
131 xmlns:og="http://ogp.me/ns#"
132 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
133 xmlns:sioc="http://rdfs.org/sioc/ns#"
134 xmlns:sioct="http://rdfs.org/sioc/types#"
135 xmlns:skos="http://www.w3.org/2004/02/skos/core#"
136 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
137 xmlns:mml="http://www.w3.org/1998/Math/MathML">
138 ';
139 $output .= '<head>';
140
141 // Get the javascript and filter out the default misc/drupal.js - it's already been added by the calling page
142 $javascript = drupal_add_js();
143 unset($javascript['misc/drupal.js']);
144 $output .= drupal_get_js('header', $javascript);
145
146 // Add CSS
147 $output .= drupal_get_css();
148 $output .= "<link rel='stylesheet' type='text/css' href='".base_path().$layout['path'].'/'.$layout['css']."' />";
149
150 $output .= '</head>';
151
152 // Print the output of the panel
153 $output .= '<body>';
154 $output .= theme('status_messages');
155 $output .= '<div class="panels-ajax-tab-panel panels-ajax-tab-panel-'.str_replace('_', '-', $mini->name).'">';
156 $output .= $panels_output;
157 $output .= '</div>';
158 $output .= '</body></html>';
159
160 return $output;
161 }
162
163
164 function theme_panels_ajax_tab_tabs_edit_form($variables) {
165 $form = $variables['form'];
166 $output = '';
167 // Render elements that are supposed to be on top.
168 foreach(element_children($form) as $element) {
169 if($element == 'mini_panels') {
170 break;
171 }
172 else {
173 $output .= drupal_render($form[$element]);
174 }
175 }
176 $rows = array();
177
178 foreach(element_children($form['mini_panels']) as $mini_panel) {
179 $form['mini_panels'][$mini_panel]['weight']['#attributes']['class'] = array('panel-weight');
180 $rows[] = array(
181 'data' => array(
182 drupal_render($form['mini_panels'][$mini_panel]['selected']),
183 drupal_render($form['mini_panels'][$mini_panel]['name']),
184 drupal_render($form['mini_panels'][$mini_panel]['weight']),
185 ),
186 'class' => array('draggable'),
187 );
188
189 }
190 $header = array(t('Select'), t('Mini panel name'), t('Weight'));
191 $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'mini-panels-table')));
192 $output .= drupal_render_children($form);
193 drupal_add_tabledrag('mini-panels-table', 'order', 'sibling', 'panel-weight');
194 return $output;
195 }