formaction.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * Form action extendable class.
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2008, 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Form action extendable class
  28. *
  29. * @category Action
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @author Mikael Nordfeldth <evan@status.net>
  33. * @copyright 2008, 2009 StatusNet, Inc.
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class FormAction extends ManagedAction
  37. {
  38. protected $form = null;
  39. protected $formOpts = array();
  40. protected $type = null;
  41. protected $needLogin = true;
  42. protected $canPost = true;
  43. protected function prepare(array $args = [])
  44. {
  45. parent::prepare($args);
  46. $this->form = $this->form ?: ucfirst($this->action);
  47. $this->args['form'] = $this->form;
  48. $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
  49. $this->args['context'] = $this->trimmed('action'); // reply for notice for example
  50. if (!$this->type) {
  51. $this->type = null;
  52. }
  53. return true;
  54. }
  55. public function isReadOnly($args)
  56. {
  57. return !$this->isPost();
  58. }
  59. public function showPageNotice()
  60. {
  61. $this->showInstructions();
  62. if (!empty($msg = $this->getError())) {
  63. $this->element('div', 'error', $msg);
  64. }
  65. if (!empty($msg = $this->getInfo())) {
  66. $this->element('div', 'info', $msg);
  67. }
  68. }
  69. /**
  70. * Outputs the instructions for the form
  71. *
  72. * SHOULD overload
  73. */
  74. public function showInstructions()
  75. {
  76. // instructions are nice, so users know what to do
  77. $this->raw(common_markup_to_html($this->getInstructions()));
  78. }
  79. /**
  80. * @return string with instructions to pass into common_markup_to_html()
  81. */
  82. protected function getInstructions()
  83. {
  84. return null;
  85. }
  86. public function showForm($msg=null, $success=false)
  87. {
  88. $this->msg = $msg;
  89. $this->success = $success;
  90. $this->showPage();
  91. }
  92. protected function showContent()
  93. {
  94. $form = $this->getForm();
  95. $form->show();
  96. }
  97. protected function getForm()
  98. {
  99. $class = $this->form.'Form';
  100. $form = new $class($this, $this->formOpts);
  101. return $form;
  102. }
  103. }