Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/examples/test.php
/test*.php
*~
.phpunit.result.cache
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $client = new Client([
]);
```
Besides using a combination of `username` and `password`, you can alternatively give an `http_token` or an `oauth2_token`.
**Important:** You have to activate API access in Zammad.
**Important:** You have to activate API access in Zammad. Should be active by default.

### Fetching a single Resource object
To fetch a `Resource` object by ID, e. g. a ticket with ID 34, use the `Client` object:
Expand Down Expand Up @@ -83,6 +83,7 @@ Additionally you can have a look at the REST interface documentation of Zammad:
* [Ticket priorities](https://docs.zammad.org/en/latest/api/ticket-priority.html)
* [Ticket states](https://docs.zammad.org/en/latest/api/ticket-state.html)
* [Tags](https://docs.zammad.org/en/latest/api/tags.html)
* [Linking Tickets](https://docs.zammad.org/en/latest/api/ticket/links.html)

#### Fetching a ticket's articles
If you already have a ticket object, you can easily fetch its articles:
Expand Down Expand Up @@ -244,6 +245,21 @@ use ZammadAPIClient\ResourceType;

$tags = $client->resource( ResourceType::TAG )->search('my tag');
```
### Linking Tickets

#### Linking two Tickets

Zammad can link two or more Ticket objects. Allowed Link Types are `normal`, `parent` or `child`.

```php
use ZammadAPIClient\ResourceType;

// First parameter $sourceTicket is the Ticket that should be linked
// Second parameter $targetTicket is the Ticket that $sourceTicket should be linked to
// Third parameter is the LinkType the $sourceTicket will be linked to $targetTicket with.
$client->resource( ResourceType::LINK )->add( $sourceTicket, $targetTicket, 'normal' );
```

### Object import

Besides the usual methods available for objects, there is also a method available to import these via CSV. Example for text module CSV import:
Expand Down Expand Up @@ -308,6 +324,7 @@ $client->resource( ResourceType::TICKET );
| GROUP|✔|✔|–|✔|✔|–|–|–|
| USER|✔|✔|✔|✔|✔|–|–|✔|
| TAG|✔|–|✔|–|–|✔|✔|–|
| LINK|✔|–|–|–|–|✔|✔|–|

## Publishing

Expand Down
10 changes: 8 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@ public function put( $url, array $data = [], array $url_parameters = [] )
*
* @return Response object
*/
public function delete( $url, array $url_parameters = [] )
public function delete( $url, array $url_parameters = [], array $data = [] )
{
$options = [];
if ( !empty($data) ) {
$options['json'] = $data;
}

$response = $this->request(
'DELETE',
$url,
$url_parameters
$url_parameters,
$options
);

return $response;
Expand Down
145 changes: 145 additions & 0 deletions src/Resource/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/**
* @package Zammad API Client
* @author Fran Rey <franreycastedo@gmail.com>
*/

namespace ZammadAPIClient\Resource;

class Link extends AbstractResource
{
const URLS = [
'get' => 'links',
'add' => 'links/add',
'remove' => 'links/remove'
];

const LINKTYPES = [
'normal',
'parent',
'child'
];

/**
* Fetches links of an object.
*
* @param int $object_id ID of the object to fetch links for.
* @param string $object_type Type of object to fetch links 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'),
[
'link_object' => $object_type,
'link_object_value' => $object_id,
]
);

if ( $response->hasError() ) {
$this->setError( $response->getError() );
}
else {
$this->clearError();
$this->setRemoteData( $response->getData() );
}

return $this;
}

/**
* Adds a link between two tickets.
*
* @param Ticket $source Source ticket object.
* @param Ticket $target Target ticket object.
* @param string $type Link type (default: 'normal').
*
* @return object This object.
*/
public function add(Ticket $source, Ticket $target, $type = 'normal')
{
$this->clearError();

if ( empty($source->getID()) || empty($target->getID()) ) {
$this->setError('Tickets not valid.');
return $this;
}
if ( !in_array($type, self::LINKTYPES, true) ) {
$this->setError('Linktype is not supported.');
return $this;
}

$response = $this->getClient()->post(
$this->getURL('add'),
[
'link_type' => $type,
'link_object_target' => 'Ticket',
'link_object_target_value' => $target->getID(),
'link_object_source' => 'Ticket',
'link_object_source_number' => $source->getValue('number')
]
);

if ( $response->hasError() ) {
$this->setError( $response->getError() );
}

return $this;
}

/**
* Removes a link between two tickets.
*
* @param Ticket $source Source ticket object.
* @param Ticket $target Target ticket object.
* @param string $type Link type (default: 'normal').
*
* @return object This object.
*/
public function remove(Ticket $source, Ticket $target, $type = 'normal')
{
$this->clearError();

if ( empty($source->getID()) || empty($target->getID()) ) {
$this->setError('Tickets not valid.');
return $this;
}
if ( !in_array($type, self::LINKTYPES, true) ) {
$this->setError('Linktype is not supported.');
return $this;
}

$response = $this->getClient()->delete(
$this->getURL('remove'),
[],
[
'link_type' => $type,
'link_object_source' => 'Ticket',
'link_object_source_value' => $source->getID(),
'link_object_target' => 'Ticket',
'link_object_target_value' => $target->getID()
]
);

if ( $response->hasError() ) {
$this->setError( $response->getError() );
return $this;
}

$this->clearError();
$this->clearRemoteData();
$this->clearUnsavedValues();

return $this;
}
}
1 change: 1 addition & 0 deletions src/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class ResourceType
const TICKET = '\\ZammadAPIClient\\Resource\\Ticket';
const TICKET_ARTICLE = '\\ZammadAPIClient\\Resource\\TicketArticle';
const TAG = '\\ZammadAPIClient\\Resource\\Tag';
const LINK = '\\ZammadAPIClient\\Resource\\Link';
}
Loading