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(
|
|
|
|
'Event' => array(
|
|
|
|
'className' => 'Event',
|
|
|
|
),
|
|
|
|
'Tag' => array(
|
|
|
|
'className' => 'Tag',
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|