newgroup.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Add a new group
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Group
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @copyright 2013 Free Software Foundation, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('GNUSOCIAL')) { exit(1); }
  32. /**
  33. * Add a new group
  34. *
  35. * This is the form for adding a new group
  36. *
  37. * @category Group
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class NewgroupAction extends FormAction
  44. {
  45. protected $group;
  46. protected $form = 'GroupEdit';
  47. function getGroup() {
  48. return $this->group;
  49. }
  50. function title()
  51. {
  52. // TRANS: Title for form to create a group.
  53. return _('New group');
  54. }
  55. protected function doPreparation()
  56. {
  57. // $this->scoped is the current user profile
  58. if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
  59. // TRANS: Client exception thrown when a user tries to create a group while banned.
  60. $this->clientError(_('You are not allowed to create groups on this site.'), 403);
  61. }
  62. }
  63. protected function getInstructions()
  64. {
  65. // TRANS: Form instructions for group create form.
  66. return _('Use this form to create a new group.');
  67. }
  68. protected function doPost()
  69. {
  70. if (Event::handle('StartGroupSaveForm', array($this))) {
  71. $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
  72. $fullname = $this->trimmed('fullname');
  73. $homepage = $this->trimmed('homepage');
  74. $description = $this->trimmed('description');
  75. $location = $this->trimmed('location');
  76. $private = $this->boolean('private');
  77. $aliasstring = $this->trimmed('aliases');
  78. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  79. !common_valid_http_url($homepage)) {
  80. // TRANS: Group create form validation error.
  81. throw new ClientException(_('Homepage is not a valid URL.'));
  82. } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
  83. // TRANS: Group create form validation error.
  84. throw new ClientException(_('Full name is too long (maximum 255 characters).'));
  85. } else if (User_group::descriptionTooLong($description)) {
  86. // TRANS: Group create form validation error.
  87. // TRANS: %d is the maximum number of allowed characters.
  88. throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
  89. 'Description is too long (maximum %d characters).',
  90. User_group::maxDescription()),
  91. User_group::maxDescription()));
  92. } else if (!is_null($location) && mb_strlen($location) > 255) {
  93. // TRANS: Group create form validation error.
  94. throw new ClientException(_('Location is too long (maximum 255 characters).'));
  95. }
  96. if (!empty($aliasstring)) {
  97. $aliases = array_map(array('Nickname', 'normalize'), array_unique(preg_split('/[\s,]+/', $aliasstring)));
  98. } else {
  99. $aliases = array();
  100. }
  101. if (count($aliases) > common_config('group', 'maxaliases')) {
  102. // TRANS: Group create form validation error.
  103. // TRANS: %d is the maximum number of allowed aliases.
  104. throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
  105. 'Too many aliases! Maximum %d allowed.',
  106. common_config('group', 'maxaliases')),
  107. common_config('group', 'maxaliases')));
  108. }
  109. if ($private) {
  110. $force_scope = 1;
  111. $join_policy = User_group::JOIN_POLICY_MODERATE;
  112. } else {
  113. $force_scope = 0;
  114. $join_policy = User_group::JOIN_POLICY_OPEN;
  115. }
  116. // This is set up in parent->prepare and checked in self->prepare
  117. assert(!is_null($this->scoped));
  118. $group = User_group::register(array('nickname' => $nickname,
  119. 'fullname' => $fullname,
  120. 'homepage' => $homepage,
  121. 'description' => $description,
  122. 'location' => $location,
  123. 'aliases' => $aliases,
  124. 'userid' => $this->scoped->id,
  125. 'join_policy' => $join_policy,
  126. 'force_scope' => $force_scope,
  127. 'local' => true));
  128. $this->group = $group;
  129. Event::handle('EndGroupSaveForm', array($this));
  130. common_redirect($group->homeUrl(), 303);
  131. }
  132. }
  133. }