View source
<?php
define('ASSET_WIZARD_NEXT', 'Next');
define('ASSET_WIZARD_PREVIOUS', 'Back');
define('ASSET_WIZARD_CANCEL', 'Cancel');
define('ASSET_WIZARD_DELETE', 'Delete');
define('ASSET_WIZARD_SELECT', 'Select');
define('ASSET_THUMBNAIL', 'image');
define('ASSET_WIZARD_FINAL_STEP', 4);
define('ASSET_WIZARD_FINISH_REDIRECT', 'node');
require_once drupal_get_path('module', 'asset') . '/inc/asset_wizard.themes.inc';
function asset_wizard_form(&$form_state) {
$form_values =& $form_state['post'];
if (empty($form_state['post'])) {
$form_values = NULL;
}
$oper = $_GET['op'];
if (!isset($_GET['noreset'])) {
$_SESSION['asset_op'] = $_GET['op'];
}
if (isset($_GET['textfield'])) {
list($fieldname, $lost) = split("\\]\\[", $_GET['textfield']);
$fieldname = str_replace(array(
"_",
"[",
), "-", $fieldname);
$fieldname = "edit-" . $fieldname . "-";
$_SESSION['asset_fieldname'] = $fieldname;
}
elseif (isset($_GET['textarea']) || $_GET['q'] == "asset/wizard/tinymce") {
unset($_SESSION['asset_fieldname']);
$_SESSION['asset_op'] = "tinymce";
}
if ($form_values == null && is_numeric($_GET['aid'])) {
$form_values['step'] = 2;
$form_values['aid'] = $_GET['aid'];
}
if (!isset($form_values)) {
$step = 1;
}
else {
list($lost, $lost, $field) = split("-", $fieldname);
switch ($form_values['op']) {
case t('Back'):
if ($field == ASSET_THUMBNAIL) {
$step = 1;
}
elseif (isset($form_values['delete_cancel'])) {
unset($_SESSION['messages']);
drupal_goto(filter_xss($_GET['q']), 'dir=' . $form_values['dir']);
}
else {
$step = $form_values['step'] > 1 ? $form_values['step'] - 1 : 1;
}
break;
case t('Next'):
case t('Finish'):
if ($step == 1 && !$form_values['aid']) {
break;
}
if ($field == ASSET_THUMBNAIL && $form_values['step'] == 1) {
$step = $form_values['step'] + 2;
}
else {
$step = $form_values['step'] + 1;
}
break;
case t('Edit'):
if (!empty($form_values['aid'])) {
$asset = asset_load(array(
'aid' => $form_values['aid'],
));
$form['edit-asset'] = array(
'#type' => 'hidden',
'#value' => '1',
);
$form['aid'] = array(
'#type' => 'hidden',
'#value' => $form_values['aid'],
);
$fileinfo = pathinfo($asset->filename);
if (strlen($fileinfo['extension']) == 3) {
$form['filename'] = array(
'#title' => Filename,
'#type' => 'textfield',
'#value' => $asset->filename,
'#description' => t("Warning: you are changing the actual filename. If you're not sure what you're doing, you should leave this unchanged."),
);
}
else {
$form['filename'] = array(
'#title' => Filename,
'#type' => 'item',
'#value' => $asset->filename,
'#description' => t("You can only change the filename of regular files, not of YouTube movies for instance."),
);
}
$form['old-filename'] = array(
'#type' => 'hidden',
'#value' => $asset->filename,
);
$form['old-parent'] = array(
'#type' => 'hidden',
'#value' => $asset->dirname,
);
$form = array_merge($form, asset_wizard_default_fields($form_values));
$form['title']['#value'] = $asset->title;
$form['author']['#value'] = $asset->author;
$form['description']['#value'] = $asset->description;
}
break;
case t('Delete'):
if (!empty($form_values['aid'])) {
if (!$form_values['deleteme']) {
$form_values['step'] = 1;
$form['delete_warning'] = array(
'#type' => 'markup',
'#value' => t("You are about to delete the selected asset. Press the delete button to confirm deletion."),
);
$form['deleteme'] = array(
'#type' => 'hidden',
'#value' => 1,
);
form_set_error('aid');
}
elseif ($form_values['deleteme'] == 1) {
$form['deleteme'] = array(
'#type' => 'hidden',
'#value' => 0,
);
}
}
default:
$step = $form_values['step'];
break;
}
}
if ($oper == t('Upload')) {
$step = 1;
}
$form['step'] = array(
'#type' => 'hidden',
'#value' => $step,
);
drupal_set_title("Asset Wizard - Step {$step}");
if ($form_values['macro']) {
$macros = asset_get_macros(' ' . $form_values['macro'] . ' ');
$macro = array_pop($macros);
if ($macro['aid']) {
$asset = asset_load($macro['aid']);
}
}
else {
if ($form_values['aid']) {
$asset = asset_load($form_values['aid']);
$macro['aid'] = $form_values['aid'];
}
}
if (is_object($asset) && $step > 1) {
$form[] = array(
'#value' => '<div class="asset-selected-preview">' . asset_js_preview($asset->aid, false, true) . '</div>',
);
}
switch ($step) {
case 1:
asset_wizard_form_asset_selection($form, $form_values, $asset);
break;
case 2:
$form['macro'] = array(
'#type' => 'hidden',
'#value' => asset_build_macro($macro),
);
$form['help'] = array(
'#value' => t('Select a formatting option, or <em>Choose</em> or <em>Upload</em> a new asset.'),
);
$formatters = asset_get_formatters();
$ext = strtolower($asset->extension);
$default = variable_get('asset_default_formatter_' . $asset->type . '_' . $ext, 'asset:link');
if (is_array($formatters[$asset->type][$ext])) {
foreach ($formatters[$asset->type][$ext] as $formatter) {
$form['formatter'][] = array(
'#type' => 'radio',
'#title' => $formatter['name'],
'#return_value' => $formatter['module'] . ':' . $formatter['format'],
'#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
'#description' => $formatter['description'] ? $formatter['description'] : NULL,
'#parents' => array(
'formatter',
),
);
}
}
if (is_array($formatters[$asset->type]['*'])) {
foreach ($formatters[$asset->type]['*'] as $formatter) {
$form['formatter'][] = array(
'#type' => 'radio',
'#title' => $formatter['name'],
'#return_value' => $formatter['module'] . ':' . $formatter['format'],
'#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
'#description' => $formatter['description'] ? $formatter['description'] : NULL,
'#parents' => array(
'formatter',
),
);
}
}
if (is_array($formatters['*']['*'])) {
foreach ($formatters['*']['*'] as $formatter) {
$form['formatter'][] = array(
'#type' => 'radio',
'#title' => $formatter['name'],
'#return_value' => $formatter['module'] . ':' . $formatter['format'],
'#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
'#description' => $formatter['description'] ? $formatter['description'] : NULL,
'#parents' => array(
'formatter',
),
);
}
}
break;
case 3:
list($macro['formatter'], $macro['format']) = explode(':', $form_values['formatter']);
$form['macro'] = array(
'#type' => 'hidden',
'#value' => asset_build_macro($macro),
);
$default_options['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $asset->title,
'#size' => 40,
);
list($lost, $lost, $field) = split("-", $fieldname);
if (!preg_match('@link@i', $form_values['formatter'])) {
if ($field != ASSET_THUMBNAIL) {
$default_options['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => !isset($_GET['width']) ? $asset->width : filter_xss($_GET['width']),
);
$default_options['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => !isset($_GET['height']) ? $asset->height : filter_xss($_GET['height']),
);
$default_options['resizable'] = array(
'#type' => 'hidden',
'#value' => 'true',
);
}
else {
$default_options['width'] = array(
'#type' => 'hidden',
'#title' => t('Width'),
'#default_value' => $asset->height,
);
$default_options['height'] = array(
'#type' => 'hidden',
'#title' => t('Height'),
'#default_value' => $asset->width,
);
}
$default_options['align'] = array(
'#type' => 'select',
'#title' => t('Alignment'),
'#options' => array(
'none' => t('None'),
'left' => t('Left'),
'center' => t('Center'),
'right' => t('Right'),
),
'#default_value' => !isset($_GET['align']) ? 'none' : filter_xss($_GET['align']),
);
}
$module_options = (array) module_invoke($macro['formatter'], 'asset_formatter', 'options', $asset, $macro);
$form['options'] = array_merge($default_options, $module_options);
$form['options']['#tree'] = true;
break;
case 4:
if (is_array($form_values['options'])) {
foreach ($form_values['options'] as $k => $v) {
if (!in_array($k, array(
'aid',
'formatter',
'format',
)) && $v) {
$macro[$k] = $v;
}
}
}
$form['macro'] = array(
'#type' => 'textarea',
'#title' => 'Paste the following code into your text.',
'#value' => asset_build_macro($macro),
);
$is_image = array(
'jpg',
'jpeg',
'png',
'gif',
'tif',
'tiff',
'bmp',
);
$a = asset_load(array(
'aid' => $macro['aid'],
));
if (in_array($a->extension, $is_image)) {
if (variable_get('asset_imagecache', 0)) {
$presets = imagecache_presets();
$preset = $presets[variable_get('asset_imagecache', 0)]['presetname'];
$preview = theme('imagecache', $preset, str_replace("//", "/", $a->filepath));
}
else {
$asset = array(
'aid' => $macro['aid'],
'format' => 'image',
'formatter' => 'asset',
'width' => '120',
'height' => '80',
);
$preview = asset_render_macro($asset);
}
}
else {
$preview = asset_preview($macro['aid']);
}
$form['preview'] = array(
'#type' => 'hidden',
'#value' => $preview,
);
$form['author'] = array(
'#type' => 'hidden',
'#value' => $a->author,
);
$form[] = array(
'#value' => asset_wizard_finish(),
);
break;
}
$form['#redirect'] = FALSE;
if ($form['#redirect'] == FALSE) {
if ($step == 1) {
$form['#redirect'] = NULL;
}
}
$form['#multistep'] = TRUE;
$form['#tree'] = TRUE;
$form['footer']['#tree'] = false;
if ($step != 1 || !empty($_SESSION['asset_op']) || $_GET['op'] == 'Delete Folder') {
if ($_GET['op'] == t("Delete Folder")) {
$form['delete_cancel'] = array(
'#type' => 'hidden',
'#value' => 1,
);
$form['dir'] = array(
'#type' => 'hidden',
'#value' => filter_xss($_GET['dir']),
);
}
$form['footer']['previous'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_PREVIOUS),
'#attributes' => isset($form_values['op']) || $step > 1 || $_GET['op'] == t("Delete Folder") ? array() : array(
'disabled' => 'disabled',
),
);
}
if ($_GET['op'] == 'Delete Folder') {
$form['footer']['next'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_NEXT),
);
}
elseif ($_SESSION['asset_op'] == 'Upload' || $_SESSION['asset_op'] == 'New Folder' || $oper == t('Upload') || $oper == 'Embed' || $oper == 'YouTube Embed' || $oper == 'YouTube Sync') {
$form['footer']['previous'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_PREVIOUS),
);
$form['footer']['finish'] = array(
'#type' => 'submit',
'#value' => t('Next'),
);
}
elseif ($form_state['post']['op'] == 'Edit' && !empty($form_state['post']['aid'])) {
$form['footer']['previous'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_PREVIOUS),
);
$form['footer']['finish'] = array(
'#type' => 'submit',
'#value' => t('Finish'),
);
}
elseif ($step != 1 || $form_state['post']['op'] != 'Edit') {
if ($_SESSION['asset_op'] != "tinymce") {
$form['footer']['select'] = array(
'#type' => 'button',
'#value' => t('Select'),
);
}
elseif ($step != ASSET_WIZARD_FINAL_STEP - 1) {
$form['footer']['next'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_NEXT),
);
}
$form['footer']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
$form['footer']['edit'] = array(
'#type' => 'submit',
'#value' => t('Edit'),
);
}
$form['footer']['cancel'] = array(
'#type' => 'submit',
'#value' => t(ASSET_WIZARD_CANCEL),
'#attributes' => array(
'alt' => t('Cancel and Return'),
),
);
if ($step == ASSET_WIZARD_FINAL_STEP - 1) {
$form['footer']['finish'] = array(
'#type' => 'submit',
'#value' => t('Finish'),
);
}
return $form;
}
function asset_wizard_form_asset_selection(&$form, &$form_state, $current_asset = NULL) {
global $user;
static $types, $ops;
if (!isset($types)) {
foreach (module_implements('asset_type') as $module) {
$types[$module] = module_invoke($module, 'asset_type', 'info');
foreach ($types[$module] as $delta => $type) {
$ops[$type['value']] = array(
'module' => $module,
'delta' => $delta,
);
}
}
}
if ($_SESSION['asset_op'] == 'Delete Folder') {
$dir = filter_xss($_GET['dir']);
if (empty($dir) || strtolower($dir) == strtolower($user->name)) {
drupal_set_message(t('You can not delete the main folder.'), 'error');
drupal_goto(filter_xss($_GET['q']), 'dir=' . $dir);
}
else {
$subdirs = db_query("SELECT aid FROM {asset} WHERE type='directory' AND dirname='%s' LIMIT 1", $dir);
if (db_result($subdirs)) {
drupal_set_message(t('This folder contains at least one sub-folder.<br/>You need to delete these first before continuing.'), 'error');
drupal_goto(filter_xss($_GET['q']), 'dir=' . $dir);
}
else {
drupal_set_message('Warning: you are about to delete this folder and all assets within this folder.');
$form['warning'] = array(
'#value' => t('Click the Next button if you are sure you want to delete this folder and its content.<br/>Click the Back button if you want to go back.<br/><br/>This operation CAN NOT be undone.'),
);
$form['delete_confirm'] = array(
'#type' => 'hidden',
'#value' => 1,
);
return;
}
}
}
$op = false;
if ($form_state['op'] == t('Back')) {
$op = false;
}
elseif (isset($form_state['op']) && isset($ops[$state['op']])) {
$op = $form_state['op'];
}
elseif ($ops[$_GET['op']]) {
$op = $_GET['op'];
unset($_GET['op']);
}
if (!empty($op)) {
$module = $ops[$op]['module'];
$delta = $ops[$op]['delta'];
$form = array_merge($form, asset_wizard_default_fields($form_state));
$form = array_merge($form, module_invoke($module, 'asset_type', 'form', $delta, $form_state));
$form['module'] = array(
'#type' => 'value',
'#value' => $module,
);
$form['delta'] = array(
'#type' => 'value',
'#value' => $delta,
);
}
elseif (empty($op) && ($form_state['op'] != 'Edit' || empty($form_state['aid']))) {
if ($current_asset && asset_check_directory($current_asset->dirname)) {
$dir = $current_asset->dirname;
}
elseif (isset($_GET['dir']) && asset_check_directory($_GET['dir'])) {
$dir = $_GET['dir'];
}
elseif (asset_check_directory($user->name, FILE_CREATE_DIRECTORY, NULL, array(
'parent' => '',
'title' => $user->name,
))) {
$dir = $user->name;
}
else {
$dir = '';
}
$form['parent'] = array(
'#type' => 'hidden',
'#value' => $form_state['parent'] ? $form_state['parent'] : $dir,
);
$query = $_GET;
unset($query['q']);
$crumbs = array();
if (file_create_path($dir) != file_directory_path()) {
$parts = explode('/', $dir);
while ($current = array_pop($parts)) {
$query['dir'] = $parts ? join('/', $parts) . '/' . $current : $current;
$crumbs[] = l($current, $_GET['q'], array(
'query' => asset_build_query($query),
));
}
$query['dir'] = substr($dir, 0, strrpos($dir, '/'));
$list = '<li class="parent">' . l('..', $_GET['q'], array(
'query' => asset_build_query($query),
)) . '</li>';
}
$query['dir'] = '';
$crumbs[] = l('assets', $_GET['q'], array(
'query' => asset_build_query($query),
));
$sql = 'SELECT DISTINCT(a.aid),a.filename FROM {asset} a ';
$args = array();
if (user_access('administer assets')) {
$sql .= 'WHERE a.dirname=\'%s\' ';
$args = array(
$dir,
);
}
else {
$sql .= 'LEFT JOIN {asset_role} r ON a.aid=r.aid ' . 'WHERE (a.uid = %d ' . 'OR a.status = 1 ' . 'OR (r.rid IN (%s) AND r.status = 1) ) ' . 'AND a.dirname=\'%s\' ';
$args = array(
$user->uid,
join(array_keys($user->roles), ','),
$dir,
);
}
$sql .= "ORDER BY a.filename";
$result = db_query($sql, $args);
$options = array();
while ($asset = db_fetch_object($result)) {
$asset = asset_load($asset->aid);
if ($asset->type == 'directory') {
if ($query['aid']) {
unset($query['aid']);
}
$query['dir'] = ($asset->dirname ? $asset->dirname . '/' : '') . $asset->filename;
$list .= '<li class="folder">' . l($asset->title, $_GET['q'], array(
'query' => asset_build_query($query),
)) . '</li>';
}
else {
$options[$asset->aid] = $asset->filename;
}
}
$form['dir_crumb'] = array(
'#value' => '/ ' . join(' / ', array_reverse($crumbs)),
);
if ($list) {
$form['folder_list'] = array(
'#value' => '<ul class="folder-list">' . $list . '</ul>',
);
}
$form['aid'] = array(
'#type' => 'select',
'#size' => 10,
'#options' => $options,
);
if ($current_asset->aid) {
$form['aid']['#default_value'] = $current_asset->aid;
}
$form['asset_preview'] = array(
'#value' => '<div class="asset-preview"></div>',
);
$form['#tree'] = false;
$form['#theme'] = 'asset_wizard_selection_form';
return $form;
}
}
function asset_wizard_default_fields($form_values) {
$parentdir = $form_values['parent'] ? $form_values['parent'] : $_GET['dir'];
$parentdir = trim(str_replace(file_directory_path(), '', $parentdir), '/');
$parent = asset_load(array(
'dirname' => dirname($parentdir),
'filename' => basename($parentdir),
));
$form['parent'] = array(
'#type' => 'hidden',
'#value' => file_create_path($form_values['parent'] ? $form_values['parent'] : $_GET['dir']),
);
$form['parent'] = array(
'#type' => 'select',
'#title' => t('Directory'),
'#default_value' => $form_values['parent'] ? $form_values['parent'] : $_GET['dir'],
'#options' => asset_wizard_directory_options(),
'#weight' => -5,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
);
$form['author'] = array(
'#type' => 'textfield',
'#title' => t('Author'),
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#rows' => 3,
);
$form['permissions'] = array(
'#theme' => 'asset_upload_permissions',
'#tree' => false,
);
$default_status = 1;
if (isset($form_values['aid']) && is_numeric($form_values['aid'])) {
$default_status = db_result(db_query("SELECT status FROM {asset} WHERE aid=%d LIMIT 1", $form_values['aid']));
}
$form['permissions']['status'] = array(
'#type' => 'radios',
'#title' => t('Permissions'),
'#description' => t('Select the permissions for this asset. Selecting Public will allow anyone with asset permissions to use this asset. By selecting Private, you can restrict which roles, if any, can access this asset. If you select Private and do not select any roles, you will be the only user with access to this asset.'),
'#required' => true,
'#default_value' => $default_status,
'#options' => array(
ASSET_PUBLIC => t('Public'),
ASSET_PRIVATE => t('Private'),
),
);
$form['permissions']['roles'] = array(
'#type' => 'checkboxes',
'#options' => user_roles(0, 'create assets'),
'#value' => $parent->roles,
);
$form['preview'] = array(
'#type' => 'hidden',
'#value' => $preview,
);
return $form;
}
function asset_wizard_form_validate($form, &$form_state) {
if ($form_state['values']['op'] == t(ASSET_WIZARD_CANCEL)) {
drupal_get_messages('error');
drupal_goto(ASSET_WIZARD_FINISH_REDIRECT);
return;
}
elseif ($form_state['values']['op'] == t(ASSET_WIZARD_DELETE)) {
if ($form_state['values']['aid'] == "") {
form_set_error('aid', 'You need to select an asset to delete.');
}
elseif ($form_state['clicked_button']['#post']['deleteme'] == '1') {
$asset = asset_load(array(
'aid' => $form_state['values']['aid'],
));
@unlink($asset->filepath);
db_query('DELETE FROM {asset} WHERE aid=%d LIMIT 1', $form_state['values']['aid']);
db_query('DELETE FROM {asset_node} WHERE aid=%d', $form_state['values']['aid']);
drupal_set_message(t('The selected asset has been deleted.'));
}
}
if ($form_state['clicked_button']['#post']['op'] == 'Edit') {
if (empty($form_state['values']['aid'])) {
form_set_error('aid', 'You need to select an asset to edit.');
}
}
elseif ($form_state['values']['op'] == t('Select') && isset($_SESSION['asset_fieldname'])) {
$delta = split("-", $_SESSION['asset_fieldname']);
$delta_count = substr_count($_SESSION['asset_fieldname'], "-");
$delta_count--;
$delta = $delta[$delta_count];
$fieldname = str_replace(array(
"edit-field-",
"-{$delta}-",
"-",
), array(
"",
"",
"_",
), $_SESSION['asset_fieldname']);
if (!$form_state['values']['aid']) {
form_set_error('aid', 'You need to select an asset before inserting it.');
return;
}
$is_image = array(
'jpg',
'jpeg',
'png',
'gif',
'tif',
'tiff',
'bmp',
);
$a = asset_load(array(
'aid' => $form_state['values']['aid'],
));
if (in_array($a->extension, $is_image)) {
if (variable_get('asset_imagecache', 0)) {
$presets = imagecache_presets();
$preset = $presets[variable_get('asset_imagecache', 0)]['presetname'];
$preview = theme('imagecache', $preset, str_replace("//", "/", $a->filepath));
}
else {
$asset = array(
'aid' => $form_state['values']['aid'],
'format' => 'image',
'formatter' => 'asset',
'width' => '120',
'height' => '80',
);
$preview = asset_render_macro($asset);
}
}
else {
$preview = "Media placeholder";
}
$fieldname = "field_" . $fieldname;
print '<script>
window.opener.document.getElementById("' . $_SESSION['asset_fieldname'] . 'value").value = "' . $a->filename . '";
window.opener.document.getElementById("' . $_SESSION['asset_fieldname'] . 'aid").value = "' . $form_state['values']['aid'] . '";
window.opener.document.getElementById("preview_' . $fieldname . '_' . $delta . '").innerHTML = "' . str_replace("\"", "'", $preview) . '";
window.opener.document.getElementById("' . $_SESSION['asset_fieldname'] . 'caption").value = "' . $a->title . '";
window.opener.document.getElementById("' . $_SESSION['asset_fieldname'] . 'copyright").value = "' . $a->author . '";
window.close();
</script>';
exit;
}
if ($form_state['clicked_button']['#post']['op'] == "Next") {
if (isset($form_state['clicked_button']['#post']['delete_confirm'])) {
db_query("DELETE FROM {asset} WHERE type='local' AND dirname='%s'", $form_state['clicked_button']['#post']['dir']);
$path = pathinfo($form_state['clicked_button']['#post']['dir']);
db_query("DELETE FROM {asset} WHERE type='directory' AND filename='%s' LIMIT 1", $path['basename']);
asset_rmdir(file_directory_path() . '/' . $form_state['clicked_button']['#post']['dir']);
unset($_SESSION['messages']);
drupal_set_message("The directory \"" . $path['basename'] . "\" and its content have been removed");
drupal_goto(filter_xss($_GET['q']), 'dir=');
}
}
if ($form_state['buttons']['submit'][0]['#post']['op'] == "Finish" || $form_state['values']['op'] == "Next") {
if (isset($form_state['buttons']['submit'][0]['#post']['edit-asset'])) {
db_query('DELETE FROM {asset_role} WHERE aid=%d', $form_state['buttons']['submit'][0]['#post']['aid']);
if (!empty($form_state['buttons']['submit'][0]['#post']['roles'])) {
foreach ($form_state['buttons']['submit'][0]['#post']['roles'] as $role => $access) {
db_query('INSERT INTO {asset_role} (aid, rid, status) VALUES (%d, %d, %d)', array(
$form_state['buttons']['submit'][0]['#post']['aid'],
$role,
$access,
));
}
}
if (!isset($form_state['buttons']['submit'][0]['#post']['filename'])) {
$form_state['buttons']['submit'][0]['#post']['filename'] = $form_state['buttons']['submit'][0]['#post']['old-filename'];
}
$newdir = 0;
if ($form_state['buttons']['submit'][0]['#post']['old-parent'] != $form_state['buttons']['submit'][0]['#post']['parent']) {
$newdir = 1;
}
if ($form_state['buttons']['submit'][0]['#post']['old-filename'] != $form_state['buttons']['submit'][0]['#post']['filename']) {
$newdir = 1;
}
if ($newdir) {
empty($form_state['buttons']['submit'][0]['#post']['parent']) ? $dir = "" : ($dir = $form_state['buttons']['submit'][0]['#post']['parent'] . "/");
empty($form_state['buttons']['submit'][0]['#post']['old-parent']) ? $olddir = "" : ($olddir = $form_state['buttons']['submit'][0]['#post']['old-parent'] . "/");
if (rename(file_directory_path() . '/' . $olddir . $form_state['buttons']['submit'][0]['#post']['old-filename'], file_directory_path() . '/' . $dir . $form_state['buttons']['submit'][0]['#post']['filename'])) {
db_query("UPDATE {asset} SET dirname='%s' WHERE aid=%d LIMIT 1", $form_state['buttons']['submit'][0]['#post']['parent'], $form_state['buttons']['submit'][0]['#post']['aid']);
db_query("UPDATE {asset} SET filename='%s', title='%s', author='%s', status=%d, description='%s' WHERE aid=%d LIMIT 1", $form_state['buttons']['submit'][0]['#post']['filename'], $form_state['buttons']['submit'][0]['#post']['title'], $form_state['buttons']['submit'][0]['#post']['author'], $form_state['buttons']['submit'][0]['#post']['status'], $form_state['buttons']['submit'][0]['#post']['description'], $form_state['buttons']['submit'][0]['#post']['aid']);
drupal_set_message(t('Your changes have been saved.'));
drupal_goto($_GET['q'], 'dir=' . $form_state['buttons']['submit'][0]['#post']['parent'] . '&noreset=1');
break;
}
else {
drupal_set_message(t('There was an error copying the file to the new directory.'), 'error');
}
}
else {
db_query("UPDATE {asset} SET filename='%s', title='%s', author='%s', status=%d, description='%s' WHERE aid=%d LIMIT 1", $form_state['buttons']['submit'][0]['#post']['filename'], $form_state['buttons']['submit'][0]['#post']['title'], $form_state['buttons']['submit'][0]['#post']['author'], $form_state['buttons']['submit'][0]['#post']['status'], $form_state['buttons']['submit'][0]['#post']['description'], $form_state['buttons']['submit'][0]['#post']['aid']);
drupal_set_message(t('Your changes have been saved.'));
}
}
if ($form_state['values']['step'] == 1 && isset($form_state['values']['module'])) {
$retval = module_invoke($form_state['values']['module'], 'asset_type', 'validate', $form_state['values']['delta'], $form_state['values']);
if (is_numeric($retval)) {
$_POST['aid'] = $retval;
$form_state['values']['aid'] = $retval;
}
}
if ($form_state['values']['step'] == 1 && !isset($_GET['op']) && !$form_state['values']['aid']) {
form_set_error('aid', t('Please select a file.'));
}
if ($form_state['values']['step'] == 2 && !$form_state['values']['formatter']) {
form_set_error('formatter', t('Please select a formatter.'));
}
}
}
function asset_wizard_form_submit($form, &$form_state) {
if ($form_state['values']['delta'] == "directory") {
if (!$form_state['values']['title']) {
form_set_error('folder', t('You must specify a folder name.'));
}
elseif (preg_match('/[^A-Za-z0-9-_.]/', $form_state['values']['title'])) {
form_set_error('title', t('The folder name may only contain alpha-numeric characters and dashes(-), underscores(_) and periods(.).'));
}
else {
$dir = file_create_path($form_state['values']['parent'] . '/' . $form_state['values']['title']);
if (!asset_check_directory($dir, FILE_CREATE_DIRECTORY, 'folder', $form_state['values'])) {
drupal_set_message(t('Error creating %dir.', array(
'%dir' => $dir,
)));
}
}
return FALSE;
}
if ($form_state['values']['delta'] == "upload") {
if ($file = file_save_upload('upload')) {
$parent_tmp = $form_state['values']['parent'] ? $form_state['values']['parent'] . '/' : $form_state['values']['parent'];
$path = file_create_path($parent_tmp . $file->filename);
if (file_copy($file, $path, FILE_EXISTS_RENAME)) {
$asset = asset_save($file, $form_state['values']);
drupal_set_message(t("The file {$file->filename} has been successfully uploaded."));
if (isset($_SESSION['asset_fieldname'])) {
drupal_goto(filter_xss($_GET['q']), 'dir=' . filter_xss($_GET['dir']));
}
else {
return $asset->aid;
}
}
else {
form_set_error('upload', 'Error saving file to <em>' . $path . '</em>');
}
}
else {
form_set_error('upload', 'Error uploading file</em>');
}
}
if ($form_state['values']['step'] == 1 && isset($form_state['values']['module'])) {
$retval = module_invoke($form_state['values']['module'], 'asset_type', 'submit', $form_state['values']['delta'], $form_state['values']);
if (is_numeric($retval)) {
$_POST['aid'] = $retval;
}
return FALSE;
}
return FALSE;
}
function asset_wizard_directory_options($dir = '') {
global $user;
$sql = 'SELECT DISTINCT(a.aid),a.dirname FROM {asset} a ';
$args = array();
if (user_access('administer assets')) {
$sql .= 'WHERE a.dirname LIKE \'%s%\' ';
$args = array(
$dir,
);
}
else {
$sql .= 'LEFT JOIN {asset_role} r ON a.aid=r.aid ' . 'WHERE (a.uid = %d ' . 'OR a.status = 1 ' . 'OR (r.rid IN (%s) AND r.status = 1) ) ' . 'AND a.dirname LIKE \'%s%\' ';
$args = array(
$user->uid,
join(array_keys($user->roles), ','),
$dir,
);
}
$sql .= ' AND a.type = \'directory\' ORDER BY a.dirname ASC';
$result = db_query($sql, $args);
$options = array();
while ($asset = db_fetch_object($result)) {
$asset = asset_load($asset->aid);
$dir = $asset->dirname . ($asset->dirname ? '/' : '') . $asset->filename;
$parts = explode('/', $dir);
if (count($parts)) {
$name = str_repeat('--', count($parts)) . ' ' . $asset->title;
}
else {
$name = $asset->title;
}
$options[$dir] = $name;
}
if (user_access('administer assets')) {
$options[''] = 'assets';
}
ksort($options);
return $options;
}
function asset_wizard_location_bar($dir = '') {
global $user;
$defaults = array_unique(array(
$dir,
'PUBLIC',
$user->name,
));
$qstr = $_GET;
unset($qstr['q']);
foreach ($defaults as $path) {
$info = asset_pathinfo($path);
$asset = asset_load(array(
'dirname' => $info['dirname'],
'filename' => $info['filename'],
));
$qstr['dir'] = $asset->filepath;
$links[] = l($asset->title, $_GET['q'], array(
'query' => asset_build_query($query),
));
}
return theme('item_list', $links);
}
function asset_wizard_toolbar($dir = '') {
$qarray = $_GET;
$qarray['dir'] = $dir;
$buttons = array();
$choose = t('Choose');
$html = theme('image', drupal_get_path('module', 'asset') . '/misc/icon.png', $choose, $choose, NULL, FALSE) . '<br/>' . $choose;
$qstring = drupal_query_string_encode($qarray, array(
'q',
'aid',
'op',
));
$buttons[] = l($html, 'asset/wizard/' . arg(2), array(
'query' => $qstring,
'html' => true,
));
foreach (module_implements('asset_type') as $module) {
$types = module_invoke($module, 'asset_type', 'info');
foreach ($types as $delta => $type) {
if (user_access('create ' . $delta . ' assets') || user_access('administer assets')) {
if ($type['src']) {
$html = theme('image', $type['src'], $type['value'], $type['value'], NULL, FALSE) . '<br/>' . $type['value'];
}
else {
$html = $type['value'];
}
$qarray['op'] = $type['value'];
$qstring = drupal_query_string_encode($qarray, array(
'q',
'aid',
));
$buttons[] = l($html, 'asset/wizard/' . arg(2), array(
'query' => $qstring,
'html' => true,
));
}
}
$i = count($buttons) - 1;
if (!is_array($buttons[$i])) {
$buttons[$i] = array(
'data' => $buttons[$i],
'class' => 'last',
);
}
}
return theme('item_list', $buttons);
}
function asset_wizard_finish() {
return '<script type="text/javascript">$(insertToEditor);</script>';
}
function asset_rmdir($folder) {
if (is_dir($folder)) {
foreach (scandir($folder) as $value) {
if ($value != "." && $value != "..") {
$value = $folder . "/" . $value;
if (is_dir($value)) {
asset_rmdir($value);
}
elseif (is_file($value)) {
@unlink($value);
}
}
}
return rmdir($folder);
}
else {
return FALSE;
}
}