newgroup.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * Add a new group
  36. *
  37. * This is the form for adding a new group
  38. *
  39. * @category Group
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class NewgroupAction extends FormAction
  46. {
  47. protected $group;
  48. function getGroup() {
  49. return $this->group;
  50. }
  51. function title()
  52. {
  53. // TRANS: Title for form to create a group.
  54. return _('New group');
  55. }
  56. /**
  57. * Prepare to run
  58. */
  59. protected function prepare(array $args=array())
  60. {
  61. parent::prepare($args);
  62. // $this->scoped is the current user profile
  63. if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
  64. // TRANS: Client exception thrown when a user tries to create a group while banned.
  65. $this->clientError(_('You are not allowed to create groups on this site.'), 403);
  66. }
  67. return true;
  68. }
  69. public function showContent()
  70. {
  71. $form = new GroupEditForm($this);
  72. $form->show();
  73. }
  74. public function showInstructions()
  75. {
  76. $this->element('p', 'instructions',
  77. // TRANS: Form instructions for group create form.
  78. _('Use this form to create a new group.'));
  79. }
  80. protected function handlePost()
  81. {
  82. parent::handlePost();
  83. if (Event::handle('StartGroupSaveForm', array($this))) {
  84. $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
  85. $fullname = $this->trimmed('fullname');
  86. $homepage = $this->trimmed('homepage');
  87. $description = $this->trimmed('description');
  88. $location = $this->trimmed('location');
  89. $private = $this->boolean('private');
  90. $aliasstring = $this->trimmed('aliases');
  91. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  92. !common_valid_http_url($homepage)) {
  93. // TRANS: Group create form validation error.
  94. throw new ClientException(_('Homepage is not a valid URL.'));
  95. } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
  96. // TRANS: Group create form validation error.
  97. throw new ClientException(_('Full name is too long (maximum 255 characters).'));
  98. } else if (User_group::descriptionTooLong($description)) {
  99. // TRANS: Group create form validation error.
  100. // TRANS: %d is the maximum number of allowed characters.
  101. throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
  102. 'Description is too long (maximum %d characters).',
  103. User_group::maxDescription()),
  104. User_group::maxDescription()));
  105. } else if (!is_null($location) && mb_strlen($location) > 255) {
  106. // TRANS: Group create form validation error.
  107. throw new ClientException(_('Location is too long (maximum 255 characters).'));
  108. }
  109. if (!empty($aliasstring)) {
  110. $aliases = array_map(array('Nickname', 'normalize'), array_unique(preg_split('/[\s,]+/', $aliasstring)));
  111. } else {
  112. $aliases = array();
  113. }
  114. if (count($aliases) > common_config('group', 'maxaliases')) {
  115. // TRANS: Group create form validation error.
  116. // TRANS: %d is the maximum number of allowed aliases.
  117. throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
  118. 'Too many aliases! Maximum %d allowed.',
  119. common_config('group', 'maxaliases')),
  120. common_config('group', 'maxaliases')));
  121. return;
  122. }
  123. if ($private) {
  124. $force_scope = 1;
  125. $join_policy = User_group::JOIN_POLICY_MODERATE;
  126. } else {
  127. $force_scope = 0;
  128. $join_policy = User_group::JOIN_POLICY_OPEN;
  129. }
  130. // This is set up in parent->prepare and checked in self->prepare
  131. assert(!is_null($this->scoped));
  132. $group = User_group::register(array('nickname' => $nickname,
  133. 'fullname' => $fullname,
  134. 'homepage' => $homepage,
  135. 'description' => $description,
  136. 'location' => $location,
  137. 'aliases' => $aliases,
  138. 'userid' => $this->scoped->id,
  139. 'join_policy' => $join_policy,
  140. 'force_scope' => $force_scope,
  141. 'local' => true));
  142. $this->group = $group;
  143. Event::handle('EndGroupSaveForm', array($this));
  144. common_redirect($group->homeUrl(), 303);
  145. }
  146. }
  147. }