1<?php 2 3use dokuwiki\Extension\AdminPlugin; 4use dokuwiki\Form\Form; 5use FYKOS\dokuwiki\Extension\PluginNewsFeed\Model\ModelStream; 6use FYKOS\dokuwiki\Extension\PluginNewsFeed\Model\ModelNews; 7 8/** 9 * Class admin_plugin_newsfeed_push 10 * @author Michal Červeňák <miso@fykos.cz> 11 */ 12class admin_plugin_newsfeed_push extends AdminPlugin { 13 14 private helper_plugin_newsfeed $helper; 15 16 public function __construct() { 17 $this->helper = $this->loadHelper('newsfeed'); 18 } 19 20 public function getMenuSort(): int { 21 return 291; 22 } 23 24 public function forAdminOnly(): bool { 25 return false; 26 } 27 28 public function getMenuText($lang): string { 29 return $this->getLang('push_menu'); 30 } 31 32 public function handle() { 33 global $INPUT; 34 35 if (!checkSecurityToken()) { 36 return; 37 } 38 $stream = $this->helper->serviceStream->findByName($INPUT->param('news')['stream']); 39 $newsId = $INPUT->param('news')['id']; 40 41 if ($stream && $newsId) { 42 $targetStreamId = $stream->streamId; 43 $streamIds = [$targetStreamId]; 44 if ($INPUT->str('all_dependence')) { 45 $this->helper->fullParentDependence($targetStreamId, $streamIds); 46 } 47 48 foreach ($streamIds as $streamId) { 49 $this->helper->servicePriority->store($newsId, $streamId); 50 } 51 header('Location: ' . $_SERVER['REQUEST_URI']); 52 exit(); 53 } 54 } 55 56 public function html(): void { 57 global $INPUT; 58 $streamName = $INPUT->param('news')['stream']; 59 $stream = $this->helper->serviceStream->findByName($streamName); 60 echo '<h1>' . $this->getLang('push_menu') . '</h1>'; 61 echo '<div class="info">' . $this->getLang('push_in_stream') . ': ' . $stream->name . '</div>'; 62 63 $streams = $this->helper->serviceStream->getAll(); 64 echo $this->getChangeStreamForm($streams)->toHTML(); 65 66 if ($stream->name) { 67 echo '<h2>' . $this->getLang('push_menu') . ': ' . $stream->name . '</h2>'; 68 69 $newsInStream = $this->newsToId($stream->getNews()); 70 $allNews = $this->helper->serviceNews->getAll(); 71 foreach ($this->newsToId($allNews) as $id) { 72 if (array_search($id, $newsInStream) === false) { 73 $news = $this->helper->serviceNews->getById($id); 74 echo $news->render('even', ' ', ' ', false); 75 echo $this->newsAddForm($stream->name, $id); 76 echo '<hr class="clearfix"/>'; 77 tpl_flush(); 78 } 79 } 80 } 81 } 82 83 /** 84 * @param $news ModelNews[] 85 * @return integer[] 86 */ 87 private function newsToId(array $news): array { 88 return array_map(function (ModelNews $value) { 89 return $value->newsId; 90 }, $news); 91 } 92 93 /** 94 * @param $streamValues ModelStream[] 95 * @return Form 96 * 97 */ 98 private function getChangeStreamForm(array $streamValues = []): Form { 99 $form = new Form(); 100 $form->addDropdown('news[stream]', array_map(function (ModelStream $value) { 101 return $value->name; 102 }, $streamValues), $this->getLang('stream')); 103 $form->addButton('submit', $this->getLang('push_choose_stream')); 104 return $form; 105 } 106 107 private function newsAddForm(string $stream, int $newsId): string { 108 $newsForm = new Form(); 109 $newsForm->setHiddenField('do', 'admin'); 110 $newsForm->setHiddenField('page', 'newsfeed_push'); 111 $newsForm->setHiddenField('news[do]', 'push'); 112 $newsForm->setHiddenField('news[id]', $newsId); 113 $newsForm->setHiddenField('news[stream]', $stream); 114 $newsForm->addCheckbox('all_dependence', $this->getLang('alw_dep')); 115 $newsForm->addButton('submit', $this->getLang('btn_push_news') . $stream); 116 return $newsForm->toHTML(); 117 } 118} 119