Skip to content

Commit b6a5ba2

Browse files
authored
[document repository] prevent to add same category name under same parent directory (#7255)
Preventing to add same category name under same parent directory. The path to the category must be unique, but different categories can have subcategories with the same name. Resolves #7232
1 parent 6ddd785 commit b6a5ba2

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

modules/document_repository/jsx/categoryForm.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import PropTypes from 'prop-types';
22
import Loader from 'Loader';
3+
import swal from 'sweetalert2';
4+
35
/**
46
* Document Upload Form
57
*
@@ -120,16 +122,28 @@ class DocCategoryForm extends React.Component {
120122
credentials: 'same-origin',
121123
body: formObj,
122124
})
123-
.then((resp) => resp.json())
124-
.then(()=>{
125+
.then((resp) => {
126+
if (resp.ok) {
125127
this.props.refreshPage();
126128
this.fetchData();
127129
// refresh the upload page
128130
this.props.newCategoryState();
129131
this.setState({
130132
formData: {}, // reset form data after successful file upload
131133
});
132-
swal('Add Successful!', '', 'success');
134+
swal.fire('Category Successfully Added!', '', 'success');
135+
} else {
136+
resp.json().then((data) => {
137+
swal.fire('Could not add category!', data.error, 'error');
138+
}).catch((error) => {
139+
console.error(error);
140+
swal.fire(
141+
'Unknown Error!',
142+
'Please report the issue or contact your administrator.',
143+
'error'
144+
);
145+
});
146+
}
133147
});
134148
}
135149
/**

modules/document_repository/php/uploadcategory.class.inc

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ class UploadCategory extends \NDB_Page
5050
public function handle(ServerRequestInterface $request) : ResponseInterface
5151
{
5252
if ($request->getMethod() == "POST") {
53-
$this->uploadDocCategory($request);
54-
return (new \LORIS\Http\Response())
55-
->withHeader("Content-Type", "text/plain")
56-
->withStatus(200)
57-
->withBody(
58-
new \LORIS\Http\StringStream(
59-
json_encode("uploaded successfully")
60-
)
61-
);
53+
if ($this->uploadDocCategory($request)) {
54+
return (new \LORIS\Http\Response())
55+
->withHeader("Content-Type", "text/plain")
56+
->withStatus(200)
57+
->withBody(
58+
new \LORIS\Http\StringStream(
59+
json_encode("uploaded successfully")
60+
)
61+
);
62+
}
63+
return new \LORIS\Http\Response\JSON\Conflict(
64+
"Duplicate category name under same parent folder."
65+
);
6266
} else {
6367
return (new \LORIS\Http\Response())
6468
->withHeader("Content-Type", "text/plain")
@@ -71,16 +75,19 @@ class UploadCategory extends \NDB_Page
7175
*
7276
* @param ServerRequestInterface $request The incoming PSR7 request
7377
*
74-
* @return void
78+
* @return bool
7579
*/
76-
function uploadDocCategory($request): void
80+
function uploadDocCategory($request): bool
7781
{
7882
$DB = \Database::singleton();
7983
$req = $request->getParsedBody();
8084
$category_name = $req['categoryName']; // required
8185
$parent_id = isset($req['parentId']) ? $req['parentId'] : '0';
8286
$comments = isset($req['comments']) ? $req['comments'] : null;
83-
// todo check duplicate name category
87+
//check duplicate name category
88+
if ($this->existCategoryName($category_name, $parent_id)) {
89+
return false;
90+
}
8491
$DB->insert(
8592
"document_repository_categories",
8693
array(
@@ -89,5 +96,28 @@ class UploadCategory extends \NDB_Page
8996
"comments" => $comments,
9097
)
9198
);
99+
return true;
100+
}
101+
/**
102+
* Check the category name under the same parent folder in the database,
103+
* if exists return true
104+
*
105+
* @param string $categoryName file name
106+
* @param int $parent_id parent folder id
107+
*
108+
* @return bool
109+
*/
110+
function existCategoryName(string $categoryName, int $parent_id): bool
111+
{
112+
$DB = \NDB_Factory::singleton()->database();
113+
$categoryCount = $DB->pselectOne(
114+
"SELECT COUNT(*) FROM document_repository_categories
115+
WHERE category_name=:name and parent_id=:id ",
116+
array('name' => $categoryName, 'id' => $parent_id)
117+
);
118+
if ((int)$categoryCount > 0) {
119+
return true;
120+
}
121+
return false;
92122
}
93123
}

0 commit comments

Comments
 (0)