Skip to content

Commit 44496e8

Browse files
Merge pull request #4519 from SteveGilvarry/EventTags2
Add Tags to event search and return tag data with events
2 parents ea6e387 + 58a0e68 commit 44496e8

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

web/api/app/Controller/EventsController.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,22 @@ public function index() {
5050

5151
$this->FilterComponent = $this->Components->load('Filter');
5252
$named_params = $this->request->params['named'];
53+
$tag_filter_value = null;
54+
$tag_filter_field = null;
5355
if ($named_params) {
56+
if (isset($named_params['TagId'])) {
57+
$tag_filter_value = $named_params['TagId'];
58+
$tag_filter_field = 'Id';
59+
unset($named_params['TagId']);
60+
} else if (isset($named_params['Tag'])) {
61+
$tag_filter_value = $named_params['Tag'];
62+
$tag_filter_field = 'Name';
63+
unset($named_params['Tag']);
64+
} else if (isset($named_params['Tags'])) {
65+
$tag_filter_value = $named_params['Tags'];
66+
$tag_filter_field = 'Name';
67+
unset($named_params['Tags']);
68+
}
5469
# In 1.35.13 we renamed StartTime and EndTime to StartDateTime and EndDateTime.
5570
# This hack renames the query string params
5671
foreach ( $named_params as $k=>$v ) {
@@ -101,7 +116,21 @@ public function index() {
101116
#ZM\Debug(print_r($conditions, true));
102117

103118
} else {
104-
$conditions = $this->FilterComponent->buildFilter($_REQUEST);
119+
$raw_params = $_REQUEST;
120+
if (isset($raw_params['TagId'])) {
121+
$tag_filter_value = $raw_params['TagId'];
122+
$tag_filter_field = 'Id';
123+
unset($raw_params['TagId']);
124+
} else if (isset($raw_params['Tag'])) {
125+
$tag_filter_value = $raw_params['Tag'];
126+
$tag_filter_field = 'Name';
127+
unset($raw_params['Tag']);
128+
} else if (isset($raw_params['Tags'])) {
129+
$tag_filter_value = $raw_params['Tags'];
130+
$tag_filter_field = 'Name';
131+
unset($raw_params['Tags']);
132+
}
133+
$conditions = $this->FilterComponent->buildFilter($raw_params);
105134
}
106135
$settings = array(
107136
// https://github.com/ZoneMinder/ZoneMinder/issues/995
@@ -118,6 +147,7 @@ public function index() {
118147
'paramType' => 'querystring',
119148
);
120149

150+
$settings['contain'] = array('Tag');
121151
if ( isset($conditions['GroupId']) ) {
122152
$settings['joins'] = array(
123153
array(
@@ -128,7 +158,34 @@ public function index() {
128158
),
129159
),
130160
);
131-
$settings['contain'] = array('Group');
161+
$settings['contain'][] = 'Group';
162+
}
163+
if ($tag_filter_value !== null) {
164+
if (!isset($settings['joins'])) {
165+
$settings['joins'] = array();
166+
}
167+
$settings['joins'][] = array(
168+
'table' => 'Events_Tags',
169+
'type' => 'inner',
170+
'conditions' => array(
171+
'Events_Tags.EventId = Event.Id'
172+
),
173+
);
174+
$settings['joins'][] = array(
175+
'table' => 'Tags',
176+
'type' => 'inner',
177+
'conditions' => array(
178+
'Tags.Id = Events_Tags.TagId'
179+
),
180+
);
181+
if ($tag_filter_field === 'Id') {
182+
$tag_ids = is_array($tag_filter_value) ? $tag_filter_value : explode(',', $tag_filter_value);
183+
$tag_ids = array_map('intval', $tag_ids);
184+
$conditions[] = array('Tags.Id' => $tag_ids);
185+
} else {
186+
$conditions[] = array('Tags.Name' => $tag_filter_value);
187+
}
188+
$settings['group'] = 'Event.Id';
132189
}
133190
$settings['conditions'] = array($conditions, $mon_options);
134191

web/api/app/Model/Event.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class Event extends AppModel {
125125
);
126126

127127
public $actsAs = array(
128+
'Containable',
128129
'CakePHP-Enum-Behavior.Enum' => array(
129130
'Orientation' => array('ROTATE_0','ROTATE_90','ROTATE_180','ROTATE_270','FLIP_HORI','FLIP_VERT'),
130131
'Scheme' => array('Deep','Medium','Shallow')

0 commit comments

Comments
 (0)