You are here

barcode.module in Barcode 6.2

Same filename and directory in other branches
  1. 8 barcode.module
  2. 6 barcode.module
  3. 7.2 barcode.module

File

barcode.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function barcode_help($path, $arg) {
  switch ($path) {
    case 'admin/help#upload_element':
      $output = '<p>' . t('A module that provides a new FAPI element to handle barcodes.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_menu().
 */
function barcode_menu() {
  $items['admin/settings/barcode'] = array(
    'title' => t('Barcode'),
    'description' => t('Configure barcode settings.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'barcode_settings',
    ),
    'access arguments' => array(
      'administer barcodes',
    ),
    'file' => 'includes/barcode.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function barcode_perm() {
  return array(
    'administer barcodes',
  );
}

/**
 * Implementation of hook_theme().
 */
function barcode_theme() {
  return array(
    'barcode' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'barcode_image' => array(
      'arguments' => array(
        'barcode_value' => NULL,
        'encoding' => NULL,
      ),
    ),
  );
}

/**
 * The barcode settings.
 */
function barcode_get_settings() {
  $barcode['default_path'] = variable_get('barcode_default_path', 'barcodes');
  $barcode['encoding'] = variable_get('barcode_encoding', 'EAN-13');
  $barcode['height'] = variable_get('barcode_height', 30);
  $barcode['scale'] = variable_get('barcode_scale', 2.0);
  $barcode['bgcolor'] = variable_get('barcode_bgcolor', '#FFFFFF');
  $barcode['barcolor'] = variable_get('barcode_barcolor', '#000000');
  $barcode['font'] = variable_get('barcode_font', drupal_get_path('module', 'barcode') . "/fonts/liberation/LiberationSans-Bold.ttf");
  $barcode['image_format'] = variable_get('barcode_image_format', 'png');
  return $barcode;
}

/**
 * Theme function for the barcode element.
 *
 * @ingroup themeable
 */
function theme_barcode($element) {
  return theme('textfield', $element);
}

/**
 * Theme function for the barcode image.
 *
 * @ingroup themeable
 */
function theme_barcode_image($barcode_value, $encoding = NULL) {
  if (empty($barcode_value)) {
    return '';
  }
  $barcode = barcode_get_settings();
  $barcode['value'] = $barcode_value;
  if (isset($encoding)) {
    $barcode['encoding'] = $encoding;
  }
  module_load_include('inc', 'barcode', 'includes/barcode.plugins');
  $filename = barcode_generate_image($barcode);
  if (!$filename) {
    drupal_set_message(t('An error occured while generating the barcode.'), 'error');
    return '';
  }
  return theme('image', $filename, $barcode_value, $barcode_value, array(
    'class' => 'barcode',
  ));
}

/**
 * Implementation of FAPI hook_elements().
 */
function barcode_elements() {
  $type['barcode'] = array(
    '#input' => TRUE,
    '#default_value' => '',
    '#size' => 30,
    '#encoding' => variable_get('barcode_encoding', 'EAN-13'),
    '#element_validate' => array(
      'barcode_element_validate',
    ),
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'barcode_process',
    ),
  );
  return $type;
}

/**
 * Process an individual barcode element.
 */
function barcode_process($element, $edit, &$form_state, $complete_form) {
  module_load_include('inc', 'barcode', 'includes/barcode.plugins');
  $element['#maxlength'] = barcode_plugin_max_length($element['#encoding']);
  return form_expand_ahah($element);
}

/**
 * Validate an individual barcode element.
 */
function barcode_element_validate($element, &$form_state) {
  $value = trim(check_plain($element['#value']));
  form_set_value($element, $value, $form_state);
  $length = strlen($value);

  // Check length.
  switch ($element['#encoding']) {
    case 'ISBN':
      if ($length != $element['#maxlength']) {
        form_error($element, t('The barcode must have %count digits.', array(
          '%count' => $element['#maxlength'],
        )));
      }
      break;
    case 'POSTNET':
      if ($length != 5 and $length != 9 and $length != 11) {
        form_error($element, t('Postnet number must have 5, 9 or 11 digits.'));
      }
      break;
  }

  // Check ISBN start sequence.
  if ($element['#encoding'] == 'ISBN' and substr($value, 0, 3) != '978') {
    form_error($element, t('ISBN barcode number must start with 978.'));
  }
  return $element;
}

Functions

Namesort descending Description
barcode_elements Implementation of FAPI hook_elements().
barcode_element_validate Validate an individual barcode element.
barcode_get_settings The barcode settings.
barcode_help Implementation of hook_help().
barcode_menu Implementation of hook_menu().
barcode_perm Implementation of hook_perm().
barcode_process Process an individual barcode element.
barcode_theme Implementation of hook_theme().
theme_barcode Theme function for the barcode element.
theme_barcode_image Theme function for the barcode image.