You are here

google_qr_code.module in Google QR Code Generator 7

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

Provides block using Google Charts to render QR code.

File

google_qr_code.module
View source
<?php

/**
 * @file
 * Provides block using Google Charts to render QR code.
 */

/**
 * Google_qr_code Implements hook_block_info().
 */
function google_qr_code_block_info() {
  $blocks['qr_code'] = array(
    'info' => t('Google QR Code Generator'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

/**
 * Google_qr_code Implements hook_block_view().
 *
 * Choose block to render (1 choice currently).
 */
function google_qr_code_block_view($delta = '') {
  switch ($delta) {
    case 'qr_code':

      // Variables to make available to JS!
      $js_to_pass = array(
        'googleQRcode' => array(
          'height' => variable_get('google_qr_code_height', '250'),
          'width' => variable_get('google_qr_code_width', '250'),
          'whenShow' => variable_get('google_qr_code_when_show', ''),
          'url' => url(current_path(), array(
            'absolute' => TRUE,
          )),
        ),
      );
      drupal_add_js($js_to_pass, 'setting');
      $block['subject'] = t('Google QR Code Generator');
      $block['content'] = google_qr_code_contents($delta);
      break;
  }
  return $block;
}

/**
 * A custom module-defined block content function.
 */
function google_qr_code_contents($which_block) {
  switch ($which_block) {
    case 'qr_code':
      $render_type = variable_get('google_qr_code_when_show', "on_pageload");
      if ($render_type == 'on_click') {
        $output = l(t('Click To Render QR Code'), '', array(
          'attributes' => array(
            'class' => 'inner',
          ),
        ));
      }
      else {
        $google_qr_current_url = url(current_path(), array(
          'absolute' => TRUE,
        ));
        $google_qr_image_url = "https://chart.googleapis.com/chart?chs=" . variable_get('google_qr_code_width', '250') . "x" . variable_get('google_qr_code_height', '250') . "&cht=qr&chl=" . $google_qr_current_url . '&chld=H|0';
        $google_qr_alt = t('QR Code for @url', array(
          '@url' => $google_qr_current_url,
        ));
        $output = theme('image', array(
          'path' => $google_qr_image_url,
          'alt' => $google_qr_alt,
          'attributes' => array(
            'class' => 'googleQRcode',
          ),
        ));
      }
      return array(
        '#markup' => '<div id="google-qr-code">' . $output . '</div>',
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', 'google_qr_code') . '/google.qrcode-execute.js',
          ),
        ),
      );
  }
}

/**
 * Creation of admin settings form for callback.
 */
function google_qr_code_admin_settings_form($form, &$form_state) {
  $form['google_qr_code_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Google QR Code Configuration'),
    '#description' => t('Configure the Google QR Code Generator Here:'),
  );
  $form['google_qr_code_settings']['google_qr_code_when_show'] = array(
    '#type' => 'select',
    '#title' => t('When to render QR COde'),
    '#options' => array(
      "on_pageload" => t('On Page Load'),
      "on_click" => t('On Click'),
    ),
    '#default_value' => variable_get('google_qr_code_when_show', "on_pageload"),
    '#required' => FALSE,
    '#description' => t('Choose whether you want the QR code to load
  everytime the page loads (jQuery code) or only get the QR code when
  generate text in block is clicked.'),
  );
  $form['google_qr_code_image_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Google QR Code Image Settings'),
    '#description' => t('Configure QR code width and height here. There is a
  maximum size of 1000 pixels for any single dimension, and a total size
  of 300,000 pixels'),
  );
  $form['google_qr_code_image_settings']['google_qr_code_height'] = array(
    '#type' => 'textfield',
    '#title' => t('QR Code Height'),
    '#default_value' => variable_get('google_qr_code_height', '250'),
    '#size' => 40,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => t('Enter the QR Code Height'),
    '#element_validate' => array(
      '_google_qr_code_max_single_dimension',
    ),
  );
  $form['google_qr_code_image_settings']['google_qr_code_width'] = array(
    '#type' => 'textfield',
    '#title' => t('QR Code Width'),
    '#default_value' => variable_get('google_qr_code_width', '250'),
    '#size' => 40,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => t('Enter the QR Code Width'),
    '#element_validate' => array(
      '_google_qr_code_max_single_dimension',
    ),
  );
  $form['#validate'][] = 'google_qr_code_max_total_px';
  return system_settings_form($form);
}

/**
 * Custom function. Field Validation due to max pixel size of 100 pixels.
 */
function _google_qr_code_max_single_dimension($element, &$form_state) {
  if (!empty($element['#value']) && !is_numeric($element['#value'])) {
    form_error($element, t('Has to be a number. Do not include "px"'));
  }
  else {
    if (!empty($element['#value']) && $element['#value'] > 1000) {
      form_error($element, t('Google does not allow single dimensions over
    1000px'));
    }
  }
}

/**
 * Custom Form validation for a maximum dimension size of 300,000 pixels.
 */
function google_qr_code_max_total_px($form, &$form_state) {
  $qr_width = $form_state['values']['google_qr_code_width'];
  $qr_height = $form_state['values']['google_qr_code_height'];
  $total_pixels = $qr_width * $qr_height;
  if (is_numeric($total_pixels) && $total_pixels > 300000) {
    $error_text = t('Total dimensions cannot exceed 300,000px. Currently at @total', array(
      '@total' => $total_pixels,
    ));
    form_set_error('google_qr_code_image_settings', t($error_text));
  }
}

/**
 * Google_qr_code Implements hook_menu().
 */
function google_qr_code_menu() {
  $items = array();
  $items['admin/config/system/googleqrcode'] = array(
    'title' => 'Google QR Code Generator',
    'description' => 'Configuration for the Google QR Code Generator.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'google_qr_code_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

Functions

Namesort descending Description
google_qr_code_admin_settings_form Creation of admin settings form for callback.
google_qr_code_block_info Google_qr_code Implements hook_block_info().
google_qr_code_block_view Google_qr_code Implements hook_block_view().
google_qr_code_contents A custom module-defined block content function.
google_qr_code_max_total_px Custom Form validation for a maximum dimension size of 300,000 pixels.
google_qr_code_menu Google_qr_code Implements hook_menu().
_google_qr_code_max_single_dimension Custom function. Field Validation due to max pixel size of 100 pixels.