2014-01-23 12:25:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tag Model
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Tag extends AppModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use table
|
|
|
|
*
|
|
|
|
* @var mixed False or table name
|
|
|
|
*/
|
|
|
|
public $useTable = 'tags';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $displayField = 'name';
|
|
|
|
public $actsAs = array(
|
|
|
|
'SysLogLogable.SysLogLogable' => array( // TODO Audit, logable
|
|
|
|
'roleModel' => 'Tag',
|
|
|
|
'roleKey' => 'tag_id',
|
|
|
|
'change' => 'full'
|
|
|
|
),
|
2014-02-02 18:10:21 +01:00
|
|
|
'Containable'
|
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 $validate = array(
|
|
|
|
'name' => array(
|
2015-10-09 15:59:25 +02:00
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
2014-01-23 12:25:04 +01:00
|
|
|
),
|
|
|
|
'unique' => array(
|
|
|
|
'rule' => 'isUnique',
|
|
|
|
'message' => 'A similar name already exists.',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'colour' => array(
|
2015-10-09 15:59:25 +02:00
|
|
|
'valueNotEmpty' => array(
|
|
|
|
'rule' => array('valueNotEmpty'),
|
2014-01-23 12:25:04 +01:00
|
|
|
),
|
|
|
|
'userdefined' => array(
|
|
|
|
'rule' => 'validateColour',
|
|
|
|
'message' => 'Colour has to be in the RGB format (#FFFFFF)',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-01-23 12:25:04 +01:00
|
|
|
public $hasMany = array(
|
|
|
|
'EventTag' => array(
|
|
|
|
'className' => 'EventTag',
|
2014-06-12 13:59:54 +02:00
|
|
|
),
|
|
|
|
'TemplateTag',
|
2016-04-28 15:39:44 +02:00
|
|
|
'FavouriteTag' => array(
|
|
|
|
'dependent' => true
|
|
|
|
)
|
2014-01-23 12:25:04 +01:00
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
|
|
|
|
2014-02-20 10:07:51 +01:00
|
|
|
public function beforeDelete($cascade = true) {
|
|
|
|
$this->EventTag->deleteAll(array('EventTag.tag_id' => $this->id));
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-01-23 12:25:04 +01:00
|
|
|
public function validateColour($fields) {
|
|
|
|
if (!preg_match('/^#[0-9a-f]{6}$/i', $fields['colour'])) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-02-02 18:10:21 +01:00
|
|
|
// find all of the event Ids that belong to the accepted tags and the rejected tags
|
|
|
|
public function fetchEventTagIds($accept=array(), $reject=array()) {
|
2014-02-02 18:34:38 +01:00
|
|
|
$acceptIds = array();
|
|
|
|
$rejectIds = array();
|
2014-02-02 18:10:21 +01:00
|
|
|
if (!empty($accept)) {
|
|
|
|
$acceptIds = $this->findTags($accept);
|
2014-02-02 18:34:38 +01:00
|
|
|
if (empty($acceptIds)) $acceptIds[] = -1;
|
2014-02-02 18:10:21 +01:00
|
|
|
}
|
|
|
|
if (!empty($reject)) {
|
|
|
|
$rejectIds = $this->findTags($reject);
|
|
|
|
}
|
|
|
|
return array($acceptIds, $rejectIds);
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-08-31 12:55:42 +02:00
|
|
|
// find all of the event Ids that belong to tags with certain names
|
2014-02-02 18:10:21 +01:00
|
|
|
public function findTags($array) {
|
|
|
|
$ids = array();
|
|
|
|
foreach ($array as $a) {
|
2014-11-20 10:40:24 +01:00
|
|
|
$conditions['OR'][] = array('LOWER(name) like' => '%' . strtolower($a) . '%');
|
2014-02-02 18:10:21 +01:00
|
|
|
}
|
|
|
|
$params = array(
|
|
|
|
'recursive' => 1,
|
|
|
|
'contain' => 'EventTag',
|
|
|
|
//'fields' => array('id', 'name'),
|
|
|
|
'conditions' => $conditions
|
|
|
|
);
|
|
|
|
$result = $this->find('all', $params);
|
|
|
|
foreach ($result as $tag) {
|
|
|
|
foreach ($tag['EventTag'] as $eventTag) {
|
|
|
|
$ids[] = $eventTag['event_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-04-27 00:44:28 +02:00
|
|
|
public function captureTag($tag, $user) {
|
|
|
|
$existingTag = $this->find('first', array(
|
|
|
|
'recursive' => -1,
|
2016-02-17 09:07:05 +01:00
|
|
|
'conditions' => array('LOWER(name)' => strtolower($tag['name']))
|
2015-04-27 00:44:28 +02:00
|
|
|
));
|
|
|
|
if (empty($existingTag)) {
|
2016-03-30 18:32:17 +02:00
|
|
|
if ($user['Role']['perm_tag_editor']) {
|
|
|
|
$this->create();
|
|
|
|
$tag = array(
|
|
|
|
'name' => $tag['name'],
|
|
|
|
'colour' => $tag['colour'],
|
|
|
|
'exportable' => $tag['exportable'],
|
|
|
|
);
|
|
|
|
$this->save($tag);
|
|
|
|
return $this->id;
|
|
|
|
} else return false;
|
2015-04-27 00:44:28 +02:00
|
|
|
}
|
|
|
|
return $existingTag['Tag']['id'];
|
|
|
|
}
|
2015-08-31 12:55:42 +02:00
|
|
|
|
|
|
|
// find all tags that belong to a given eventId
|
|
|
|
public function findEventTags($eventId) {
|
|
|
|
$tags = array();
|
|
|
|
$params = array(
|
|
|
|
'recursive' => 1,
|
|
|
|
'contain' => 'EventTag',
|
|
|
|
);
|
|
|
|
$result = $this->find('all', $params);
|
|
|
|
foreach ($result as $tag) {
|
|
|
|
foreach ($tag['EventTag'] as $eventTag) {
|
|
|
|
if ($eventTag['event_id'] == $eventId) {
|
|
|
|
$tags[] = $tag['Tag'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tags;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-10-30 16:28:51 +01:00
|
|
|
public function random_color() {
|
|
|
|
$colour = '#';
|
|
|
|
for ($i = 0; $i < 3; $i++) $colour .= str_pad(dechex(mt_rand(0,255)), 2, '0', STR_PAD_LEFT);
|
|
|
|
return $colour;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-11-26 04:31:24 +01:00
|
|
|
public function quickAdd($name, $colour = false) {
|
2015-11-24 03:27:14 +01:00
|
|
|
$this->create();
|
2015-11-26 04:31:24 +01:00
|
|
|
if ($colour === false) $colour = $this->random_color();
|
2015-11-24 03:27:14 +01:00
|
|
|
$data = array(
|
|
|
|
'name' => $name,
|
2015-11-26 04:31:24 +01:00
|
|
|
'colour' => $colour,
|
2015-11-24 03:27:14 +01:00
|
|
|
'exportable' => 1
|
|
|
|
);
|
|
|
|
return ($this->save($data));
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-11-26 04:31:24 +01:00
|
|
|
public function quickEdit($tag, $name, $colour) {
|
|
|
|
if ($tag['Tag']['colour'] !== $colour || $tag['Tag']['name'] !== $name) {
|
|
|
|
$tag['Tag']['name'] = $name;
|
|
|
|
$tag['Tag']['colour'] = $colour;
|
|
|
|
return ($this->save($tag['Tag']));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-11-26 04:31:24 +01:00
|
|
|
public function getTagsForNamespace($namespace) {
|
|
|
|
$tags_temp = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'contain' => 'EventTag',
|
|
|
|
'conditions' => array('UPPER(name) LIKE' => strtoupper($namespace) . '%'),
|
|
|
|
));
|
|
|
|
$tags = array();
|
|
|
|
foreach ($tags_temp as &$temp) $tags[strtoupper($temp['Tag']['name'])] = $temp;
|
|
|
|
return $tags;
|
|
|
|
}
|
2015-08-31 12:55:42 +02:00
|
|
|
}
|