2017-08-09 17:53:25 +02:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2021-06-29 14:57:47 +02:00
/**
* @ property ObjectReference $ObjectReference
*/
2018-07-19 11:48:22 +02:00
class ObjectReferencesController extends AppController
{
2021-11-23 14:53:27 +01:00
public $components = array ( 'RequestHandler' , 'Session' );
2017-08-09 17:53:25 +02:00
2018-07-19 11:48:22 +02:00
public $paginate = array (
'limit' => 20 ,
'order' => array (
'ObjectReference.id' => 'desc'
),
);
2017-08-09 17:53:25 +02:00
2018-07-19 11:48:22 +02:00
public function add ( $objectId = false )
{
if ( empty ( $objectId )) {
if ( $this -> request -> is ( 'post' ) && ! empty ( $this -> request -> data [ 'object_uuid' ])) {
$objectId = $this -> request -> data [ 'object_uuid' ];
}
}
if ( empty ( $objectId )) {
throw new MethodNotAllowedException ( 'No object defined.' );
}
if ( Validation :: uuid ( $objectId )) {
2021-06-29 14:57:47 +02:00
$conditions = [ 'Object.uuid' => $objectId ];
} else {
$conditions = [ 'Object.id' => $objectId ];
2018-07-19 11:48:22 +02:00
}
2021-06-29 14:57:47 +02:00
$conditions [ 'Object.deleted' ] = 0 ;
2018-07-19 11:48:22 +02:00
$object = $this -> ObjectReference -> Object -> find ( 'first' , array (
2021-06-29 14:57:47 +02:00
'conditions' => $conditions ,
2018-07-19 11:48:22 +02:00
'recursive' => - 1 ,
'contain' => array (
'Event' => array (
2021-02-16 15:02:34 +01:00
'fields' => array ( 'Event.id' , 'Event.orgc_id' , 'Event.user_id' , 'Event.extends_uuid' )
2018-07-19 11:48:22 +02:00
)
)
));
2020-07-25 18:38:41 +02:00
if ( empty ( $object ) || ! $this -> __canModifyEvent ( $object )) {
throw new NotFoundException ( 'Invalid object.' );
2018-07-19 11:48:22 +02:00
}
2021-06-29 14:57:47 +02:00
$this -> set ( 'objectId' , $object [ 'Object' ][ 'id' ]);
2018-07-19 11:48:22 +02:00
if ( $this -> request -> is ( 'post' )) {
if ( ! isset ( $this -> request -> data [ 'ObjectReference' ])) {
$this -> request -> data [ 'ObjectReference' ] = $this -> request -> data ;
}
2021-06-29 14:57:47 +02:00
list ( $referenced_id , $referenced_uuid , $referenced_type ) = $this -> ObjectReference -> getReferencedInfo ( trim ( $this -> request -> data [ 'ObjectReference' ][ 'referenced_uuid' ]), $object , true , $this -> Auth -> user ());
2018-07-19 11:48:22 +02:00
$relationship_type = empty ( $this -> request -> data [ 'ObjectReference' ][ 'relationship_type' ]) ? '' : $this -> request -> data [ 'ObjectReference' ][ 'relationship_type' ];
if ( ! empty ( $this -> request -> data [ 'ObjectReference' ][ 'relationship_type_select' ]) && $this -> request -> data [ 'ObjectReference' ][ 'relationship_type_select' ] !== 'custom' ) {
$relationship_type = $this -> request -> data [ 'ObjectReference' ][ 'relationship_type_select' ];
}
$data = array (
'referenced_id' => $referenced_id ,
'referenced_uuid' => $referenced_uuid ,
'relationship_type' => $relationship_type ,
'comment' => ! empty ( $this -> request -> data [ 'ObjectReference' ][ 'comment' ]) ? $this -> request -> data [ 'ObjectReference' ][ 'comment' ] : '' ,
'event_id' => $object [ 'Event' ][ 'id' ],
'object_uuid' => $object [ 'Object' ][ 'uuid' ],
2021-02-18 15:18:40 +01:00
'source_uuid' => $object [ 'Object' ][ 'uuid' ],
2021-06-29 14:57:47 +02:00
'object_id' => $object [ 'Object' ][ 'id' ],
2018-07-19 11:48:22 +02:00
'referenced_type' => $referenced_type ,
'uuid' => CakeText :: uuid ()
);
2019-08-05 16:49:25 +02:00
$object_uuid = $object [ 'Object' ][ 'uuid' ];
2018-07-19 11:48:22 +02:00
$this -> ObjectReference -> create ();
$result = $this -> ObjectReference -> save ( array ( 'ObjectReference' => $data ));
if ( $result ) {
2021-10-18 13:20:12 +02:00
$this -> ObjectReference -> updateTimestamps ( $data );
2018-07-19 11:48:22 +02:00
if ( $this -> _isRest ()) {
$object = $this -> ObjectReference -> find ( " first " , array (
'recursive' => - 1 ,
'conditions' => array ( 'ObjectReference.id' => $this -> ObjectReference -> id )
));
2019-08-05 16:49:25 +02:00
$object [ 'ObjectReference' ][ 'object_uuid' ] = $object_uuid ;
2018-07-19 11:48:22 +02:00
return $this -> RestResponse -> viewData ( $object , $this -> response -> type ());
} elseif ( $this -> request -> is ( 'ajax' )) {
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => true , 'success' => 'Object reference added.' )), 'status' => 200 , 'type' => 'json' ));
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveFailResponse ( 'ObjectReferences' , 'add' , false , $this -> ObjectReference -> validationErrors , $this -> response -> type ());
} elseif ( $this -> request -> is ( 'ajax' )) {
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => 'Object reference could not be added.' )), 'status' => 200 , 'type' => 'json' ));
}
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> describe ( 'ObjectReferences' , 'add' , false , $this -> response -> type ());
} else {
2021-02-17 13:43:17 +01:00
$events = $this -> ObjectReference -> Object -> Event -> find ( 'all' , array (
'conditions' => array (
'OR' => array (
'Event.id' => $object [ 'Event' ][ 'id' ],
'AND' => array (
'Event.uuid' => $object [ 'Event' ][ 'extends_uuid' ],
$this -> ObjectReference -> Object -> Event -> createEventConditions ( $this -> Auth -> user ())
)
),
),
2018-07-19 11:48:22 +02:00
'recursive' => - 1 ,
'fields' => array ( 'Event.id' ),
'contain' => array (
'Attribute' => array (
'conditions' => array ( 'Attribute.deleted' => 0 , 'Attribute.object_id' => 0 ),
'fields' => array ( 'Attribute.id' , 'Attribute.uuid' , 'Attribute.type' , 'Attribute.category' , 'Attribute.value' , 'Attribute.to_ids' )
),
'Object' => array (
2021-06-29 14:57:47 +02:00
'conditions' => array ( 'NOT' => array ( 'Object.id' => $object [ 'Object' ][ 'id' ]), 'Object.deleted' => 0 ),
2018-07-19 11:48:22 +02:00
'fields' => array ( 'Object.id' , 'Object.uuid' , 'Object.name' , 'Object.meta-category' ),
'Attribute' => array (
'conditions' => array ( 'Attribute.deleted' => 0 ),
'fields' => array ( 'Attribute.id' , 'Attribute.uuid' , 'Attribute.type' , 'Attribute.category' , 'Attribute.value' , 'Attribute.to_ids' )
)
)
)
));
2021-02-17 13:43:17 +01:00
if ( ! empty ( $events )) {
$event = $events [ 0 ];
}
for ( $i = 1 ; $i < count ( $events ); $i ++ ) {
$event [ 'Attribute' ] = array_merge ( $event [ 'Attribute' ], $events [ $i ][ 'Attribute' ]);
$event [ 'Object' ] = array_merge ( $event [ 'Object' ], $events [ $i ][ 'Object' ]);
}
2018-07-19 11:48:22 +02:00
$toRearrange = array ( 'Attribute' , 'Object' );
foreach ( $toRearrange as $d ) {
if ( ! empty ( $event [ $d ])) {
$temp = array ();
foreach ( $event [ $d ] as $data ) {
$temp [ $data [ 'uuid' ]] = $data ;
}
$event [ $d ] = $temp ;
}
}
$this -> loadModel ( 'ObjectRelationship' );
2021-06-29 14:57:47 +02:00
$relationships = $this -> ObjectRelationship -> find ( 'column' , array (
'recursive' => - 1 ,
'fields' => [ 'name' ],
2018-07-19 11:48:22 +02:00
));
2021-06-29 14:57:47 +02:00
$relationships = array_combine ( $relationships , $relationships );
2018-07-19 11:48:22 +02:00
$relationships [ 'custom' ] = 'custom' ;
2018-12-22 09:54:12 +01:00
ksort ( $relationships );
2018-07-19 11:48:22 +02:00
$this -> set ( 'relationships' , $relationships );
$this -> set ( 'event' , $event );
2021-06-29 14:57:47 +02:00
$this -> set ( 'objectId' , $object [ 'Object' ][ 'id' ]);
2022-04-28 14:51:21 +02:00
$this -> layout = false ;
2018-07-19 11:48:22 +02:00
$this -> render ( 'ajax/add' );
}
}
}
2017-08-09 17:53:25 +02:00
2018-07-19 11:48:22 +02:00
public function delete ( $id , $hard = false )
{
$objectReference = $this -> ObjectReference -> find ( 'first' , array (
2021-06-30 09:35:01 +02:00
'conditions' => Validation :: uuid ( $id ) ? [ 'ObjectReference.uuid' => $id ] : [ 'ObjectReference.id' => $id ],
2018-07-19 11:48:22 +02:00
'recursive' => - 1 ,
'contain' => array ( 'Object' => array ( 'Event' ))
));
if ( empty ( $objectReference )) {
2020-08-05 10:12:53 +02:00
throw new NotFoundException ( __ ( 'Invalid object reference.' ));
2018-07-19 11:48:22 +02:00
}
2020-08-05 10:12:53 +02:00
if ( ! $this -> __canModifyEvent ( $objectReference [ 'Object' ])) {
throw new ForbiddenException ( __ ( 'Invalid object reference.' ));
2018-07-19 11:48:22 +02:00
}
2021-06-30 09:35:01 +02:00
$id = $objectReference [ 'ObjectReference' ][ 'id' ];
2018-07-19 11:48:22 +02:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' ) || $this -> request -> is ( 'delete' )) {
$result = $this -> ObjectReference -> smartDelete ( $objectReference [ 'ObjectReference' ][ 'id' ], $hard );
if ( $result === true ) {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'ObjectReferences' , 'delete' , $id , $this -> response -> type ());
} else {
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => true , 'success' => 'Object reference deleted.' )), 'status' => 200 , 'type' => 'json' ));
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveFailResponse ( 'ObjectReferences' , 'delete' , $id , $result , $this -> response -> type ());
} else {
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => 'Object reference was not deleted.' )), 'status' => 200 , 'type' => 'json' ));
}
}
} else {
if ( ! $this -> request -> is ( 'ajax' )) {
throw new MethodNotAllowedException ( 'This action is only accessible via POST request.' );
}
$this -> set ( 'hard' , $hard );
$this -> set ( 'id' , $id );
$this -> set ( 'event_id' , $objectReference [ 'Object' ][ 'Event' ][ 'id' ]);
$this -> render ( 'ajax/delete' );
}
}
2017-08-09 17:53:25 +02:00
2018-07-19 11:48:22 +02:00
public function view ( $id )
{
2021-02-18 15:42:00 +01:00
$objectReference = $this -> ObjectReference -> find ( 'first' , array (
2021-06-30 09:35:01 +02:00
'conditions' => Validation :: uuid ( $id ) ? [ 'ObjectReference.uuid' => $id ] : [ 'ObjectReference.id' => $id ],
2021-02-18 15:42:00 +01:00
'recursive' => - 1 ,
));
if ( empty ( $objectReference )) {
throw new NotFoundException ( __ ( 'Invalid object reference.' ));
}
2021-06-30 09:35:01 +02:00
// Check if user can view object that contains this reference
2021-11-01 08:44:44 +01:00
$object = $this -> ObjectReference -> Object -> fetchObjectSimple ( $this -> Auth -> user (), [
'conditions' => [ 'Object.id' => $objectReference [ 'ObjectReference' ][ 'object_id' ]],
2021-06-30 09:35:01 +02:00
]);
2021-11-01 08:44:44 +01:00
if ( empty ( $object )) {
2021-06-30 09:35:01 +02:00
throw new NotFoundException ( __ ( 'Invalid object reference.' ));
2021-02-18 15:42:00 +01:00
}
2021-06-30 09:35:01 +02:00
return $this -> RestResponse -> viewData ( $objectReference , 'json' );
2018-07-19 11:48:22 +02:00
}
2017-08-09 17:53:25 +02:00
}