hello.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Give a warm greeting to our friendly user
  18. *
  19. * @package GNU social
  20. * @author Brion Vibber <brionv@status.net>
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * This sample action shows some basic ways of doing output in an action
  28. * class.
  29. *
  30. * Action classes have several output methods that they override from
  31. * the parent class.
  32. *
  33. * @category Sample
  34. * @package GNU social
  35. * @author Evan Prodromou <evan@status.net>
  36. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class HelloAction extends Action
  40. {
  41. public $user = null;
  42. public $gc = null;
  43. /**
  44. * Take arguments for running
  45. *
  46. * This method is called first, and it lets the action class get
  47. * all its arguments and validate them. It's also the time
  48. * to fetch any relevant data from the database.
  49. *
  50. * Action classes should run parent::prepare($args) as the first
  51. * line of this method to make sure the default argument-processing
  52. * happens.
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return bool success flag
  57. * @throws ClientException
  58. */
  59. public function prepare(array $args = [])
  60. {
  61. parent::prepare($args);
  62. $this->user = common_current_user();
  63. if (!empty($this->user)) {
  64. $this->gc = User_greeting_count::inc($this->user->getID());
  65. }
  66. return true;
  67. }
  68. /**
  69. * Handle request
  70. *
  71. * This is the main method for handling a request. Note that
  72. * most preparation should be done in the prepare() method;
  73. * by the time handle() is called the action should be
  74. * more or less ready to go.
  75. *
  76. * @return void
  77. * @throws ClientException
  78. * @throws ReflectionException
  79. * @throws ServerException
  80. */
  81. public function handle()
  82. {
  83. parent::handle();
  84. $this->showPage();
  85. }
  86. /**
  87. * Title of this page
  88. *
  89. * Override this method to show a custom title.
  90. *
  91. * @return string Title of the page
  92. * @throws Exception
  93. */
  94. public function title()
  95. {
  96. if (empty($this->user)) {
  97. // TRANS: Page title for sample plugin.
  98. return _m('Hello');
  99. } else {
  100. // TRANS: Page title for sample plugin. %s is a user nickname.
  101. return sprintf(_m('Hello, %s!'), $this->user->getNickname());
  102. }
  103. }
  104. /**
  105. * Show content in the content area
  106. *
  107. * The default StatusNet page has a lot of decorations: menus,
  108. * logos, tabs, all that jazz. This method is used to show
  109. * content in the content area of the page; it's the main
  110. * thing you want to overload.
  111. *
  112. * This method also demonstrates use of a plural localized string.
  113. *
  114. * @return void
  115. * @throws Exception
  116. */
  117. public function showContent()
  118. {
  119. if (empty($this->user)) {
  120. $this->element(
  121. 'p',
  122. ['class' => 'greeting'],
  123. // TRANS: Message in sample plugin.
  124. _m('Hello, stranger!')
  125. );
  126. } else {
  127. $this->element(
  128. 'p',
  129. ['class' => 'greeting'],
  130. // TRANS: Message in sample plugin. %s is a user nickname.
  131. sprintf(_m('Hello, %s'), $this->user->getNickname())
  132. );
  133. $this->element(
  134. 'p',
  135. ['class' => 'greeting_count'],
  136. // TRANS: Message in sample plugin.
  137. // TRANS: %d is the number of times a user is greeted.
  138. sprintf(
  139. _m(
  140. 'I have greeted you %d time.',
  141. 'I have greeted you %d times.',
  142. $this->gc->greeting_count
  143. ),
  144. $this->gc->greeting_count
  145. )
  146. );
  147. }
  148. }
  149. /**
  150. * Return true if read only.
  151. *
  152. * Some actions only read from the database; others read and write.
  153. * The simple database load-balancer built into StatusNet will
  154. * direct read-only actions to database mirrors (if they are configured),
  155. * and read-write actions to the master database.
  156. *
  157. * This defaults to false to avoid data integrity issues, but you
  158. * should make sure to overload it for performance gains.
  159. *
  160. * @param array $args other arguments, if RO/RW status depends on them.
  161. *
  162. * @return bool is read only action?
  163. */
  164. public function isReadOnly($args)
  165. {
  166. return false;
  167. }
  168. }