admin.php 4.8 KB

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