You are here

web_widgets.module in Web Widgets 7

Same filename and directory in other branches
  1. 6 web_widgets.module

web_widgets module main. Contains the views api hook and theming functions.

File

web_widgets.module
View source
<?php

/**
 * @file
 *   web_widgets module main. Contains the views api hook and theming functions.
 */

/**
 * Implements hook_views_api().
 */
function web_widgets_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'web_widgets') . '/views',
  );
}

/**
 * Implements hook_theme().
 */
function web_widgets_theme($existing, $type, $theme, $path) {
  $theme = array(
    'web_widgets_embed_code' => array(
      'variables' => array(
        'code' => NULL,
        'style' => NULL,
      ),
    ),
    'web_widgets_tracking_code' => array(
      'variables' => array(
        'id' => NULL,
      ),
    ),
  );
  $style_files = array_keys(web_widgets_get_styles());
  foreach ($style_files as $style) {
    require_once DRUPAL_ROOT . '/' . web_widgets_get_style_inc($style);
    $definition = call_user_func('web_widgets_style_' . $style);
    if (is_array($definition['theme'])) {
      $theme += $definition['theme'];
    }
  }
  return $theme;
}

/**
 * Get a list of styles from the subdirectories.
 *
 * @return
 *   Associative array where the keys of the array are style id strings and
 *   the values are human readable style names.
 */
function web_widgets_get_styles() {
  $styles = array();
  $module_main = drupal_get_path('module', 'web_widgets');
  $dir = opendir($module_main);
  while (($style = readdir($dir)) !== FALSE) {
    if (is_dir($module_main . '/' . $style) && is_file(web_widgets_get_style_inc($style))) {
      require_once DRUPAL_ROOT . '/' . web_widgets_get_style_inc($style);
      $def = 'web_widgets_style_' . $style;
      $definition = $def();
      $styles[$style] = isset($definition['human_readable']) ? $definition['human_readable'] : $style;
    }
  }
  return $styles;
}

/**
 * Renders the embed code for a given widget style.
 *
 * @param $style
 *   One of the widget styles returned by widgets_get_styles().
 * @param $path
 *   Path that returns a widget.
 * @param $width
 *   Width of the widget (not respected by all styles).
 * @param $height
 *   Height of the widget (not respected by all styles).
 * @return
 *   HTML that displays the widget embed code.
 */
function web_widgets_render_embed_code($style, $variables) {
  require_once DRUPAL_ROOT . '/' . web_widgets_get_style_inc($style);

  // TODO Please change this theme call to use an associative array for the $variables parameter.
  $code = theme('web_widgets_' . $style, $variables);

  // TODO Please change this theme call to use an associative array for the $variables parameter.
  return theme('web_widgets_embed_code', array(
    'code' => $code,
    'style' => $style,
  ));
}

/**
 * Renders a given content as a widget. This function returns the
 * actual content of a widget wrapped in style specific markup.
 *
 * Warning: you cannot use all styles. Exception is: uwa
 *
 * @param $style
 *   One of the widget styles returned by widgets_get_styles().
 * @param $content
 *   Content to render as widget.
 * @param $add_target
 *   If TRUE, the anchors tags will be modified so they will open in new window.
 * @return
 *   HTML wrapped in style specific markup.
 */
function web_widgets_render_widget($style, $content, $title, $add_target = TRUE, $scripts = array(), $styles = array()) {
  require_once DRUPAL_ROOT . '/' . web_widgets_get_style_inc($style);
  if ($add_target === TRUE) {
    $content = _web_widgets_rewrite_anchors($content);
  }
  module_invoke_all('web_widgets_render_widget', $content, $title);
  return theme('web_widgets_' . $style . '_wrapper', array(
    'content' => $content,
    'title' => $title,
    'scripts' => $scripts,
    'styles' => $styles,
  ));
}

/**
 * Stores the fact that if we're inside web_widget processing.
 */
function web_widgets_inside_widget($in = NULL) {
  static $base_path;
  if ($in === TRUE) {
    $base_path = $GLOBALS['base_path'];
    $options = array(
      'language' => new stdClass(),
      'absolute' => TRUE,
      'purl' => array(
        'disabled' => TRUE,
      ),
    );

    // TODO The second parameter to this function call should be an array.
    $GLOBALS['base_path'] = url('', $options);
  }
  elseif ($in === FALSE) {
    $GLOBALS['base_path'] = $base_path;
  }
  static $inside = FALSE;
  if (is_null($in)) {
    return $inside;
  }
  else {
    $inside = $in;
  }
}

