2015-02-23 11:33:38 +01:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2020-10-09 18:44:18 +02:00
/**
* @ property Organisation $Organisation
*/
2018-07-19 11:48:22 +02:00
class OrganisationsController extends AppController
{
public $components = array ( 'Session' , 'RequestHandler' );
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function beforeFilter ()
{
parent :: beforeFilter ();
if ( ! empty ( $this -> request -> params [ 'admin' ]) && ! $this -> _isSiteAdmin ()) {
$this -> redirect ( '/' );
}
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public $paginate = array (
'limit' => 60 ,
2019-02-10 13:08:12 +01:00
'maxLimit' => 9999 , // LATER we will bump here on a problem once we have more than 9999 events <- no we won't, this is the max a user van view/page.
2018-07-05 11:09:13 +02:00
'order' => 'LOWER(Organisation.name)'
2018-07-19 11:48:22 +02:00
//'order' => array(
2019-02-10 13:08:12 +01:00
// 'Organisation.name' => 'ASC'
2018-07-19 11:48:22 +02:00
//),
);
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function index ()
{
if ( ! $this -> Auth -> user ( 'Role' )[ 'perm_sharing_group' ] && Configure :: read ( 'Security.hide_organisation_index_from_users' )) {
2019-03-21 15:38:40 +01:00
throw new MethodNotAllowedException ( __ ( 'This feature is disabled on this instance for normal users.' ));
2018-07-19 11:48:22 +02:00
}
$conditions = array ();
// We can either index all of the organisations existing on this instance (default)
// or we can pass the 'external' keyword in the URL to look at the added external organisations
$scope = isset ( $this -> passedArgs [ 'scope' ]) ? $this -> passedArgs [ 'scope' ] : 'local' ;
if ( $scope !== 'all' ) {
$conditions [ 'AND' ][] = array ( 'Organisation.local' => $scope === 'external' ? 0 : 1 );
}
$passedArgs = $this -> passedArgs ;
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
if ( isset ( $this -> request -> data [ 'searchall' ])) {
$searchall = $this -> request -> data [ 'searchall' ];
} elseif ( isset ( $this -> passedArgs [ 'all' ])) {
$searchall = $this -> passedArgs [ 'all' ];
} elseif ( isset ( $this -> passedArgs [ 'searchall' ])) {
$searchall = $this -> passedArgs [ 'searchall' ];
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
if ( isset ( $searchall ) && ! empty ( $searchall )) {
$passedArgs [ 'searchall' ] = $searchall ;
$allSearchFields = array ( 'name' , 'description' , 'nationality' , 'sector' , 'type' , 'contacts' , 'restricted_to_domain' , 'uuid' );
foreach ( $allSearchFields as $field ) {
$conditions [ 'OR' ][] = array ( 'LOWER(Organisation.' . $field . ') LIKE' => '%' . strtolower ( $passedArgs [ 'searchall' ]) . '%' );
}
}
$this -> set ( 'passedArgs' , json_encode ( $passedArgs ));
$this -> paginate [ 'conditions' ] = $conditions ;
$usersPerOrg = $this -> User -> getMembersCount ();
if ( $this -> _isRest ()) {
unset ( $this -> paginate [ 'limit' ]);
$orgs = $this -> Organisation -> find ( 'all' , $this -> paginate );
} else {
if ( isset ( $this -> params [ 'named' ][ 'viewall' ]) && $this -> params [ 'named' ][ 'viewall' ]) {
$orgCount = $this -> Organisation -> find ( 'count' );
$this -> paginate [ 'limit' ] = $orgCount ;
}
$this -> set ( 'viewall' , isset ( $this -> params [ 'named' ][ 'viewall' ]) ? $this -> params [ 'named' ][ 'viewall' ] : false );
$orgs = $this -> paginate ();
}
$this -> loadModel ( 'User' );
$org_creator_ids = array ();
foreach ( $orgs as $k => $org ) {
if ( isset ( $usersPerOrg [ $org [ 'Organisation' ][ 'id' ]])) {
$orgs [ $k ][ 'Organisation' ][ 'user_count' ] = $usersPerOrg [ $org [ 'Organisation' ][ 'id' ]];
}
if ( $this -> _isSiteAdmin ()) {
if ( ! in_array ( $org [ 'Organisation' ][ 'created_by' ], array_keys ( $org_creator_ids ))) {
$email = $this -> User -> find ( 'first' , array ( 'recursive' => - 1 , 'fields' => array ( 'id' , 'email' ), 'conditions' => array ( 'id' => $org [ 'Organisation' ][ 'created_by' ])));
if ( ! empty ( $email )) {
$org_creator_ids [ $org [ 'Organisation' ][ 'created_by' ]] = $email [ 'User' ][ 'email' ];
} else {
$org_creator_ids [ $org [ 'Organisation' ][ 'created_by' ]] = 'Unknown' ;
}
}
$orgs [ $k ][ 'Organisation' ][ 'created_by_email' ] = $org_creator_ids [ $org [ 'Organisation' ][ 'created_by' ]];
}
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $orgs , $this -> response -> type ());
} else {
2020-10-09 19:10:21 +02:00
foreach ( $orgs as & $org ) {
$org [ 'Organisation' ][ 'country_code' ] = $this -> Organisation -> getCountryCode ( $org [ 'Organisation' ][ 'nationality' ]);
}
2018-07-19 11:48:22 +02:00
$this -> set ( 'named' , $this -> params [ 'named' ]);
$this -> set ( 'scope' , $scope );
$this -> set ( 'orgs' , $orgs );
}
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function admin_add ()
{
if ( $this -> request -> is ( 'post' )) {
if ( $this -> _isRest ()) {
if ( isset ( $this -> request -> data [ 'request' ])) {
$this -> request -> data = $this -> request -> data [ 'request' ];
}
if ( ! isset ( $this -> request -> data [ 'Organisation' ])) {
$this -> request -> data [ 'Organisation' ] = $this -> request -> data ;
}
if ( isset ( $this -> request -> data [ 'Organisation' ][ 'id' ])) {
unset ( $this -> request -> data [ 'Organisation' ][ 'id' ]);
}
}
$this -> Organisation -> create ();
$this -> request -> data [ 'Organisation' ][ 'created_by' ] = $this -> Auth -> user ( 'id' );
if ( $this -> _isRest ()) {
if ( ! isset ( $this -> request -> data [ 'Organisation' ][ 'local' ])) {
$this -> request -> data [ 'Organisation' ][ 'local' ] = true ;
}
}
if ( $this -> Organisation -> save ( $this -> request -> data )) {
if ( isset ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'size' ]) && $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'size' ] > 0 && $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'error' ] == 0 ) {
2018-09-12 14:40:05 +02:00
$filename = basename ( $this -> Organisation -> id . '.png' );
2018-07-19 11:48:22 +02:00
if ( preg_match ( " /^[0-9a-z \ - \ _ \ .]* \ .(png) $ /i " , $filename )) {
if ( ! empty ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ]) && is_uploaded_file ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ])) {
$result = move_uploaded_file ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ], APP . 'webroot/img/orgs/' . $filename );
}
}
}
if ( $this -> _isRest ()) {
$org = $this -> Organisation -> find ( 'first' , array (
'conditions' => array ( 'Organisation.id' => $this -> Organisation -> id ),
'recursive' => - 1
));
return $this -> RestResponse -> viewData ( $org , $this -> response -> type ());
} else {
2019-03-21 15:38:40 +01:00
$this -> Flash -> success ( __ ( 'The organisation has been successfully added.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'admin' => false , 'action' => 'index' ));
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveFailResponse ( 'Organisations' , 'admin_add' , false , $this -> Organisation -> validationErrors , $this -> response -> type ());
} else {
2019-03-21 15:38:40 +01:00
$this -> Flash -> error ( __ ( 'The organisation could not be added.' ));
2018-07-19 11:48:22 +02:00
}
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> describe ( 'Organisations' , 'admin_add' , false , $this -> response -> type ());
2020-04-07 13:21:01 +02:00
} else {
if ( ! empty ( $this -> params [ 'named' ][ 'name' ])) {
$this -> request -> data [ 'Organisation' ][ 'name' ] = $this -> params [ 'named' ][ 'name' ];
}
if ( ! empty ( $this -> params [ 'named' ][ 'uuid' ])) {
$this -> request -> data [ 'Organisation' ][ 'uuid' ] = $this -> params [ 'named' ][ 'uuid' ];
}
2018-07-19 11:48:22 +02:00
}
}
2020-10-09 19:16:29 +02:00
$countries = array_merge ([ '' => __ ( 'Not specified' )], $this -> _arrayToValuesIndexArray ( $this -> Organisation -> getCountries ()));
$this -> set ( 'countries' , $countries );
2018-07-19 11:48:22 +02:00
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function admin_edit ( $id )
{
2019-07-29 16:32:24 +02:00
if ( Validation :: uuid ( $id )) {
$temp = $this -> Organisation -> find ( 'first' , array ( 'recursive' => - 1 , 'fields' => array ( 'Organisation.id' ), 'conditions' => array ( 'Organisation.uuid' => $id )));
if ( empty ( $temp )) {
throw new NotFoundException ( __ ( 'Invalid organisation.' ));
}
$id = $temp [ 'Organisation' ][ 'id' ];
}
2018-07-19 11:48:22 +02:00
$this -> Organisation -> id = $id ;
if ( ! $this -> Organisation -> exists ()) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation' ));
2018-07-19 11:48:22 +02:00
}
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
if ( $this -> _isRest ()) {
if ( isset ( $this -> request -> data [ 'request' ])) {
$this -> request -> data = $this -> request -> data [ 'request' ];
}
if ( ! isset ( $this -> request -> data [ 'Organisation' ])) {
$this -> request -> data [ 'Organisation' ] = $this -> request -> data ;
}
$existingOrg = $this -> Organisation -> find ( 'first' , array ( 'conditions' => array ( 'Organisation.id' => $id )));
$changeFields = array ( 'name' , 'type' , 'nationality' , 'sector' , 'contacts' , 'description' , 'local' , 'uuid' );
$temp = array ( 'Organisation' => array ());
foreach ( $changeFields as $field ) {
if ( isset ( $this -> request -> data [ 'Organisation' ][ $field ])) {
$temp [ 'Organisation' ][ $field ] = $this -> request -> data [ 'Organisation' ][ $field ];
} else {
$temp [ 'Organisation' ][ $field ] = $existingOrg [ 'Organisation' ][ $field ];
}
}
2019-01-02 12:38:59 +01:00
$this -> request -> data = $temp ;
2018-07-19 11:48:22 +02:00
}
$this -> request -> data [ 'Organisation' ][ 'id' ] = $id ;
if ( $this -> Organisation -> save ( $this -> request -> data )) {
if ( isset ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'size' ]) && $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'size' ] > 0 && $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'error' ] == 0 ) {
$filename = basename ( $this -> request -> data [ 'Organisation' ][ 'id' ] . '.png' );
if ( preg_match ( " /^[0-9a-z \ - \ _ \ .]* \ .(png) $ /i " , $filename )) {
if ( ! empty ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ]) && is_uploaded_file ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ])) {
$result = move_uploaded_file ( $this -> request -> data [ 'Organisation' ][ 'logo' ][ 'tmp_name' ], APP . 'webroot/img/orgs/' . $filename );
}
}
}
if ( $this -> _isRest ()) {
$org = $this -> Organisation -> find ( 'first' , array (
'conditions' => array ( 'Organisation.id' => $this -> Organisation -> id ),
'recursive' => - 1
));
return $this -> RestResponse -> viewData ( $org , $this -> response -> type ());
} else {
2019-03-21 15:38:40 +01:00
$this -> Flash -> success ( __ ( 'Organisation updated.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'admin' => false , 'action' => 'view' , $this -> Organisation -> id ));
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveFailResponse ( 'Organisations' , 'admin_edit' , false , $this -> Organisation -> validationErrors , $this -> response -> type ());
} else {
if ( isset ( $this -> Organisation -> validationErrors [ 'uuid' ])) {
$duplicate_org = $this -> Organisation -> find ( 'first' , array (
'recursive' => - 1 ,
'conditions' => array ( 'Organisation.uuid' => trim ( $this -> request -> data [ 'Organisation' ][ 'uuid' ])),
'fields' => array ( 'Organisation.id' )
));
$this -> set ( 'duplicate_org' , $duplicate_org [ 'Organisation' ][ 'id' ]);
}
2019-03-21 15:38:40 +01:00
$this -> Flash -> error ( __ ( 'The organisation could not be updated.' ));
2018-07-19 11:48:22 +02:00
}
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> describe ( 'Organisations' , 'admin_edit' , false , $this -> response -> type ());
}
$this -> Organisation -> read ( null , $id );
$this -> request -> data = $this -> Organisation -> data ;
}
2020-10-09 18:44:18 +02:00
2020-10-09 19:16:29 +02:00
$countries = array_merge ([ '' => __ ( 'Not specified' )], $this -> _arrayToValuesIndexArray ( $this -> Organisation -> getCountries ()));
2020-10-09 18:44:18 +02:00
if ( ! empty ( $this -> Organisation -> data [ 'Organisation' ][ 'nationality' ])) {
$currentCountry = $this -> Organisation -> data [ 'Organisation' ][ 'nationality' ];
if ( ! isset ( $countries [ $currentCountry ])) {
// Append old country name to list to keep backward compatibility
$countries [ $currentCountry ] = $currentCountry ;
}
}
$this -> set ( 'countries' , $countries );
2018-07-19 11:48:22 +02:00
$this -> set ( 'orgId' , $id );
if ( is_array ( $this -> request -> data [ 'Organisation' ][ 'restricted_to_domain' ])) {
$this -> request -> data [ 'Organisation' ][ 'restricted_to_domain' ] = implode ( " \n " , $this -> request -> data [ 'Organisation' ][ 'restricted_to_domain' ]);
}
$this -> set ( 'id' , $id );
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function admin_delete ( $id )
{
2019-07-10 13:55:33 +02:00
if ( ! $this -> request -> is ( 'post' ) && ! $this -> request -> is ( 'delete' )) {
2019-07-10 11:57:21 +02:00
throw new MethodNotAllowedException ( __ ( 'Action not allowed, post or delete request expected.' ));
2018-07-19 11:48:22 +02:00
}
2019-08-14 11:30:11 +02:00
if ( Validation :: uuid ( $id )) {
$temp = $this -> Organisation -> find ( 'first' , array ( 'recursive' => - 1 , 'fields' => array ( 'Organisation.id' ), 'conditions' => array ( 'Organisation.uuid' => $id )));
if ( empty ( $temp )) {
throw new NotFoundException ( __ ( 'Invalid organisation' ));
}
$id = $temp [ 'Organisation' ][ 'id' ];
}
2018-07-19 11:48:22 +02:00
$this -> Organisation -> id = $id ;
if ( ! $this -> Organisation -> exists ()) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation' ));
2018-07-19 11:48:22 +02:00
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
$org = $this -> Organisation -> find ( 'first' , array (
'conditions' => array ( 'id' => $id ),
'recursive' => - 1 ,
'fields' => array ( 'local' )
));
if ( $org [ 'Organisation' ][ 'local' ]) {
$url = '/organisations/index' ;
} else {
$url = '/organisations/index/remote' ;
}
if ( $this -> Organisation -> delete ()) {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Organisations' , 'admin_delete' , $id , $this -> response -> type ());
} else {
$this -> Flash -> success ( __ ( 'Organisation deleted' ));
$this -> redirect ( $url );
}
} else {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveFailResponse ( 'Organisations' , 'admin_delete' , $id , $this -> Organisation -> validationErrors , $this -> response -> type ());
} else {
$this -> Flash -> error ( __ ( 'Organisation could not be deleted. Generally organisations should never be deleted, instead consider moving them to the known remote organisations list. Alternatively, if you are certain that you would like to remove an organisation and are aware of the impact, make sure that there are no users or events still tied to this organisation before deleting it.' ));
$this -> redirect ( $url );
}
}
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function admin_generateuuid ()
{
$this -> set ( 'uuid' , CakeText :: uuid ());
$this -> set ( '_serialize' , array ( 'uuid' ));
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function view ( $id )
{
if ( Validation :: uuid ( $id )) {
$temp = $this -> Organisation -> find ( 'first' , array ( 'recursive' => - 1 , 'fields' => array ( 'Organisation.id' ), 'conditions' => array ( 'Organisation.uuid' => $id )));
if ( empty ( $temp )) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation.' ));
2018-07-19 11:48:22 +02:00
}
$id = $temp [ 'Organisation' ][ 'id' ];
} elseif ( ! is_numeric ( $id )) {
$temp = $this -> Organisation -> find ( 'first' , array ( 'recursive' => - 1 , 'fields' => array ( 'Organisation.id' ), 'conditions' => array ( 'Organisation.name' => urldecode ( $id ))));
if ( empty ( $temp )) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation.' ));
2018-07-19 11:48:22 +02:00
}
$id = $temp [ 'Organisation' ][ 'id' ];
}
$this -> Organisation -> id = $id ;
if ( ! $this -> Organisation -> exists ()) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation' ));
2018-07-19 11:48:22 +02:00
}
$fullAccess = false ;
$fields = array ( 'id' , 'name' , 'date_created' , 'date_modified' , 'type' , 'nationality' , 'sector' , 'contacts' , 'description' , 'local' , 'uuid' , 'restricted_to_domain' );
if ( $this -> _isSiteAdmin () || ( $this -> _isAdmin () && $this -> Auth -> user ( 'Organisation' )[ 'id' ] == $id )) {
$fullAccess = true ;
$fields = array_merge ( $fields , array ( 'created_by' ));
}
$org = $this -> Organisation -> find ( 'first' , array (
'conditions' => array ( 'id' => $id ),
'fields' => $fields ,
'recursive' => - 1
));
if ( ! $this -> Auth -> user ( 'Role' )[ 'perm_sharing_group' ] && Configure :: read ( 'Security.hide_organisation_index_from_users' )) {
$this -> loadModel ( 'Event' );
$event = $this -> Event -> find ( 'first' , array (
'fields' => array ( 'Event.id' ),
'recursive' => - 1 ,
'conditions' => array ( 'Event.orgc_id' => $org [ 'Organisation' ][ 'id' ])
));
if ( empty ( $event )) {
$proposal = $this -> Event -> ShadowAttribute -> find ( 'first' , array (
'fields' => array ( 'ShadowAttribute.id' ),
'recursive' => - 1 ,
'conditions' => array ( 'ShadowAttribute.org_id' => $org [ 'Organisation' ][ 'id' ])
));
if ( empty ( $proposal )) {
2019-03-21 15:38:40 +01:00
throw new NotFoundException ( __ ( 'Invalid organisation' ));
2018-07-19 11:48:22 +02:00
}
}
}
$this -> set ( 'local' , $org [ 'Organisation' ][ 'local' ]);
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
if ( $fullAccess ) {
$creator = $this -> Organisation -> User -> find (
'first' ,
array (
'conditions' => array ( 'User.id' => $org [ 'Organisation' ][ 'created_by' ]),
'fields' => array ( 'email' ),
'recursive' => - 1
)
);
if ( ! empty ( $creator )) {
$org [ 'Organisation' ][ 'created_by_email' ] = $creator [ 'User' ][ 'email' ];
}
}
if ( $this -> _isRest ()) {
$org [ 'Organisation' ][ 'user_count' ] = $this -> Organisation -> User -> getMembersCount ( $org [ 'Organisation' ][ 'id' ]);
return $this -> RestResponse -> viewData ( $org , $this -> response -> type ());
} else {
2020-10-09 19:10:21 +02:00
$org [ 'Organisation' ][ 'country_code' ] = $this -> Organisation -> getCountryCode ( $org [ 'Organisation' ][ 'nationality' ]);
2018-07-19 11:48:22 +02:00
$this -> set ( 'fullAccess' , $fullAccess );
$this -> set ( 'org' , $org );
$this -> set ( 'id' , $id );
}
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function fetchOrgsForSG ( $idList = '{}' , $type )
{
if ( $type === 'local' ) {
$local = 1 ;
} else {
$local = 0 ;
}
$idList = json_decode ( $idList , true );
$id_exclusion_list = array_merge ( $idList , array ( $this -> Auth -> user ( 'Organisation' )[ 'id' ]));
$temp = $this -> Organisation -> find ( 'all' , array (
'conditions' => array (
'local' => $local ,
'id !=' => $id_exclusion_list ,
),
'recursive' => - 1 ,
'fields' => array ( 'id' , 'name' ),
'order' => array ( 'lower(name) ASC' )
));
$orgs = array ();
foreach ( $temp as $org ) {
$orgs [] = array ( 'id' => $org [ 'Organisation' ][ 'id' ], 'name' => $org [ 'Organisation' ][ 'name' ]);
}
$this -> set ( 'local' , $local );
$this -> layout = false ;
$this -> autoRender = false ;
$this -> set ( 'orgs' , $orgs );
$this -> render ( 'ajax/fetch_orgs_for_sg' );
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function fetchSGOrgRow ( $id , $removable = false , $extend = false )
{
$this -> layout = false ;
$this -> autoRender = false ;
$this -> set ( 'id' , $id );
$this -> set ( 'removable' , $removable );
$this -> set ( 'extend' , $extend );
$this -> render ( 'ajax/sg_org_row_empty' );
}
2016-01-19 16:25:54 +01:00
2018-07-19 11:48:22 +02:00
public function getUUIDs ()
{
if ( ! $this -> Auth -> user ( 'Role' )[ 'perm_sync' ]) {
2019-03-21 15:38:40 +01:00
throw new MethodNotAllowedException ( __ ( 'This action is restricted to sync users' ));
2018-07-19 11:48:22 +02:00
}
$temp = $this -> Organisation -> find ( 'all' , array (
'recursive' => - 1 ,
'conditions' => array ( 'local' => 1 ),
'fields' => array ( 'Organisation.uuid' )
));
$orgs = array ();
foreach ( $temp as $t ) {
$orgs [] = $t [ 'Organisation' ][ 'uuid' ];
}
return new CakeResponse ( array ( 'body' => json_encode ( $orgs ), 'type' => 'json' ));
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function admin_merge ( $id , $target_id = false )
{
if ( ! $this -> _isSiteAdmin ()) {
2019-03-21 15:38:40 +01:00
throw new MethodNotAllowedException ( __ ( 'You are not authorised to do that.' ));
2018-07-19 11:48:22 +02:00
}
if ( $this -> request -> is ( 'Post' )) {
$result = $this -> Organisation -> orgMerge ( $id , $this -> request -> data , $this -> Auth -> user ());
if ( $result ) {
2019-03-21 15:38:40 +01:00
$this -> Flash -> success ( __ ( 'The organisation has been successfully merged.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'admin' => false , 'action' => 'view' , $result ));
} else {
2019-03-21 15:38:40 +01:00
$this -> Flash -> error ( __ ( 'There was an error while merging the organisations. To find out more about what went wrong, refer to the audit logs. If you would like to revert the changes, you can find a .sql file' ));
2018-07-19 11:48:22 +02:00
}
$this -> redirect ( array ( 'admin' => false , 'action' => 'index' ));
} else {
$currentOrg = $this -> Organisation -> find ( 'first' , array ( 'fields' => array ( 'id' , 'name' , 'uuid' , 'local' ), 'recursive' => - 1 , 'conditions' => array ( 'Organisation.id' => $id )));
$orgs [ 'local' ] = $this -> Organisation -> find ( 'all' , array (
'fields' => array ( 'id' , 'name' , 'uuid' ),
'conditions' => array ( 'Organisation.id !=' => $id , 'Organisation.local' => 1 ),
'order' => 'lower(Organisation.name) ASC'
));
$orgs [ 'external' ] = $this -> Organisation -> find ( 'all' , array (
'fields' => array ( 'id' , 'name' , 'uuid' ),
'conditions' => array ( 'Organisation.id !=' => $id , 'Organisation.local' => 0 ),
'order' => 'lower(Organisation.name) ASC'
));
foreach ( array ( 'local' , 'external' ) as $type ) {
$orgOptions [ $type ] = Hash :: combine ( $orgs [ $type ], '{n}.Organisation.id' , '{n}.Organisation.name' );
$orgs [ $type ] = Hash :: combine ( $orgs [ $type ], '{n}.Organisation.id' , '{n}' );
}
if ( ! empty ( $target_id )) {
$target = array ();
foreach ( array ( 'local' , 'external' ) as $type ) {
foreach ( $orgOptions [ $type ] as $k => $v ) {
if ( $k == $target_id ) {
$target = array ( 'id' => $k , 'type' => $type );
}
}
}
if ( ! empty ( $target )) {
$this -> set ( 'target' , $target );
}
}
$this -> set ( 'orgs' , json_encode ( $orgs ));
$this -> set ( 'orgOptions' , $orgOptions );
$this -> set ( 'currentOrg' , $currentOrg );
$this -> layout = false ;
$this -> autoRender = false ;
$this -> render ( 'ajax/merge' );
}
}
2016-06-06 10:09:55 +02:00
}