comparison sites/all/modules/quicktabs/quicktabs.module @ 2:b74b41bb73f0

-- Google analytics module
author danieleb <danielebarchiesi@me.com>
date Thu, 22 Aug 2013 17:22:54 +0100
parents
children a75ead649730
comparison
equal deleted inserted replaced
1:67ce89da90df 2:b74b41bb73f0
1 <?php
2
3 /**
4 * Implements hook_help().
5 */
6 function quicktabs_help($path, $arg) {
7 switch ($path) {
8 case 'admin/help#quicktabs':
9 $output = '<p>' . t('The Quicktabs module allows you to create blocks of tabbed content. Clicking on the tabs makes the corresponding content display instantly (it uses jQuery). The content for each tabbed section can be a node, view, block or another Quicktabs instance. You can create an unlimited number of Quicktabs instances, each of which will automatically have an associated block.') . '</p>';
10 $output .= '<p>' . t('The <a href="@quicktabs">quicktabs page</a> displays all quicktabs currently available on your site. Create new quicktabs using the <a href="@add-quicktab">add quicktab page</a> (the block containing a new quicktab must also be enabled on the <a href="@blocks">blocks administration page</a>).', array('@quicktabs' => url('admin/structure/quicktabs'), '@add-quicktab' => url('admin/structure/quicktab/add'), '@blocks' => url('admin/structure/block'))) . '</p>';
11 return $output;
12 }
13 if ($path == 'admin/structure/quicktabs' && module_exists('block')) {
14 return '<p>' . t('Each Quicktabs instance has a corresponding block that is managed on the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
15 }
16 }
17
18 /**
19 * Implements hook_menu().
20 */
21 function quicktabs_menu() {
22 $items['admin/structure/quicktabs'] = array(
23 'title' => 'Quicktabs',
24 'description' => 'Create blocks of tabbed content.',
25 'page callback' => 'quicktabs_list',
26 'access callback' => 'user_access',
27 'access arguments' => array('administer quicktabs'),
28 'type' => MENU_NORMAL_ITEM,
29 'file' => 'quicktabs.admin.inc',
30 );
31 $items['admin/structure/quicktabs/list'] = array(
32 'title' => 'List quicktabs',
33 'type' => MENU_DEFAULT_LOCAL_TASK,
34 );
35 $items['admin/structure/quicktabs/add'] = array(
36 'title' => 'Add Quicktabs Instance',
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('quicktabs_form', 'add'),
39 'access arguments' => array('administer quicktabs'),
40 'type' => MENU_LOCAL_ACTION,
41 'file' => 'quicktabs.admin.inc',
42 );
43 $items['admin/structure/quicktabs/manage/%quicktabs'] = array(
44 'title' => 'Edit quicktab',
45 'page callback' => 'drupal_get_form',
46 'page arguments' => array('quicktabs_form', 'edit', 4),
47 'access arguments' => array('administer quicktabs'),
48 'file' => 'quicktabs.admin.inc',
49 );
50 $items['admin/structure/quicktabs/manage/%quicktabs/edit'] = array(
51 'title' => 'Edit quicktab',
52 'type' => MENU_DEFAULT_LOCAL_TASK,
53 'context' => MENU_CONTEXT_INLINE,
54 );
55 $items['admin/structure/quicktabs/manage/%quicktabs/delete'] = array(
56 'title' => 'Delete quicktab',
57 'page callback' => 'drupal_get_form',
58 'page arguments' => array('quicktabs_block_delete', 4),
59 'access arguments' => array('administer quicktabs'),
60 'type' => MENU_LOCAL_TASK,
61 'file' => 'quicktabs.admin.inc',
62 );
63 $items['admin/structure/quicktabs/manage/%quicktabs/clone'] = array(
64 'title' => 'Clone quicktab',
65 'page callback' => 'quicktabs_clone',
66 'page arguments' => array(4),
67 'access arguments' => array('administer quicktabs'),
68 'type' => MENU_LOCAL_TASK,
69 'file' => 'quicktabs.admin.inc',
70 );
71 $items['admin/structure/quicktabs/manage/%quicktabs/export'] = array(
72 'title' => 'Export',
73 'page callback' => 'drupal_get_form',
74 'page arguments' => array('quicktabs_export_form', 4),
75 'access arguments' => array('administer quicktabs'),
76 'type' => MENU_LOCAL_TASK,
77 'file' => 'quicktabs.admin.inc',
78 );
79 $items['quicktabs/ajax'] = array(
80 'page callback' => 'quicktabs_ajax',
81 'access callback' => 'user_access',
82 'access arguments' => array('access content'),
83 'type' => MENU_CALLBACK,
84 );
85 return $items;
86 }
87
88
89 /**
90 * Implements hook_permission().
91 */
92 function quicktabs_permission() {
93 return array(
94 'administer quicktabs' => array(
95 'title' => t('Administer Quicktabs'),
96 ),
97 );
98 }
99
100 /**
101 * Implements hook_theme().
102 */
103 function quicktabs_theme() {
104 return array(
105 'quicktabs_admin_form_tabs' => array(
106 'render element' => 'tabs',
107 'file' => 'quicktabs.admin.inc',
108 ),
109 'qt_ui_tabs' => array(
110 'render element' => 'element',
111 ),
112 'qt_ui_tabs_tabset' => array(
113 'render element' => 'tabset',
114 ),
115 'qt_quicktabs' => array(
116 'render element' => 'element',
117 ),
118 'qt_quicktabs_tabset' => array(
119 'render element' => 'tabset',
120 ),
121 'qt_accordion' => array(
122 'render element' => 'element',
123 ),
124 'quicktabs_tab_access_denied' => array(
125 'variables' => array('tab'),
126 ),
127 );
128 }
129
130 /**
131 * Implements hook_block_info().
132 */
133 function quicktabs_block_info() {
134 $blocks = array();
135 foreach (quicktabs_load_multiple() as $qt_name => $quicktabs) {
136 $blocks[$qt_name]['info'] = $quicktabs->title;
137 }
138 return $blocks;
139 }
140
141 /**
142 * Implements hook_block_view().
143 */
144 function quicktabs_block_view($delta = '') {
145 $block = array();
146 if ($qt = quicktabs_build_quicktabs($delta)) {
147 if (isset($qt['content']) && !empty($qt['content'])) {
148 $block['content'] = $qt['content'];
149 $block['content']['#contextual_links']['quicktabs'] = array('admin/structure/quicktabs/manage', array($delta));
150 $block['subject'] = check_plain($qt['#title']);
151 }
152 }
153 return $block;
154 }
155
156 /**
157 * Constructs a Quicktabs instance.
158 *
159 * This function can be called by other modules to programmatically build a
160 * quicktabs instance.
161 *
162 * @param name. The machine name of the Quicktabs instance to build - if a name
163 * is passed that does not correspond to an existing instance, then it is taken
164 * to be a completely custom instance and is built from only the custom tabs
165 * that are passed in.
166 *
167 * @param settings. An array of settings that will override the options of the Quicktabs
168 * instance from the database, or if no existing instance is being used, these
169 * will override the default settings. Possible keys are 'style', 'hide_empty_tabs',
170 * ajax', 'default_tab', 'renderer', 'title' and 'options'.
171 *
172 * @param custom_tabs. An array representing custom tab contents, which will be
173 * appended to the Quicktabs instance from the database, or if no existing instance
174 * is being used, the custom tabs will be the entire contents. An example custom_tabs
175 * array would be array(array('title' => 'custom', 'contents' => array('#markup' =>
176 * t('Some markup'), 'weight' => 5));
177 *
178 * @return A render array that can be used as block content in hook_block_view
179 * (see quicktabs_block_view()), but can also just be added to the page array
180 * during hook_page_alter, or output anywhere else where it's sure to get
181 * passed through drupal_render().
182 */
183 function quicktabs_build_quicktabs($name, $settings = array(), $custom_tabs = array()) {
184 if ($info = quicktabs_load($name)) {
185 // Allow other modules to alter the Quicktabs instance before it gets output.
186 drupal_alter('quicktabs', $info);
187 $info = (array) $info;
188 $settings = array_merge($info, $settings);
189 $contents = $settings['tabs'];
190 unset($settings['tabs'], $settings['machine_name']);
191 }
192 elseif (!empty($custom_tabs)) {
193 // We'll be creating a custom Quicktabs instance. Make sure we're using an
194 // alphanumeric name.
195 $name = preg_replace('/[^[a-zA-Z]_]/', '_', $name);
196 $contents = array();
197 }
198 else {
199 // If $name doesn't correspond to an existing Quicktabs instance, and there
200 // are no custom tabs to render, then we have nothing to do.
201 return array();
202 }
203 $renderer = isset($settings['renderer']) ? $settings['renderer'] : 'quicktabs';
204 unset($settings['renderer']);
205 foreach ($custom_tabs as &$tab) {
206 $tab += array(
207 'type' => 'prerendered',
208 'weight' => 0,
209 );
210 }
211 $contents = array_merge($custom_tabs, $contents);
212 $weight = array();
213 foreach ($contents as $key => $item) {
214 // Load the plugin responsible for rendering this item, if it is not a
215 // prerendered tab.
216 if ($item['type'] != 'prerendered') {
217 ctools_plugin_load_class('quicktabs', 'contents', $item['type'], 'handler');
218 }
219
220 // Add item's weight to our weights array so that we can then sort by weight.
221 $weight[$key] = $item['weight'];
222
223 // Make sure we're not going to try to load the same QuickSet instance
224 // inside itself.
225 if ($item['type'] == 'qtabs' && $item['machine_name'] == $name) {
226 unset($contents[$key]);
227 unset($weight[$key]);
228 }
229 }
230 // Only sort by weight if the tabs haven't already been sorted by some other
231 // mechanism, e.g. Views in the case of the Views style plugin.
232 if (!isset($settings['sorted']) || !$settings['sorted']) {
233 array_multisort($weight, SORT_ASC, $contents);
234 }
235 else {
236 unset($settings['sorted']);
237 }
238 if ($qt = quickset_renderer_factory($name, $contents, $renderer, $settings)) {
239 $renderable_qt = array('#title' => $qt->getTitle(), 'content' => $qt->render());
240 return $renderable_qt;
241 }
242 return array();
243 }
244
245 /**
246 * Ajax callback for tab content.
247 *
248 * @param name The machine name of the quicktabs instance.
249 *
250 * @param index The tab index we're returning content for.
251 *
252 * @param type The type of content we're rendering.
253 *
254 * @return a json-formatted ajax commands array.
255 */
256 function quicktabs_ajax($name, $index, $type) {
257
258 $args = func_get_args();
259 $variable_args = array_slice($args, 3);
260 // Add the Quicktabs machine name to the args we pass to the content renderer
261 array_unshift($variable_args, $name);
262
263 $data = QuickSet::ajaxRenderContent($type, $variable_args);
264
265 $commands = array();
266 $tabpage_id = 'quicktabs-tabpage-'. $name .'-' . $index;
267 $commands[] = ajax_command_append('#quicktabs-container-'. $name, '<div id="' . $tabpage_id .'" class="quicktabs-tabpage">'. $data .'</div>');
268 $page = array('#type' => 'ajax', '#commands' => $commands);
269 ajax_deliver($page);
270 }
271
272 /**
273 * Load the quicktabs data for a particular instance.
274 */
275 function quicktabs_load($name) {
276 $qts = quicktabs_load_multiple(array($name));
277 return isset($qts[$name]) ? $qts[$name] : NULL;
278 }
279
280 /**
281 * Load the quicktabs data.
282 */
283 function quicktabs_load_multiple($names = array()) {
284 ctools_include('export');
285 $defaults = empty($names) ? ctools_export_load_object('quicktabs', 'all') : ctools_export_load_object('quicktabs', 'names', $names);
286 return $defaults;
287 }
288
289 /**
290 * Exports the specified Quicktabs instance with translatable strings.
291 */
292 function quicktabs_export($qt, $indent = '') {
293 $output = ctools_export_object('quicktabs', $qt, $indent);
294 $translatables = array();
295 if (!empty($qt->title)) {
296 $translatables[] = $qt->title;
297 }
298 foreach ($qt->tabs as $tab) {
299 $translatables[] = $tab['title'];
300 }
301 $translatables = array_filter(array_unique($translatables));
302 if (!empty($translatables)) {
303 $output .= "\n";
304 $output .= "{$indent}// Translatables\n";
305 $output .= "{$indent}// Included for use with string extractors like potx.\n";
306 sort($translatables);
307 foreach ($translatables as $string) {
308 $output .= "{$indent}t(" . ctools_var_export($string) . ");\n";
309 }
310 $output .= "\n";
311 }
312 return $output;
313 }
314
315 /**
316 * Implements hook_i18n_string_info()
317 */
318 function quicktabs_i18n_string_info() {
319 $groups['quicktabs'] = array(
320 'title' => t('Quicktabs'),
321 'description' => t('Vocabulary titles and term names for localizable quicktabs.'),
322 'format' => FALSE, // This group doesn't have strings with format
323 'list' => TRUE, // This group can list all strings
324 );
325 return $groups;
326 }
327
328 function quicktabs_translate($name, $string, $langcode = NULL, $textgroup = 'quicktabs') {
329 return function_exists('i18n_string') ? i18n_string($textgroup . ':' . $name, $string, array('langcode' => $langcode)) : $string;
330 }
331
332 /**
333 * Update translatable strings.
334 */
335 function quicktabs_i18n_update_strings($names = array()) {
336 if (!function_exists('i18n_string_update')) return;
337 $qts = quicktabs_load_multiple($names);
338 foreach ($qts as $name => $quicktabs) {
339 i18n_string_update("quicktabs:title:$name", $quicktabs->title);
340 foreach ($quicktabs->tabs as $tabkey => $tab) {
341 i18n_string_update("quicktabs:tab:$name-$tabkey:title", $tab['title']);
342 }
343 }
344 }
345
346 /**
347 * Implements hook_i18n_string_refresh().
348 *
349 * Refresh translations for all user-generated strings managed by quicktabs.
350 * This will load all strings inputted via the quicktabs user interface and
351 * register them (and their translations, if there are any) with the
352 * i18n_strings system.
353 */
354 function quicktabs_i18n_string_refresh($group) {
355 if ($group === 'quicktabs') {
356 quicktabs_i18n_update_strings();
357 }
358 return TRUE;
359 }
360
361 /**
362 * Implements hook_ctools_plugin_type().
363 */
364 function quicktabs_ctools_plugin_type() {
365 return array(
366 // Renderer plugins control the display of sets of items, e.g. as tabs.
367 'renderers' => array(
368 'cache' => TRUE,
369 'use hooks' => TRUE,
370 'classes' => array('handler'),
371 ),
372 // Content plugins control the display of individual items.
373 'contents' => array(
374 'cache' => TRUE,
375 'use hooks' => TRUE,
376 'classes' => array('handler'),
377 )
378 );
379 }
380
381 /**
382 * Implements hook_quicktabs_renderers().
383 */
384 function quicktabs_quicktabs_renderers() {
385 $info = array();
386 $path = drupal_get_path('module', 'quicktabs') . '/plugins';
387 $info['quicktabs'] = array(
388 'path' => $path,
389 'handler' => array(
390 'file' => 'QuickQuicktabs.inc',
391 'class' => 'QuickQuicktabs',
392 ),
393 );
394 $info['ui_tabs'] = array(
395 'path' => $path,
396 'handler' => array(
397 'file' => 'QuickUiTabs.inc',
398 'class' => 'QuickUitabs',
399 ),
400 );
401 $info['accordion'] = array(
402 'path' => $path,
403 'handler' => array(
404 'file' => 'QuickAccordion.inc',
405 'class' => 'QuickAccordion',
406 ),
407 );
408 return $info;
409 }
410
411 /**
412 * Implements hook_quicktabs_contents().
413 */
414 function quicktabs_quicktabs_contents() {
415 $info = array();
416 $path = drupal_get_path('module', 'quicktabs') . '/plugins';
417 $info['block'] = array(
418 'path' => $path,
419 'handler' => array(
420 'file' => 'QuickBlockContent.inc',
421 'class' => 'QuickBlockContent',
422 ),
423 'dependencies' => array('block'),
424 );
425 $info['view'] = array(
426 'path' => $path,
427 'handler' => array(
428 'file' => 'QuickViewContent.inc',
429 'class' => 'QuickViewContent',
430 ),
431 'dependencies' => array('views'),
432 );
433 $info['node'] = array(
434 'path' => $path,
435 'handler' => array(
436 'file' => 'QuickNodeContent.inc',
437 'class' => 'QuickNodeContent',
438 ),
439 );
440 $info['qtabs'] = array(
441 'path' => $path,
442 'handler' => array(
443 'file' => 'QuickQtabsContent.inc',
444 'class' => 'QuickQtabsContent',
445 ),
446 );
447 $info['callback'] = array(
448 'path' => $path,
449 'handler' => array(
450 'file' => 'QuickCallbackContent.inc',
451 'class' => 'QuickCallbackContent',
452 ),
453 );
454 return $info;
455 }
456
457 /**
458 * Returns a renderered QuickSet.
459 */
460 function quickset_renderer_factory($name, $contents, $renderer, $settings) {
461 return QuickSet::QuickSetRendererFactory($name, $contents, $renderer, $settings);
462 }
463
464 /**
465 * Returns an object that implements the QuickContent interface.
466 */
467 function quick_content_factory($name, $item) {
468 return QuickContent::factory($name, $item);
469 }
470
471 /**
472 * Determine if the machine name is in use.
473 */
474 function quicktabs_machine_name_exists($machine_name) {
475 $qt_exists = db_query_range('SELECT 1 FROM {quicktabs} WHERE machine_name = :name', 0, 1, array(':name' => $machine_name))->fetchField();
476 return $qt_exists;
477 }
478
479 /**
480 * Implementation of hook_views_api().
481 */
482 function quicktabs_views_api() {
483 return array(
484 'api' => 3,
485 );
486 }
487
488 /**
489 * Theme function to display the access denied tab.
490 *
491 * @ingroup themeable
492 */
493 function theme_quicktabs_tab_access_denied($variables) {
494 return t('You are not authorized to access this content.');
495 }
496
497
498 /**
499 * Fetch the necessary CSS files for the tab style.
500 */
501 function quicktabs_get_css($style) {
502 if ($style == 'default') {
503 // Get the default style.
504 $style = variable_get('quicktabs_tabstyle', 'nostyle');
505 }
506 if ($style == 'nostyle') return array();
507 $style_css = _quicktabs_get_style_css($style);
508 return $style_css;
509 }
510
511 /**
512 * Helper function to get the css file for given style.
513 */
514 function _quicktabs_get_style_css($style) {
515 $tabstyles = &drupal_static(__FUNCTION__);
516 if (empty($tabstyles)) {
517 $cid = 'quicktabs_tabstyles';
518 $cache = cache_get($cid);
519 if (!$cache) {
520 $tabstyles = module_invoke_all('quicktabs_tabstyles');
521 cache_set($cid, $tabstyles);
522 }
523 else {
524 $tabstyles = $cache->data;
525 }
526 }
527 if ($css_file = array_search($style, $tabstyles)) {
528 return array('data' => $css_file);
529 }
530 return array();
531 }
532
533 /**
534 * Theme function to output tablinks for jQuery UI style tabs.
535 *
536 * @ingroup themeable
537 */
538 function theme_qt_ui_tabs_tabset($vars) {
539
540 $output = '<ul>';
541 foreach ($vars['tabset']['tablinks'] as $i => $tab) {
542 if (is_array($tab)) {
543 $output .= '<li>'. drupal_render($tab) .'</li>';
544 }
545 }
546 $output .= '</ul>';
547 return $output;
548 }
549
550 /**
551 * Theme function to output content for jQuery UI style tabs.
552 *
553 * @ingroup themeable
554 */
555 function theme_qt_ui_tabs($variables) {
556 $element = $variables['element'];
557 $output = '<div '. drupal_attributes($element['#options']['attributes']) .'>';
558 $output .= drupal_render($element['tabs']);
559
560 if (isset($element['active'])) {
561 $output .= drupal_render($element['active']);
562 }
563 else {
564 foreach ($element['divs'] as $div) {
565 $output .= drupal_render($div);
566 }
567 }
568
569 $output .= '</div>';
570 return $output;
571 }
572
573 /**
574 * Theme function to output tablinks for classic Quicktabs style tabs.
575 *
576 * @ingroup themeable
577 */
578 function theme_qt_quicktabs_tabset($vars) {
579 $variables = array(
580 'attributes' => array(
581 'class' => 'quicktabs-tabs quicktabs-style-' . $vars['tabset']['#options']['style'],
582 ),
583 'items' => array(),
584 );
585 foreach (element_children($vars['tabset']['tablinks']) as $key) {
586 $item = array();
587 if (is_array($vars['tabset']['tablinks'][$key])) {
588 $tab = $vars['tabset']['tablinks'][$key];
589 if ($key == $vars['tabset']['#options']['active']) {
590 $item['class'] = array('active');
591 }
592 $item['data'] = drupal_render($tab);
593 $variables['items'][] = $item;
594 }
595 }
596 return theme('item_list', $variables);
597 }
598
599 /**
600 * Theme function to output content for classic Quicktabs style tabs.
601 *
602 * @ingroup themeable
603 */
604 function theme_qt_quicktabs($variables) {
605 $element = $variables['element'];
606 $output = '<div '. drupal_attributes($element['#options']['attributes']) .'>';
607 $output .= drupal_render($element['tabs']);
608
609 $output .= drupal_render($element['container']);
610
611 $output .= '</div>';
612 return $output;
613 }
614
615
616 /**
617 * Theme function to output markup for the accordion style.
618 *
619 * @ingroup themeable
620 */
621 function theme_qt_accordion($variables) {
622 $element = $variables['element'];
623 $output = '<div '. drupal_attributes($element['#options']['attributes']) .'>';
624 foreach ($element['divs'] as $div) {
625 $output .= drupal_render($div);
626 }
627
628 $output .= '</div>';
629 return $output;
630 }