You are here

page_url_qr_code_block.module in Page URL QR Code Block 7

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

Contains module logic for page_url_qr_code_block.

File

page_url_qr_code_block.module
View source
<?php

/**
 * @file
 * Contains module logic for page_url_qr_code_block.
 */

/**
 * Implements hook_help().
 */
function page_url_qr_code_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#page_url_qr_code_block':
      $output = '<p>' . t('A tiny module that provides a Block with a QR Code for that Page automatically. You can configure through block configure page.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_block_info().
 */
function page_url_qr_code_block_block_info() {
  $blocks = array();
  $blocks['page_url_qr_code_block'] = array(
    'info' => t('Page URL QR Code'),
  );
  return $blocks;
}
function page_url_qr_code_block_block_configure($delta = '') {

  // This example comes from node.module.
  $form = array();
  if ($delta == 'page_url_qr_code_block') {
    $form['page_url_qr_code_caption'] = array(
      '#type' => 'textfield',
      '#title' => t('Caption'),
      '#description' => t('Will display under the QR Code'),
      '#default_value' => variable_get('page_url_qr_code_caption', 'This page URL'),
    );
    $form['page_url_qr_code_alt'] = array(
      '#type' => 'textfield',
      '#title' => t('Alt Text'),
      '#default_value' => variable_get('page_url_qr_code_alt', 'QR code for this page URL'),
    );
    $form['page_url_qr_code_width_height'] = array(
      '#type' => 'textfield',
      '#title' => t('QR Code Width & Height'),
      '#description' => t('Width & Height will be same. i.e. 150'),
      '#default_value' => variable_get('page_url_qr_code_width_height', 150),
    );
    $form['page_url_qr_code_api'] = array(
      '#type' => 'radios',
      '#title' => t('Select API'),
      '#description' => t('Google is recommended except where Google is restricted'),
      '#options' => array(
        'google' => 'Google API (recommended)',
        'liantu' => 'Liantu API',
      ),
      '#default_value' => variable_get('page_url_qr_code_api', 'google'),
    );
  }
  return $form;
}
function page_url_qr_code_block_block_save($delta = '', $edit = array()) {

  // This example comes from node.module.
  if ($delta == 'page_url_qr_code_block') {
    variable_set('page_url_qr_code_caption', $edit['page_url_qr_code_caption']);
    variable_set('page_url_qr_code_alt', $edit['page_url_qr_code_alt']);
    variable_set('page_url_qr_code_width_height', $edit['page_url_qr_code_width_height']);
    variable_set('page_url_qr_code_api', $edit['page_url_qr_code_api']);
  }
}

/**
 * Implements hook_block_view().
 */
function page_url_qr_code_block_block_view($delta = '') {
  $block = array();
  $path = drupal_get_path('module', 'page_url_qr_code_block');
  switch ($delta) {
    case 'page_url_qr_code_block':
      $block['subject'] = '';
      $block['content'] = array(
        '#markup' => theme('page_url_qr_code_block', array(
          'barcode' => _page_url_qr_code_block_content(),
          'caption' => t(variable_get('page_url_qr_code_caption', 'This page URL')),
        )),
        '#attached' => array(
          'css' => array(
            $path . '/page_url_qr_code_block.css',
          ),
        ),
      );
      break;
  }
  return $block;
}

/**
 * Content Generator.
 * @global type $base_root
 * @return type
 */
function _page_url_qr_code_block_content() {
  global $base_root;
  $request = request_uri();
  $alias = drupal_get_path_alias($request);
  $path = $request;
  if ($alias != $request) {
    $path = $alias;
  }
  $path = $base_root . $path;
  $url = urlencode($path);
  $width = variable_get('page_url_qr_code_width_height', 150);
  $height = variable_get('page_url_qr_code_width_height', 150);
  $alt = t(variable_get('page_url_qr_code_alt', 'QR code for this page URL'));
  $api = variable_get('page_url_qr_code_api', 'google');
  if ('liantu' == $api) {
    $barcode = "<img src='http://qr.liantu.com/api.php?bg=ffffff&amp;w={$width}&amp;text={$url}' alt='{$alt}' width='{$width}' height='{$height}' />";
  }
  else {
    $barcode = "<img src='http://chart.apis.google.com/chart?chs={$width}x{$width}&amp;cht=qr&amp;chl={$url}' alt='{$alt}' width='{$width}' height='{$height}' />";
  }
  return $barcode;
}

/**
 * Implements hook_theme().
 */
function page_url_qr_code_block_theme() {
  return array(
    'page_url_qr_code_block' => array(
      'variables' => array(
        'barcode' => array(),
        'caption' => array(),
      ),
      'template' => 'page_url_qr_code_block',
    ),
  );
}