Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[document_repository] Rewrite and Reactify #3971

Merged
merged 111 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
7cb1307
1st
kongtiaowang Sep 21, 2018
57b2201
js
kongtiaowang Sep 21, 2018
d3f50e2
add js files
kongtiaowang Sep 21, 2018
2e7fb8b
Merge branch 'major' of https://github.com/aces/Loris into 2018-09-22
kongtiaowang Sep 27, 2018
c5281e7
permisson
kongtiaowang Sep 27, 2018
0ecf6cf
& => /
kongtiaowang Sep 27, 2018
c558c98
phpcs
kongtiaowang Oct 1, 2018
a2a863c
modify comment
kongtiaowang Oct 2, 2018
d256c85
fix
kongtiaowang Oct 2, 2018
e47af54
phpcs
kongtiaowang Oct 2, 2018
3c2a833
d
kongtiaowang Oct 2, 2018
f646f60
d
kongtiaowang Oct 2, 2018
bea6469
d
kongtiaowang Oct 2, 2018
0992860
cbf
kongtiaowang Oct 2, 2018
7506165
propstype
kongtiaowang Oct 2, 2018
732341e
compile
kongtiaowang Oct 2, 2018
ff4fab3
test
kongtiaowang Oct 3, 2018
023240b
new author
kongtiaowang Oct 10, 2018
165151f
new php
kongtiaowang Oct 16, 2018
1707772
remove columnformat
kongtiaowang Oct 16, 2018
06f9d1f
remove format from webpack
kongtiaowang Oct 16, 2018
1f8be9c
hidden
kongtiaowang Oct 16, 2018
9967d5a
gitignore
kongtiaowang Oct 16, 2018
31fd470
.gitignore
kongtiaowang Oct 16, 2018
12bd80f
fix
kongtiaowang Oct 22, 2018
84d3a35
string ref
kongtiaowang Nov 5, 2018
1c42cf5
fix comment
kongtiaowang Nov 5, 2018
777b04b
add miss retrun type
kongtiaowang Nov 5, 2018
dfb9f8b
phpcs
kongtiaowang Nov 6, 2018
b843d8c
cs
kongtiaowang Nov 6, 2018
f7a17ae
new template
kongtiaowang Dec 12, 2018
3a50af1
rebase 1
kongtiaowang Dec 12, 2018
4b0b186
new template
kongtiaowang Dec 12, 2018
2450b7d
rr
kongtiaowang Dec 13, 2018
95ce70d
phan fix
kongtiaowang Dec 13, 2018
d97b040
rename the function names
kongtiaowang Dec 13, 2018
6d64678
phan
kongtiaowang Dec 13, 2018
eabaeb2
Merge branch 'major' of https://github.com/aces/Loris into 2018-09-22
kongtiaowang Dec 13, 2018
fa886d7
cs
kongtiaowang Dec 13, 2018
22b68e3
update Synchronize
kongtiaowang Dec 13, 2018
bd5083d
remove useless state
kongtiaowang Dec 13, 2018
7fe66f0
fix conflict
kongtiaowang Dec 18, 2018
48bc174
files
kongtiaowang Jan 22, 2019
ef8ae93
add files.class.inc
kongtiaowang Jan 22, 2019
de4b2ce
uploadform
kongtiaowang Jan 23, 2019
6cd3311
remove file
kongtiaowang Jan 23, 2019
ba38acc
cate and edit
kongtiaowang Jan 23, 2019
0be371c
todo
kongtiaowang Jan 25, 2019
2a01964
not done
kongtiaowang Jan 26, 2019
7be3f4d
0128
kongtiaowang Jan 28, 2019
42ac7b8
done
kongtiaowang Jan 28, 2019
94d8c9f
phpcs
kongtiaowang Jan 28, 2019
757c044
phpcbf
kongtiaowang Jan 28, 2019
29f2b77
phan
kongtiaowang Jan 28, 2019
9df3983
cs
kongtiaowang Jan 28, 2019
c9153d7
todo null check
kongtiaowang Jan 29, 2019
71833ba
todo get and upload
kongtiaowang Jan 30, 2019
4b9b1c9
last commit
kongtiaowang Jan 30, 2019
21a95c3
Data to data
kongtiaowang Feb 5, 2019
ff01033
conflict
kongtiaowang Feb 5, 2019
6180feb
Data to data
kongtiaowang Feb 5, 2019
eb225af
fix type error
kongtiaowang Feb 5, 2019
6d8192e
check duplicate file names
kongtiaowang Feb 5, 2019
71203ef
cs
kongtiaowang Feb 8, 2019
8c44acd
phpcs
kongtiaowang Feb 15, 2019
458bd9f
phan comment
kongtiaowang Feb 15, 2019
4e8da3f
phpcs again
kongtiaowang Feb 15, 2019
9ea8226
Merge branch 'major' into 2018-09-22
kongtiaowang Feb 15, 2019
9d87f36
lint format
kongtiaowang Feb 20, 2019
9784bb8
PSR7 and driusan's comments/to do add comment
kongtiaowang Feb 21, 2019
d095cd8
phpcs and comments
kongtiaowang Feb 22, 2019
3737066
phan
kongtiaowang Feb 22, 2019
637ba4c
new files.class restful
kongtiaowang Feb 27, 2019
0999df8
add return type
kongtiaowang Feb 27, 2019
dd66ccd
phan
kongtiaowang Feb 28, 2019
6ffb4fe
remove last ajax file
kongtiaowang Mar 7, 2019
238bd9c
phpcs
kongtiaowang Mar 7, 2019
9d96155
getUri
kongtiaowang Mar 7, 2019
6ba0872
phpcs
kongtiaowang Mar 7, 2019
d6933f8
fix file
kongtiaowang Mar 11, 2019
c933e90
body
kongtiaowang Mar 11, 2019
cd971cf
phpcs
kongtiaowang Mar 11, 2019
3c354d2
phpcs
kongtiaowang Mar 11, 2019
19cd3a3
template
kongtiaowang Mar 11, 2019
0b56afd
cs
kongtiaowang Mar 11, 2019
4f41c55
filename and comments
kongtiaowang Mar 13, 2019
27571df
null to 0
kongtiaowang Mar 15, 2019
93e0db7
ing...
kongtiaowang Apr 2, 2019
1b80b41
doc tree
kongtiaowang Apr 2, 2019
392a2a7
todo
kongtiaowang Apr 15, 2019
5ea486e
todo css and meeting
kongtiaowang Apr 18, 2019
a07ee5f
todo null table
kongtiaowang Apr 23, 2019
a709828
new component
kongtiaowang Apr 23, 2019
04c35db
rm tree
kongtiaowang Apr 24, 2019
fcac6e1
phcs
kongtiaowang Apr 24, 2019
f218df0
fix confilct
kongtiaowang Apr 24, 2019
e69b352
cs
kongtiaowang Apr 24, 2019
abb93b1
debug database
kongtiaowang Apr 24, 2019
425cd40
Merge branch 'major' of https://github.com/aces/Loris into 2018-09-22
kongtiaowang Apr 29, 2019
1570749
remove debug
kongtiaowang May 6, 2019
1b57fa8
font-awesome
kongtiaowang May 7, 2019
a352e8c
test
kongtiaowang May 7, 2019
107a026
rebase Email
kongtiaowang May 22, 2019
3008140
rebase NDB_Notifier_Abstract.class.inc
kongtiaowang May 22, 2019
899ae2f
update editForm.js
kongtiaowang May 22, 2019
b555631
avoid sql injection
kongtiaowang May 22, 2019
3d956a5
Update document_repositoryTest.php
kongtiaowang May 22, 2019
9837e11
Merge branch '21.0-release' into 2018-09-22
kongtiaowang May 22, 2019
5c2863b
phan
kongtiaowang May 22, 2019
e7a765c
phan
kongtiaowang May 22, 2019
07debcb
add space
kongtiaowang May 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
366 changes: 366 additions & 0 deletions modules/document_repository/ajax/FileUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
<?php
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
/**
* Document Repository.
*
* Handles document upload and update actions received from a front-end ajax call
*
* PHP Version 7
*
* @category Loris
* @package Document_Repository
* @author Shen Wang <wangshen.mcin@gmail.com>
* @license Loris license
* @link https://github.com/aces/Loris-Trunk
*/

