15faeb1e6SAndreas Gohr<?php 25faeb1e6SAndreas Gohr/** 35faeb1e6SAndreas Gohr * Popularity Feedback Plugin 45faeb1e6SAndreas Gohr * 55faeb1e6SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 65faeb1e6SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 75faeb1e6SAndreas Gohr */ 85faeb1e6SAndreas Gohrclass admin_plugin_popularity extends DokuWiki_Admin_Plugin { 938479cbbSDominik Eckelmann 1038479cbbSDominik Eckelmann /** 1138479cbbSDominik Eckelmann * @var helper_plugin_popularity 1238479cbbSDominik Eckelmann */ 13*3dc2d50cSAndreas Gohr protected $helper; 14*3dc2d50cSAndreas Gohr protected $sentStatus = null; 155faeb1e6SAndreas Gohr 16*3dc2d50cSAndreas Gohr public function __construct(){ 1798be6429SGuillaume Turri $this->helper = $this->loadHelper('popularity', false); 185faeb1e6SAndreas Gohr } 195faeb1e6SAndreas Gohr 205faeb1e6SAndreas Gohr /** 215faeb1e6SAndreas Gohr * return prompt for admin menu 22*3dc2d50cSAndreas Gohr * @param $language 23*3dc2d50cSAndreas Gohr * @return string 245faeb1e6SAndreas Gohr */ 25*3dc2d50cSAndreas Gohr public function getMenuText($language) { 265faeb1e6SAndreas Gohr return $this->getLang('name'); 275faeb1e6SAndreas Gohr } 285faeb1e6SAndreas Gohr 295faeb1e6SAndreas Gohr /** 305faeb1e6SAndreas Gohr * return sort order for position in admin menu 315faeb1e6SAndreas Gohr */ 32*3dc2d50cSAndreas Gohr public function getMenuSort() { 335faeb1e6SAndreas Gohr return 2000; 345faeb1e6SAndreas Gohr } 355faeb1e6SAndreas Gohr 365faeb1e6SAndreas Gohr /** 371bda8618SAndreas Gohr * Accessible for managers 381bda8618SAndreas Gohr */ 39*3dc2d50cSAndreas Gohr public function forAdminOnly() { 401bda8618SAndreas Gohr return false; 411bda8618SAndreas Gohr } 421bda8618SAndreas Gohr 431bda8618SAndreas Gohr 441bda8618SAndreas Gohr /** 455faeb1e6SAndreas Gohr * handle user request 465faeb1e6SAndreas Gohr */ 47*3dc2d50cSAndreas Gohr public function handle() { 48f21e024aSHakan Sandell global $INPUT; 49f21e024aSHakan Sandell 5098be6429SGuillaume Turri //Send the data 51f21e024aSHakan Sandell if ( $INPUT->has('data') ){ 52f21e024aSHakan Sandell $this->sentStatus = $this->helper->sendData( $INPUT->str('data') ); 535827ba0bSGuillaume Turri if ( $this->sentStatus === '' ){ 545827ba0bSGuillaume Turri //Update the last time we sent the data 555827ba0bSGuillaume Turri touch ( $this->helper->popularityLastSubmitFile ); 565827ba0bSGuillaume Turri } 5798be6429SGuillaume Turri //Deal with the autosubmit option 58f21e024aSHakan Sandell $this->_enableAutosubmit( $INPUT->has('autosubmit') ); 5998be6429SGuillaume Turri } 6098be6429SGuillaume Turri } 6198be6429SGuillaume Turri 6298be6429SGuillaume Turri /** 6398be6429SGuillaume Turri * Enable or disable autosubmit 6498be6429SGuillaume Turri * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it. 6598be6429SGuillaume Turri */ 66*3dc2d50cSAndreas Gohr protected function _enableAutosubmit( $enable ){ 6798be6429SGuillaume Turri if ( $enable ){ 6898be6429SGuillaume Turri io_saveFile( $this->helper->autosubmitFile, ' '); 6998be6429SGuillaume Turri } else { 7098be6429SGuillaume Turri @unlink($this->helper->autosubmitFile); 7198be6429SGuillaume Turri } 725faeb1e6SAndreas Gohr } 735faeb1e6SAndreas Gohr 745faeb1e6SAndreas Gohr /** 751bda8618SAndreas Gohr * Output HTML form 765faeb1e6SAndreas Gohr */ 77*3dc2d50cSAndreas Gohr public function html() { 78f21e024aSHakan Sandell global $INPUT; 79f21e024aSHakan Sandell 80f21e024aSHakan Sandell if ( ! $INPUT->has('data') ){ 815faeb1e6SAndreas Gohr echo $this->locale_xhtml('intro'); 825faeb1e6SAndreas Gohr 8398be6429SGuillaume Turri //If there was an error the last time we tried to autosubmit, warn the user 8498be6429SGuillaume Turri if ( $this->helper->isAutoSubmitEnabled() ){ 8579e79377SAndreas Gohr if ( file_exists($this->helper->autosubmitErrorFile) ){ 8698be6429SGuillaume Turri echo $this->getLang('autosubmitError'); 8798be6429SGuillaume Turri echo io_readFile( $this->helper->autosubmitErrorFile ); 8898be6429SGuillaume Turri } 8998be6429SGuillaume Turri } 9098be6429SGuillaume Turri 915faeb1e6SAndreas Gohr flush(); 9298be6429SGuillaume Turri echo $this->buildForm('server'); 935827ba0bSGuillaume Turri 945827ba0bSGuillaume Turri //Print the last time the data was sent 955827ba0bSGuillaume Turri $lastSent = $this->helper->lastSentTime(); 965827ba0bSGuillaume Turri if ( $lastSent !== 0 ){ 97a375d5e5SGuillaume Turri echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent); 985827ba0bSGuillaume Turri } 995faeb1e6SAndreas Gohr } else { 10098be6429SGuillaume Turri //If we just submitted the form 10198be6429SGuillaume Turri if ( $this->sentStatus === '' ){ 10298be6429SGuillaume Turri //If we successfully sent the data 10398be6429SGuillaume Turri echo $this->locale_xhtml('submitted'); 10498be6429SGuillaume Turri } else { 10598be6429SGuillaume Turri //If we failed to submit the data, try directly with the browser 10698be6429SGuillaume Turri echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />'; 10798be6429SGuillaume Turri echo $this->getLang('submitDirectly'); 108f21e024aSHakan Sandell echo $this->buildForm('browser', $INPUT->str('data')); 1095faeb1e6SAndreas Gohr } 1105faeb1e6SAndreas Gohr } 1115faeb1e6SAndreas Gohr } 1125faeb1e6SAndreas Gohr 1135faeb1e6SAndreas Gohr 1145faeb1e6SAndreas Gohr /** 11598be6429SGuillaume Turri * Build the form which presents the data to be sent 11638479cbbSDominik Eckelmann * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server') 11798be6429SGuillaume Turri * @param string $data The popularity data, if it has already been computed. NULL otherwise. 11838479cbbSDominik Eckelmann * @return string The form, as an html string 1195faeb1e6SAndreas Gohr */ 120*3dc2d50cSAndreas Gohr protected function buildForm($submissionMode, $data = null){ 12198be6429SGuillaume Turri $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script()); 12298be6429SGuillaume Turri if ( is_null($data) ){ 12398be6429SGuillaume Turri $data = $this->helper->gatherAsString(); 1245faeb1e6SAndreas Gohr } 1255faeb1e6SAndreas Gohr 12698be6429SGuillaume Turri $form = '<form method="post" action="'. $url .'" accept-charset="utf-8">' 12798be6429SGuillaume Turri .'<fieldset style="width: 60%;">' 12898be6429SGuillaume Turri .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">' 12998be6429SGuillaume Turri .$data 13098be6429SGuillaume Turri .'</textarea><br />'; 13198be6429SGuillaume Turri 13298be6429SGuillaume Turri //If we submit via the server, we give the opportunity to suscribe to the autosubmission option 13398be6429SGuillaume Turri if ( $submissionMode !== 'browser' ){ 13498be6429SGuillaume Turri $form .= '<label for="autosubmit">' 13598be6429SGuillaume Turri .'<input type="checkbox" name="autosubmit" id="autosubmit" ' 13698be6429SGuillaume Turri .($this->helper->isAutosubmitEnabled() ? 'checked' : '' ) 13798be6429SGuillaume Turri .'/> ' . $this->getLang('autosubmit') .'<br />' 13898be6429SGuillaume Turri .'</label>' 1396cd259d7SAnika Henke .'<input type="hidden" name="do" value="admin" />' 1406cd259d7SAnika Henke .'<input type="hidden" name="page" value="popularity" />'; 1415faeb1e6SAndreas Gohr } 142ae614416SAnika Henke $form .= '<button type="submit">'.$this->getLang('submit').'</button>' 14398be6429SGuillaume Turri .'</fieldset>' 14498be6429SGuillaume Turri .'</form>'; 14598be6429SGuillaume Turri return $form; 1465faeb1e6SAndreas Gohr } 1475faeb1e6SAndreas Gohr} 148