Skip to content

Commit 3219678

Browse files
committed
Add inline update
1 parent 2e4c58a commit 3219678

File tree

9 files changed

+317
-21
lines changed

9 files changed

+317
-21
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Free MongoDB GUI powered by PHP
22

3-
Visually administrate your MongoDB database. Create, read and delete operations are supported.<br>
3+
Visually administrate your MongoDB database. Create, read, update and delete your documents.<br>
44
Autocompletion is available for collection fields and MongoDB keywords via `Ctrl` + `Space` keys.
55

66
Screenshots

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @var string
2222
*/
23-
define('MPG_APP_VERSION', '0.9.3');
23+
define('MPG_APP_VERSION', '0.9.4');
2424

2525
/**
2626
* Development mode?

routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
CollectionController::class . '@findAction'
4747
);
4848

49+
$router->post(
50+
'/ajax/database/{databaseName}/collection/{collectionName}/updateOne',
51+
CollectionController::class . '@updateOneAction'
52+
);
53+
4954
$router->get(
5055
'/ajax/database/{databaseName}/collection/{collectionName}/enumFields',
5156
CollectionController::class . '@enumFieldsAction'

src/Controllers/CollectionController.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,45 @@ public function findAction($databaseName, $collectionName) : Response {
147147

148148
}
149149

150+
/**
151+
* @see https://docs.mongodb.com/php-library/v1.6/reference/method/MongoDBCollection-updateOne/index.html
152+
*/
153+
public function updateOneAction($databaseName, $collectionName) : Response {
154+
155+
global $mongoDBClient;
156+
157+
$collection = $mongoDBClient->$databaseName->$collectionName;
158+
159+
$requestBody = $this->getRequestBody();
160+
161+
if ( is_null($requestBody) ) {
162+
return new Response(400, 'Request body is missing.');
163+
}
164+
165+
$decodedRequestBody = json_decode($requestBody, JSON_OBJECT_AS_ARRAY);
166+
167+
if ( is_null($decodedRequestBody) ) {
168+
return new Response(400, 'Request body is invalid.');
169+
}
170+
171+
if ( isset($decodedRequestBody['filter']['_id'])
172+
&& is_string($decodedRequestBody['filter']['_id']) ) {
173+
$decodedRequestBody['filter']['_id'] =
174+
new \MongoDB\BSON\ObjectId($decodedRequestBody['filter']['_id']);
175+
}
176+
177+
$updateResult = $collection->updateOne(
178+
$decodedRequestBody['filter'], $decodedRequestBody['update']
179+
);
180+
181+
return new Response(
182+
200,
183+
json_encode($updateResult->getModifiedCount()),
184+
['Content-Type' => 'application/json']
185+
);
186+
187+
}
188+
150189
public function enumFieldsAction($databaseName, $collectionName) : Response {
151190

152191
global $mongoDBClient;

static/css/mpg.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ button, input {
4343

4444
}
4545

46+
.json-container .json-value[data-document-field-is-updatable="true"]:hover {
47+
48+
cursor: url("/static/images/edit-icon-32x32.png") 16 16, text;
49+
outline: 1.5px solid #ddd;
50+
51+
}
52+
4653
#mpg-databases-list, #mpg-collections-list {
4754

4855
list-style-type: none;

static/images/edit-icon-32x32.png

512 Bytes
Loading

static/js/jsonview.bundle.js renamed to static/js/jsonview.bundle.mod.js

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ var JsonView = (function (exports) {
2828
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2929
var key = params.key,
3030
value = params.value,
31-
type = params.type;
32-
return "\n <div class=\"line\">\n <div class=\"empty-icon\"></div>\n <div class=\"json-key\">".concat(key, "</div>\n <div class=\"json-separator\">:</div>\n <div class=\"json-value json-").concat(type, "\">").concat(value, "</div>\n </div>\n ");
31+
type = params.type,
32+
33+
// XXX Modification made for MongoDB PHP GUI.
34+
documentFieldIsUpdatable = params.documentFieldIsUpdatable,
35+
documentId = params.documentId,
36+
documentFieldName = params.documentFieldName;
37+
38+
// XXX Modification made for MongoDB PHP GUI.
39+
return "\n <div class=\"line\">\n <div class=\"empty-icon\"></div>\n <div class=\"json-key\">".concat(key, "</div>\n <div class=\"json-separator\">:</div>\n <div data-document-id=\"" + documentId + "\" data-document-field-is-updatable=\"" + documentFieldIsUpdatable + "\" data-document-field-name=\"" + documentFieldName + "\" data-document-field-type=\"" + type + "\" class=\"json-value json-").concat(type, "\">").concat(value, "</div>\n </div>\n ");
3340
}
3441

3542
function hideNodeChildren(node) {
@@ -90,6 +97,77 @@ var JsonView = (function (exports) {
9097
return el;
9198
}
9299

100+
// XXX Modification made for MongoDB PHP GUI.
101+
function getDocFieldFromNode(node) {
102+
103+
var docFieldArray = [];
104+
105+
docFieldArray.push(node.key);
106+
107+
switch (node.depth) {
108+
109+
case 2:
110+
if ( node.parent.depth >= 2 ) {
111+
docFieldArray.push(node.parent.key);
112+
}
113+
if ( node.parent.parent.depth >= 2 ) {
114+
docFieldArray.push(node.parent.parent.key);
115+
}
116+
break;
117+
118+
case 3:
119+
if ( node.parent.depth >= 2 ) {
120+
docFieldArray.push(node.parent.key);
121+
}
122+
if ( node.parent.parent.depth >= 2 ) {
123+
docFieldArray.push(node.parent.parent.key);
124+
}
125+
if ( node.parent.parent.parent.depth >= 2 ) {
126+
docFieldArray.push(node.parent.parent.parent.key);
127+
}
128+
break;
129+
130+
case 4:
131+
if ( node.parent.depth >= 2 ) {
132+
docFieldArray.push(node.parent.key);
133+
}
134+
if ( node.parent.parent.depth >= 2 ) {
135+
docFieldArray.push(node.parent.parent.key);
136+
}
137+
if ( node.parent.parent.parent.depth >= 2 ) {
138+
docFieldArray.push(node.parent.parent.parent.key);
139+
}
140+
if ( node.parent.parent.parent.parent.depth >= 2 ) {
141+
docFieldArray.push(node.parent.parent.parent.parent.key);
142+
}
143+
break;
144+
145+
case 5:
146+
if ( node.parent.depth >= 2 ) {
147+
docFieldArray.push(node.parent.key);
148+
}
149+
if ( node.parent.parent.depth >= 2 ) {
150+
docFieldArray.push(node.parent.parent.key);
151+
}
152+
if ( node.parent.parent.parent.depth >= 2 ) {
153+
docFieldArray.push(node.parent.parent.parent.key);
154+
}
155+
if ( node.parent.parent.parent.parent.depth >= 2 ) {
156+
docFieldArray.push(node.parent.parent.parent.parent.key);
157+
}
158+
if ( node.parent.parent.parent.parent.parent.depth >= 2 ) {
159+
docFieldArray.push(node.parent.parent.parent.parent.parent.key);
160+
}
161+
break;
162+
163+
}
164+
165+
docFieldArray.reverse();
166+
167+
return docFieldArray.join('.');
168+
169+
}
170+
93171
function createNodeElement(node) {
94172
var el = document.createElement('div');
95173

@@ -110,10 +188,27 @@ var JsonView = (function (exports) {
110188
toggleNode(node);
111189
});
112190
} else {
191+
192+
// XXX Modification made for MongoDB PHP GUI.
193+
if ( node.key === '$oid' ) {
194+
MPG.documentId = node.value;
195+
}
196+
if ( node.depth >= 2 && node.depth <= 5 && node.key !== '$oid' ) {
197+
var documentFieldIsUpdatable = true;
198+
} else {
199+
var documentFieldIsUpdatable = false;
200+
}
201+
113202
el.innerHTML = notExpandedTemplate({
114203
key: node.key,
115204
value: node.value,
116-
type: _typeof(node.value)
205+
type: _typeof(node.value),
206+
207+
// XXX Modification made for MongoDB PHP GUI.
208+
documentFieldIsUpdatable: ( documentFieldIsUpdatable ) ? 'true' : 'false',
209+
documentId: MPG.documentId,
210+
documentFieldName: getDocFieldFromNode(node)
211+
117212
});
118213
}
119214

0 commit comments

Comments
 (0)