Skip to content

Commit 568e25c

Browse files
committed
Add import page
1 parent 25aea3e commit 568e25c

17 files changed

+572
-21
lines changed

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Autocompletion is available for fields, MongoDB & SQL keywords via `Ctrl` `Space
77
Screenshots
88
-----------
99

10-
![MongoDB PHP GUI](https://raw.githubusercontent.com/SamuelTS/MongoDB-PHP-GUI/master/docs/screenshots/mpg-database-query.png)
10+
![MongoDB PHP GUI](https://raw.githubusercontent.com/SamuelTS/MongoDB-PHP-GUI/master/docs/screenshots/new-mpg-database-query.png)
1111

12-
![MongoDB PHP GUI](https://raw.githubusercontent.com/SamuelTS/MongoDB-PHP-GUI/master/docs/screenshots/mpg-collection-indexes.png)
12+
![MongoDB PHP GUI](https://raw.githubusercontent.com/SamuelTS/MongoDB-PHP-GUI/master/docs/screenshots/new-mpg-collection-indexes.png)
1313

1414
Installation
1515
------------
-77.5 KB
Binary file not shown.
-98.7 KB
Binary file not shown.
82.3 KB
Loading
111 KB
Loading

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @var string
2020
*/
21-
define('MPG_APP_VERSION', '1.0.4');
21+
define('MPG_APP_VERSION', '1.0.5');
2222

2323
/**
2424
* Development mode?

routes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
DatabaseController::class . '@renderCreateViewAction'
4242
);
4343

44+
$router->get(
45+
MPG_SERVER_PATH . '/importDocuments',
46+
CollectionController::class . '@renderImportViewAction'
47+
);
48+
49+
$router->post(
50+
MPG_SERVER_PATH . '/importDocuments',
51+
CollectionController::class . '@renderImportViewAction'
52+
);
53+
4454
$router->get(
4555
MPG_SERVER_PATH . '/queryDatabase',
4656
DatabaseController::class . '@renderQueryViewAction'

src/Controllers/CollectionController.php

Lines changed: 127 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,85 @@
99

1010
class CollectionController extends Controller {
1111

12+
/**
13+
* @see https://docs.mongodb.com/php-library/v1.6/reference/method/MongoDBCollection-insertMany/
14+
*/
15+
public function importFile($documentsFilename, $databaseName, $collectionName) : int {
16+
17+
$documentsFileContents = @file_get_contents($documentsFilename);
18+
19+
if ( $documentsFileContents === false ) {
20+
throw new \Exception('Impossible to read the import file.');
21+
}
22+
23+
$documents = json_decode($documentsFileContents, JSON_OBJECT_AS_ARRAY);
24+
25+
if ( is_null($documents) ) {
26+
throw new \Exception('Import file is invalid... Malformed JSON?');
27+
}
28+
29+
foreach ($documents as &$document) {
30+
31+
if ( isset($document['_id'])
32+
&& preg_match(MongoDBHelper::MDB_OBJECT_ID_REGEX, $document['_id']) ) {
33+
$document['_id'] = new \MongoDB\BSON\ObjectId($document['_id']);
34+
}
35+
36+
array_walk_recursive($document, function(&$documentValue) {
37+
38+
if ( preg_match(MongoDBHelper::ISO_DATE_TIME_REGEX, $documentValue) ) {
39+
$documentValue = new \MongoDB\BSON\UTCDateTime(new \DateTime($documentValue));
40+
}
41+
42+
});
43+
44+
}
45+
46+
$collection = MongoDBHelper::getClient()->selectCollection(
47+
$databaseName, $collectionName
48+
);
49+
50+
$insertManyResult = $collection->insertMany($documents);
51+
52+
return $insertManyResult->getInsertedCount();
53+
54+
}
55+
56+
public function renderImportViewAction() : Response {
57+
58+
LoginController::ensureUserIsLogged();
59+
60+
$successMessage = '';
61+
$errorMessage = '';
62+
63+
if ( isset($_FILES['import']) && isset($_FILES['import']['tmp_name'])
64+
&& isset($_POST['database_name']) && isset($_POST['collection_name']) ) {
65+
66+
try {
67+
68+
$importedDocumentsCount = $this->importFile(
69+
$_FILES['import']['tmp_name'],
70+
$_POST['database_name'],
71+
$_POST['collection_name']
72+
);
73+
74+
$successMessage = $importedDocumentsCount . ' document(s) imported.';
75+
76+
} catch (\Throwable $th) {
77+
$errorMessage = $th->getMessage();
78+
}
79+
80+
}
81+
82+
return new Response(200, $this->renderView('collection.import', [
83+
'databaseNames' => DatabaseController::getDatabaseNames(),
84+
'maxFileSize' => ini_get('upload_max_filesize'),
85+
'successMessage' => $successMessage,
86+
'errorMessage' => $errorMessage
87+
]));
88+
89+
}
90+
1291
public function renderIndexesViewAction() : Response {
1392

1493
LoginController::ensureUserIsLogged();
@@ -36,10 +115,10 @@ public function insertOneAction() : Response {
36115
new \MongoDB\BSON\ObjectId($decodedRequestBody['document']['_id']);
37116
}
38117

39-
array_walk_recursive($decodedRequestBody['document'], function(&$insertValue) {
118+
array_walk_recursive($decodedRequestBody['document'], function(&$documentValue) {
40119

41-
if ( preg_match(MongoDBHelper::ISO_DATE_TIME_REGEX, $insertValue) ) {
42-
$insertValue = new \MongoDB\BSON\UTCDateTime(new \DateTime($insertValue));
120+
if ( preg_match(MongoDBHelper::ISO_DATE_TIME_REGEX, $documentValue) ) {
121+
$documentValue = new \MongoDB\BSON\UTCDateTime(new \DateTime($documentValue));
43122
}
44123

45124
});
@@ -161,15 +240,32 @@ public function findAction() : Response {
161240

162241
$document = $document->jsonSerialize();
163242

164-
array_walk_recursive($document, function(&$documentValue) {
243+
if ( property_exists($document, '_id')
244+
&& is_a($document->_id, '\MongoDB\BSON\ObjectId') ) {
245+
$document->_id = (string) $document->_id;
246+
}
165247

166-
if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
167-
$documentValue = (string) $documentValue;
168-
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
248+
foreach ($document as &$documentValue) {
249+
250+
if ( is_a($documentValue, '\MongoDB\Model\BSONDocument') ) {
251+
252+
$documentValue = $documentValue->jsonSerialize();
253+
254+
foreach ($documentValue as &$documentSubValue) {
255+
256+
if ( is_a($documentSubValue, '\MongoDB\BSON\UTCDateTime') ) {
257+
$documentSubValue = $documentSubValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
258+
}
259+
260+
// TODO: Support more nested documents.
261+
262+
}
263+
264+
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDateTime') ) {
169265
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
170266
}
171-
172-
});
267+
268+
}
173269

174270
}
175271

@@ -244,15 +340,32 @@ public function enumFieldsAction() : Response {
244340

245341
$document = $documents[0]->jsonSerialize();
246342

247-
array_walk_recursive($document, function(&$documentValue) {
343+
if ( property_exists($document, '_id')
344+
&& is_a($document->_id, '\MongoDB\BSON\ObjectId') ) {
345+
$document->_id = (string) $document->_id;
346+
}
347+
348+
foreach ($document as &$documentValue) {
349+
350+
if ( is_a($documentValue, '\MongoDB\Model\BSONDocument') ) {
351+
352+
$documentValue = $documentValue->jsonSerialize();
248353

249-
if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
250-
$documentValue = (string) $documentValue;
251-
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
354+
foreach ($documentValue as &$documentSubValue) {
355+
356+
if ( is_a($documentSubValue, '\MongoDB\BSON\UTCDateTime') ) {
357+
$documentSubValue = $documentSubValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
358+
}
359+
360+
// TODO: Support more nested documents.
361+
362+
}
363+
364+
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDateTime') ) {
252365
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
253366
}
254367

255-
});
368+
}
256369

257370
$array = json_decode(json_encode($document), JSON_OBJECT_AS_ARRAY);
258371

static/css/mpg.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@
112112

113113
}
114114

115+
#mpg-import-notice {
116+
117+
margin-top: 10px;
118+
119+
}
120+
121+
#mpg-import-file {
122+
123+
border: none;
124+
width: auto;
125+
126+
}
127+
128+
@media screen and (max-width: 576px) {
129+
130+
#mpg-import-button {
131+
132+
margin-top: 10px;
133+
134+
}
135+
136+
}
137+
115138
code {
116139

117140
font-size: 100%;

0 commit comments

Comments
 (0)