1<?php 2/** 3 * DokuWiki Plugin IssueLinks (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10use dokuwiki\plugin\issuelinks\classes\ServiceProvider; 11use dokuwiki\plugin\issuelinks\services\ServiceInterface; 12 13class admin_plugin_issuelinks_repoadmin extends DokuWiki_Admin_Plugin 14{ 15 16 private $orgs = []; 17 private $configNeeded = []; 18 19 /** 20 * @return int sort number in admin menu 21 */ 22 public function getMenuSort() 23 { 24 return 500; 25 } 26 27 /** 28 * Return the text that is displayed at the main admin menu 29 * (Default localized language string 'menu' is returned, override this function for setting another name) 30 * 31 * @param string $language language code 32 * 33 * @return string menu string 34 */ 35 public function getMenuText($language) 36 { 37 return $this->getLang('menu:repo-admin'); 38 } 39 40 public function getMenuIcon() 41 { 42 $plugin = $this->getPluginName(); 43 return DOKU_PLUGIN . $plugin . '/images/issue-opened.svg'; 44 } 45 46 /** 47 * @return bool true if only access for superuser, false is for superusers and moderators 48 */ 49 public function forAdminOnly() 50 { 51 return true; 52 } 53 54 /** 55 * Should carry out any processing required by the plugin. 56 */ 57 public function handle() 58 { 59 global $INPUT; 60 61 $serviceProvider = ServiceProvider::getInstance(); 62 /** @var ServiceInterface[] $services */ 63 $services = $serviceProvider->getServices(); 64 65 if ($INPUT->has('authorize')) { 66 $serviceID = $INPUT->str('authorize'); 67 $service = $services[$serviceID]::getInstance(); 68 $service->handleAuthorization(); 69 } 70 71 foreach ($services as $serviceID => $serviceClass) { 72 $service = $serviceClass::getInstance(); 73 $this->orgs[$serviceID] = []; 74 if ($INPUT->str('reconfigureService') === $serviceID || !$service->isConfigured()) { 75 $this->configNeeded[] = $serviceID; 76 continue; 77 } 78 79 $this->orgs[$serviceID] = $service->getListOfAllUserOrganisations(); 80 sort($this->orgs[$serviceID]); 81 } 82 } 83 84 /** 85 * Render HTML output, e.g. helpful text and a form 86 */ 87 public function html() 88 { 89 $activeServices = array_keys($this->orgs); 90 $html = "<div id='plugin__issuelinks_repoadmin'>"; 91 $html .= "<div><ul class='tabs'>"; 92 $html = array_reduce($activeServices, [$this, 'appendServiceTab'], $html); 93 $html .= '</ul></div>'; 94 $html = array_reduce($activeServices, [$this, 'appendServicePage'], $html); 95 96 $html .= '</div>'; // <div id='plugin__issuelinks_repoadmin'> 97 98 echo $html; 99 } 100 101 /** 102 * Callback to append a `<li>`-tab for services like GitLab or GitHub to a tabbar 103 * 104 * @param string $html the html to which we append the tab 105 * @param string $serviceID 106 * 107 * @return string 108 */ 109 protected function appendServiceTab($html, $serviceID) 110 { 111 $serviceProvider = ServiceProvider::getInstance(); 112 $services = $serviceProvider->getServices(); 113 $service = $services[$serviceID]; 114 $serviceName = $service::DISPLAY_NAME; 115 return $html . "<li><a data-service='$serviceID'>" . $serviceName . '</a></li>'; 116 } 117 118 /** 119 * Callback creating and appending a service's page for adjusting its webhooks 120 * 121 * @param string $html the html to which we append the page 122 * @param string $serviceID 123 * 124 * @return string 125 */ 126 protected function appendServicePage($html, $serviceID) 127 { 128 $serviceProvider = ServiceProvider::getInstance(); 129 $services = $serviceProvider->getServices(); 130 $service = $services[$serviceID]::getInstance(); 131 $serviceName = $service::DISPLAY_NAME; 132 133 $html .= "<div data-service='$serviceID' class='service_wrapper'>"; 134 135 if (in_array($serviceID, $this->configNeeded)) { 136 $configForm = new \dokuwiki\Form\Form(); 137 $configForm->addClass('plugin__repoadmin_serviceConfig'); 138 $configForm->setHiddenField('authorize', $serviceID); 139 $configForm->addFieldsetOpen(); 140 $service->hydrateConfigForm($configForm); 141 $configForm->addButton('', $this->getLang('btn:Submit'))->attr('type', 'submit'); 142 $configForm->addFieldsetClose(); 143 $html .= $configForm->toHTML(); 144 } elseif (count($this->orgs[$serviceID]) === 0) { 145 $html .= '<p>No organisations available for ' . $serviceName . '</p>'; 146 } else { 147 global $INPUT; 148 $reconfigureURL = $INPUT->server->str('REQUEST_URI') . '&reconfigureService=' . $serviceID; 149 $reconfigureLink = "<a href=\"$reconfigureURL\">{$this->getLang('label: reconfigure service')}</a>"; 150 $authorizedUserLabel = sprintf($this->getLang('label: authorized with user'), $service->getUserString()); 151 $form = new \dokuwiki\Form\Form(['data-service' => $serviceID]); 152 $form->addFieldsetOpen($this->getLang('legend:user')); 153 $form->addTagOpen('p'); 154 $form->addHTML($authorizedUserLabel . ' ' . $reconfigureLink); 155 $form->addTagClose('p'); 156 $form->addFieldsetClose(); 157 $form->addFieldsetOpen($this->getLang("legend:group $serviceID")); 158 $form->addDropdown( 159 'mm_organisation', 160 array_merge([''], $this->orgs[$serviceID]), 161 $this->getLang("label $serviceID:choose organisation") 162 ); 163 $form->addFieldsetClose(); 164 $html .= $form->toHTML(); 165 $html .= "<div data-service='$serviceID' class='repo_area'></div>"; 166 } 167 $html .= '</div>'; // <div data-service='$servicename' class='service_area'> 168 return $html; 169 } 170} 171 172// vim:ts=4:sw=4:et: 173