You are here

function library_node_validate in Library 7

Implements hook_node_validate().

Check that barcodes are unique if the node is in the library and barcodes are enabled.

File

./library.module, line 602

Code

function library_node_validate($node, $form) {
  $library_type = variable_get('library_' . $node->type, LIBRARY_ITEM_NOT_IN_LIBRARY);
  $barcodes_used = variable_get('library_item_barcodes', LIBRARY_NO_BARCODES);
  if ($library_type == LIBRARY_ITEM_IN_LIBRARY && $barcodes_used == LIBRARY_BARCODES) {
    if (isset($node->library_items) && !$node->library_items) {

      // @ignore rule:sniffer_files_linelength_toolong
      form_set_error('library_items', t('Since this content is in the library and barcodes are enabled, you must enter a unique barcode for at least one item.'));
    }
    $items = $node->library_items;
    if ($items) {

      // Check that each barcode is unique.
      foreach ($items as $key => $item) {
        $result = db_select('library', 'l')
          ->fields('l', array(
          'id',
        ))
          ->condition('barcode', '', '<>')
          ->condition('barcode', check_plain($item['barcode'] . ''), '=')
          ->condition('id', $item['id'], '<>')
          ->execute()
          ->rowCount();
        if (empty($item['id']) && $result > 0 || !empty($item['id']) && $result > 1) {

          // @ignore rule:sniffer_files_linelength_toolong
          form_set_error('library_items][' . $key . '][barcode', t('The barcode %barcode already exists. Please enter a different barcode.', array(
            '%barcode' => $item['barcode'],
          )));
        }
      }
    }
    else {

      // @ignore rule:sniffer_files_linelength_toolong
      form_set_error('library_items', t('Since this content is in the library and barcodes are enabled, you must enter a unique barcode for at least one item.'));
    }
  }
}