2015-04-07 00:24:44 +02:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
class SharingGroupOrg extends AppModel {
public $actsAs = array ( 'Containable' );
public $validate = array (
2016-06-04 01:08:16 +02:00
2015-04-07 00:24:44 +02:00
);
2016-06-04 01:08:16 +02:00
2015-04-07 00:24:44 +02:00
public $belongsTo = array (
'SharingGroup' => array (
'className' => 'SharingGroup' ,
'foreignKey' => 'sharing_group_id'
),
'Organisation' => array (
'className' => 'Organisation' ,
2015-04-14 17:51:38 +02:00
'foreignKey' => 'org_id' ,
2015-04-07 00:24:44 +02:00
//'conditions' => array('SharingGroupElement.organisation_uuid' => 'Organisation.uuid')
)
);
public function beforeValidate ( $options = array ()) {
parent :: beforeValidate ();
}
2016-06-04 01:08:16 +02:00
2015-04-20 11:46:55 +02:00
public function updateOrgsForSG ( $id , $new_orgs , $old_orgs , $user ) {
$log = ClassRegistry :: init ( 'Log' );
2015-04-07 00:24:44 +02:00
// Loop through all of the organisations we want to add.
foreach ( $new_orgs as $org ) {
$SgO = array (
'sharing_group_id' => $id ,
2015-04-14 17:51:38 +02:00
'org_id' => $org [ 'id' ],
2015-04-07 00:24:44 +02:00
'extend' => $org [ 'extend' ]
);
$found = false ;
// If there is a match between a new org and an old org, keep the org in $found and unset it in the old org array.
foreach ( $old_orgs as $k => $old_org ) {
2015-04-14 17:51:38 +02:00
if ( $old_org [ 'org_id' ] == $org [ 'id' ]) {
2015-04-07 00:24:44 +02:00
$found = $old_orgs [ $k ];
unset ( $old_orgs [ $k ]);
break ;
}
}
// If we have not found the org previously, create a new sharing group org object.
// Otherwise, if we have found it check whether the extended field has been altered, if not just continue without saving
if ( ! $found ) {
$this -> create ();
2015-04-20 11:46:55 +02:00
$isChange = false ;
2015-04-07 00:24:44 +02:00
} else {
if ( $found [ 'extend' ] == $SgO [ 'extend' ]) continue ;
$SgO [ 'id' ] = $found [ 'id' ];
2015-04-20 11:46:55 +02:00
$isChange = true ;
}
if ( $this -> save ( $SgO )) {
$log -> create ();
if ( $isChange ) $log -> createLogEntry ( $user , 'edit' , 'SharingGroupOrg' , $this -> id , 'Sharing group (' . $id . '): Modified right to alter sharing group for organisation (' . $org [ 'id' ] . ').' , ( $org [ 'extend' ] ? 'Organisation (' . $org [ 'id' ] . ') can now extend the sharing group.' : 'Organisation (' . $org [ 'id' ] . ') can no longer extend the sharing group.' ));
else $log -> createLogEntry ( $user , 'add' , 'SharingGroupOrg' , $this -> id , 'Sharing group (' . $id . '): Added organisation (' . $org [ 'id' ] . ').' , 'Organisation (' . $org [ 'id' ] . ') added to Sharing group.' . ( $org [ 'extend' ] ? ' Organisation (' . $org [ 'id' ] . ') can extend the sharing group.' : '' ));
2015-04-07 00:24:44 +02:00
}
}
// We are left with some "old orgs" that are not in the new list. This means that they can be safely deleted.
2015-04-20 11:46:55 +02:00
foreach ( $old_orgs as $old_org ) {
if ( $this -> delete ( $old_org [ 'id' ])) {
$log -> create ();
$log -> createLogEntry ( $user , 'delete' , 'SharingGroupOrg' , $old_org [ 'id' ], 'Sharing group (' . $id . '): Removed organisation (' . $old_org [ 'id' ] . ').' , 'Organisation (' . $org [ 'id' ] . ') removed from Sharing group.' );
}
}
2015-04-07 00:24:44 +02:00
}
2016-06-04 01:08:16 +02:00
2015-04-07 00:24:44 +02:00
public function fetchAllAuthorised ( $org_id ) {
$sgs = $this -> find ( 'all' , array (
2015-04-14 17:51:38 +02:00
'conditions' => array ( 'org_id' => $org_id ),
2015-04-07 00:24:44 +02:00
'recursive' => - 1 ,
2015-04-14 17:51:38 +02:00
'fields' => array ( 'org_id' , 'sharing_group_id' ),
2015-04-07 00:24:44 +02:00
));
$ids = array ();
foreach ( $sgs as $sg ) $ids [] = $sg [ 'SharingGroupOrg' ][ 'sharing_group_id' ];
return $ids ;
}
2016-06-04 01:08:16 +02:00
2015-04-07 00:24:44 +02:00
// pass a sharing group ID and an organisation ID, returns true if it has a matching attached organisation object
public function checkIfAuthorised ( $id , $org_id ) {
$sg = $this -> find ( 'first' , array (
2015-04-14 17:51:38 +02:00
'conditions' => array ( 'sharing_group_id' => $id , 'org_id' => $org_id ),
2015-04-07 00:24:44 +02:00
'recursive' => - 1 ,
2015-04-13 17:52:04 +02:00
'fields' => array ( 'id' ),
2015-04-07 00:24:44 +02:00
));
if ( ! empty ( $sg )) return true ;
return false ;
}
}