You are here

function asset_save in Asset 5

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

Persist an asset object to the db

Parameters

$asset: asset object with filename, filepath, filemime, and filesize properties

$form_values: form_values array from asset creation form. this allows asset_save to set the defaults for fields like title, author and permissions.

6 calls to asset_save()
asset_asset_type in ./asset.module
Implementation of hook_asset_type().
asset_check_directory in ./asset.module
Wrapper for file_check_directory that also checks/adds a matching asset
asset_embed_asset_type in asset_embed/asset_embed.module
asset_import_form_submit in asset_import/asset_import.module
asset_youtube_asset_type in asset_youtube/asset_youtube.module

... See full list

File

./asset.module, line 123

Code

function asset_save($asset, $form_values = array()) {
  $path = file_directory_path();
  if (substr($asset->filepath, 0, strlen($path)) == $path) {
    $asset->filepath = trim(substr($asset->filepath, strlen($path)), '/');
  }
  $info = pathinfo($asset->filepath);
  if ($info['dirname'] == '.') {
    $info['dirname'] = '';
  }
  if ($asset->type == "directory") {
    $info['dirname'] = $form_values['parent'];
    $info['basename'] = $form_values['title'];
  }
  $default_fields = array(
    'title',
    'author',
    'description',
    'roles',
    'status',
  );
  foreach ($default_fields as $field) {
    $asset->{$field} = $form_values[$field];
  }
  $asset->uid = $asset->uid ? $asset->uid : $GLOBALS['user']->uid;
  $asset->status = $asset->status ? ASSET_PUBLIC : ASSET_PRIVATE;
  $asset->type = $asset->type ? $asset->type : 'local';

  // make sure to set aid on the asset object, so modules can use it in assetapi insert
  $asset->aid = db_next_id('{asset}_aid');
  db_query("INSERT INTO {asset} (aid,type,dirname,filename,extension,filesize,uid,status,title,author,description) VALUES (%d, '%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s')", $asset->aid, $asset->type, $info['dirname'], $info['basename'], $info['extension'], $asset->filesize, $asset->uid, $asset->status, $asset->title, $asset->author, $asset->description);
  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,
      ));
    }
  }
  module_invoke_all('assetapi', 'insert', $asset);
  return asset_load($asset->aid);
}