|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Example for Tag administration scope. |
| 5 | + * |
| 6 | + * Manage tags globally: list, create, rename and delete tags. |
| 7 | + * See https://docs.zammad.org/en/latest/api/ticket/tags.html#administration-scope |
| 8 | + */ |
| 9 | + |
| 10 | +use ZammadAPIClient\Client; |
| 11 | +use ZammadAPIClient\ResourceType; |
| 12 | + |
| 13 | +require __DIR__ . '/../vendor/autoload.php'; |
| 14 | + |
| 15 | +$client = new Client([ |
| 16 | + 'url' => 'https://my.zammad.com', |
| 17 | + 'username' => 'my-username', |
| 18 | + 'password' => 'my-password', |
| 19 | +]); |
| 20 | + |
| 21 | +// List all tags (administration scope) |
| 22 | +$tags = $client->resource(ResourceType::TAG)->all(); |
| 23 | +echo "All tags:\n"; |
| 24 | +foreach ($tags as $tag) { |
| 25 | + echo sprintf( |
| 26 | + " ID: %s, Name: %s, Count: %s\n", |
| 27 | + $tag->getID(), |
| 28 | + $tag->getValue('name'), |
| 29 | + $tag->getValue('count') |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +// Create a new tag |
| 34 | +$tag = $client->resource(ResourceType::TAG); |
| 35 | +$tag->setValue('name', 'example-tag'); |
| 36 | +$tag->save(); |
| 37 | + |
| 38 | +echo "\nTag 'example-tag' created.\n"; |
| 39 | + |
| 40 | +// Find the created tag to get its ID |
| 41 | +$tag_id = null; |
| 42 | +$tags = $client->resource(ResourceType::TAG)->all(); |
| 43 | +foreach ($tags as $t) { |
| 44 | + if ($t->getValue('name') === 'example-tag') { |
| 45 | + $tag_id = $t->getID(); |
| 46 | + break; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +if ($tag_id) { |
| 51 | + echo "Tag ID: $tag_id\n"; |
| 52 | + |
| 53 | + // Rename the tag (update) |
| 54 | + // Set the tag's ID so save() will call update() instead of create() |
| 55 | + $tag = $client->resource(ResourceType::TAG); |
| 56 | + $tag->setRemoteData(['id' => $tag_id]); |
| 57 | + $tag->setValue('name', 'example-tag-renamed'); |
| 58 | + $tag->save(); |
| 59 | + |
| 60 | + echo "Tag renamed to 'example-tag-renamed'.\n"; |
| 61 | + |
| 62 | + // Delete the tag |
| 63 | + $tag = $client->resource(ResourceType::TAG); |
| 64 | + $tag->setRemoteData(['id' => $tag_id]); |
| 65 | + $tag->delete(); |
| 66 | + |
| 67 | + echo "Tag deleted.\n"; |
| 68 | +} |
0 commit comments