View source
<?php
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);
$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,
));
}
}
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);
}
function asset_load($param = array(), $reset = NULL) {
static $assets = array();
if ($reset) {
$assets = array();
}
$arguments = array();
if (is_numeric($param)) {
if (isset($assets[$param])) {
return is_object($assets[$param]) ? drupal_clone($assets[$param]) : $assets[$param];
}
$cond = 'a.aid = %d';
$arguments[] = $param;
}
elseif (is_array($param)) {
foreach ($param as $key => $value) {
$cond[] = 'a.' . db_escape_string($key) . " = '%s'";
$arguments[] = $value;
}
$cond = implode(' AND ', $cond);
}
else {
return false;
}
$asset = db_fetch_object(db_query('SELECT a.* FROM {asset} a WHERE ' . $cond, $arguments));
if (!$asset) {
return false;
}
$dirname_tmp = $asset->dirname ? $asset->dirname . '/' : $asset->dirname;
$result = db_query('SELECT * FROM {asset_role} WHERE aid = %d', $asset->aid);
while ($role = db_fetch_array($result)) {
$asset->roles[$role['rid']] = $role['status'] ? $role['rid'] : 0;
}
$asset->filepath = file_create_path($dirname_tmp . $asset->filename);
$asset->url = file_create_url($dirname_tmp . $asset->filename);
$asset->extension = strtolower($asset->extension);
$asset->title = $asset->title ? $asset->title : $asset->filename;
if ($asset->type == 'directory' && $asset->dirname == '' && $asset->filename == $GLOBALS['user']->name) {
$asset->title = t('My Assets');
}
if ($asset->aid) {
if ($extra = module_invoke_all('assetapi', 'load', $asset)) {
foreach ($extra as $key => $value) {
$asset->{$key} = $value;
}
}
$assets[$asset->aid] = is_object($asset) ? drupal_clone($asset) : $asset;
}
return $asset;
}
function _content_widget_invoke($op, &$node) {
$type_name = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
$type = content_types($type_name);
$widget_types = _content_widget_types();
$return = array();
if (count($type['fields'])) {
foreach ($type['fields'] as $field) {
if ($field['type'] == 'asset') {
$node->{$field}['field_name'] = $_POST[$field['field_name']];
$node->{$field['field_name']}[0]['options'] = $node->{$field['field_name']}[0]['value'];
}
}
}
return $return;
}
function asset_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'insert':
case 'update':
_content_widget_invoke($op, $node);
db_query('DELETE FROM {asset_node} WHERE nid = %d', $node->nid);
$filters = filter_list_format($node->format);
if (!$filters['asset/0']) {
return;
}
$refs = array();
foreach (asset_get_macros($node->body) as $macro) {
$refs[$macro['aid']]++;
}
foreach ($refs as $aid => $ref) {
db_query('INSERT INTO {asset_node} (aid, nid, refs) VALUES (%d, %d, %d)', $aid, $node->nid, $ref);
}
break;
case 'load':
$result = db_query('SELECT * FROM {asset_node} WHERE nid = %d AND refs > 0', $node->nid);
while ($asset = db_fetch_object($result)) {
$additions['assets'][] = asset_load($asset->aid);
}
return $additions;
case 'view':
foreach ($node as $fieldname => $value) {
if (substr($fieldname, 0, 6) == "field_") {
$fields = content_fields($fieldname, $node->type);
if ($fields['type'] == "asset") {
$assetcheck = 1;
foreach (array_keys($node->{$fieldname}) as $key) {
if (!empty($value[$key]['aid'])) {
$value[$key]['value'] = asset_preview($value[$key]['aid']);
}
}
}
}
}
break;
}
}
function asset_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Inline file assets'),
);
case 'description':
return t('Add formatted file assets to your posts.');
case 'process':
foreach (asset_get_macros($text) as $unexpanded_macro => $macro) {
$expanded_macro = asset_render_macro($macro);
$text = str_replace($unexpanded_macro, $expanded_macro, $text);
}
return $text;
default:
return $text;
}
}
function asset_filter_tips($delta, $format, $long = false) {
if ($long) {
return t('Inline assets are allowed. Use the Insert Assets link or the WYSIWYG editor button to insert the proper format.');
}
else {
return t('Inline assets are allowed.');
}
}