/**
 * Retrieve the path to a style include file.
 *
 * @param $style
 *   One of the widget styles returned by widgets_get_styles().
 * @return
 *   Path to widget style .inc file relative to Drupal's root directory.
 */
function web_widgets_get_style_inc($style) {
  return drupal_get_path('module', 'web_widgets') . '/' . $style . '/web_widgets_style_' . $style . '.inc';
}

/**
 * Extend all anchor tags by target attribute.
 *
 * @content
 *   HTML code.
 */
function _web_widgets_rewrite_anchors($content) {
  return preg_replace_callback('/(<a[^>]*>)/', "_web_widgets_add_attrib", $content);
}

/**
 * Helper function for preg_replace_callback.
 * @see _web_widgets_rewrite_anchors()
 *
 * @param $matches
 *   Regexp matches
 */
function _web_widgets_add_attrib($matches) {
  return str_replace('>', ' target="_top">', $matches[0]);
}

/**
 * Shows the widget code to the user.
 * Usually appears when the widget display is attached to a page display.
 * In most cases you don't want to call this function directly, but use
 * web_widgets_render_embed_code() to render embed code.
 *
 * @param $code
 *   The code that can be copy/pasted from this site to another site.
 * @param $style
 *   The embed style of the widget.
 * @return
 *   HTML that displays the widget embed code.
 */
function theme_web_widgets_embed_code($variables) {
  $code = $variables['code'];
  $style = $variables['style'];
  $styles = web_widgets_get_styles();
  $style_name = $styles[$style];
  static $num = 0;
  $form = array(
    '#type' => 'textarea',
    '#title' => t('Embed code'),
    '#description' => t('Copy and paste this code to your website.'),
    '#id' => 'web_widgets_' . $num++,
    '#name' => 'web_widgets',
    '#value' => $code,
    '#parents' => array(
      'none',
    ),
    '#maxlength' => FALSE,
    '#attributes' => array(
      'onclick' => 'this.focus(); this.select();',
    ),
    '#cols' => 30,
    '#rows' => 5,
  );
  if ($style == 'uwa') {
    $form['#type'] = 'item';
    $form['#value'] = l($code, $code);
    $form['#title'] = t('@style widget', array(
      '@style' => $style_name,
    ));
    $form['#description'] = t('Embed your widget on NetVibes or iGoogle.');
  }
  else {
    if ($style == 'google_gadget') {
      $form['#title'] = t('@style widget', array(
        '@style' => $style_name,
      ));
      $form['#description'] = t('Copy and paste this code to your iGoogle page.');
    }
  }
  return drupal_render($form);
}

/**
 * Enables the tracking of the
 *
 * @param $id
 *   Web Property ID from Google Analytics
 */
function theme_web_widgets_tracking_code($id) {

  // TODO: Should this theme web_widgets_tracking_code be declared in hook_theme()?
  return '
<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  try {
    var pageTracker = _gat._getTracker("' . $id . '");
    pageTracker._trackPageview();
  } catch(err) {}
</script>';
}

Functions

Namesort descending Description
theme_web_widgets_embed_code Shows the widget code to the user. Usually appears when the widget display is attached to a page display. In most cases you don't want to call this function directly, but use web_widgets_render_embed_code() to render embed code.
theme_web_widgets_tracking_code Enables the tracking of the
web_widgets_get_styles Get a list of styles from the subdirectories.
web_widgets_get_style_inc Retrieve the path to a style include file.
web_widgets_inside_widget Stores the fact that if we're inside web_widget processing.
web_widgets_render_embed_code Renders the embed code for a given widget style.
web_widgets_render_widget Renders a given content as a widget. This function returns the actual content of a widget wrapped in style specific markup.
web_widgets_theme Implements hook_theme().
web_widgets_views_api Implements hook_views_api().
_web_widgets_add_attrib Helper function for preg_replace_callback.
_web_widgets_rewrite_anchors Extend all anchor tags by target attribute.