diff --git a/README.md b/README.md index d733c89..25432fc 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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: @@ -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: @@ -308,6 +315,7 @@ $client->resource( ResourceType::TICKET ); | GROUP|✔|✔|–|✔|✔|–|–|–| | USER|✔|✔|✔|✔|✔|–|–|✔| | TAG|✔|–|✔|–|–|✔|✔|–| +|LINKS|✔|–|–|–|–|✔|–|–| ## Publishing diff --git a/src/Resource/Links.php b/src/Resource/Links.php new file mode 100644 index 0000000..9ca02ce --- /dev/null +++ b/src/Resource/Links.php @@ -0,0 +1,92 @@ + '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 []; + } + +} diff --git a/src/ResourceType.php b/src/ResourceType.php index aa5fb3c..2d9efcb 100644 --- a/src/ResourceType.php +++ b/src/ResourceType.php @@ -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'; }