1<?php 2/** 3 * Popularity Feedback Plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11/** 12 * All DokuWiki plugins to extend the admin function 13 * need to inherit from this class 14 */ 15class admin_plugin_popularity extends DokuWiki_Admin_Plugin { 16 17 /** 18 * @var helper_plugin_popularity 19 */ 20 var $helper; 21 var $sentStatus = null; 22 23 function admin_plugin_popularity(){ 24 $this->helper = $this->loadHelper('popularity', false); 25 26 $pluginInfo = $this->getInfo(); 27 $this->helper->setVersion($pluginInfo['date']); 28 } 29 30 /** 31 * return prompt for admin menu 32 */ 33 function getMenuText($language) { 34 return $this->getLang('name'); 35 } 36 37 /** 38 * return sort order for position in admin menu 39 */ 40 function getMenuSort() { 41 return 2000; 42 } 43 44 /** 45 * Accessible for managers 46 */ 47 function forAdminOnly() { 48 return false; 49 } 50 51 52 /** 53 * handle user request 54 */ 55 function handle() { 56 global $INPUT; 57 58 //Send the data 59 if ( $INPUT->has('data') ){ 60 $this->sentStatus = $this->helper->sendData( $INPUT->str('data') ); 61 if ( $this->sentStatus === '' ){ 62 //Update the last time we sent the data 63 touch ( $this->helper->popularityLastSubmitFile ); 64 } 65 //Deal with the autosubmit option 66 $this->_enableAutosubmit( $INPUT->has('autosubmit') ); 67 } 68 } 69 70 /** 71 * Enable or disable autosubmit 72 * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it. 73 */ 74 function _enableAutosubmit( $enable ){ 75 if ( $enable ){ 76 io_saveFile( $this->helper->autosubmitFile, ' '); 77 } else { 78 @unlink($this->helper->autosubmitFile); 79 } 80 } 81 82 /** 83 * Output HTML form 84 */ 85 function html() { 86 global $INPUT; 87 88 if ( ! $INPUT->has('data') ){ 89 echo $this->locale_xhtml('intro'); 90 91 //If there was an error the last time we tried to autosubmit, warn the user 92 if ( $this->helper->isAutoSubmitEnabled() ){ 93 if ( @file_exists($this->helper->autosubmitErrorFile) ){ 94 echo $this->getLang('autosubmitError'); 95 echo io_readFile( $this->helper->autosubmitErrorFile ); 96 } 97 } 98 99 flush(); 100 echo $this->buildForm('server'); 101 102 //Print the last time the data was sent 103 $lastSent = $this->helper->lastSentTime(); 104 if ( $lastSent !== 0 ){ 105 echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent); 106 } 107 } else { 108 //If we just submitted the form 109 if ( $this->sentStatus === '' ){ 110 //If we successfully sent the data 111 echo $this->locale_xhtml('submitted'); 112 } else { 113 //If we failed to submit the data, try directly with the browser 114 echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />'; 115 echo $this->getLang('submitDirectly'); 116 echo $this->buildForm('browser', $INPUT->str('data')); 117 } 118 } 119 } 120 121 122 /** 123 * Build the form which presents the data to be sent 124 * @param string $submissionMode How is the data supposed to be sent? (may be: 'browser' or 'server') 125 * @param string $data The popularity data, if it has already been computed. NULL otherwise. 126 * @return string The form, as an html string 127 */ 128 function buildForm($submissionMode, $data = null){ 129 $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script()); 130 if ( is_null($data) ){ 131 $data = $this->helper->gatherAsString(); 132 } 133 134 $form = '<form method="post" action="'. $url .'" accept-charset="utf-8">' 135 .'<fieldset style="width: 60%;">' 136 .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">' 137 .$data 138 .'</textarea><br />'; 139 140 //If we submit via the server, we give the opportunity to suscribe to the autosubmission option 141 if ( $submissionMode !== 'browser' ){ 142 $form .= '<label for="autosubmit">' 143 .'<input type="checkbox" name="autosubmit" id="autosubmit" ' 144 .($this->helper->isAutosubmitEnabled() ? 'checked' : '' ) 145 .'/> ' . $this->getLang('autosubmit') .'<br />' 146 .'</label>' 147 .'<input type="hidden" name="do" value="admin" />' 148 .'<input type="hidden" name="page" value="popularity" />'; 149 } 150 $form .= '<input type="submit" class="button" value="'.$this->getLang('submit').'"/>' 151 .'</fieldset>' 152 .'</form>'; 153 return $form; 154 } 155} 156