2014-01-23 12:25:04 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class EventTag extends AppModel {
|
|
|
|
|
2014-02-02 18:10:21 +01:00
|
|
|
public $actsAs = array('Containable');
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-01-23 12:25:04 +01:00
|
|
|
public $validate = array(
|
|
|
|
'event_id' => array(
|
2015-10-18 18:03:48 +02:00
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
2014-01-23 12:25:04 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'tag_id' => array(
|
2015-10-18 18:03:48 +02:00
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
2014-01-23 12:25:04 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-01-23 12:25:04 +01:00
|
|
|
public $belongsTo = array(
|
2016-09-06 23:14:48 +02:00
|
|
|
'Event',
|
|
|
|
'Tag'
|
2014-01-23 12:25:04 +01:00
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-01-26 19:27:27 +01:00
|
|
|
public function afterSave($created, $options = array()) {
|
|
|
|
parent::afterSave($created, $options);
|
|
|
|
if (Configure::read('Plugin.ZeroMQ_enable') && Configure::read('Plugin.ZeroMQ_tag_notifications_enable')) {
|
|
|
|
$pubSubTool = $this->getPubSubTool();
|
|
|
|
$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']);
|
|
|
|
$pubSubTool->tag_save($tag, 'attached to event');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeDelete($cascade = true) {
|
|
|
|
if (Configure::read('Plugin.ZeroMQ_enable') && Configure::read('Plugin.ZeroMQ_tag_notifications_enable')) {
|
|
|
|
if (!empty($this->id)) {
|
|
|
|
$pubSubTool = $this->getPubSubTool();
|
|
|
|
$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']);
|
|
|
|
$pubSubTool->tag_save($tag, 'detached from event');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 18:07:15 +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;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-12-21 11:46:33 +01: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;
|
|
|
|
}
|
2017-01-16 16:15:06 +01:00
|
|
|
|
2016-11-04 13:14:03 +01: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;
|
|
|
|
}
|
2017-11-09 11:53:57 +01:00
|
|
|
|
|
|
|
public function countForTag($tag_id, $user, $sgids = array()) {
|
|
|
|
$db = $this->getDataSource();
|
|
|
|
$subQuery = $db->buildStatement(
|
|
|
|
array(
|
|
|
|
'fields' => array('EventTag.event_id'),
|
|
|
|
'table' => 'event_tags',
|
|
|
|
'alias' => 'EventTag',
|
|
|
|
'limit' => null,
|
|
|
|
'offset' => null,
|
|
|
|
'joins' => array(),
|
|
|
|
'conditions' => array(
|
|
|
|
'EventTag.tag_id' => $tag_id
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
$subQuery = 'Event.id IN (' . $subQuery . ') ';
|
|
|
|
$conditions = array(
|
|
|
|
$db->expression($subQuery)->value
|
|
|
|
);
|
|
|
|
if (!$user['Role']['perm_site_admin']) {
|
|
|
|
$conditions = array_merge(
|
|
|
|
$conditions,
|
|
|
|
array('OR' => array(
|
|
|
|
array('Event.distribution' => array(1, 2, 3)),
|
|
|
|
array('Event.orgc_id' => $user['org_id'])
|
|
|
|
))
|
|
|
|
);
|
|
|
|
if (!empty($sgids)) {
|
|
|
|
$conditions['OR'][] = array('AND' => array(
|
|
|
|
'Event.distribution' => 4,
|
|
|
|
'Event.sharing_group_id' => $sgids
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->Event->find('count', array(
|
|
|
|
'fields' => array('Event.id', 'Event.distribution', 'Event.orgc_id', 'Event.sharing_group_id'),
|
|
|
|
'conditions' => $conditions
|
|
|
|
));
|
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|