Fix upload document with ajax return CDocument entity #2326

pull/2715/head
Julio Montoya 7 years ago
parent fe53b0aec2
commit acd64d3511
  1. 1
      main/document/upload.php
  2. 15
      main/inc/ajax/document.ajax.php
  3. 38
      main/inc/lib/document.lib.php
  4. 44
      main/inc/lib/fileUpload.lib.php

@ -4,7 +4,6 @@
/** /**
* @package chamilo.document * @package chamilo.document
*/ */
// Including the global initialization file
require_once __DIR__.'/../inc/global.inc.php'; require_once __DIR__.'/../inc/global.inc.php';
// Including additional libraries // Including additional libraries

@ -88,7 +88,7 @@ switch ($action) {
foreach ($fileList as $file) { foreach ($fileList as $file) {
$globalFile = []; $globalFile = [];
$globalFile['files'] = $file; $globalFile['files'] = $file;
$result = DocumentManager::upload_document( $document = DocumentManager::upload_document(
$globalFile, $globalFile,
$currentDirectory, $currentDirectory,
'', '',
@ -101,15 +101,16 @@ switch ($action) {
); );
$json = []; $json = [];
if (!empty($result) && is_array($result)) { if (!empty($document)) {
$json['name'] = Display::url( $json['name'] = Display::url(
api_htmlentities($result['title']), api_htmlentities($document->getTitle()),
api_htmlentities($result['url']), api_htmlentities($document->getTitle()),
['target' => '_blank'] ['target' => '_blank']
); );
$json['url'] = $result['url']; $json['url'] = '#';
$json['size'] = format_file_size($file['size']); $json['size'] = format_file_size($document->getSize());
$json['type'] = api_htmlentities($file['type']); //$json['type'] = api_htmlentities($file['type']);
$json['type'] = '';
$json['result'] = Display::return_icon( $json['result'] = Display::return_icon(
'accept.png', 'accept.png',
get_lang('Uploaded') get_lang('Uploaded')

@ -2826,7 +2826,7 @@ class DocumentManager
* @param string $fileKey * @param string $fileKey
* @param bool $treat_spaces_as_hyphens * @param bool $treat_spaces_as_hyphens
* *
* @return array|bool * @return CDocument|false
*/ */
public static function upload_document( public static function upload_document(
$files, $files,
@ -2849,7 +2849,7 @@ class DocumentManager
if (isset($files[$fileKey])) { if (isset($files[$fileKey])) {
$uploadOk = process_uploaded_file($files[$fileKey], $show_output); $uploadOk = process_uploaded_file($files[$fileKey], $show_output);
if ($uploadOk) { if ($uploadOk) {
$new_path = handle_uploaded_document( $document = handle_uploaded_document(
$course_info, $course_info,
$files[$fileKey], $files[$fileKey],
$base_work_dir, $base_work_dir,
@ -2868,7 +2868,7 @@ class DocumentManager
); );
// Showing message when sending zip files // Showing message when sending zip files
if ($new_path === true && $unzip == 1) { if ($document && $unzip == 1) {
if ($show_output) { if ($show_output) {
echo Display::return_message( echo Display::return_message(
get_lang('UplUploadSucceeded').'<br />', get_lang('UplUploadSucceeded').'<br />',
@ -2877,20 +2877,11 @@ class DocumentManager
); );
} }
return [ return $document;
'title' => $files[$fileKey]['name'],
'url' => '#',
];
} }
if ($new_path) { if ($document) {
$documentId = self::get_document_id( /*
$course_info,
$new_path,
$sessionId
);
if (!empty($documentId)) {
$table_document = Database::get_course_table(TABLE_DOCUMENT); $table_document = Database::get_course_table(TABLE_DOCUMENT);
$params = []; $params = [];
@ -2912,29 +2903,20 @@ class DocumentManager
], ],
] ]
); );
} }*/
if ($index_document) { if ($index_document) {
self::index_document( self::index_document(
$documentId, $document->getId(),
$course_info['code'], $course_info['code'],
null, null,
$_POST['language'], $_POST['language'] ?? '',
$_REQUEST, $_REQUEST,
$ifExists $ifExists
); );
} }
if (!empty($documentId) && is_numeric($documentId)) { return $document;
$documentData = self::get_document_data_by_id(
$documentId,
$course_info['code'],
false,
$sessionId
);
return $documentData;
}
} }
} }
} }

@ -216,11 +216,12 @@ function process_uploaded_file($uploaded_file, $show_output = true)
* @param string $comment * @param string $comment
* @param int $sessionId * @param int $sessionId
* @param bool $treat_spaces_as_hyphens * @param bool $treat_spaces_as_hyphens
* @param string $uploadKey
* *
* So far only use for unzip_uploaded_document function. * So far only use for unzip_uploaded_document function.
* If no output wanted on success, set to false. * If no output wanted on success, set to false.
* *
* @return string path of the saved file * @return CDocument|false
*/ */
function handle_uploaded_document( function handle_uploaded_document(
$courseInfo, $courseInfo,
@ -546,7 +547,6 @@ function handle_uploaded_document(
break; break;
case 'rename': case 'rename':
// Rename the file if it exists // Rename the file if it exists
// Always rename.
$cleanName = DocumentManager::getUniqueFileName( $cleanName = DocumentManager::getUniqueFileName(
$uploadPath, $uploadPath,
$cleanName, $cleanName,
@ -577,7 +577,7 @@ function handle_uploaded_document(
if (true) { if (true) {
///chmod($fullPath, $filePermissions); ///chmod($fullPath, $filePermissions);
// Put the document data in the database // Put the document data in the database
$documentId = add_document( $document = add_document(
$courseInfo, $courseInfo,
$filePath, $filePath,
'file', 'file',
@ -593,41 +593,19 @@ function handle_uploaded_document(
$content $content
); );
if ($documentId) {
// Update document item_property
/*api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentAdded',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);*/
// Redo visibility
//api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo);
}
// If the file is in a folder, we need to update all parent folders
//item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message to user // Display success message to user
if ($output) { if ($output) {
Display::addFlash( Display::addFlash(
Display::return_message( Display::return_message(
get_lang('UplUploadSucceeded').'<br />'. get_lang('UplUploadSucceeded').'<br />'.
get_lang('UplFileSavedAs').' '.$documentTitle, get_lang('UplFileSavedAs').' '.$document->getTitle(),
'success', 'success',
false false
) )
); );
} }
return $filePath; return $document;
} else { } else {
/*if ($output) { /*if ($output) {
Display::addFlash( Display::addFlash(
@ -1333,7 +1311,7 @@ function filter_extension(&$filename)
* @param bool $sendNotification * @param bool $sendNotification
* @param string $content * @param string $content
* *
* @return int id if inserted document * @return CDocument
*/ */
function add_document( function add_document(
$courseInfo, $courseInfo,
@ -1459,7 +1437,7 @@ function add_document(
$sql = "UPDATE $table SET id = iid WHERE iid = $documentId"; $sql = "UPDATE $table SET id = iid WHERE iid = $documentId";
Database::query($sql); Database::query($sql);
if ($saveVisibility) { /*if ($saveVisibility) {
api_set_default_visibility( api_set_default_visibility(
$documentId, $documentId,
TOOL_DOCUMENT, TOOL_DOCUMENT,
@ -1468,7 +1446,7 @@ function add_document(
$sessionId, $sessionId,
$userId $userId
); );
} }*/
$allowNotification = api_get_configuration_value('send_notification_when_document_added'); $allowNotification = api_get_configuration_value('send_notification_when_document_added');
if ($sendNotification && $allowNotification) { if ($sendNotification && $allowNotification) {
@ -1493,10 +1471,10 @@ function add_document(
MessageManager::sendMessageToAllUsersInCourse($subject, $message, $courseInfo, $sessionId); MessageManager::sendMessageToAllUsersInCourse($subject, $message, $courseInfo, $sessionId);
} }
return $documentId; return $document;
} else {
return false;
} }
return false;
} }
/** /**

Loading…
Cancel
Save