if (isset($_GET['action'])) {
$action = $_GET['action'];
if ($action == "getData") {
viewData();
} else if ($action == "upload") {
uploadFile();
} else if ($action == "edit") {
editFile();
} else if ($action == "getCategory") {
viewCategory();
} else if ($action == "uploadCategory") {
uploadCategory();
}
}

/**
* Handles the document update/edit process
*
* @throws DatabaseException
*
* @return void
*/
function editFile(): void
{
$db = \Database::singleton();
$user = \User::singleton();
if (!$user->hasPermission('document_repository_delete')) {
header("HTTP/1.1 403 Forbidden");
return;
}

// Read JSON from STDIN
$stdin = file_get_contents('php://input');
$req = json_decode($stdin, true);
$idDocFile = $req['idDocFile'];

if (!$idDocFile) {
showError("Error! Invalid doc file ID!");
}

$updateValues = [
'File_category' => $req['category'],
'For_site' => $req['forSite'],
'Instrument' => $req['instrument'],
'comments' => $req['comments'],
'version' => $req['version'],
'visitLabel' => $req['visitLabel'],
'pscid' => $req['pscid'],
];

try {
$db->update(
'document_repository',
$updateValues,
['record_id' => $idDocFile]
);
} catch (DatabaseException $e) {
showError("Could not update the file. Please try again!");
}

}


