Initial import
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\models;
|
||||
|
||||
/**
|
||||
* Class CmsRoles
|
||||
* @package app\models
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property int $user_type_id;
|
||||
* @property \app\models\CmsRr[] $roleRights
|
||||
*/
|
||||
class CmsRoles extends _Base
|
||||
{
|
||||
public function getRoleRights()
|
||||
{
|
||||
return $this->hasMany(CmsRr::class, ['role_id' => 'id'])->innerJoinWith('right')->where(['is_active' => 1])->all();
|
||||
}
|
||||
|
||||
public function getRightsIds()
|
||||
{
|
||||
$rightsIds = [];
|
||||
foreach ($this->roleRights as $roleRight) {
|
||||
$rightsIds[] = $roleRight->right_id;
|
||||
}
|
||||
return $rightsIds;
|
||||
}
|
||||
|
||||
public function updateRights($rightsIds)
|
||||
{
|
||||
list($create_list, $delete_list) = [[], []];
|
||||
|
||||
//Delete OLD
|
||||
foreach ($this->getRightsIds() as $rightId) {
|
||||
if (!in_array($rightId, $rightsIds))
|
||||
$delete_list[] = $rightId;
|
||||
}
|
||||
$deleteCmsRr = CmsRr::find()->where(['role_id' => $this->id])->andWhere(['IN', 'right_id', $delete_list])->all();
|
||||
foreach ($deleteCmsRr as $cmsRrModel)
|
||||
$cmsRrModel->delete();
|
||||
|
||||
//Create new
|
||||
foreach ($rightsIds as $currentId) {
|
||||
if (!in_array($currentId, $this->getRightsIds()))
|
||||
$create_list[] = $currentId;
|
||||
}
|
||||
|
||||
foreach ($create_list as $rightId){
|
||||
$cmsRrModel = new CmsRr();
|
||||
$cmsRrModel->role_id = $this->id;
|
||||
$cmsRrModel->right_id = $rightId;
|
||||
$cmsRrModel->save();
|
||||
}
|
||||
}
|
||||
|
||||
public static function roleList($role_id = null) {
|
||||
$data = [];
|
||||
foreach (self::find()->all() as $item) {
|
||||
$data[$item->id] = $item->name;
|
||||
}
|
||||
if($role_id) {
|
||||
return $data[$role_id] ?? null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
public static function roleUserTypes() {
|
||||
return [
|
||||
1 => 'Партньори'
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user