Пример #1
0
 /**
  * Create a group
  *
  * @apiMethod POST
  * @apiUri    /groups
  * @apiParameter {
  * 		"name":          "cn",
  * 		"description":   "Group alias that appears in the url for group. Only lowercase alphanumeric chars allowed.",
  * 		"type":          "string",
  * 		"required":      true,
  * 		"default":       null
  * }
  * @apiParameter {
  * 		"name":          "title",
  * 		"description":   "Group title",
  * 		"type":          "string",
  * 		"required":      true,
  * 		"default":       null
  * }
  * @apiParameter {
  * 		"name":          "tags",
  * 		"description":   "Group tags",
  * 		"type":          "string (comma separated)",
  * 		"required":      false,
  * 		"default":       null
  * }
  * @apiParameter {
  * 		"name":          "public_description",
  * 		"description":   "Group public description",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       null
  * }
  * @apiParameter {
  * 		"name":          "private_description",
  * 		"description":   "Group private description",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       null
  * }
  * @apiParameter {
  * 		"name":          "join_policy",
  * 		"description":   "Membership join policy",
  * 		"type":          "string",
  * 		"required":      true,
  * 		"default":       "open",
  * 		"allowedValues": "open, restricted, invite_only, closed"
  * }
  * @apiParameter {
  * 		"name":          "discoverability",
  * 		"description":   "Is the group shown in hub searches/listings.",
  * 		"type":          "string",
  * 		"required":      true,
  * 		"default":       "visible",
  * 		"allowedValues": "visible, hidden"
  * }
  * @return  void
  */
 public function createTask()
 {
     $this->requiresAuthentication();
     $cn = Request::getWord('cn', '');
     $title = Request::getVar('title', '');
     $tags = Request::getVar('tags', '');
     $publicDesc = Request::getVar('public_description', '');
     $privateDesc = Request::getVar('private_description', '');
     $joinPolicy = strtolower(Request::getWord('join_policy', 'open'));
     $discoverability = Request::getWord('discoverability', 'visible');
     // var to hold errors
     $errors = array();
     // check for required fields (cn & title)
     if ($cn == '') {
         $errors[] = array('field' => 'cn', 'message' => Lang::txt('Group cn cannot be empty.'));
     }
     if ($title == '') {
         $errors[] = array('field' => 'title', 'message' => Lang::txt('Group title cannot be empty.'));
     }
     // check to make sure cn is valid & isnt taken
     if (!\Hubzero\Utility\Validate::group($cn, false)) {
         $errors[] = array('field' => 'cn', 'message' => Lang::txt('COM_GROUPS_SAVE_ERROR_INVALID_ID'));
     }
     if (\Hubzero\User\Group::exists($cn, false)) {
         $errors[] = array('field' => 'cn', 'message' => Lang::txt('COM_GROUPS_SAVE_ERROR_ID_TAKEN'));
     }
     // valid join policy
     $policies = array(0 => 'open', 1 => 'restricted', 2 => 'invite_only', 3 => 'closed');
     // make sure we have a valid policy
     if (!in_array($joinPolicy, $policies)) {
         $errors[] = array('field' => 'join_policy', 'message' => Lang::txt('Group "join_policy" value must be one of the following: %s', implode(', ', $policies)));
     }
     // valid discoverabilities
     $discoverabilities = array(0 => 'visible', 1 => 'hidden');
     // make sure we have a valid discoverability
     if (!in_array($discoverability, $discoverabilities)) {
         $errors[] = array('field' => 'discoverability', 'message' => Lang::txt('Group "discoverability" value must be one of the following: %s', implode(', ', $discoverabilities)));
     }
     // check for errors at this point
     if (!empty($errors)) {
         throw new Exception(Lang::txt('Validation Failed') . ': ' . implode("\n", $errors), 422);
     }
     // make sure we have a public desc of none was entered
     if ($publicDesc == '') {
         $publicDesc = $title;
     }
     // map the join policy & discoverability values to their int value
     $joinPolicy = array_search($joinPolicy, $policies);
     $discoverability = array_search($discoverability, $discoverabilities);
     // bind all our fields to the group object
     $group = new \Hubzero\User\Group();
     $group->set('cn', $cn);
     $group->set('type', 1);
     $group->set('published', 1);
     $group->set('approved', \App::get('component')->params('com_groups')->get('auto_approve', 1));
     $group->set('description', $title);
     $group->set('public_desc', $publicDesc);
     $group->set('private_desc', $privateDesc);
     $group->set('join_policy', $joinPolicy);
     $group->set('discoverability', $discoverability);
     $group->set('created', with(new Date('now'))->toSql());
     $group->set('created_by', User::get('id'));
     $group->add('managers', array(User::get('id')));
     $group->add('members', array(User::get('id')));
     if (!$group->create() || !$group->update()) {
         throw new Exception(Lang::txt('Failed to create group.'), 500);
     }
     $this->send($group);
 }