summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaScheduledDowntime.php
blob: 7fc3f78acbfedb282437fcb9022b9ee209cba702 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use RuntimeException;

class IcingaScheduledDowntime extends IcingaObject
{
    protected $table = 'icinga_scheduled_downtime';

    protected $defaultProperties = [
        'id'                => null,
        'uuid'              => null,
        'zone_id'           => null,
        'object_name'       => null,
        'object_type'       => null,
        'disabled'          => 'n',
        'author'            => null,
        'comment'           => null,
        'fixed'             => null,
        'duration'          => null,
        'apply_to'          => null,
        'assign_filter'     => null,
        'with_services'     => null,
    ];

    protected $uuidColumn = 'uuid';

    protected $supportsImports = true;

    protected $supportsRanges = true;

    protected $supportsApplyRules = true;

    protected $relations = [
        'zone' => 'IcingaZone',
    ];

    protected $booleans = [
        'fixed' => 'fixed',
    ];

    protected $intervalProperties = [
        'duration' => 'duration',
    ];

    protected $propertiesNotForRendering = [
        'id',
        'apply_to',
        'object_name',
        'object_type',
        'with_services',
    ];

    /**
     * @return string
     */
    protected function renderObjectHeader()
    {
        if ($this->isApplyRule()) {
            if (($to = $this->get('apply_to')) === null) {
                throw new RuntimeException(sprintf(
                    'Applied notification "%s" has no valid object type',
                    $this->getObjectName()
                ));
            }

            return sprintf(
                "%s %s %s to %s {\n",
                $this->getObjectTypeName(),
                $this->getType(),
                c::renderString($this->getObjectName()),
                ucfirst($to)
            );
        } else {
            return parent::renderObjectHeader();
        }
    }

    public function getOnDeleteUrl()
    {
        if ($this->isApplyRule()) {
            return 'director/scheduled-downtimes/applyrules';
        } elseif ($this->isTemplate()) {
            return 'director/scheduled-downtimes/templates';
        } else {
            return 'director/scheduled-downtimes';
        }
    }

    public function isActive($now = null)
    {
        if ($now === null) {
            $now = time();
        }

        foreach ($this->ranges()->getRanges() as $range) {
            if ($range->isActive($now)) {
                return true;
            }
        }

        // TODO: no range currently means (and renders) "never", Icinga behaves
        //       different. Figure out whether and how we should support this
        return false;
    }

    /**
     * @return string
     */
    protected function renderSuffix()
    {
        if ($this->get('with_services') === 'y' && $this->get('apply_to') === 'host') {
            return parent::renderSuffix() . $this->renderCloneForServices();
        } else {
            return parent::renderSuffix();
        }
    }

    protected function prefersGlobalZone()
    {
        return false;
    }

    protected function renderCloneForServices()
    {
        $services = clone($this);
        $services
            ->set('with_services', 'n')
            ->set('apply_to', 'service');

        return $services->toConfigString();
    }
}