|
9 | 9 |
|
10 | 10 | class CollectionController extends Controller { |
11 | 11 |
|
| 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 | + |
12 | 91 | public function renderIndexesViewAction() : Response { |
13 | 92 |
|
14 | 93 | LoginController::ensureUserIsLogged(); |
@@ -36,10 +115,10 @@ public function insertOneAction() : Response { |
36 | 115 | new \MongoDB\BSON\ObjectId($decodedRequestBody['document']['_id']); |
37 | 116 | } |
38 | 117 |
|
39 | | - array_walk_recursive($decodedRequestBody['document'], function(&$insertValue) { |
| 118 | + array_walk_recursive($decodedRequestBody['document'], function(&$documentValue) { |
40 | 119 |
|
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)); |
43 | 122 | } |
44 | 123 |
|
45 | 124 | }); |
@@ -161,15 +240,32 @@ public function findAction() : Response { |
161 | 240 |
|
162 | 241 | $document = $document->jsonSerialize(); |
163 | 242 |
|
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 | + } |
165 | 247 |
|
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') ) { |
169 | 265 | $documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z'); |
170 | 266 | } |
171 | | - |
172 | | - }); |
| 267 | + |
| 268 | + } |
173 | 269 |
|
174 | 270 | } |
175 | 271 |
|
@@ -244,15 +340,32 @@ public function enumFieldsAction() : Response { |
244 | 340 |
|
245 | 341 | $document = $documents[0]->jsonSerialize(); |
246 | 342 |
|
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(); |
248 | 353 |
|
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') ) { |
252 | 365 | $documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z'); |
253 | 366 | } |
254 | 367 |
|
255 | | - }); |
| 368 | + } |
256 | 369 |
|
257 | 370 | $array = json_decode(json_encode($document), JSON_OBJECT_AS_ARRAY); |
258 | 371 |
|
|
0 commit comments