You are here

function asset_save in Asset 6

Same name and namespace in other branches
  1. 5.2 asset.module \asset_save()
  2. 5 asset.module \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.

9 calls to asset_save()
asset_asset_type in inc/asset.api.inc
Implementation of hook_asset_type().
asset_check_directory in inc/asset.routines.inc
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 contrib/asset_import/asset_import.module
asset_import_form_submit in asset_import/inc/asset_import.admin.inc

... See full list

File

inc/asset.node.inc, line 11

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';
  db_query("INSERT INTO {asset}\n\t\t(type,dirname,filename,extension,filesize,uid,status,title,author,description)\n\t\tVALUES\n\t\t('%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s')", $asset->type, $info['dirname'], $info['basename'], $info['extension'], $asset->filesize, $asset->uid, $asset->status, $asset->title, $asset->author, $asset->description);

  // make sure to set aid on the asset object, so modules can use it in assetapi insert
  $asset->aid = db_last_insert_id('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,
        $rid,
      ));
    }
  }

  // We're also saving this in the files table to support the private download method
  if (!$asset->fid) {
    $mime = function_exists(mime - content - type) ? mime - content - type($info['dirname'] . "/" . $info['basename']) : "";
    $filepath = file_directory_path() . "/" . $info['dirname'] . "/" . $info['basename'];
    db_query("INSERT INTO {files}\n\t\t\t(uid, filename, filepath, filemime, filesize, status, timestamp)\n\t\t\tVALUES\n\t\t\t(%d, '%s', '%s', '%s', %d, %d, %d)", $GLOBALS['user']->uid, $info['basename'], $filepath, $mime, $asset->filesize, 1, time());
  }
  module_invoke_all('assetapi', 'insert', $asset);
  return asset_load($asset->aid);
}