You are here

function asset_save in Asset 5.2

Same name and namespace in other branches
  1. 5 asset.module \asset_save()
  2. 6 inc/asset.node.inc \asset_save()

Save an asset object into the database.

Related topics

3 calls to asset_save()
asset_form_submit in ./asset.module
Form submit for asset_form
asset_import_form_submit in contrib/asset_import/asset_import.module
asset_search_wizard_preview_form_submit in contrib/asset_search/asset_search.module
Submit handler for asset_search_wizard_preview_form

File

./asset.module, line 792
Main module.

Code

function asset_save(&$asset) {
  global $user;
  $asset->is_new = FALSE;

  // Apply filters to some default asset fields:
  if (empty($asset->aid)) {

    // Insert a new asset.
    $asset->is_new = TRUE;
    $asset->aid = db_next_id('{asset}_aid');
  }
  else {

    // We need to ensure that all asset fields are filled.
    $asset_current = asset_load($asset->aid);
    foreach ($asset as $field => $data) {
      $asset_current->{$field} = $data;
    }
    $asset = $asset_current;
  }

  // Set some required fields:
  if (empty($asset->created)) {
    $asset->created = time();
  }

  // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
  $asset->changed = time();
  $asset_table_values = array(
    'aid' => $asset->aid,
    'title' => $asset->title,
    'type' => $asset->type,
    'uid' => $asset->uid,
    'status' => $asset->status,
    'created' => $asset->created,
    'changed' => $asset->changed,
    'pid' => $asset->pid,
    'credit' => $asset->credit,
    'description' => $asset->description,
  );
  $asset_table_types = array(
    'aid' => '%d',
    'title' => "'%s'",
    'type' => "'%s'",
    'uid' => '%d',
    'status' => '%d',
    'created' => '%d',
    'changed' => '%d',
    'pid' => '%d',
    'credit' => "'%s'",
    'description' => "'%s'",
  );

  //Generate the asset table query
  if ($asset->is_new) {
    $query = 'INSERT INTO {asset} (' . implode(', ', array_keys($asset_table_types)) . ') VALUES (' . implode(', ', $asset_table_types) . ')';
  }
  else {
    $arr = array();
    foreach ($asset_table_types as $key => $value) {
      $arr[] = $key . ' = ' . $value;
    }
    $asset_table_values[] = $asset->aid;
    $query = 'UPDATE {asset} SET ' . implode(', ', $arr) . ' WHERE aid = %d';
  }

  // save the asset into the database:
  db_query($query, $asset_table_values);

  // save the asset permissions
  db_query('DELETE FROM {asset_role} WHERE aid=%d', $asset->aid);
  if ($asset->status == ASSET_PRIVATE && is_array($asset->roles)) {
    foreach ($asset->roles as $rid => $status) {
      db_query('INSERT INTO {asset_role} (aid, rid, status) VALUES (%d, %d, %d)', array(
        $asset->aid,
        $rid,
        $status ? ASSET_PUBLIC : ASSET_PRIVATE,
      ));
    }
  }

  // Call the asset specific callback (if any):
  if ($asset->is_new) {
    asset_type_invoke($asset, 'insert');
    module_invoke_all('assetapi', 'insert', $asset);
  }
  else {
    asset_type_invoke($asset, 'update');
    module_invoke_all('assetapi', 'update', $asset);
  }

  // Clear the cache so an anonymous poster can see the asset being added or updated.
  cache_clear_all();
  return asset_load($asset->aid);
}