summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/Detail/CommentDetail.php
blob: 5b0923e435fca6a370cb1cd973482cccc214ed8e (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
136
137
138
139
140
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Widget\Detail;

use Icinga\Date\DateFormatter;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Common\TicketLinks;
use Icinga\Module\Icingadb\Model\Comment;
use Icinga\Module\Icingadb\Widget\MarkdownText;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
use ipl\Stdlib\Filter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Widget\HorizontalKeyValue;
use ipl\Web\Widget\StateBall;
use ipl\Web\Widget\TimeUntil;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;

class CommentDetail extends BaseHtmlElement
{
    use Auth;
    use TicketLinks;

    protected $comment;

    protected $defaultAttributes = ['class' => ['object-detail', 'comment-detail']];

    protected $tag = 'div';

    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
    }

    protected function createComment(): array
    {
        return [
            Html::tag('h2', t('Comment')),
            new MarkdownText($this->createTicketLinks($this->comment->text))
        ];
    }

    protected function createDetails(): array
    {
        $details = [];

        if (getenv('ICINGAWEB_EXPORT_FORMAT') === 'pdf') {
            if ($this->comment->object_type === 'host') {
                $details[] = new HorizontalKeyValue(t('Host'), [
                    $this->comment->host->name,
                    ' ',
                    new StateBall($this->comment->host->state->getStateText())
                ]);
            } else {
                $details[] = new HorizontalKeyValue(t('Service'), Html::sprintf(
                    t('%s on %s', '<service> on <host>'),
                    [$this->comment->service->name, ' ', new StateBall($this->comment->service->state->getStateText())],
                    $this->comment->host->name
                ));
            }

            $details[] = new HorizontalKeyValue(t('Author'), $this->comment->author);
            $details[] = new HorizontalKeyValue(
                t('Acknowledgement'),
                $this->comment->entry_type === 'ack' ? t('Yes') : t('No')
            );
            $details[] = new HorizontalKeyValue(
                t('Persistent'),
                $this->comment->is_persistent ? t('Yes') : t('No')
            );
            $details[] = new HorizontalKeyValue(
                t('Created'),
                DateFormatter::formatDateTime($this->comment->entry_time->getTimestamp())
            );
            $details[] = new HorizontalKeyValue(t('Expires'), $this->comment->expire_time !== null
                ? DateFormatter::formatDateTime($this->comment->expire_time->getTimestamp())
                : t('Never'));
        } else {
            if ($this->comment->expire_time !== null) {
                $details[] = Html::tag(
                    'p',
                    Html::sprintf(
                        $this->comment->entry_type === 'ack'
                            ? t('This acknowledgement expires %s.', '..<time-until>')
                            : t('This comment expires %s.', '..<time-until>'),
                        new TimeUntil($this->comment->expire_time->getTimestamp())
                    )
                );
            }

            if ($this->comment->is_sticky) {
                $details[] = Html::tag('p', t('This acknowledgement is sticky.'));
            }
        }

        if (! empty($details)) {
            array_unshift($details, Html::tag('h2', t('Details')));
        }

        return $details;
    }

    protected function createRemoveCommentForm()
    {
        if (getenv('ICINGAWEB_EXPORT_FORMAT') === 'pdf') {
            return null;
        }

        $action = Links::commentsDelete();
        $action->setFilter(Filter::equal('name', $this->comment->name));

        return (new DeleteCommentForm())
            ->setObjects([$this->comment])
            ->populate(['redirect' => '__BACK__'])
            ->setAction($action->getAbsoluteUrl());
    }

    protected function assemble()
    {
        $this->add($this->createComment());

        $details = $this->createDetails();

        if (! empty($details)) {
            $this->add($details);
        }

        if (
            $this->isGrantedOn(
                'icingadb/command/comment/delete',
                $this->comment->{$this->comment->object_type}
            )
        ) {
            $this->add($this->createRemoveCommentForm());
        }
    }
}