1<?php
2
3namespace FYKOS\dokuwiki\Extension\PluginNewsFeed\Renderer;
4
5use FYKOS\dokuwiki\Extension\PluginNewsFeed\Model\ModelNews;
6use dokuwiki\Form\Form;
7use dokuwiki\Form\InputElement;
8
9/**
10 * Class VyfukRenderer
11 * @author Michal Červeňák <miso@fykos.cz>
12 */
13class VyfukRenderer extends AbstractRenderer {
14
15    public function render(string $innerHtml, string $formHtml, ModelNews $news): string {
16        $html = '<div class="col-12 row mb-3">';
17        $html .= '<div class="col-12">';
18        $html .= '<div class="card card-outline-' . $news->category . ' card-outline-vyfuk-orange">';
19        $html .= '<div class="card-block">';
20        $html .= $innerHtml;
21        $html .= $formHtml;
22        $html .= '</div>';
23        $html .= '</div>';
24        $html .= '</div>';
25        $html .= '</div>';
26        return $html;
27    }
28
29    protected function getHeader(ModelNews $news) {
30        return '<h4 class="card-title">' . $news->title . '</h4>' .
31            '<p class="card-text">' .
32            '<small class="text-muted">' . $news->getLocalDate(function ($key) {
33                return $this->helper->getLang($key);
34            }) . '</small>' .
35            '</p>';
36    }
37
38    public function renderContent(ModelNews $data, array $params): string {
39        $innerHtml = $this->getHeader($data);
40        $innerHtml .= $this->getText($data);
41
42        $innerHtml .= $this->getLink($data);
43        $innerHtml .= $this->getSignature($data);
44        return $innerHtml;
45    }
46
47    public function renderEditFields(array $params): string {
48
49        if (auth_quickaclcheck('start') < AUTH_EDIT) {
50            return '';
51        }
52        $html = '<button data-toggle="modal" data-target="#feedModal' . $params['id'] . '" class="btn btn-primary" >' .
53            $this->helper->getLang('btn_opt') . '</button>';
54        $html .= '<div id="feedModal' . $params['id'] . '" class="modal" data-id="' . $params["id"] . '">';
55        $html .= '<div class="modal-dialog">';
56        $html .= '<div class="modal-content">';
57        $html .= $this->getModalHeader();
58        $html .= $this->getPriorityField($params['id'], $params['stream'], $params);
59        $html .= $this->btnEditNews($params['id'], $params['stream']);
60        $html .= '</div>';
61        $html .= '</div>';
62        $html .= '</div>';
63        return $html;
64    }
65
66    protected function getModalHeader() {
67        $html = '';
68        $html .= '<div class="modal-header">';
69        $html .= '<h5 class="modal-title">' . $this->helper->getLang('') . 'Upaviť novinku</h5>';
70        $html .= '<button type="button" class="close" data-dismiss="modal" aria-label="Close">';
71        $html .= '<span aria-hidden="true">×</span>';
72        $html .= '</button>';
73        $html .= ' </div>';
74        return $html;
75    }
76
77    /**
78     * @param $id
79     * @param $streamName
80     * @param $params
81     * @return string
82     */
83    protected function getPriorityField($id, $streamName, $params) {
84        $html = '';
85        if ($params['editable'] !== 'true') {
86            return '';
87        }
88        if (!$params['stream']) {
89            return '';
90        }
91
92        $html .= '<div class="modal-body">';
93        $form = new Form();
94        $form->addClass('block');
95
96        $form->setHiddenField('do', \helper_plugin_newsfeed::FORM_TARGET);
97        $form->setHiddenField('news[id]', $id);
98        $form->setHiddenField('news[stream]', $streamName);
99        $form->setHiddenField('news[do]', 'priority');
100
101        $stream = $this->helper->serviceStream->findByName($streamName);
102
103        $priority = $this->helper->servicePriority->findByNewsAndStream($id, $stream->streamId);
104        $form->addTagOpen('div')->addClass('form-group');
105        $priorityValue = new InputElement('number', 'priority[value]', $this->helper->getLang('valid_from'));
106        $priorityValue->attr('class', 'form-control')->val($priority->getPriorityValue());
107        $form->addElement($priorityValue);
108        $form->addTagClose('div');
109
110        $form->addTagOpen('div')->addClass('form-group');
111        $priorityFromElement = new InputElement('datetime-local', 'priority[from]', $this->helper->getLang('valid_from'));
112        $priorityFromElement->val($priority->priorityFrom ?: date('Y-m-d\TH:i:s', time()))
113            ->attr('class', 'form-control');
114        $form->addElement($priorityFromElement);
115        $form->addTagClose('div');
116
117        $form->addTagOpen('div')->addClass('form-group');
118        $priorityToElement = new InputElement('datetime-local', 'priority[to]', $this->helper->getLang('valid_to'));
119        $priorityToElement->val($priority->priorityTo ?: date('Y-m-d\TH:i:s', time()))
120            ->attr('class', 'form-control');
121        $form->addElement($priorityToElement);
122        $form->addTagClose('div');
123
124        $form->addButton('submit', $this->helper->getLang('btn_save_priority'))->addClass('btn btn-success');
125        $html .= $form->toHTML();
126        $html .= '</div>';
127
128        return $html;
129    }
130
131    protected function btnEditNews($id, $stream) {
132        $html = '';
133
134        $html .= '<div class="modal-footer">';
135        $html .= '<div class="btn-group">';
136        $editForm = new Form();
137        $editForm->setHiddenField('do', \helper_plugin_newsfeed::FORM_TARGET);
138        $editForm->setHiddenField('news[id]', $id);
139        $editForm->setHiddenField('news[do]', 'edit');
140        $editForm->addButton('submit', $this->helper->getLang('btn_edit_news'))->addClass('btn btn-info');
141        $html .= $editForm->toHTML();
142
143        if ($stream) {
144            $deleteForm = new Form();
145            $deleteForm->setHiddenField('do', \helper_plugin_newsfeed::FORM_TARGET);
146            $deleteForm->setHiddenField('news[do]', 'delete');
147            $deleteForm->setHiddenField('news[stream]', $stream);
148            $deleteForm->setHiddenField('news[id]', $id);
149            $deleteForm->addButton('submit', $this->helper->getLang('delete_news'))->attr('data-warning', true)
150                ->addClass('btn btn-danger');
151            $html .= $deleteForm->toHTML();
152        }
153
154        $purgeForm = new Form();
155        $purgeForm->setHiddenField('do', \helper_plugin_newsfeed::FORM_TARGET);
156        $purgeForm->setHiddenField('news[do]', 'purge');
157        $purgeForm->setHiddenField('news[id]', $id);
158        $purgeForm->addButton('submit', $this->helper->getLang('cache_del'))->addClass('btn btn-warning');
159        $html .= $purgeForm->toHTML();
160        $html .= '</div>';
161        $html .= '</div>';
162
163        return $html;
164    }
165
166    /**
167     * @param $news ModelNews
168     * @return null|string
169     */
170    protected function getText(ModelNews $news) {
171        return $news->renderText();
172    }
173
174    /**
175     * @param $news ModelNews
176     * @return string
177     */
178    protected function getSignature(ModelNews $news) {
179        return '<div class="card-text text-right">
180            <a href="mailto:' . hsc($news->authorEmail) . '" class="mail" title="' . hsc($news->authorEmail) .
181            '"><span class="fa fa-envelope"></span>' . hsc($news->authorName) . '</a>
182        </div>';
183    }
184
185    /**
186     * @param $news ModelNews
187     * @return string
188     */
189    protected function getLink(ModelNews $news) {
190        if ($news->hasLink()) {
191            if (preg_match('|^https?://|', $news->linkHref)) {
192                $href = hsc($news->linkHref);
193            } else {
194                $href = wl($news->linkHref, null, true);
195            }
196            return '<p><a class="btn btn-secondary" href="' . $href . '">' . $news->linkTitle . '</a></p>';
197        }
198        return '';
199    }
200}
201