Skip to content

Added: Support for Ticket Linking #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ This client supports Zammad 3.4.1 and newer.
The API client needs [composer](https://getcomposer.org/). For installation have a look at its [documentation](https://getcomposer.org/download/).
Additionally, the API client needs PHP 7.2 or newer.

### Integration into your project
Add the following to the "require" section of your project's composer.json file:
```json
"zammad/zammad-api-client-php": "2.0.*"
```

### Installing the API client's dependencies
Fetch the API client's code and its dependencies by updating your project's dependencies with composer:
:
```
$ composer update
```

Once installed, you have to include the generated autoload.php into your project's code:
```php
require_once dirname(__DIR__).'/vendor/autoload.php';
$ composer require zammad/zammad-api-client-php
```

## How to use the API client
Expand All @@ -47,7 +36,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 +72,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 +234,23 @@ 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::LINKS )->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 +315,7 @@ $client->resource( ResourceType::TICKET );
| GROUP|✔|✔|–|✔|✔|–|–|–|
| USER|✔|✔|✔|✔|✔|–|–|✔|
| TAG|✔|–|✔|–|–|✔|✔|–|
|LINKS|✔|–|–|–|–|✔|–|–|

## Publishing

Expand Down
92 changes: 92 additions & 0 deletions src/Resource/Links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace ZammadAPIClient\Resource;

use ZammadAPIClient\ResourceType;

class Links extends AbstractResource
{
const URLS = [
'get' => 'links',
'add' => 'links/add',
// TODO: 'delete' => 'links/remove'
];

const LINKTYPES = [
'normal',
'parent',
'child'
];
public function add(Ticket $source, Ticket $target, $type = 'normal')
{
$this->clearError();
if(empty($source->getID()) || empty($target->getID())){
$this->setError('Tickets not valid.');
return [];
}
if(!in_array($type,self::LINKTYPES, true)){
$this->setError('Linktype is not supported.');
return [];
}
$data = [
"link_type" => $type,
"link_object_target" => "Ticket",
"link_object_target_value" => $target->getValue('id'),
"link_object_source" => "Ticket",
"link_object_source_number" => $source->getValue('number')
];
$url = $this->getURL('add');
$response = $this->getClient()->post(
$url,
$data,
[
'expand' => true
]
);
if($response->hasError()){
$this->setError($response->getError());
return $this;
}
$this->clearError();
$this->setRemoteData($response->getData());
$this->clearUnsavedValues();
return $this;
}

public function get($object_id)
{
$this->clearError();
if(empty($object_id)){
$this->setError('LinkID Object not given');
return [];
}

$url = $this->getURL('get');
$response = $this->getClient()->post(
$url,
[
"link_object" => "Ticket",
"link_object_value" => $object_id
],
[
'expand' => true
]
);
if($response->hasError()){
$this->setError($response->getError());
return $this;
}
$this->clearError();
$this->setRemoteData($response->getData());
$this->clearUnsavedValues();
return $this;
}

public function delete()
{
$this->clearError();
$this->setError('not yet supported.'); //TODO: implement delete();
return [];
}

}
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 LINKS = '\\ZammadAPIClient\\Resource\\LINKS';
}