You are here

function log_name_generate in Log entity 7

Generate a log name.

Parameters

Log $log: The log entity.

Return value

string The log name.

2 calls to log_name_generate()
log_log_insert in ./log.module
Implements hook_log_insert().
log_log_update in ./log.module
Implements hook_log_update().

File

./log.module, line 1042
Log - A general purpose record keeping system.

Code

function log_name_generate(Log $log) {

  // If the log doesn't have an id, bail!
  if (empty($log->id) || !is_numeric($log->id)) {
    return $log->name;
  }

  // Load the log type.
  $log_type = log_type_load($log->type);

  // If the log type's name is editable, only generate a name if
  // the current name is empty.
  if (!empty($log_type->name_edit) && !empty($log->name)) {
    return $log->name;
  }

  // Get the name pattern from the log type.
  $pattern = $log_type->name_pattern;

  // If Token is installed, run replacements.
  if (module_exists('token')) {
    $name = token_replace($pattern, array(
      'log' => $log,
    ), array(
      'clear' => TRUE,
      'sanitize' => FALSE,
    ));
  }
  else {
    $name = str_replace('[log:id]', $log->id, $pattern);
  }

  // Strip extraneous spaces (explode on spaces, remove empty items, implode
  // with spaces).
  $name = implode(' ', array_filter(explode(' ', $name)));

  // If the name is empty, set it to a sensible default.
  if (empty($name)) {
    $name = 'Log ' . $log->id;
  }

  // If the name length is greater than 255 (the size of the database column),
  // truncate it with word-safe boundaries and an ellipsis.
  if (strlen($name) > 255) {
    $name = truncate_utf8($name, 255, TRUE, TRUE);
  }

  // Update the log's name in the database.
  db_update('log')
    ->fields(array(
    'name' => $name,
  ))
    ->condition('id', $log->id)
    ->execute();

  // Add it to the log entity (for downstream code).
  $log->name = $name;

  // Return it.
  return $name;
}