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(
|
2021-10-10 14:26:33 +02:00
|
|
|
'event_id' => [
|
|
|
|
'rule' => 'numeric',
|
|
|
|
'required' => true,
|
|
|
|
],
|
|
|
|
'tag_id' => [
|
|
|
|
'rule' => 'numeric',
|
|
|
|
'required' => true,
|
|
|
|
],
|
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 $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');
|
2021-10-10 18:41:44 +02:00
|
|
|
$kafkaTopic = $this->kafkaTopic('tag');
|
|
|
|
if ($pubToZmq || $kafkaTopic) {
|
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');
|
|
|
|
}
|
2021-10-10 18:41:44 +02:00
|
|
|
if ($kafkaTopic) {
|
2019-03-05 12:24:56 +01:00
|
|
|
$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');
|
2021-10-10 18:41:44 +02:00
|
|
|
$kafkaTopic = $this->kafkaTopic('tag');
|
|
|
|
if ($pubToZmq || $kafkaTopic) {
|
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');
|
|
|
|
}
|
2021-10-10 18:41:44 +02:00
|
|
|
if ($kafkaTopic) {
|
2019-03-05 12:24:56 +01:00
|
|
|
$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 {
|
2022-10-05 14:48:18 +02:00
|
|
|
$result = $this->detachTagFromEvent($event_id, $tag['id'], null, $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
|
|
|
{
|
2022-10-03 12:14:46 +02:00
|
|
|
$existingAssociation = $this->find('first', [
|
|
|
|
'conditions' => [
|
|
|
|
'tag_id' => $tag['id'],
|
|
|
|
'event_id' => $event_id,
|
|
|
|
],
|
|
|
|
'recursive' => -1
|
|
|
|
]);
|
2021-10-05 19:10:55 +02:00
|
|
|
if (!$existingAssociation) {
|
2018-07-19 11:48:22 +02:00
|
|
|
$this->create();
|
2022-10-03 12:14:46 +02:00
|
|
|
if (
|
|
|
|
!$this->save(
|
|
|
|
[
|
|
|
|
'event_id' => $event_id,
|
|
|
|
'tag_id' => $tag['id'],
|
|
|
|
'relationship_type' => !empty($tag['relationship_type']) ? $tag['relationship_type'] : null,
|
|
|
|
'local' => !empty($tag['local'])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
) {
|
2018-07-19 11:48:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-22 13:10:52 +02:00
|
|
|
} else {
|
2022-10-03 12:14:46 +02:00
|
|
|
if (isset($tag['relationship_type']) && $existingAssociation['EventTag']['relationship_type'] != $tag['relationship_type']) {
|
|
|
|
$existingAssociation['EventTag']['relationship_type'] = $tag['relationship_type'];
|
|
|
|
$this->save($existingAssociation);
|
|
|
|
}
|
2020-07-22 13:10:52 +02:00
|
|
|
$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
|
|
|
|
*/
|
2022-10-05 14:48:18 +02:00
|
|
|
public function detachTagFromEvent($event_id, $tag_id, $local, &$nothingToChange = false)
|
2019-11-26 13:40:15 +01:00
|
|
|
{
|
2022-10-05 14:48:18 +02:00
|
|
|
$conditions = [
|
|
|
|
'tag_id' => $tag_id,
|
|
|
|
'event_id' => $event_id,
|
|
|
|
];
|
|
|
|
if (!is_null($local)) {
|
|
|
|
$conditions['local'] = !empty($local);
|
|
|
|
}
|
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'],
|
2022-10-05 14:48:18 +02:00
|
|
|
'conditions' => $conditions,
|
2019-11-26 13:40:15 +01:00
|
|
|
));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:34:51 +02:00
|
|
|
/**
|
|
|
|
* Find all of the event Ids that belong to the accepted tags and the rejected tags
|
|
|
|
* @param array $accept
|
|
|
|
* @param array $reject
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function fetchEventTagIds(array $accept = array(), array $reject = array())
|
|
|
|
{
|
|
|
|
$acceptIds = array();
|
|
|
|
$rejectIds = array();
|
|
|
|
if (!empty($accept)) {
|
|
|
|
$acceptIds = $this->findEventIdsByTagNames($accept);
|
|
|
|
if (empty($acceptIds)) {
|
|
|
|
$acceptIds = [-1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($reject)) {
|
|
|
|
$rejectIds = $this->findEventIdsByTagNames($reject);
|
|
|
|
}
|
|
|
|
return array($acceptIds, $rejectIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $tagIdsOrNames
|
|
|
|
* @return array|int|null
|
|
|
|
*/
|
|
|
|
private function findEventIdsByTagNames(array $tagIdsOrNames)
|
|
|
|
{
|
|
|
|
$conditions = [];
|
|
|
|
foreach ($tagIdsOrNames as $tagIdOrName) {
|
|
|
|
if (is_numeric($tagIdOrName)) {
|
|
|
|
$conditions[] = array('Tag.id' => $tagIdOrName);
|
|
|
|
} else {
|
|
|
|
$conditions[] = array('LOWER(Tag.name)' => mb_strtolower($tagIdOrName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->find('column', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'contain' => 'Tag',
|
|
|
|
'conditions' => ['OR' => $conditions],
|
|
|
|
'fields' => ['EventTag.event_id'],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function getSortedTagList($context = false)
|
|
|
|
{
|
|
|
|
$tag_counts = $this->find('all', array(
|
2022-05-22 20:20:07 +02:00
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('tag_id', 'count(*)'),
|
|
|
|
'group' => array('tag_id'),
|
|
|
|
'contain' => array('Tag.name')
|
2018-07-19 11:48:22 +02:00
|
|
|
));
|
|
|
|
$temp = array();
|
|
|
|
$tags = array();
|
|
|
|
foreach ($tag_counts as $tag_count) {
|
|
|
|
$temp[$tag_count['Tag']['name']] = array(
|
2022-05-22 20:20:07 +02:00
|
|
|
'tag_id' => $tag_count['Tag']['id'],
|
|
|
|
'eventCount' => $tag_count[0]['count(*)'],
|
|
|
|
'name' => $tag_count['Tag']['name'],
|
2018-07-19 11:48:22 +02:00
|
|
|
);
|
|
|
|
$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
|
|
|
}
|