-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathTag.php
More file actions
184 lines (156 loc) · 5.15 KB
/
Tag.php
File metadata and controls
184 lines (156 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* @package Zammad API Client
* @author Alexander Reichardt <alexander.reichardt@icloud.com>
*/
namespace ZammadAPIClient\Resource;
class Tag extends AbstractResource
{
const URLS = [
'get' => 'tags',
'search' => 'tag_search?term={query}',
'add' => 'tags/add',
'remove' => 'tags/remove',
'all' => 'tag_list',
'create' => 'tag_list',
'update' => 'tag_list/{object_id}',
'delete' => 'tag_list/{object_id}',
];
/**
* Fetches tags of an object.
*
* @param integer $object_id ID of the object to fetch tags for.
* @param string $object_type Type of object to fetch tags for (e. g. 'Ticket').
*
* @return object This object.
*/
public function get($object_id, $object_type = 'Ticket')
{
$this->clearError();
$object_id = intval($object_id);
if ( empty($object_id) ) {
throw new \RuntimeException('Missing object ID');
}
$response = $this->getClient()->get(
$this->getURL('get'),
[
'object' => $object_type,
'o_id' => $object_id,
]
);
if ( $response->hasError() ) {
$this->setError( $response->getError() );
}
else {
$this->clearError();
$this->setRemoteData( $response->getData() );
}
return $this;
}
/**
* Adds a tag to an object.
*
* @param integer $object_id ID of the object to fetch tags for.
* @param string $tag Tag to add.
* @param string $object_type Type of object to fetch tags for (e. g. 'Ticket').
*
* @return object This object.
*/
public function add($object_id, $tag, $object_type = 'Ticket')
{
$this->clearError();
$object_id = intval($object_id);
if ( empty($object_id) ) {
throw new \RuntimeException('Missing object ID');
}
if ( empty($tag) ) {
throw new \RuntimeException('Missing tag');
}
$response = $this->getClient()->post(
$this->getURL('add'),
[
'object' => $object_type,
'o_id' => $object_id,
'item' => $tag,
]
);
if ( $response->hasError() ) {
$this->setError( $response->getError() );
}
return $this;
}
/**
* Fetches tags for given search term.
* Pagination available.
*
* @param string $search_term Search term.
* @param integer $page Page of tags, optional, if given, $objects_per_page must also be given
* @param integer $objects_per_page Number of tags per page, optional, if given, $page must also be given
*
* @return mixed Returns array of ZammadAPIClient\Resource\... objects
* or this object on failure.
*/
public function search($search_term, $page = null, $objects_per_page = null)
{
$this->clearError();
if ( empty($search_term) ) {
throw new \RuntimeException('Missing search term');
}
$response = $this->getClient()->get(
$this->getURL('search'),
[
'term' => $search_term,
]
);
if ( $response->hasError() ) {
$this->setError( $response->getError() );
}
$this->clearError();
// Return array of resource objects if no $object_id was given.
// Note: the resource object (this object) used to execute get() will be left empty in this case.
$objects = [];
foreach ( $response->getData() as $object_data ) {
$object = $this->getClient()->resource( get_class($this) );
$object->setRemoteData($object_data);
$objects[] = $object;
}
return $objects;
}
/**
* Removes a tag from an object.
*
* @param integer $object_id ID of the object to fetch tags for.
* @param string $tag Tag to remove.
* @param string $object_type Type of object to fetch tags for (e. g. 'Ticket').
*
* @return object This object.
*/
public function remove($object_id, $tag, $object_type = 'Ticket')
{
$this->clearError();
if ( empty($object_id) ) {
throw new \RuntimeException('Missing object ID');
}
if ( empty($tag) ) {
throw new \RuntimeException('Missing tag');
}
// Delete object in Zammad.
$response = $this->getClient()->delete(
$this->getURL('remove'),
[
'object' => $object_type,
'o_id' => $object_id,
'item' => $tag,
]
);
if ( $response->hasError() ) {
$this->setError( $response->getError() );
return $this;
}
// Clear data of this (local) object.
$this->clearError();
$this->clearRemoteData();
$this->clearUnsavedValues();
return $this;
}
}