2014-01-23 12:25:04 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
class EventTag extends AppModel
|
|
|
|
{
|
|
|
|
public $actsAs = array('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);
|
|
|
|
}
|
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
// take an array of tag names to be included and an array with tagnames to be excluded and find all event IDs that fit the criteria
|
|
|
|
public function getEventIDsFromTags($includedTags, $excludedTags)
|
|
|
|
{
|
|
|
|
$conditions = array();
|
|
|
|
if (!empty($includedTags)) {
|
|
|
|
$conditions['OR'] = array('name' => $includedTags);
|
|
|
|
}
|
|
|
|
if (!empty($excludedTags)) {
|
|
|
|
$conditions['NOT'] = array('name' => $excludedTags);
|
|
|
|
}
|
|
|
|
$tags = $this->Tag->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('id', 'name'),
|
|
|
|
'conditions' => $conditions
|
|
|
|
));
|
|
|
|
$tagIDs = array();
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$tagIDs[] = $tag['Tag']['id'];
|
|
|
|
}
|
|
|
|
$eventTags = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array('tag_id' => $tagIDs)
|
|
|
|
));
|
|
|
|
$eventIDs = array();
|
|
|
|
foreach ($eventTags as $eventTag) {
|
|
|
|
$eventIDs[] = $eventTag['EventTag']['event_id'];
|
|
|
|
}
|
|
|
|
$eventIDs = array_unique($eventIDs);
|
|
|
|
return $eventIDs;
|
|
|
|
}
|
2018-01-26 19:27:27 +01:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function attachTagToEvent($event_id, $tag_id)
|
|
|
|
{
|
|
|
|
$existingAssociation = $this->find('first', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array(
|
|
|
|
'tag_id' => $tag_id,
|
|
|
|
'event_id' => $event_id
|
|
|
|
)
|
|
|
|
));
|
|
|
|
if (empty($existingAssociation)) {
|
|
|
|
$this->create();
|
|
|
|
if (!$this->save(array('event_id' => $event_id, 'tag_id' => $tag_id))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
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
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function countForTag($tag_id, $user)
|
|
|
|
{
|
|
|
|
return $this->find('count', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'conditions' => array('EventTag.tag_id' => $tag_id)
|
|
|
|
));
|
|
|
|
}
|
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)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'fields' => array('Tag.name', 'Event.attribute_count as value')
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// get score of galaxy
|
|
|
|
$db = $this->getDataSource();
|
|
|
|
$statementArray = array(
|
|
|
|
'fields' => array('event_tag.tag_id as id', 'count(event_tag.tag_id) as value'),
|
|
|
|
'table' => $db->fullTableName($this),
|
|
|
|
'alias' => 'event_tag',
|
|
|
|
'group' => 'tag_id'
|
|
|
|
);
|
|
|
|
if ($eventId != 0) {
|
|
|
|
$statementArray['conditions'] = array('event_id' => $eventId);
|
|
|
|
}
|
|
|
|
// tag along with its occurence in the event
|
|
|
|
$subQuery = $db->buildStatement(
|
|
|
|
$statementArray,
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
$subQueryExpression = $db->expression($subQuery)->value;
|
|
|
|
// get related galaxies
|
|
|
|
$eventTagScores = $this->query("SELECT name, value FROM (" . $subQueryExpression . ") AS Event, Tag WHERE tags.id=score.id;");
|
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) {
|
|
|
|
$score = $item['Event']['value'];
|
|
|
|
$name = $item['Tag']['name'];
|
2018-07-19 11:48:22 +02:00
|
|
|
if (in_array($name, $allowedTags)) {
|
|
|
|
$maxScore = $score > $maxScore ? $score : $maxScore;
|
|
|
|
$scores[$name] = $score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array('scores' => $scores, 'maxScore' => $maxScore);
|
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|