Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
[GIT-1] Updated reference links to support custom ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Surowiec committed Jun 8, 2010
1 parent 72009d2 commit 6e94009
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions mongodbadmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
* @param string $document
* @return string $preview
*/
function renderDocumentPreview($document)
function renderDocumentPreview($mongo, $document)
{
$document = prepareMongoDBDocumentForEdit($document);
$preview = linkDocumentReferences($document);
$preview = linkDocumentReferences($mongo, $document);
$preview = print_r($preview, true);
return $preview;
}
Expand All @@ -58,13 +58,26 @@ function renderDocumentPreview($document)
* @param array $document
* @return array $document
*/
function linkDocumentReferences($document)
function linkDocumentReferences($mongo, $document)
{
foreach ($document as $key => $value) {
if (is_array($value)) {
if (isset($value['$ref'])) {
$collection = $mongo->selectDB($_REQUEST['db'])->selectCollection($value['$ref']);
$id = $value['$id'];

$ref = $collection->findOne(array('_id' => new MongoId($value['$id'])));
if (!$ref) {
$ref = $collection->findOne(array('_id' => $value['$id']));
}

$document[$key]['$ref'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'">'.$document[$key]['$ref'].'</a>';
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'">'.$document[$key]['$id'].'</a>';

if ($ref['_id'] instanceof MongoId) {
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'">'.$document[$key]['$id'].'</a>';
} else {
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'&custom_id=1">'.$document[$key]['$id'].'</a>';
}
} else {
$document[$key] = linkDocumentReferences($value);
}
Expand Down Expand Up @@ -136,6 +149,21 @@ function prepareMongoDBDocumentForEdit($value)
return $prepared;
}

function findMongoDbDocument($id, $custom_id = false)
{
global $mongo;

$collection = $mongo->selectDB($_REQUEST['db'])->selectCollection($_REQUEST['collection']);

if (isset($_REQUEST['custom_id'])) {
$document =$collection->findOne(array('_id' => $_REQUEST['id']));
} else {
$document = $collection->findOne(array('_id' => new MongoId($_REQUEST['id'])));
}

return $document;
}

// Actions
try {
// DELETE DB
Expand Down Expand Up @@ -523,7 +551,7 @@ function prepareMongoDBDocumentForEdit($value)
$collection = $mongo->selectDB($_REQUEST['db'])->selectCollection($_REQUEST['collection']);

if (isset($_REQUEST['custom_id'])) {
$document =$collection->findOne(array('_id' => $_REQUEST['id']));
$document = $collection->findOne(array('_id' => $_REQUEST['id']));
} else {
$document = $collection->findOne(array('_id' => new MongoId($_REQUEST['id'])));
}
Expand All @@ -535,7 +563,7 @@ function prepareMongoDBDocumentForEdit($value)
<input type="hidden" name="<?php echo $k ?>" value="<?php echo $v ?>" />
<?php endforeach; ?>

<pre><code><?php echo renderDocumentPreview($document) ?></code></pre>
<pre><code><?php echo renderDocumentPreview($mongo, $document) ?></code></pre>

<?php $prepared = prepareMongoDBDocumentForEdit($document) ?>

Expand Down

0 comments on commit 6e94009

Please sign in to comment.