ComunicAPI/RestControllers/GroupsController.php

204 lines
4.8 KiB
PHP
Raw Normal View History

2018-07-02 10:03:33 +00:00
<?php
/**
* API Groups controller
*
* @author Pierre HUBERT
*/
class GroupsController {
2018-07-03 12:33:53 +00:00
/**
* API groups membership levels
*/
const GROUPS_MEMBERSHIP_LEVELS = array(
0 => "administrator",
1 => "moderator",
2 => "member",
3 => "pending",
4 => "visitor"
);
2018-07-02 10:03:33 +00:00
/**
* Create a group
*
* @url POST /groups/create
*/
public function create(){
//Login required
user_login_required();
//Get the name of the new group
$name = postString("name", 3);
//Prepare group creation
$newGroup = new NewGroup();
$newGroup->set_name($name);
$newGroup->set_userID(userID);
$newGroup->set_time_sent(time());
//Try to create the group
$groupID = components()->groups->create($newGroup);
//Check for errors
if($groupID < 1)
Rest_fatal_error(500, "An error occurred while trying to create the group!");
//Success
return array(
"success" => "The group has been successfully created!",
"id" => $groupID
);
}
2018-07-03 07:10:04 +00:00
/**
* Get information about a group
*
* @url POST /groups/get_info
*/
public function getInfo(){
//Get the ID of the requested group
2018-07-04 05:44:37 +00:00
$id = getPostGroupId("id");
2018-07-03 07:10:04 +00:00
//Get information about the group
$group = components()->groups->get_info($id);
//Check if the group was not found
if(!$group->isValid())
Rest_fatal_error(404, "The requested group was not found !");
//Parse and return information about the group
return self::GroupInfoToAPI($group);
}
/**
* Get advanced information about a group
*
* @url POST /groups/get_advanced_info
*/
public function getAdvancedInfo(){
//Get the ID of the requested group
2018-07-04 05:44:37 +00:00
$id = getPostGroupId("id");
2018-07-03 07:10:04 +00:00
//Get information about the group
$group = components()->groups->get_advanced_info($id);
//Check if the group was not found
if(!$group->isValid())
Rest_fatal_error(404, "The requested group was not found !");
//Parse and return information about the group
return self::AdvancedGroupInfoToAPI($group);
}
2018-07-04 05:44:37 +00:00
/**
* Get the settings of a group
*
* @url POST /groups/get_settings
*/
public function getSettings(){
//Get the ID of the group (with admin access)
$groupID = $this->getPostGroupIDWithAdmin("id");
//Retrieve the settings of the group
$settings = components()->groups->get_settings($groupID);
//Check for error
if(!$settings->isValid())
Rest_fatal_error(500, "Could not get the settings of the group!");
//Return parsed settings
return self::GroupSettingsToAPI($settings);
}
2018-07-04 09:36:32 +00:00
/**
* Set (update) the settings of a group
*
* @url POST /groups/set_settings
*/
public function setSettings(){
//Get the ID of the group (with admin access)
$groupID = $this->getPostGroupIDWithAdmin("id");
//Create and fill a GroupSettings object with new values
$settings = new GroupSettings();
$settings->set_id($groupID);
$settings->set_name(postString("name", 3));
//Try to save the new settings of the group
if(!components()->groups->set_settings($settings))
Rest_fatal_error(500, "An error occured while trying to update group settings!");
return array("success" => "Group settings have been successfully updated!");
}
2018-07-04 05:44:37 +00:00
/**
* Get and return a group ID specified in the POST request
* in which the current user has admin rigths
*
* @param string $name The name of the POST field
* @return int The ID of the group
*/
private function getPostGroupIDWithAdmin(string $name) : int {
//User must be signed in
user_login_required();
//Get the ID of the group
$groupID = getPostGroupId($name);
//Check if the user is an admin of the group or not
if(!components()->groups->isAdmin(userID, $groupID))
Rest_fatal_error(401, "You are not an administrator of this group!");
return $groupID;
}
2018-07-03 07:10:04 +00:00
/**
* Parse a GroupInfo object into an array for the API
*
* @param GroupInfo $info Information about the group
* @return array Generated API data
*/
public static function GroupInfoToAPI(GroupInfo $info) : array {
$data = array();
$data["id"] = $info->get_id();
$data["name"] = $info->get_name();
2018-07-03 09:14:16 +00:00
$data["icon_url"] = $info->get_icon_url();
2018-07-03 07:10:04 +00:00
$data["number_members"] = $info->get_number_members();
2018-07-03 12:33:53 +00:00
$data["membership"] = self::GROUPS_MEMBERSHIP_LEVELS[$info->get_membership_level()];
2018-07-03 07:10:04 +00:00
return $data;
}
/**
* Parse an AdvancedGroupInfo object into an array for the API
*
* @param AdvancedGroupInfo $info Information about the group
* @return array Generated API data
*/
public static function AdvancedGroupInfoToAPI(AdvancedGroupInfo $info) : array {
$data = self::GroupInfoToAPI($info);
$data["time_create"] = $info->get_time_create();
return $data;
}
2018-07-04 05:44:37 +00:00
/**
* Parse a GroupSettings object into an array for the API
*
* @param GroupSettings $settings The settings to parse
* @return array Generated array
*/
public static function GroupSettingsToAPI(GroupSettings $info) : array {
$data = self::AdvancedGroupInfoToAPI($info);
return $data;
}
2018-07-02 10:03:33 +00:00
}