2020-05-29 13:41:58 +02:00
< ? php
namespace App\Controller ;
use App\Controller\AppController ;
use Cake\Utility\Hash ;
use Cake\Utility\Text ;
use \Cake\Database\Expression\QueryExpression ;
use Cake\Http\Exception\NotFoundException ;
use Cake\Http\Exception\MethodNotAllowedException ;
use Cake\Http\Exception\ForbiddenException ;
use Cake\Error\Debugger ;
class AlignmentsController extends AppController
{
public function index ( $organisation_id = null )
{
$query = $this -> Alignments -> find ();
if ( ! empty ( $organisation_id )) {
$query -> where ([ 'organisation_id' => $organisation_id ]);
}
2020-06-19 00:37:56 +02:00
if ( $this -> ParamHandler -> isRest ()) {
2020-05-29 13:41:58 +02:00
$alignments = $query -> all ();
return $this -> RestResponse -> viewData ( $alignments , 'json' );
} else {
2022-09-19 02:17:36 +02:00
$this -> paginate [ 'contain' ] = [ 'Individuals' , 'Organisations' ];
2022-08-17 14:00:38 +02:00
$alignments = $this -> paginate ( $query );
2020-05-29 13:41:58 +02:00
$this -> set ( 'data' , $alignments );
$this -> set ( 'metaGroup' , 'ContactDB' );
}
}
public function view ( $id )
{
if ( empty ( $id )) {
throw new NotFoundException ( __ ( 'Invalid alignment.' ));
}
$individual = $this -> Alignments -> get ( $id );
2021-01-11 16:28:07 +01:00
if ( $this -> ParamHandler -> isRest () || $this -> ParamHandler -> isAjax ()) {
2020-05-29 13:41:58 +02:00
return $this -> RestResponse -> viewData ( $individual , 'json' );
}
$this -> set ( 'metaGroup' , 'ContactDB' );
$this -> set ( 'alignment' , $individual );
}
public function delete ( $id )
{
if ( empty ( $id )) {
throw new NotFoundException ( __ ( 'Invalid alignment.' ));
}
2021-01-11 16:28:07 +01:00
$alignment = $this -> Alignments -> get ( $id );
2020-05-29 13:41:58 +02:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'delete' )) {
2021-01-11 16:28:07 +01:00
if ( $this -> Alignments -> delete ( $alignment )) {
$message = __ ( 'Alignments deleted.' );
if ( $this -> ParamHandler -> isRest () || $this -> ParamHandler -> isAjax ()) {
2020-05-29 13:41:58 +02:00
return $this -> RestResponse -> saveSuccessResponse ( 'Alignments' , 'delete' , $id , 'json' , $message );
} else {
$this -> Flash -> success ( $message );
return $this -> redirect ( $this -> referer ());
}
}
}
$this -> set ( 'metaGroup' , 'ContactDB' );
$this -> set ( 'scope' , 'alignments' );
2021-01-11 16:28:07 +01:00
$this -> set ( 'id' , $alignment [ 'id' ]);
$this -> set ( 'alignment' , $alignment );
2020-05-29 13:41:58 +02:00
$this -> viewBuilder () -> setLayout ( 'ajax' );
$this -> render ( '/genericTemplates/delete' );
}
public function add ( $scope , $source_id )
{
if ( empty ( $scope ) || empty ( $source_id )) {
throw new NotAcceptableException ( __ ( 'Invalid input. scope and source_id expected as URL parameters in the format /alignments/add/[scope]/[source_id].' ));
}
$this -> loadModel ( 'Individuals' );
$this -> loadModel ( 'Organisations' );
$alignment = $this -> Alignments -> newEmptyEntity ();
if ( $this -> request -> is ( 'post' )) {
$this -> Alignments -> patchEntity ( $alignment , $this -> request -> getData ());
if ( $scope === 'individuals' ) {
$alignment [ 'individual_id' ] = $source_id ;
} else {
$alignment [ 'organisation_id' ] = $source_id ;
}
2021-01-11 16:28:07 +01:00
$alignment = $this -> Alignments -> save ( $alignment );
if ( $alignment ) {
2020-05-29 13:41:58 +02:00
$message = __ ( 'Alignment added.' );
2020-06-19 00:37:56 +02:00
if ( $this -> ParamHandler -> isRest ()) {
2020-05-29 13:41:58 +02:00
return $this -> RestResponse -> viewData ( $alignment , 'json' );
2021-01-11 16:28:07 +01:00
} else if ( $this -> ParamHandler -> isAjax ()) {
return $this -> RestResponse -> ajaxSuccessResponse ( 'Alignment' , 'add' , $alignment , $message );
2020-05-29 13:41:58 +02:00
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( $this -> referer ());
}
} else {
$message = __ ( 'Alignment could not be added.' );
2021-01-11 16:28:07 +01:00
if ( $this -> ParamHandler -> isRest () || $this -> ParamHandler -> isAjax ()) {
2020-05-29 13:41:58 +02:00
return $this -> RestResponse -> saveFailResponse ( 'Individuals' , 'addAlignment' , false , $message );
} else {
$this -> Flash -> error ( $message );
//$this->redirect($this->referer());
}
}
}
if ( $scope === 'organisations' ) {
2023-01-17 09:25:11 +01:00
$individuals = $this -> Individuals -> find ( 'list' , [ 'valueField' => 'email' ]) -> toArray ();
2020-05-29 13:41:58 +02:00
$this -> set ( 'individuals' , $individuals );
$organisation = $this -> Organisations -> find () -> where ([ 'id' => $source_id ]) -> first ();
if ( empty ( $organisation )) {
throw new NotFoundException ( __ ( 'Invalid organisation' ));
}
$this -> set ( compact ( 'organisation' ));
} else {
2023-01-17 09:25:11 +01:00
$organisations = $this -> Organisations -> find ( 'list' , [ 'valueField' => 'name' ]) -> toArray ();
2020-05-29 13:41:58 +02:00
$this -> set ( 'organisations' , $organisations );
$individual = $this -> Individuals -> find () -> where ([ 'id' => $source_id ]) -> first ();
if ( empty ( $individual )) {
throw new NotFoundException ( __ ( 'Invalid individual' ));
}
$this -> set ( compact ( 'individual' ));
}
$this -> set ( compact ( 'alignment' ));
$this -> set ( 'scope' , $scope );
$this -> set ( 'source_id' , $source_id );
$this -> set ( 'metaGroup' , 'ContactDB' );
}
}