2014-01-23 12:25:04 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
2020-11-22 10:12:08 +01:00
|
|
|
/**
|
|
|
|
* @property Event $Event
|
2021-10-10 12:36:28 +02:00
|
|
|
* @property Tag $Tag
|
2020-11-22 10:12:08 +01:00
|
|
|
*/
|
2018-07-19 11:48:22 +02:00
|
|
|
class EventTag extends AppModel
|
|
|
|
{
|
2021-01-22 13:01:23 +01:00
|
|
|
public $actsAs = array('AuditLog', 'Containable');
|
2014-01-23 12:25:04 +01:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public $validate = array(
|
|
|
|
'event_id' => array(
|
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'tag_id' => array(
|
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public $belongsTo = array(
|
|
|
|
'Event',
|
|
|
|
'Tag'
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function afterSave($created, $options = array())
|
|
|
|
{
|
|
|
|
parent::afterSave($created, $options);
|
2019-03-05 12:24:56 +01:00
|
|
|
$pubToZmq = Configure::read('Plugin.ZeroMQ_enable') && Configure::read('Plugin.ZeroMQ_tag_notifications_enable');
|
|
|
|
$kafkaTopic = Configure::read('Plugin.Kafka_tag_notifications_topic');
|
|
|
|
$pubToKafka = Configure::read('Plugin.Kafka_enable') && Configure::read('Plugin.Kafka_tag_notifications_enable') && !empty($kafkaTopic);
|
|
|
|
if ($pubToZmq || $pubToKafka) {
|
2018-07-19 11:48:22 +02:00
|
|
|
$tag = $this->find('first', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array('EventTag.id' => $this->id),
|
|
|
|
'contain' => array('Tag')
|
|
|
|
));
|
|
|
|
$tag['Tag']['event_id'] = $tag['EventTag']['event_id'];
|
|
|
|
$tag = array('Tag' => $tag['Tag']);
|
2019-03-05 12:24:56 +01:00
|
|
|
if ($pubToZmq) {
|
|
|
|
$pubSubTool = $this->getPubSubTool();
|
|
|
|
$pubSubTool->tag_save($tag, 'attached to event');
|
|
|
|
}
|
|
|
|
if ($pubToKafka) {
|
|
|
|
$kafkaPubTool = $this->getKafkaPubTool();
|
|
|
|
$kafkaPubTool->publishJson($kafkaTopic, $tag, 'attached to event');
|
|
|
|
}
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function beforeDelete($cascade = true)
|
|
|
|
{
|
2019-03-05 12:24:56 +01:00
|
|
|
$pubToZmq = Configure::read('Plugin.ZeroMQ_enable') && Configure::read('Plugin.ZeroMQ_tag_notifications_enable');
|
|
|
|
$kafkaTopic = Configure::read('Plugin.Kafka_tag_notifications_topic');
|
|
|
|
$pubToKafka = Configure::read('Plugin.Kafka_enable') && Configure::read('Plugin.Kafka_tag_notifications_enable') && !empty($kafkaTopic);
|
|
|
|
if ($pubToZmq || $pubToKafka) {
|
2018-07-19 11:48:22 +02:00
|
|
|
if (!empty($this->id)) {
|
|
|
|
$tag = $this->find('first', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array('EventTag.id' => $this->id),
|
|
|
|
'contain' => array('Tag')
|
|
|
|
));
|
|
|
|
$tag['Tag']['event_id'] = $tag['EventTag']['event_id'];
|
|
|
|
$tag = array('Tag' => $tag['Tag']);
|
2019-03-05 12:24:56 +01:00
|
|
|
if ($pubToZmq) {
|
|
|
|
$pubSubTool = $this->getPubSubTool();
|
|
|
|
$pubSubTool->tag_save($tag, 'detached from event');
|
|
|
|
}
|
|
|
|
if ($pubToKafka) {
|
|
|
|
$kafkaPubTool = $this->getKafkaPubTool();
|
|
|
|
$kafkaPubTool->publishJson($kafkaTopic, $tag, 'detached from event');
|
|
|
|
}
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 19:27:27 +01:00
|
|
|
|
2019-01-31 10:54:11 +01:00
|
|
|
public function softDelete($id)
|
|
|
|
{
|
|
|
|
$this->delete($id);
|
|
|
|
}
|
|
|
|
|
2020-07-24 11:41:27 +02:00
|
|
|
public function handleEventTag($event_id, $tag, &$nothingToChange = false)
|
2020-02-27 11:07:17 +01:00
|
|
|
{
|
|
|
|
if (empty($tag['deleted'])) {
|
2021-03-25 16:04:23 +01:00
|
|
|
$result = $this->attachTagToEvent($event_id, $tag, $nothingToChange);
|
2020-02-27 11:07:17 +01:00
|
|
|
} else {
|
2020-07-24 11:41:27 +02:00
|
|
|
$result = $this->detachTagFromEvent($event_id, $tag['id'], $nothingToChange);
|
2020-02-27 11:07:17 +01:00
|
|
|
}
|
2020-07-24 11:41:27 +02:00
|
|
|
return $result;
|
2020-02-27 11:07:17 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 19:10:55 +02:00
|
|
|
/**
|
|
|
|
* @param int $event_id
|
|
|
|
* @param int $tagId
|
|
|
|
* @param bool $nothingToChange
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function attachTagToEvent($event_id, array $tag, &$nothingToChange = false)
|
2018-07-19 11:48:22 +02:00
|
|
|
{
|
2021-10-05 19:10:55 +02:00
|
|
|
$existingAssociation = $this->hasAny([
|
|
|
|
'tag_id' => $tag['id'],
|
|
|
|
'event_id' => $event_id,
|
|
|
|
]);
|
|
|
|
if (!$existingAssociation) {
|
2018-07-19 11:48:22 +02:00
|
|
|
$this->create();
|
2021-03-25 16:04:23 +01:00
|
|
|
if (!$this->save(array('event_id' => $event_id, 'tag_id' => $tag['id'], 'local' => !empty($tag['local'])))) {
|
2018-07-19 11:48:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-22 13:10:52 +02:00
|
|
|
} else {
|
|
|
|
$nothingToChange = true;
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2021-10-05 19:10:55 +02:00
|
|
|
/**
|
|
|
|
* @param int $event_id
|
|
|
|
* @param int $tag_id
|
|
|
|
* @param bool $nothingToChange
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-07-24 11:41:27 +02:00
|
|
|
public function detachTagFromEvent($event_id, $tag_id, &$nothingToChange = false)
|
2019-11-26 13:40:15 +01:00
|
|
|
{
|
|
|
|
$existingAssociation = $this->find('first', array(
|
|
|
|
'recursive' => -1,
|
2021-10-05 19:10:55 +02:00
|
|
|
'fields' => ['id'],
|
2019-11-26 13:40:15 +01:00
|
|
|
'conditions' => array(
|
|
|
|
'tag_id' => $tag_id,
|
|
|
|
'event_id' => $event_id
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2021-10-05 19:10:55 +02:00
|
|
|
if ($existingAssociation) {
|
2019-11-26 13:40:15 +01:00
|
|
|
$result = $this->delete($existingAssociation['EventTag']['id']);
|
|
|
|
if ($result) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-24 11:41:27 +02:00
|
|
|
} else {
|
|
|
|
$nothingToChange = true;
|
2019-11-26 13:40:15 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function getSortedTagList($context = false)
|
|
|
|
{
|
|
|
|
$conditions = array();
|
|
|
|
$tag_counts = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('tag_id', 'count(*)'),
|
|
|
|
'group' => array('tag_id'),
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'contain' => array('Tag.name')
|
|
|
|
));
|
|
|
|
$temp = array();
|
|
|
|
$tags = array();
|
|
|
|
foreach ($tag_counts as $tag_count) {
|
|
|
|
$temp[$tag_count['Tag']['name']] = array(
|
|
|
|
'tag_id' => $tag_count['Tag']['id'],
|
|
|
|
'eventCount' => $tag_count[0]['count(*)'],
|
|
|
|
'name' => $tag_count['Tag']['name'],
|
|
|
|
);
|
|
|
|
$tags[$tag_count['Tag']['name']] = $tag_count[0]['count(*)'];
|
|
|
|
}
|
|
|
|
arsort($tags);
|
|
|
|
foreach ($tags as $k => $v) {
|
|
|
|
$tags[$k] = $temp[$k];
|
|
|
|
}
|
|
|
|
return $tags;
|
|
|
|
}
|
2019-05-15 11:12:09 +02:00
|
|
|
|
2020-11-22 10:12:08 +01:00
|
|
|
/**
|
|
|
|
* @param int $tagId
|
|
|
|
* @param array $user
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countForTag($tagId, array $user)
|
2018-07-19 11:48:22 +02:00
|
|
|
{
|
2020-11-22 10:12:08 +01:00
|
|
|
$count = $this->countForTags([$tagId], $user);
|
|
|
|
return isset($count[$tagId]) ? (int)$count[$tagId] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $tagIds
|
|
|
|
* @param array $user
|
|
|
|
* @return array Key is tag ID, value is event count that user can see
|
|
|
|
*/
|
|
|
|
public function countForTags(array $tagIds, array $user)
|
|
|
|
{
|
|
|
|
if (empty($tagIds)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$conditions = $this->Event->createEventConditions($user);
|
|
|
|
$conditions['AND']['EventTag.tag_id'] = $tagIds;
|
|
|
|
$this->virtualFields['event_count'] = 'COUNT(EventTag.id)';
|
|
|
|
$counts = $this->find('list', [
|
2018-07-19 11:48:22 +02:00
|
|
|
'recursive' => -1,
|
2020-11-22 10:12:08 +01:00
|
|
|
'contain' => ['Event'],
|
|
|
|
'fields' => ['EventTag.tag_id', 'event_count'],
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'group' => ['EventTag.tag_id'],
|
|
|
|
]);
|
|
|
|
unset($this->virtualFields['event_count']);
|
|
|
|
return $counts;
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
2017-01-16 16:15:06 +01:00
|
|
|
|
2019-05-15 11:12:09 +02:00
|
|
|
public function getTagScores($eventId=0, $allowedTags=array(), $propagateToAttribute=false)
|
2018-07-19 11:48:22 +02:00
|
|
|
{
|
2019-05-15 11:12:09 +02:00
|
|
|
if ($propagateToAttribute) {
|
|
|
|
$eventTagScores = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array('Tag.id !=' => null),
|
|
|
|
'contain' => array(
|
|
|
|
'Event',
|
|
|
|
'Tag' => array(
|
|
|
|
'conditions' => array('name' => $allowedTags)
|
|
|
|
)
|
|
|
|
),
|
2019-05-15 11:55:22 +02:00
|
|
|
'fields' => array('Tag.name', 'Event.attribute_count')
|
2019-05-15 11:12:09 +02:00
|
|
|
));
|
|
|
|
} else {
|
2019-05-15 11:55:22 +02:00
|
|
|
$conditions = array('Tag.id !=' => null);
|
2019-05-15 11:12:09 +02:00
|
|
|
if ($eventId != 0) {
|
2019-05-15 11:55:22 +02:00
|
|
|
$conditions['event_id'] = $eventId;
|
2019-05-15 11:12:09 +02:00
|
|
|
}
|
2019-05-15 11:55:22 +02:00
|
|
|
$eventTagScores = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'contain' => array(
|
|
|
|
'Tag' => array(
|
|
|
|
'conditions' => array('name' => $allowedTags)
|
|
|
|
)
|
|
|
|
),
|
2019-07-23 10:17:56 +02:00
|
|
|
'group' => array('tag_id', 'Tag.name', 'Tag.id'),
|
2019-05-15 11:55:22 +02:00
|
|
|
'fields' => array('Tag.name', 'EventTag.tag_id', 'count(EventTag.tag_id) as score')
|
|
|
|
));
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
2018-06-18 16:51:29 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
// arrange data
|
|
|
|
$scores = array();
|
|
|
|
$maxScore = 0;
|
2019-05-15 11:12:09 +02:00
|
|
|
foreach ($eventTagScores as $item) {
|
2019-05-15 11:55:22 +02:00
|
|
|
$score = isset($item['Event']) ? $item['Event']['attribute_count'] : $item[0]['score'];
|
2019-05-15 11:12:09 +02:00
|
|
|
$name = $item['Tag']['name'];
|
2018-07-19 11:48:22 +02:00
|
|
|
if (in_array($name, $allowedTags)) {
|
|
|
|
$maxScore = $score > $maxScore ? $score : $maxScore;
|
2019-06-11 14:13:17 +02:00
|
|
|
if (!isset($scores[$name])) {
|
|
|
|
$scores[$name] = 0;
|
|
|
|
}
|
|
|
|
$scores[$name] += $score;
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return array('scores' => $scores, 'maxScore' => $maxScore);
|
|
|
|
}
|
2019-06-11 15:09:02 +02:00
|
|
|
|
2019-10-04 13:02:59 +02:00
|
|
|
// Fetch all tags contained in an event (both event and attributes) ignoring the occurrence. No ACL
|
2019-06-11 15:09:02 +02:00
|
|
|
public function getTagScoresUniform($eventId=0, $allowedTags=array())
|
|
|
|
{
|
|
|
|
$conditions = array('Tag.id !=' => null);
|
|
|
|
if ($eventId != 0) {
|
|
|
|
$conditions['event_id'] = $eventId;
|
|
|
|
}
|
|
|
|
$event_tag_scores = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'contain' => array(
|
|
|
|
'Tag' => array(
|
|
|
|
'conditions' => array('name' => $allowedTags)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'fields' => array('Tag.name', 'EventTag.event_id')
|
|
|
|
));
|
|
|
|
$attribute_tag_scores = $this->Event->Attribute->AttributeTag->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'contain' => array(
|
|
|
|
'Tag' => array(
|
|
|
|
'conditions' => array('name' => $allowedTags)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'fields' => array('Tag.name', 'AttributeTag.event_id')
|
|
|
|
));
|
|
|
|
|
|
|
|
$score_aggregation = array();
|
|
|
|
foreach ($event_tag_scores as $event_tag_score) {
|
|
|
|
$score_aggregation[$event_tag_score['Tag']['name']][$event_tag_score['EventTag']['event_id']] = 1;
|
|
|
|
}
|
|
|
|
foreach ($attribute_tag_scores as $attribute_tag_score) {
|
|
|
|
$score_aggregation[$attribute_tag_score['Tag']['name']][$attribute_tag_score['AttributeTag']['event_id']] = 1;
|
|
|
|
}
|
|
|
|
$scores = array('scores' => array(), 'maxScore' => 0);
|
|
|
|
foreach ($score_aggregation as $name => $array_ids) {
|
|
|
|
$event_count = count($array_ids);
|
|
|
|
$scores['scores'][$name] = $event_count;
|
|
|
|
$scores['maxScore'] = $event_count > $scores['maxScore'] ? $event_count : $scores['maxScore'];
|
|
|
|
}
|
|
|
|
return $scores;
|
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|