formaction.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Form action extendable class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2008, 2009, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Form action extendable class
  32. *
  33. * @category Action
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @author Mikael Nordfeldth <evan@status.net>
  37. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  38. * @link http://status.net/
  39. */
  40. class FormAction extends ManagedAction
  41. {
  42. protected $form = null;
  43. protected $formOpts = array();
  44. protected $type = null;
  45. protected $needLogin = true;
  46. protected $canPost = true;
  47. protected function prepare(array $args = []) {
  48. parent::prepare($args);
  49. $this->form = $this->form ?: ucfirst($this->action);
  50. $this->args['form'] = $this->form;
  51. $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
  52. $this->args['context'] = $this->trimmed('action'); // reply for notice for example
  53. if (!$this->type) {
  54. $this->type = null;
  55. }
  56. return true;
  57. }
  58. public function isReadOnly($args) {
  59. return !$this->isPost();
  60. }
  61. public function showPageNotice()
  62. {
  63. $this->showInstructions();
  64. if ($msg = $this->getError()) {
  65. $this->element('div', 'error', $msg);
  66. }
  67. if ($msg = $this->getInfo()) {
  68. $this->element('div', 'info', $msg);
  69. }
  70. }
  71. /**
  72. * Outputs the instructions for the form
  73. *
  74. * SHOULD overload
  75. */
  76. public function showInstructions()
  77. {
  78. // instructions are nice, so users know what to do
  79. $this->raw(common_markup_to_html($this->getInstructions()));
  80. }
  81. /**
  82. * @return string with instructions to pass into common_markup_to_html()
  83. */
  84. protected function getInstructions()
  85. {
  86. return null;
  87. }
  88. public function showForm($msg=null, $success=false)
  89. {
  90. $this->msg = $msg;
  91. $this->success = $success;
  92. $this->showPage();
  93. }
  94. protected function showContent()
  95. {
  96. $form = $this->getForm();
  97. $form->show();
  98. }
  99. protected function getForm()
  100. {
  101. $class = $this->form.'Form';
  102. $form = new $class($this, $this->formOpts);
  103. return $form;
  104. }
  105. }