View source
<?php
define('ISSN_8_DIGIT', 8);
define('ISBN_10_DIGIT', 10);
define('ISBN_13_DIGIT', 13);
define('ISBN_NO_VALIDATION', 0);
define('ISBN_SUBMIT_VALIDATION', 1);
define('ISBN_JS_VALIDATION', 2);
function isbn_menu() {
$items['isbn/validate'] = array(
'title' => 'ISBN Validate',
'page callback' => 'isbn_validate_ajax',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function isbn_validate_ajax($isbn_format, $isbn_number) {
drupal_json(isbn_validate_number($isbn_number, $isbn_format));
}
function isbn_validate_number($number, $format) {
module_load_include('inc', 'isbn');
$number = isbn_clean($number);
$validate_function = "isbn_validate_{$format}";
$return = $validate_function($number);
if ($return['valid'] == false && !isset($return['message'])) {
$return['message'] = 'confirmFailure';
$length = strlen($number);
if ($length < $format) {
$return['message'] = 'notEnoughDigits';
}
elseif ($length > $format) {
$return['message'] = 'tooManyDigits';
}
}
return $return;
}
function isbn_validate_10($number) {
return array(
'valid' => ISN_checksum_OK($number, ISBN_10_DIGIT),
);
}
function isbn_validate_8($number) {
return array(
'valid' => ISN_checksum_OK($number, ISSN_8_DIGIT),
);
}
function isbn_validate_13($string) {
$return['valid'] = _isbn_validate_13($string);
$prefix = substr($string, 0, 3);
if ($prefix !== "978" && $prefix !== "979") {
$return['valid'] = false;
$return['message'] = 'missingPrefix13';
}
return $return;
}
function _isbn_validate_13($string) {
settype($string, 'string');
if (strlen($string) != 13) {
return false;
}
$stack = 0;
$even = false;
for ($i = 12; $i >= 0; $i--) {
if ($even === true) {
$stack += $string[$i] * 3;
$even = false;
}
else {
$stack += $string[$i];
$even = true;
}
}
if ($stack % 10 == 0) {
return true;
}
return false;
}
function isbn_theme() {
return array(
'isbn_field' => array(
'arguments' => array(
'element' => NULL,
),
),
'isbn_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
),
'isbn_formatter_raw' => array(
'arguments' => array(
'element' => NULL,
),
),
'isbn_formatter_clean' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function isbn_field_info() {
return array(
'isbn' => array(
'label' => t('ISBN Number'),
'description' => t('Stores an ISBN number in the database.'),
),
);
}
function isbn_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
$form['isbn_format'] = array(
'#type' => 'radios',
'#title' => t('ISBN Number Format'),
'#default_value' => is_numeric($field['isbn_format']) ? $field['isbn_format'] : ISSN_8_DIGIT,
'#options' => array(
ISSN_8_DIGIT => t('ISSN 8 Digit'),
ISBN_10_DIGIT => t('ISBN 10 Digit'),
ISBN_13_DIGIT => t('ISBN 13 Digit'),
),
);
$form['isbn_validation'] = array(
'#type' => 'radios',
'#title' => t('ISBN Validation'),
'#default_value' => is_numeric($field['isbn_validation']) ? $field['isbn_validation'] : ISBN_NO_VALIDATION,
'#options' => array(
ISBN_NO_VALIDATION => t('No validation'),
ISBN_SUBMIT_VALIDATION => t('Validation on Submit (confirms check digit)'),
ISBN_JS_VALIDATION => t('Validation with Javascript (Confirms check digit. Also validates on Submit)'),
),
);
return $form;
case 'save':
return array(
'isbn_format',
'isbn_validation',
);
case 'database columns':
$columns['value'] = array(
'type' => 'varchar',
'length' => 20,
'not null' => FALSE,
'sortable' => TRUE,
'views' => TRUE,
);
return $columns;
case 'views data':
}
}
function isbn_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
if (is_array($items)) {
foreach ($items as $delta => $item) {
$error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
if (is_array($item) && isset($item['_error_element'])) {
unset($item['_error_element']);
}
if (!empty($item['value'])) {
if ($field['isbn_validation'] != ISBN_NO_VALIDATION) {
$valid = isbn_validate_number($item['value'], $field['isbn_format']);
if (!isset($valid) || $valid['valid'] == false) {
switch ($valid['message']) {
case 'confirmSuccess':
form_set_error($error_element, t('Valid ISBN/ISSN number.'));
break;
case 'confirmFailure':
form_set_error($error_element, t('Not a valid ISBN/ISSN Number.'));
break;
case 'notEnoughDigits':
form_set_error($error_element, t('Not Enough Digits! '));
break;
case 'tooManyDigits':
form_set_error($error_element, t('Too Many Digits! '));
break;
case 'requiredDigits':
form_set_error($error_element, t(' digits are required.'));
break;
case 'missingPrefix13':
form_set_error($error_element, t('13 Digit ISBNs need to begin with 978 or 979.'));
break;
default:
form_set_error($error_element, t('ISBN/ISSN Validation Failed'));
}
}
}
}
}
}
return $items;
case 'sanitize':
foreach ($items as $delta => $item) {
$isbn_formatted = isbn_clean($item['value'], $field['isbn_format']);
$items[$delta]['clean'] = $isbn_formatted;
}
}
}
function isbn_content_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
function isbn_field_formatter_info() {
return array(
'default' => array(
'label' => t('ISBN: As Entered'),
'field types' => array(
'isbn',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'raw' => array(
'label' => t('ISBN: As Entered'),
'field types' => array(
'isbn',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'clean' => array(
'label' => t('ISBN: Clean (no dashes)'),
'field types' => array(
'isbn',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
function theme_isbn_formatter_default($element) {
return $element['#item']['value'];
}
function theme_isbn_formatter_raw($element) {
return $element['#item']['value'];
}
function theme_isbn_formatter_clean($element) {
$string = isbn_clean($element['#item']['value']);
return $string;
}
function isbn_clean($string) {
return str_replace(array(
'x',
'X',
' ',
'-',
'.',
), '', $string);
}
function isbn_widget_info() {
return array(
'isbn_field' => array(
'label' => t('ISBN field'),
'field types' => array(
'isbn',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
function isbn_elements() {
return array(
'isbn_field' => array(
'#input' => TRUE,
'#columns' => array(
'value',
),
'#delta' => 0,
'#process' => array(
'isbn_field_process',
),
'#autocomplete_path' => FALSE,
),
);
}
function isbn_widget_settings($op, $widget) {
switch ($op) {
case 'form':
return $form;
case 'save':
return array();
}
}
function isbn_widget(&$form, &$form_state, $field, $items, $delta = 0) {
switch (intval($field['isbn_format'])) {
case ISSN_8_DIGIT:
$field_description = t('8 Digit ISSN Numbers only');
break;
case ISBN_10_DIGIT:
$field_description = t('10 Digit ISBN Numbers only');
break;
case ISBN_13_DIGIT:
$field_description = t('13 Digit ISBN Numbers only');
break;
}
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
'#isbn_description' => $field_description,
);
return $element;
}
function theme_isbn_field($element) {
return $element['#children'];
}
function isbn_field_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
$field_key = $element['#columns'][0];
if ($field['isbn_validation'] == ISBN_JS_VALIDATION) {
$js_validate_class = 'isbn-validate';
isbn_dynamic_validation();
}
$element[$field_key] = array(
'#type' => 'textfield',
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
'#weight' => 0,
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
'#field_name' => $element['#field_name'],
'#type_name' => $element['#type_name'],
'#delta' => $element['#delta'],
'#columns' => $element['#columns'],
'#attributes' => array(
'class' => $js_validate_class,
'isbn-format' => $field['isbn_format'],
),
);
$element['_error_element'] = array(
'#type' => 'value',
'#value' => implode('][', array_merge($element['#parents'], array(
$field_key,
))),
);
return $element;
}
function isbn_dynamic_validation() {
static $complete = FALSE;
global $user;
if (!$complete) {
drupal_add_js(drupal_get_path('module', 'isbn') . '/isbn_attach.js', 'module');
drupal_add_js(array(
'isbn' => array(
'validateURL' => url('isbn/validate'),
'confirmSuccess' => t('Valid ISBN/ISSN number.'),
'confirmFailure' => t('Not a valid ISBN/ISSN Number.'),
'notEnoughDigits' => t('Not Enough Digits! '),
'tooManyDigits' => t('Too Many Digits! '),
'requiredDigits' => t(' digits are required.'),
'missingPrefix13' => t('13 Digit ISBNs need to begin with 978 or 979.'),
),
), 'setting');
$complete = TRUE;
}
}
function isbn_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens = array();
$tokens['isbn']['raw'] = t('Raw, unfiltered ISBN');
$tokens['isbn']['formatted'] = t('Formatted and filtered ISBN');
return $tokens;
}
}
function isbn_token_values($type, $object = NULL) {
if ($type == 'field') {
$item = $object[0];
$tokens['raw'] = $item['isbn'];
$tokens['formatted'] = isset($item['view']) ? $item['view'] : '';
return $tokens;
}
}
function isbn_content_generate($node, $field) {
$random_isbn = '';
for ($i = 1; $i <= $field['isbn_format']; $i++) {
$random_isbn .= mt_rand(0, 9);
}
return array(
'value' => $random_isbn,
);
}