summaryrefslogtreecommitdiffstats
path: root/library/Generictts/Ticket.php
blob: b8ded1dcc3992fc050f98fd7a048680e595b4c59 (plain)
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
<?php

/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Generictts;

use Icinga\Application\Config;
use Icinga\Application\Hook\TicketHook;

/**
 * GenericTTS TicketHook implementation
 */
class Ticket extends TicketHook
{
    /**
     * GenericTTS configuration
     *
     * @var \Icinga\Application\Config
     */
    protected $config;

    /**
     * Configured trouble ticket system integrations
     *
     * @var \Icinga\Application\Hook\Ticket\TicketPattern[]
     */
    protected $ticketPatterns;

    /**
     * {@inheritdoc}
     */
    protected function init()
    {
        $config = Config::module('generictts');
        $this->config = $config;

        $ticketPatterns = array();
        foreach ($config as $section => $values) {
            if ($values->get('url')) { // Skip integrations that don't contain a URL
                $ticketPattern = $this->createTicketPattern($section, $values->get('pattern'));
                if ($ticketPattern->isValid()) { // Skip integrations that don't contain a pattern
                    $ticketPatterns[$section] = $ticketPattern;
                }
            }
        }
        $this->ticketPatterns = $ticketPatterns;
    }

    /**
     * {@inheritdoc}
     *
     * @return  \Icinga\Application\Hook\Ticket\TicketPattern[]
     */
    public function getPattern()
    {
        return $this->ticketPatterns;
    }

    /**
     * {@inheritdoc}
     */
    public function createLink($match)
    {
        /** @var \Icinga\Application\Hook\Ticket\TicketPattern $match */
        return sprintf(
            '<a href="%s" target="_blank">%s</a>',
            preg_replace('/\$1/', rawurlencode($match[1]), $this->config->get($match->getName(), 'url')),
            $match[0]
        );
    }
}