/**
* Handles the document upload process
*
* @throws DatabaseException
*
* @return void
*/
function uploadFile()
{
$editNotifier = new NDB_Notifier(
"document_repository",
"edit"
);
$uploadNotifier = new NDB_Notifier(
"document_repository",
"upload"
);
$userSingleton = \User::singleton();
if (!$userSingleton->hasPermission('document_repository_view')
&& !$userSingleton->hasPermission('document_repository_delete')
) {
http_response_code(403);
exit;
}

$factory = \NDB_Factory::singleton();
$baseURL = $factory->settings()->getBaseURL();
$config = $factory->config();
$base = $config->getSetting('base');
$name = $userSingleton->getUsername();
$DB = $factory->database();

$category = $_POST['category']; // required
$site = $_POST['forSite'] !== '' ? $_POST['forSite'] : null;
$instrument = $_POST['instrument'] !== '' ? $_POST['instrument'] : null;
$pscid = $_POST['pscid'] !== '' ? $_POST['pscid'] : null;
$visit = $_POST['visitLabel'] !== '' ? $_POST['visitLabel'] : null;
$comments = $_POST['comments'] !== '' ? $_POST['comments'] : null;
$version = $_POST['version'] !== '' ? $_POST['version'] : null;
$fileSize = $_FILES["file"]["size"];
$fileName = $_FILES["file"]["name"];
$fileType = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$uploadPath = "$base/modules/document_repository/user_uploads/$name/";
$fullPath = $uploadPath . $fileName;
// $category is a string representation of an ID, and so should be at
// least equal to zero.
if (intval($category) < 0) {
throw new LorisException(
"'Category' parameter must be a positive integer."
);
}
// Check to see if $fullPath is writable. If not, throw an error. If it
// doesn't exist, create an uploads folder for the logged-in user.
if (!is_writable($fullPath)) {
if (file_exists($fullPath)) {
throw new LorisException(
"User uploads path in Document Repository is not writable."
);
}
mkdir($fullPath, 0770);
}
// Copy the uploaded file to the user's upload folder if possible.
// Insert a record of the file into the document_repository table
if (!move_uploaded_file(
$_FILES['file']['tmp_name'],
$fullPath . $fileName
)) {
throw new LorisException(
'ERROR: Could not upload file. Contact your administrator.'
);
} else {
$success = $DB->insert(
'document_repository',
array(
'File_category' => $category,
'For_site' => $site,
'comments' => $comments,
'version' => $version,
'File_name' => $fileName,
'File_size' => $fileSize,
'Data_dir' => "$name/$fileName", // e.g. 'admin/file.png'
'uploaded_by' => $name,
'Instrument' => $instrument,
'PSCID' => $pscid,
'visitLabel' => $visit,
'File_type' => $fileType,
)
);
$msg_data['newDocument']
= $baseURL . "/document_repository/";
$msg_data['document'] = $fileName;
$uploadNotifier->notify($msg_data);
http_response_code(303);
header('Location:' . $baseURL . '/document_repository/');
}
}
/**
* Handles the upload category process
*
* @return void
*/
function uploadCategory()
{
$DB = \Database::singleton();
$category_name = $_POST['category_name']; // required
$parent_id = $_POST['parent_id'] !== '' ? $_POST['parent_id'] : null;
$comment = $_POST['comments'] !== '' ? $_POST['comments'] : null;
$DB->insert(
"document_repository_categories",
array(
"category_name" => $category_name,
"parent_id" => $parent_id,
"comments" => $comments,
)
);
}
/**
* Return a json string to view the category
*
* @throws DatabaseException
*
* @return void
*/
function viewCategory()
kongtiaowang marked this conversation as resolved.
Show resolved Hide resolved
{
$user = \User::singleton();
if (!$user->hasPermission('document_repository_view')) {
header("HTTP/1.1 403 Forbidden");
return null;
}
echo json_encode(getCategoryFields());
}
/**
* Returns a list of fields from database
*
* @return array
* @throws DatabaseException
*/
function getCategoryFields(): array
{
$db = \Database::singleton();
$query = $db->pselect(
"SELECT * FROM document_repository_categories",
array()
);
//categories
$categoriesList = array();

foreach ($query as $value) {
$arr = parseCategory($value);
$categoriesList[$arr['id']] =$arr['name'];
}

$result = ['categories' => $categoriesList];

return $result;
}
/**
* Returns a list of fields from database
*
* @return void
* @throws DatabaseException
*/
function viewData()
{
$user = \User::singleton();
if (!$user->hasPermission('document_repository_view')) {
header("HTTP/1.1 403 Forbidden");
return null;
}
echo json_encode(getUploadFields());
}

