You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.3 KiB
102 lines
2.3 KiB
<?php
|
|
namespace App\Models;
|
|
use CodeIgniter\Model;
|
|
|
|
class Groups extends Model
|
|
{
|
|
/**
|
|
* table name
|
|
* @var string
|
|
*/
|
|
protected $table = 'groups';
|
|
protected $primaryKey = 'id';
|
|
protected $allowedFields = ['group_name', 'permission'];
|
|
protected $useTimestamps = false;
|
|
|
|
/**
|
|
* Get group data by groupId or all (excluding id = 1)
|
|
* @param mixed $groupId
|
|
* @return array|object|null
|
|
*/
|
|
public function getGroupData($groupId = null)
|
|
{
|
|
if ($groupId) {
|
|
return $this->find($groupId);
|
|
}
|
|
return $this->where('id !=', 1)->findAll();
|
|
}
|
|
|
|
/**
|
|
* Create new group
|
|
* @param mixed $data
|
|
* @return bool|int|string
|
|
*/
|
|
public function createGroup($data)
|
|
{
|
|
return $this->insert($data);
|
|
}
|
|
|
|
/**
|
|
* Edit group by id
|
|
* @param mixed $data
|
|
* @param mixed $id
|
|
* @return bool
|
|
*/
|
|
public function editGroup($data, $id)
|
|
{
|
|
return $this->update($id, $data);
|
|
}
|
|
|
|
/**
|
|
* Delete group by id
|
|
* @param mixed $id
|
|
* @return bool
|
|
*/
|
|
public function deleteGroup($id)
|
|
{
|
|
return $this->delete($id);
|
|
}
|
|
|
|
/**
|
|
* Check if group exists in user_group table
|
|
* @param mixed $id
|
|
* @return bool
|
|
*/
|
|
public function existInUserGroup($id)
|
|
{
|
|
return $this->db->table('user_group')->where('group_id', $id)->countAllResults() > 0;
|
|
}
|
|
|
|
/**
|
|
* Count users in a specific group
|
|
* @param mixed $id
|
|
* @return int
|
|
*/
|
|
public function countUsersInGroup($id)
|
|
{
|
|
return $this->db->table('user_group')->where('group_id', $id)->countAllResults();
|
|
}
|
|
|
|
/**
|
|
* Remove all users from a specific group
|
|
* @param mixed $id
|
|
* @return bool
|
|
*/
|
|
public function removeUsersFromGroup($id)
|
|
{
|
|
return $this->db->table('user_group')->where('group_id', $id)->delete();
|
|
}
|
|
/**
|
|
* Get user group by userId
|
|
* @param mixed $userId
|
|
* @return array|null
|
|
*/
|
|
public function getUserGroupByUserId($userId)
|
|
{
|
|
return $this->db->table('user_group')
|
|
->join('groups', 'groups.id = user_group.group_id')
|
|
->where('user_group.user_id', $userId)
|
|
->get()
|
|
->getRowArray();
|
|
}
|
|
}
|