View source
<?php
function asset_import_form() {
$form = array();
$dirpath = variable_get('asset_import_path', '');
if (!file_check_directory($dirpath)) {
drupal_set_message(t("You need to configure the import directory on the asset import module's <a href='!admin-settings-asset_import'>settings page</a>.", array(
'!admin-settings-asset_import' => url('admin/settings/asset_import'),
)), 'error');
return $form;
}
$files = file_scan_directory($dirpath, '.*');
ksort($files);
if ($files) {
include_once drupal_get_path('module', 'asset') . '/inc/asset_wizard.inc';
$list = asset_wizard_directory_options();
$form['parent'] = array(
'#type' => 'select',
'#title' => t('Parent directory'),
'#default_value' => $form_values['parent'] ? $form_values['parent'] : $_GET['dir'],
'#options' => asset_wizard_directory_options(),
);
$fields = array(
'filesize',
'dimensions',
'title',
'body',
);
foreach ($fields as $field) {
$form['files'][$field][0] = NULL;
}
$filelist = array(
0 => NULL,
);
foreach ($files as $file) {
$_title = db_fetch_array(db_query("SELECT n.title, n.type, f.filename FROM {node} n, {files} f WHERE f.filepath='%s' AND n.nid = f.nid LIMIT 1", $file->filename));
if (!empty($_title)) {
if ($_title['type'] == "image") {
$title = $_title['title'] . " (" . str_replace("_original", "original", $_title['filename']) . ")";
}
else {
$title = $_title['title'];
}
}
else {
$title = basename($file->name);
}
$info = image_get_info($file->filename);
if ($info && isset($info['extension'])) {
$filelist[] = substr($file->filename, strlen($dirpath) + 1);
$form['files']['filesize'][] = array(
'#type' => 'item',
'#value' => format_size(filesize($file->filename)),
);
$form['files']['dimensions'][] = array(
'#type' => 'item',
'#value' => $info['width'] . 'x' . $info['height'],
);
$form['files']['title'][] = array(
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $title,
);
$form['files']['body'][] = array(
'#type' => 'textfield',
'#size' => 20,
);
}
else {
$filelist[] = substr($file->filename, strlen($dirpath) + 1);
$form['files']['filesize'][] = array(
'#type' => 'item',
'#value' => format_size(filesize($file->filename)),
);
$form['files']['dimensions'][] = array(
'#type' => 'item',
'#value' => '',
);
$form['files']['title'][] = array(
'#type' => 'textfield',
'#size' => 20,
'#default_value' => basename($file->name),
);
$form['files']['body'][] = array(
'#type' => 'textfield',
'#size' => 20,
);
}
}
unset($filelist[0]);
foreach ($fields as $field) {
$form['files'][$field][0] = NULL;
}
$form['files']['title']['#tree'] = TRUE;
$form['files']['body']['#tree'] = TRUE;
$form['file_list'] = array(
'#type' => 'value',
'#value' => $filelist,
);
$form['import_file'] = array(
'#type' => 'checkboxes',
'#options' => $filelist,
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
}
else {
$form['import_file'] = array(
'#type' => 'item',
'#value' => t('No files were found'),
);
}
return $form;
}
function asset_import_form_submit($form, &$form_state) {
$nodes = array();
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
if ($op == t('Import')) {
$dirpath = variable_get('asset_import_path', '');
if (file_check_directory($dirpath)) {
$nodes = array();
$files = array();
foreach (array_filter($form_state['values']['import_file']) as $index) {
if (!ini_get('safe_mode')) {
set_time_limit(0);
}
$origname = $form_state['values']['file_list'][$index];
$filename = file_check_location($dirpath . '/' . $origname, $dirpath);
$target = file_directory_path() . '/' . $form_state['values']['parent'] . '/' . $origname;
if ($filename && rename($filename, $target)) {
$options = array(
'title' => $form_state['values']['title'][$index],
'author' => $form_state['values']['body'][$index],
'status' => 1,
);
$asset = new StdClass();
$asset->filepath = $target;
$asset->filesize = filesize($target);
asset_save($asset, $options);
$nodes[] = t('%filename', array(
'%filename' => $origname,
));
}
}
if (!empty($nodes)) {
drupal_set_message(t('Successfully imported: ') . theme('item_list', $nodes));
}
else {
drupal_set_message(t('No files were imported.'));
}
}
}
}
function asset_import_admin_settings() {
$descr = '';
$descr .= t("The directory to import assets from. Drupal will need to have write access to this directory so we can move the file.");
$descr .= '<br />';
$descr .= t("<strong>Note:</strong> a path begining with a <kbd>/</kbd> indicates the path is relative to the server's root, not the website's root. One starting without a <kbd>/</kbd> specifies a path relative to Drupal's root. For example: <kbd>/tmp/image</kbd> would be the temp directory off the root while <kbd>tmp/image</kbd> would be inside Drupal's directory.");
$form['asset_import_path'] = array(
'#type' => 'textfield',
'#title' => t('Import path'),
'#default_value' => variable_get('asset_import_path', file_directory_path()),
'#after_build' => array(
'_asset_import_settings_check_directory',
),
'#description' => $descr,
'#required' => TRUE,
);
return system_settings_form($form);
}