You are here

open_readspeaker.module in Open ReadSpeaker 7

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

Open ReadSpeaker

An open version of the official module from ReadSpeaker.

This one have support differents configuration with blocks and allow better use of the reader.

File

open_readspeaker.module
View source
<?php

/**
 * @file
 * Open ReadSpeaker
 *
 * An open version of the official module from ReadSpeaker.
 *
 * This one have support differents configuration with blocks and allow better
 * use of the reader.
 */

/**
 * Implements hook_menu().
 */
function open_readspeaker_menu() {
  $items['admin/config/services/open-readspeaker'] = array(
    'title' => 'Open ReadSpeaker',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'open_readspeaker_config',
    ),
    'access arguments' => array(
      'administer open_readspeaker',
    ),
    'file' => 'open_readspeaker.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function open_readspeaker_permission() {
  return array(
    'administer open_readspeaker' => array(
      'title' => t('Administer Open ReadSpeaker'),
      'description' => t('Access the Open ReadSpeaker administration pages.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_theme().
 */
function open_readspeaker_theme($existing, $type, $theme, $path) {
  return array(
    'open_readspeaker_ui' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function open_readspeaker_block_info() {
  $blocks['open_readspeaker_ui'] = array(
    'info' => t('Open ReadSpeaker UI'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
  );
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function open_readspeaker_block_configure($delta = '') {
  $form = array();
  if ($delta == 'open_readspeaker_ui') {
    $form['settings'] = array(
      '#title' => t('Custom settings for ReadSpeaker'),
      '#description' => t('Custom settings.'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['settings']['open_readspeaker_buttonstyle'] = array(
      '#title' => t('Style attribute for the ReadSpeaker Button'),
      '#description' => t('Set a style attribute for the ReadSpeaker Button.'),
      '#type' => 'textfield',
      '#default_value' => variable_get('open_readspeaker_buttonstyle', ''),
    );
    $form['settings']['open_readspeaker_reading_area'] = array(
      '#title' => t('Reading area ID'),
      '#description' => t('Specify content using HTML ID attribute.'),
      '#type' => 'textfield',
      '#default_value' => variable_get('open_readspeaker_reading_area', 'rs_read_this'),
    );
    $form['settings']['open_readspeaker_customparam'] = array(
      '#title' => t('Custom script parameters'),
      '#description' => t('Input any custom parameters given to you by ReadSpeaker Support here.'),
      '#type' => 'textfield',
      '#default_value' => variable_get('open_readspeaker_customparam', ''),
    );
  }
  return $form;
}

/**
 * Implementation of hook_block_save().
 */
function open_readspeaker_block_save($delta = '', $edit = array()) {
  if ($delta === 'open_readspeaker_ui') {
    if (!empty($edit['open_readspeaker_buttonstyle'])) {
      variable_set('open_readspeaker_buttonstyle', check_plain($edit['open_readspeaker_buttonstyle']));
    }
    if (!empty($edit['open_readspeaker_customparam'])) {
      variable_set('open_readspeaker_customparam', check_plain($edit['open_readspeaker_customparam']));
    }
    if (!empty($edit['open_readspeaker_reading_area'])) {
      variable_set('open_readspeaker_reading_area', check_plain($edit['open_readspeaker_reading_area']));
    }
  }
}

/**
 * Implements hook_block_view().
 */
function open_readspeaker_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'open_readspeaker_ui':
      $block['content'] = array(
        '#theme' => 'open_readspeaker_ui',
      );
      break;
  }
  return $block;
}

/**
 * ReadSpeaker button.
 */
function theme_open_readspeaker_ui() {

  // We don't use the $vars param.
  global $is_https;
  $output = '';
  $http = 'http';
  $accountid = variable_get('open_readspeaker_accountid', '');
  $open_readspeaker_i18n = variable_get('open_readspeaker_i18n', '');
  $custom_style = variable_get('open_readspeaker_buttonstyle', '');
  $custom_param = variable_get('open_readspeaker_customparam', '');
  if (empty($accountid)) {
    $output .= '<p>' . t('In order to ReadSpeaker work you whould need an <strong>account id</strong>.');
    $output .= t('Please go to @link and fill the account id.', array(
      '@link' => l(t('manage open ReadSpeaker'), 'admin/config/services/open-readspeaker'),
    ));
    $output .= '</p>';
    return $output;
  }
  if (empty($open_readspeaker_i18n)) {
    $output .= '<p>' . t('None language was selected please go to @link and select a language.', array(
      '@link' => l(t('manage open ReadSpeaker'), 'admin/config/services/open-readspeaker'),
    )) . '</p>';
    return $output;
  }
  if ($is_https) {
    $http = 'https';
  }
  drupal_add_js($http . "://f1.eu.readspeaker.com/script/{$accountid}/ReadSpeaker.js?pids=embhl{$custom_param}", 'external');
  drupal_add_css(drupal_get_path('module', 'open_readspeaker') . '/open_readspeaker.css', array(
    'group' => CSS_THEME,
    'media' => 'screen',
  ));
  $request_path = url(current_path(), array(
    'absolute' => TRUE,
  ));
  $output .= '<!-- RS_MODULE_CODE -->';
  $output .= '<div id="readspeaker_button1" class="rs_skip rsbtn rs_preserve"' . $custom_style . '>';
  $output .= '<a class="rsbtn_play" title="' . t('Listen to this page using ReadSpeaker') . '" accesskey="L" href="' . $http . '://app.eu.readspeaker.com/cgi-bin/rsent?customerid=' . $accountid . '&amp;lang=' . $open_readspeaker_i18n . '&amp;readid=' . variable_get('open_readspeaker_reading_area', 'rs_read_this') . '&amp;url=' . urlencode($request_path) . '">';
  $output .= '<span class="rsbtn_left rsimg rspart"><span class="rsbtn_text"><span>' . t('Listen') . '</span></span></span>';
  $output .= '<span class="rsbtn_right rsimg rsplay rspart"></span>';
  $output .= '</a></div>';
  $output .= '<div id="rs_read_this">';
  $output .= '</div>';
  return $output;
}