2020-05-06 07:59:57 +02:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
2021-10-30 19:31:52 +02:00
/**
* @ property Tag $Tag
*/
2020-05-06 07:59:57 +02:00
class GalaxyClusterRelationTag extends AppModel
{
public $useTable = 'galaxy_cluster_relation_tags' ;
2021-01-22 13:01:23 +01:00
public $actsAs = array ( 'AuditLog' , 'Containable' );
2020-05-06 07:59:57 +02:00
public $validate = array (
'galaxy_cluster_relation_id' => array (
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
),
),
'tag_id' => array (
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
),
),
);
public $belongsTo = array (
2020-05-07 11:03:18 +02:00
'GalaxyClusterRelation' => array (
'className' => 'GalaxyClusterRelation' ,
),
'Tag' => array (
'className' => 'Tag' ,
),
2020-05-06 07:59:57 +02:00
);
public function softDelete ( $id )
{
$this -> delete ( $id );
}
2020-11-24 13:51:13 +01:00
2020-07-09 15:32:14 +02:00
/**
* attachTags
*
* @ param array $user
* @ param int $galaxyClusterRelationId
* @ param array $tags list of tag names to be saved
* @ param bool $capture
2021-07-27 21:03:17 +02:00
* @ return bool
2020-07-09 15:32:14 +02:00
*/
public function attachTags ( array $user , $galaxyClusterRelationId , array $tags , $capture = false )
2020-05-06 07:59:57 +02:00
{
$allSaved = true ;
2020-05-29 08:56:29 +02:00
$saveResult = false ;
2020-05-06 07:59:57 +02:00
foreach ( $tags as $tagName ) {
2020-05-29 08:56:29 +02:00
if ( $capture ) {
$tagId = $this -> Tag -> captureTag ( array ( 'name' => $tagName ), $user );
} else {
$tagId = $this -> Tag -> lookupTagIdFromName ( $tagName );
}
2020-05-06 07:59:57 +02:00
$existingAssociation = $this -> find ( 'first' , array (
'recursive' => - 1 ,
'conditions' => array (
'tag_id' => $tagId ,
'galaxy_cluster_relation_id' => $galaxyClusterRelationId
)
));
2020-05-29 08:56:29 +02:00
if ( empty ( $existingAssociation ) && $tagId != - 1 ) {
2020-05-06 07:59:57 +02:00
$this -> create ();
$saveResult = $this -> save ( array ( 'galaxy_cluster_relation_id' => $galaxyClusterRelationId , 'tag_id' => $tagId ));
$allSaved = $allSaved && $saveResult ;
if ( ! $saveResult ) {
2020-05-22 09:04:01 +02:00
$this -> Log -> createLogEntry ( $user , 'attachTags' , 'GalaxyClusterRelationTag' , 0 , __ ( 'Could not attach tag %s' , $tagName ), __ ( 'relation (%s)' , $galaxyClusterRelationId ));
2020-05-06 07:59:57 +02:00
}
}
}
2020-05-29 08:56:29 +02:00
return $allSaved ;
}
public function detachTag ( $user , $relationTagId )
{
$this -> delete ( array ( 'GalaxyClusterRelationTag.relationTagId' => $relationTagId ));
2020-05-06 07:59:57 +02:00
}
}