mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-12-25 13:08:58 +00:00
Can create groups
This commit is contained in:
parent
1637885a97
commit
30d6a1fd9d
43
RestControllers/GroupsController.php
Normal file
43
RestControllers/GroupsController.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* API Groups controller
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class GroupsController {
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
72
classes/components/GroupsComponent.php
Normal file
72
classes/components/GroupsComponent.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Groups component
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class GroupsComponent {
|
||||
|
||||
/**
|
||||
* Groups list table
|
||||
*/
|
||||
const GROUPS_LIST_TABLE = DBprefix . "groups";
|
||||
|
||||
/**
|
||||
* Groups members table
|
||||
*/
|
||||
const GROUPS_MEMBERS_TABLE = DBprefix."groups_members";
|
||||
|
||||
/**
|
||||
* Create a new group
|
||||
*
|
||||
* @param NewGroup $newGroup Information about the new group
|
||||
* to create
|
||||
* @return int The ID of the created group / -1 in case of failure
|
||||
*/
|
||||
public function create(NewGroup $newGroup) : int {
|
||||
|
||||
//Insert the group in the database
|
||||
db()->addLine(self::GROUPS_LIST_TABLE, array(
|
||||
"time_create" => $newGroup->get_time_sent(),
|
||||
"userid_create" => $newGroup->get_userID(),
|
||||
"name" => $newGroup->get_name()
|
||||
));
|
||||
|
||||
//Get the ID of the last inserted group
|
||||
$groupID = db()->getLastInsertedID();
|
||||
|
||||
//Check for errors
|
||||
if(!$groupID > 0)
|
||||
return -1;
|
||||
|
||||
//Register the user who created the group as an admin of the group
|
||||
$member = new GroupMember;
|
||||
$member->set_group_id($groupID);
|
||||
$member->set_userID($newGroup->get_userID());
|
||||
$member->set_time_sent($newGroup->get_time_sent());
|
||||
$member->set_level(GroupMember::ADMINISTRATOR);
|
||||
$this->insertMember($member);
|
||||
|
||||
return $groupID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new group member
|
||||
*
|
||||
* @param GroupMember $member Information about the member to insert
|
||||
* @return bool TRUE for a success / FALSE else
|
||||
*/
|
||||
private function insertMember(GroupMember $member) : bool {
|
||||
return db()->addLine(self::GROUPS_MEMBERS_TABLE, array(
|
||||
"groups_id" => $member->get_group_id(),
|
||||
"user_id" => $member->get_userID(),
|
||||
"time_create" => $member->get_time_sent(),
|
||||
"level" => $member->get_level()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Register component
|
||||
Components::register("groups", new GroupsComponent());
|
47
classes/models/GroupMember.php
Normal file
47
classes/models/GroupMember.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Group member object model
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class GroupMember extends BaseUniqueObjectFromUser {
|
||||
|
||||
/**
|
||||
* Groups membership levels
|
||||
*/
|
||||
const ADMINISTRATOR = 0;
|
||||
const MODERATOR = 1;
|
||||
const MEMBER = 2;
|
||||
const PENDING = 3; //When the group membership has not been approved yet
|
||||
|
||||
//Private fields
|
||||
private $group_id = 1;
|
||||
private $level = -1;
|
||||
|
||||
//Set and get group id
|
||||
public function set_group_id(int $group_id){
|
||||
$this->group_id = $group_id;
|
||||
}
|
||||
|
||||
public function has_group_id() : bool {
|
||||
return $this->group_id > -1;
|
||||
}
|
||||
|
||||
public function get_group_id() : int {
|
||||
return $this->group_id;
|
||||
}
|
||||
|
||||
//Set and get user membership level
|
||||
public function set_level(int $level){
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
public function has_level() : bool {
|
||||
return $this->level > -1;
|
||||
}
|
||||
|
||||
public function get_level() : int {
|
||||
return $this->level;
|
||||
}
|
||||
}
|
25
classes/models/NewGroup.php
Normal file
25
classes/models/NewGroup.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* New Group Object
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class NewGroup extends BaseUniqueObjectFromUser {
|
||||
|
||||
//Private properties
|
||||
private $name;
|
||||
|
||||
//Set and get name
|
||||
public function set_name(string $name){
|
||||
$this->name = $name == "" ? null : $name;
|
||||
}
|
||||
|
||||
public function has_name() : bool {
|
||||
return $this->name != null;
|
||||
}
|
||||
|
||||
public function get_name() : string {
|
||||
return $this->name != null ? $this->name : "null";
|
||||
}
|
||||
}
|
15
helpers/database.php
Normal file
15
helpers/database.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Database helper
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get and return database object
|
||||
*
|
||||
* @param DBLibrary The database object
|
||||
*/
|
||||
function db() : DBLibrary {
|
||||
return CS::get()->db;
|
||||
}
|
Loading…
Reference in New Issue
Block a user