Skip to content

Commit

Permalink
define bidirectional relation is added.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadi committed Oct 6, 2017
1 parent c588a44 commit a5bfc98
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions src/SDP/Views/AssetRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
Pluf::loadFunction('Pluf_Shortcuts_GetObjectOr404');

/**
* AssetRelation views
*
* @author hadi<[email protected]>
*/
class SDP_Views_AssetRelation
{

/**
* Creates new instance of a asset relation.
* If bidirectional param in request is true it create two asset relation.
*
* @param Pluf_HTTP_Request $request
* @param array $match
* @param array $p
* @return Pluf_HTTP_Response
*/
public function create($request, $match, $p)
{
// check start
$startAsset = Pluf_Shortcuts_GetObjectOr404('SDP_Asset', $request->REQUEST['start']);
// check end
$endAsset = Pluf_Shortcuts_GetObjectOr404('SDP_Asset', $request->REQUEST['end']);
// create relation
$plufService = new Pluf_Views();
$exception = null;
try {
$resultObj = $plufService->createObject($request, $match, $p);
} catch (Exception $e) {
if ($e->getStatus() === 400 && strpos($e->getMessage(), 'Duplicate entry') === 0) {
$exception = $e;
} else {
throw $e;
}
}
// create reverse relation if relation should be bidirectional
if (array_key_exists('bidirectional', $request->REQUEST) && $request->REQUEST['bidirectional'] == 'true') {
// swap start and end
$start = $request->REQUEST['start'];
$request->REQUEST['start'] = $request->REQUEST['end'];
$request->REQUEST['end'] = $start;
// create reverse relation
try {
$plufService->createObject($request, $match, $p);
} catch (Exception $e) {
// do nothing
}
}
if($exception == null)
return new Pluf_HTTP_Response_Json($resultObj);
throw $exception;
}
}
4 changes: 2 additions & 2 deletions src/SDP/urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@
),
array(
'regex' => '#^/assetrelation/new$#',
'model' => 'Pluf_Views',
'method' => 'createObject',
'model' => 'SDP_Views_AssetRelation',
'method' => 'create',
'http-method' => 'POST',
'params' => array(
'model' => 'SDP_AssetRelation'
Expand Down

0 comments on commit a5bfc98

Please sign in to comment.