You are here

skype.module in Skype 7

Same filename and directory in other branches
  1. 8 skype.module

File

skype.module
View source
<?php

/**
 * Holds hooks and general functionality.
 */

/**
 * Implements hook_help().
 */
function skype_help($path, $arg) {
  switch ($path) {
    case 'admin/help#skype':
      $output = file_get_contents(drupal_get_path('module', 'skype') . '/README.txt');
      return module_exists('markdown') ? module_invoke('markdown', 'filter', 'process', 0, -1, $output) : '<pre>' . $output . '</pre>';
  }
}

/**
 * Implements hook_field_info().
 */
function skype_field_info() {
  return array(
    'skype' => array(
      'label' => t('Skype'),
      'settings' => array(),
      'default_widget' => 'skype',
      'default_formatter' => 'skype_button',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function skype_field_is_empty($item, $field) {
  return empty($item['skype_id']);
}

/**
 * Implements hook_field_widget_info().
 */
function skype_field_widget_info() {
  return array(
    'skype' => array(
      'label' => t('Skype'),
      'field types' => array(
        'skype',
      ),
    ),
  );
}

/**
 * Implements hook_field_instance_settings_form().
 */
function skype_field_instance_settings_form($field, $instance) {
}

/**
 * Implements hook_field_widget_form().
 */
function skype_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'skype':
      $element['skype_id'] = array(
        '#attributes' => array(
          'class' => array(
            'edit-skype-fields-skype-id',
          ),
        ),
        '#type' => 'textfield',
        '#required' => $element['#required'],
        '#description' => $element['#description'],
        '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
        '#title' => $element['#title'],
        '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
        '#delta' => $delta,
        '#prefix' => '<div class="skype-fields-skype-id-field">',
        '#suffix' => '</div>',
      );
      break;
  }
  return $element;
}

/**
 * Implements hook_field_formatter_info().
 */
function skype_field_formatter_info() {
  return array(
    'skype_button' => array(
      'label' => t('Skype Button'),
      'field types' => array(
        'skype',
      ),
      'settings' => array(
        'actions' => array(
          'call',
          'chat',
        ),
        'image_color' => 'blue',
        'image_size' => 32,
      ),
    ),
    'skype_uri' => array(
      'label' => t('Skype URI'),
      'field types' => array(
        'skype',
      ),
      'settings' => array(
        'link_text' => t('Skype me'),
        'action' => 'call',
        'show_skype_id' => FALSE,
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function skype_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  switch ($display['type']) {
    case 'skype_button':
      $element['actions'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Choose what you\'d like your button to do:'),
        '#options' => array(
          'call' => t('Call'),
          'chat' => t('Chat'),
        ),
        '#default_value' => $settings['actions'],
        '#required' => TRUE,
      );
      $element['image_color'] = array(
        '#type' => 'select',
        '#title' => t('Choose how you want your button to look:'),
        '#options' => array(
          'blue' => t('Blue'),
          'white' => t('White'),
        ),
        '#default_value' => $settings['image_color'],
        '#required' => TRUE,
      );
      $element['image_size'] = array(
        '#type' => 'select',
        '#title' => t('Choose the size of your button:'),
        '#options' => array(
          10 => '10px',
          12 => '12px',
          14 => '14px',
          16 => '16px',
          24 => '24px',
          32 => '32px',
        ),
        '#default_value' => $settings['image_size'],
        '#required' => TRUE,
      );
      break;
    case 'skype_uri':
      $element['action'] = array(
        '#type' => 'radios',
        '#title' => t('Choose what you\'d like your URI to do:'),
        '#options' => array(
          'call' => t('Call'),
          'video' => t('Video chat'),
          'chat' => t('Chat'),
        ),
        '#default_value' => $settings['action'],
        '#required' => TRUE,
      );
      $element['link_text'] = array(
        '#type' => 'textfield',
        '#title' => t('Choose the link text:'),
        '#default_value' => $settings['link_text'],
        '#required' => TRUE,
        '#description' => t('For example: "Call me" or "Video chat with me". You can use [skype:id] as token to use in your link text.'),
      );
      break;
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function skype_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $lines = array();
  switch ($display['type']) {
    case 'skype_button':
      $actions = array_filter($settings['actions']);
      $lines[] = t('Button action(s): @actions', array(
        '@actions' => implode(', ', $actions),
      ));
      $lines[] = t('Button color: @color', array(
        '@color' => $settings['image_color'],
      ));
      $lines[] = t('Button size: @sizepx', array(
        '@size' => $settings['image_size'],
      ));
      break;
    case 'skype_uri':
      $lines[] = t('URI action: @actions', array(
        '@actions' => $settings['action'],
      ));
      $lines[] = t('URI text: @text', array(
        '@text' => $settings['link_text'],
      ));
      break;
  }
  return implode('<br />', $lines);
}

/**
 * Implements hook_field_formatter_view().
 */
function skype_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'skype_button':

      // Determine protocol en lib.
      $lib = 'http://www.skypeassets.com/i/scom/js/skype-uri.js';
      if (stripos($_SERVER['SERVER_PROTOCOL'], 'https') === TRUE) {
        $lib = 'https://secure.skypeassets.com/i/scom/js/skype-uri.js';
      }

      // Render button.
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'skype_button',
          '#skype_id' => $item['skype_id'],
          '#settings' => $settings,
          '#attached' => array(
            'js' => array(
              array(
                'data' => $lib,
                'type' => 'external',
              ),
            ),
          ),
        );
      }
      break;
    case 'skype_uri':

      // Render URI.
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'skype_uri',
          '#skype_id' => $item['skype_id'],
          '#settings' => $settings,
        );
      }
      break;
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function skype_theme() {
  return array(
    'skype_button' => array(
      'variables' => array(
        'skype_id' => NULL,
        'settings' => array(),
      ),
    ),
    'skype_uri' => array(
      'variables' => array(
        'skype_id' => NULL,
        'settings' => array(),
      ),
    ),
  );
}

/**
 * Theme function to render skype button.
 */
function theme_skype_button($vars) {
  $skype_id = $vars['skype_id'];
  $settings = $vars['settings'];
  $actions = array_filter($settings['actions']);
  $skype_name = count($actions) > 1 ? 'dropdown' : array_shift($actions);
  $unique_id = uniqid();
  $output = '<div id="SkypeButton_' . $unique_id . '">
  <script type="text/javascript">
    Skype.ui({
      "name": "' . $skype_name . '",
      "element": "SkypeButton_' . $unique_id . '",
      "participants": ["' . $skype_id . '"],
      "imageColor": "' . $settings['image_color'] . '",
      "imageSize": ' . $settings['image_size'] . '
    });
  </script>
</div>';
  return $output;
}

/**
 * Theme function to render skype URI.
 */
function theme_skype_uri($vars) {
  $skype_id = $vars['skype_id'];
  $settings = $vars['settings'];
  $link_text = $settings['link_text'];
  $action = $settings['action'] == 'video' ? 'call&video=true' : $settings['action'];
  $output = '<a href="skype:' . $skype_id . '?' . $action . '">' . str_replace('[skype:id]', $skype_id, $link_text) . '</a>';
  return $output;
}