/**
* Returns a list of fields from database
*
* @return array
* @throws DatabaseException
*/
function getUploadFields(): array
{

$db = \Database::singleton();
$query = $db->pselect(
"SELECT * FROM document_repository_categories",
array()
);
//categories
$categoriesList = array();

foreach ($query as $value) {
$arr = parseCategory($value);
$categoriesList[$arr['id']] =$arr['name'];
}
//site
$siteList = \Utility::getSiteList(false);
//instrument

$instruments = $db->pselect(
"SELECT Test_name FROM test_names ORDER BY Test_name",
array()
);
$instrumentsList = toSelect($instruments, "Test_name", null);
//docFile
$docData = null;
if (isset($_GET['idDocFile'])) {
$idDocFile = $_GET['idDocFile'];
$docData = $db->pselectRow(
"SELECT " .
"record_id as id, " .
"PSCID as pscid, " .
"File_category as category," .
"visitLabel, " .
"Instrument as instrument, " .
"For_site as forSite, " .
"comments, " .
"File_Name as fileName, " .
"version " .
"FROM document_repository " .
" WHERE record_id = $idDocFile",
array()
);
}

$result = array(
'categories' => $categoriesList,
'sites' => $siteList,
'instruments' => $instrumentsList,
'docData' => $docData,
);

return $result;
}
/**
* Handler of parsing category
*
* @param string $value the value
*
* @return array
*/
function parseCategory($value): array
{
$id = $value['id'];
$depth = 0;
$DB = \Database::singleton();
$categoryName = $value['category_name'];
do {
if ($value['parent_id'] != 0) {
$depth += 1;
$parent_id = $value['parent_id'];
$query = "SELECT * FROM document_repository_categories".
" where id=$parent_id";
$value = $DB->pselectRow($query, array());
$categoryName = $value['category_name'] . ">" . $categoryName;
}
} while ($value['parent_id'] != 0);
return array(
"name" => $categoryName,
"id" => $id,
);
}

/**
* Utility function to convert data from database to a
* (select) dropdown friendly format
*
* @param array $options array of options
* @param string $item key
* @param string $item2 value
*
* @return array
*/
function toSelect($options, $item, $item2): array
{
$selectOptions = array();

$optionsValue = $item;
if (isset($item2)) {
$optionsValue = $item2;
}

foreach ($options as $key => $value) {
$selectOptions[$options[$key][$optionsValue]] = $options[$key][$item];
}

return $selectOptions;
}

Loading