hi i want to add attachment option for vendor where he can add attachment for a specific order not for project. i added following code to add attachment option in vendor panel.
{include file="addons/attachments/views/attachments/manage.tpl" object_id=$smarty.request.order_id object_type="order"}
here is the function for adding attachments
/**
* Updates or create attachment to object
*
* @param array<string, array> $attachment_data Attachment data
* @param int $attachment_id Attachment identifier
* @param string $object_type Object type
* @param int $object_id Object identifier
* @param string $type Attachment type
* @param array $files Files
* @param string $lang_code Two-letter language code
*
* @return int
*/
function fn_update_attachments(array $attachment_data, $attachment_id, $object_type, $object_id, $type = 'M', array $files = [], $lang_code = DESCR_SL)
{
$uploaded_files = [];
$object_id = intval($object_id);
$directory = $object_type . '/' . $object_id;
if (!empty($files)) {
$uploaded_data = $files;
} else {
$uploaded_data = fn_filter_uploaded_data('attachment_files');
fn_print_die($uploaded_data);
$uploaded_data = reset($uploaded_data);
}
if (!empty($attachment_id)) {
$data = [
/** @var array{usergroup_ids: array<int>} $attachment_data */
'usergroup_ids' => empty($attachment_data['usergroup_ids']) ? '0' : implode(',', $attachment_data['usergroup_ids']),
'position' => $attachment_data['position']
];
db_query('UPDATE ?:attachment_descriptions SET description = ?s WHERE attachment_id = ?i AND lang_code = ?s', $attachment_data['description'], $attachment_id, $lang_code);
db_query('UPDATE ?:attachments SET ?u WHERE attachment_id = ?i AND object_type = ?s AND object_id = ?i AND type = ?s', $data, $attachment_id, $object_type, $object_id, $type);
/**
* Executes after attachment file was updated. Allows to do additional actions.
*
* @param array $attachment_data Data of the attachment
* @param int $attachment_id Attachment identifier
* @param string $object_type Object type
* @param int $object_id Object identifier
* @param string $type Attachment type
* @param array $files Attachment files
* @param string $lang_code 2 letter language code
* @param array $uploaded_data Uploaded data
*/
fn_set_hook('attachment_update_file', $attachment_data, $attachment_id, $object_type, $object_id, $type, $files, $lang_code, $uploaded_data);
} elseif (!empty($uploaded_data)) {
// fn_print_die($uploaded_data);
$attachment_data['type'] = $type;
/** @var array{type: string, usergroup_ids: array<int>, position:int, description:string} $attachment_data */
$data = [
'object_type' => $object_type,
'object_id' => $object_id,
'usergroup_ids' => empty($attachment_data['usergroup_ids']) ? '0' : implode(',', $attachment_data['usergroup_ids']),
'position' => $attachment_data['position']
];
$data = array_merge($data, $attachment_data);
$attachment_id = db_query('INSERT INTO ?:attachments ?e', $data);
if ($attachment_id) {
$all_languages = Languages::getAll();
foreach ($all_languages as $lang_code => $v) {
if (is_array($attachment_data['description'])) {
$description = isset($attachment_data['description'][$lang_code]) ? $attachment_data['description'][$lang_code] : reset($attachment_data['description']);
} else {
$description = $attachment_data['description'];
}
$description_data = [
'attachment_id' => $attachment_id,
'lang_code' => $lang_code,
'description' => $description,
];
db_query('INSERT INTO ?:attachment_descriptions ?e', $description_data);
}
}
/**
* Executes after new file was added. Allows to do additional actions.
*
* @param array $attachment_data Data of the atttachment
* @param string $object_type Object type
* @param int $object_id Object identifier
* @param string $type Attachment type
* @param array $files Attachment files
* @param int $attachment_id Attachment identifier
* @param array $uploaded_data Uploaded data
*/
fn_set_hook('attachment_add_file', $attachment_data, $object_type, $object_id, $type, $files, $attachment_id, $uploaded_data);
}
if ($attachment_id) {
$uploaded_files[$attachment_id] = $uploaded_data;
}
if (
empty($attachment_id)
|| empty($uploaded_files[$attachment_id])
|| empty($uploaded_files[$attachment_id]['size'])
) {
return $attachment_id;
}
$old_filename = db_get_row('SELECT filename, on_server FROM ?:attachments WHERE attachment_id = ?i', $attachment_id);
if (YesNo::toBool($old_filename['on_server']) && !empty($old_filename['filename'])) {
Storage::instance('attachments')->delete($directory . '/' . $old_filename['filename']);
}
$filename = $uploaded_files[$attachment_id]['name'];
$filepath = $directory . '/' . $filename;
if (empty($uploaded_files[$attachment_id]['url']) || YesNo::toBool(Registry::get('addons.attachments.allow_save_attachments_to_server'))) {
list($filesize, $new_filename) = Storage::instance('attachments')->put($filepath, [
'file' => $uploaded_files[$attachment_id]['path']
]);
} else {
$filesize = $uploaded_files[$attachment_id]['size'];
$new_filename = $filename;
}
$update_data = [
'filesize' => $filesize,
'on_server' => YesNo::YES,
'filename' => fn_basename($new_filename),
'url' => '',
];
if (!empty($uploaded_files[$attachment_id]['url']) && !YesNo::toBool(Registry::get('addons.attachments.allow_save_attachments_to_server'))) {
$update_data['on_server'] = YesNo::NO;
$update_data['url'] = $uploaded_files[$attachment_id]['url'];
}
if (!empty($update_data['filesize'])) {
db_query('UPDATE ?:attachments SET ?u WHERE attachment_id = ?i', $update_data, $attachment_id);
}
return $attachment_id;
}
on the line marked >>>>> here i am not getting the files uploaded by picker.
getting the following result attached.