You are here

public function AdvancedQueue::createUniqueItem in Advanced Queue 7

Allows the creation of uniquely-keyed items within a single queue.

Parameters

array $data: The data for the queue item.

mixed $key: If present, the unique key for the queue item.

bool $overwrite: Whether the new item should replace the old item in the queue. Defaults to TRUE. Used for queue-but-don't-replace-existing logic.

1 call to AdvancedQueue::createUniqueItem()
AdvancedQueue::createItem in ./advancedqueue.queue.inc
Add a queue item and store it directly to the queue.

File

./advancedqueue.queue.inc, line 37

Class

AdvancedQueue
Extended queue.

Code

public function createUniqueItem($data, $key = NULL) {

  // Make sure that queue items cannot break the title column.
  $schema = drupal_get_schema('advancedqueue');
  $title_max = $schema['fields']['title']['length'];
  $title_raw = is_array($data) && isset($data['title']) ? $data['title'] : t('Unnamed item');
  $fields = array(
    'name' => $this->name,
    'uid' => is_array($data) && isset($data['uid']) ? $data['uid'] : $GLOBALS['user']->uid,
    'title' => truncate_utf8($title_raw, $title_max, FALSE, TRUE),
    'data' => serialize($data),
    // We cannot rely on REQUEST_TIME because many items might be created
    // by a single request which takes longer than 1 second. However we allow
    // the creator to post-date their queue items as with the UID and title
    // properties.
    'created' => is_array($data) && !empty($data['created']) ? $data['created'] : time(),
    'status' => ADVANCEDQUEUE_STATUS_QUEUED,
  );
  if ($key) {

    // Merge onto the existing item. This updates *all* properties, meaning
    // that if "created" is set into the future and an item is updated too
    // frequently, it might never get to run. Be careful!
    $fields['item_key'] = $key;
    $query = db_merge('advancedqueue')
      ->key(array(
      'item_key' => $key,
    ))
      ->fields($fields);
  }
  else {

    // No key means just insert a new item!
    $query = db_insert('advancedqueue')
      ->fields($fields);
  }
  return (bool) $query
    ->execute();
}