View source
<?php
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;
}
}
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;
}
function barcode_perm() {
return array(
'administer barcodes',
);
}
function barcode_theme() {
return array(
'barcode' => array(
'arguments' => array(
'element' => NULL,
),
),
'barcode_image' => array(
'arguments' => array(
'barcode_value' => NULL,
'encoding' => NULL,
),
),
);
}
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;
}
function theme_barcode($element) {
return theme('textfield', $element);
}
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',
));
}
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;
}
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);
}
function barcode_element_validate($element, &$form_state) {
$value = trim(check_plain($element['#value']));
form_set_value($element, $value, $form_state);
$length = strlen($value);
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;
}
if ($element['#encoding'] == 'ISBN' and substr($value, 0, 3) != '978') {
form_error($element, t('ISBN barcode number must start with 978.'));
}
return $element;
}