You are here

function biblio_advanced_import_create_citekey in Biblio Advanced Import 7

Same name and namespace in other branches
  1. 6 biblio_advanced_import.module \biblio_advanced_import_create_citekey()

Helper function to create a configurable biblio node citekey.

Parameters

$node: a biblio node

See also

biblio_advanced_import_settings_form()

biblio_advanced_import_settings_form_submit()

1 call to biblio_advanced_import_create_citekey()
biblio_advanced_import_node_presave in ./biblio_advanced_import.module
Implements hook_node_presave().

File

./biblio_advanced_import.module, line 301
Biblio add-on.

Code

function biblio_advanced_import_create_citekey($node) {
  $citekey = '';
  switch (variable_get('biblio_advanced_import_citekey_creation_strategy', 'biblio')) {
    case 'fields':
      $citekey_parts = array();
      $prefix = variable_get('biblio_citekey_prefix', '');
      if (!empty($prefix)) {
        $citekey_parts[] = $prefix;
      }
      foreach (variable_get('biblio_advanced_import_citekey_creation', array(
        'title' => 'title',
        'biblio_year' => 'biblio_year',
      )) as $field) {
        if (!empty($field) && !empty($node->{$field})) {
          $citekey_parts[] = $node->{$field};
        }
      }
      $citekey = implode('|', $citekey_parts);

      // biblio stores citekey as varchar(255), we need to make sure it fits
      // or a PDO Exception is thrown
      $citekey = mb_substr($citekey, 0, 255);

      // strip trailing pipe symbol, if any
      $citekey = preg_replace('@\\|+$@', '', $citekey);
      $citekey = trim($citekey);
      break;
  }
  if ($citekey) {
    if (db_query("SELECT 1 FROM {biblio} WHERE biblio_citekey = :biblio_citekey", array(
      ':biblio_citekey' => $citekey,
    ))
      ->fetchField()) {
      switch (variable_get('biblio_advanced_import_duplicate_citekey_strategy', 'skip')) {
        case 'skip':
          $citekey = '';
          break;
        case 'append counter':
          $counter = variable_get('biblio_advanced_import_citekey_creation_counter', 0) + 1;
          variable_set('biblio_advanced_import_citekey_creation_counter', $counter);

          // biblio stores citekey as varchar(255), so we have to ensure that the counter is saved
          $citekey = mb_substr($citekey, 0, 254 - strlen($counter)) . '|' . $counter;
      }
    }
  }
  return $citekey;
}