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