You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

154 lines
4.7 KiB

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