/home/mip/mip/app/Modules/Likod/Repositories/Settings/Roles/RoleRepository.php
<?php
namespace QxCMS\Modules\Likod\Repositories\Settings\Roles;
use Illuminate\Support\Arr;
use QxCMS\Modules\AbstractRepository;
use QxCMS\Modules\Likod\Repositories\Settings\Roles\RoleRepositoryInterface;
use QxCMS\Modules\Likod\Models\Settings\Roles\Role;
use QxCMS\Modules\Likod\Models\Settings\Roles\Permission;
use QxCMS\Modules\Likod\Models\Developer\Module;
use DB;
use File;
class RoleRepository extends AbstractRepository implements RoleRepositoryInterface
{
protected $model;
protected $module;
protected $permission;
function __construct(Role $model, Module $module, Permission $permission)
{
$this->model = $model;
$this->module = $module;
$this->permission = $permission;
}
public function create(array $request)
{
$role = $this->model->fill($this->makeRole($request));
$role->save();
$this->makeRolePermissions($request, $role->id);
return $role;
}
public function makeRole(array $request)
{
return Arr::except($request, ['module']);
}
public function makeRolePermissions(array $request, $role_id)
{
$modules = isset($request['module']) ? $request['module']:array();
$this->permission->where('role_id', $role_id)->delete();
if (count($modules) <= 0) return;
foreach ($modules as $module_id => $module) {
$module['role_id'] = $role_id;
$module['module_id'] = $module_id;
$this->permission->create($this->makeModulePermissions($module));
}
return $module;
}
public function makeModulePermissions(array $module)
{
return $useroles = [
'menu_id' => $module['module_id'],
'role_id' => $module['role_id'],
'can_access' => (isset($module['can_access']) && !empty($module['can_access'])) ? $module['can_access']:0,
'can_create' => (isset($module['can_create']) && !empty($module['can_create'])) ? $module['can_create']:0,
'can_update' => (isset($module['can_update']) && !empty($module['can_update'])) ? $module['can_update']:0,
'can_delete' => (isset($module['can_delete']) && !empty($module['can_delete'])) ? $module['can_delete']:0,
'can_export' => (isset($module['can_export']) && !empty($module['can_export'])) ? $module['can_export']:0,
'can_import' => (isset($module['can_import']) && !empty($module['can_upload'])) ? $module['can_import']:0,
'can_print' => (isset($module['can_print']) && !empty($module['can_print'])) ? $module['can_print']:0,
];
}
public function select($columns = ['*'])
{
$roles = $this->model->select($columns);
return $roles;
}
public function update($id, array $request)
{
$role = $this->findById($id);
$role->fill($this->makeRole($request));
$role->save();
$this->makeRolePermissions($request, $role->id);
$this->write_menu($id);
return $role;
}
public function delete($id)
{
$role = $this->model->findOrFail($id);
if(!$this->checkIfInUsed($role->users()->count())) return false;
$role->permissions()->delete();
$role->delete();
return true;
}
public function checkIfInUsed($count = 0)
{
return (($count > 0) ? false:true);
}
function build_role_permissions($role_id = 0, $disabled = '')
{
$modules = $this->module->select([
'modules.*',
'ups.can_access',
'ups.can_delete',
'ups.can_update',
'ups.can_create',
'ups.can_export',
'ups.can_import',
'ups.can_print'
])
->where('modules.parent_id', 0)
->where('modules.menu_group_id', 1)
->where('modules.show_menu', 1)
->leftJoin(DB::raw('(SELECT * FROM role_permissions where role_id = '.$role_id.' ) as ups'), 'modules.id', '=', 'ups.menu_id')
->get();
$html_out = "<div class=\"box-group\" id=\"accordion\">";
foreach ($modules as $module_key => $row )
{
$id = $row->id;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$icon = $row->icon;
$menu_group_id = $row->menu_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
$can_access = $row->can_access;
$can_create = $row->can_create;
$can_update = $row->can_update;
$can_delete = $row->can_delete;
$can_export = $row->can_export;
$can_import = $row->can_import;
$can_print = $row->can_print;
$html_out .= "<div class=\"panel box box-primary\">";
$html_out .= "<div class=\"box-header with-border\" data-toggle=\"collapse\" href=\"#collapse".$id."\" style=\"cursor:pointer;\"";
$html_out .= "<h4 class=\"box-title\">";
$html_out .= "<a href=\"\">";
$html_out .= "<b><input name=\"module[".$id."][can_access]\" value=\"1\" class=\"main-menu\" type=\"checkbox\" ".(($can_access) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"show\" data-access-id=\"".$role_id."\" ".$disabled."> <i class=\"fa fa-".$icon."\"></i> ".$title."</b>";
$html_out .= "</a>";
$html_out .= "</h4>";
$html_out .= "</div>";
$html_out .= "<div id=\"collapse".$id."\" class=\"panel-collapse collapse in\">";
$html_out .= "<div class=\"box-body\" style=\"padding:0 !important\">";
$html_out .= '<table class="table table-condensed table-bordered">';
$html_out .= "<thead>";
$html_out .= "<tr valign='middle'>";
$html_out .= "<th colspan=\"3\" > Title</th>";
$html_out .= "<th colspan=\"7\" style=\"text-align:center !important;\"> <b>Permissions</b> </th>";
$html_out .= "</tr>";
$html_out .= "</thead>";
$html_out .= "<tbody>";
$html_out .= "<tr>";
$html_out .= "<td align=\"center\" colspan=\"3 \"></td>";
$html_out .= "<td align=\"center\"> <b>Activate</b> </td>";
$html_out .= "<td align=\"center\"> <b>Create</b> </td>";
$html_out .= "<td align=\"center\"> <b>Update</b> </td>";
$html_out .= "<td align=\"center\"> <b>Delete</b> </td>";
$html_out .= "<td align=\"center\"> <b>Export</b> </td>";
$html_out .= "<td align=\"center\"> <b>Import</b> </td>";
$html_out .= "<td align=\"center\"> <b>Print</b> </td>";
$html_out .= "</tr>";
$html_out .= $this->get_childs_role_permissions($id, $role_id, $disabled);
$html_out .= "</tbody>";
$html_out .= '</table>';
$html_out .= "</div>";
$html_out .= "</div>";
$html_out .= "</div>";
}
$html_out .= '</div>';
return $html_out;
}
function get_childs_role_permissions($id, $role_id, $disabled)
{
$has_subcats = FALSE;
$html_out = '';
$modules = $this->module->select('modules.*','ups.can_access','ups.can_delete','ups.can_update', 'ups.can_create', 'ups.can_export', 'ups.can_import', 'ups.can_print')
->where('modules.parent_id', $id)
->where('modules.menu_group_id', 1)
->where('modules.show_menu', 1)
->leftJoin(DB::raw('(SELECT * FROM role_permissions where role_id = '.$role_id.' ) as ups'), 'modules.id', '=', 'ups.menu_id')
->orderBy('modules.title', 'ASC')
->get();
foreach ($modules as $module_key => $row )
{
$id = $row->id;
$hashid = $row->hashid;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$icon = $row->icon;
$dyn_group_id = $row->menu_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
$has_read = $row->has_read;
$has_create = $row->has_create;
$has_update = $row->has_update;
$has_delete = $row->has_delete;
$has_export = $row->has_export;
$has_import = $row->has_import;
$has_print = $row->has_print;
$can_access = $row->can_access;
$can_create = $row->can_create;
$can_update = $row->can_update;
$can_delete = $row->can_delete;
$can_export = $row->can_export;
$can_import = $row->can_import;
$can_print = $row->can_print;
$has_subcats = TRUE;
if ($is_parent == 1) {
$html_out .= "<tr valign='top' class='sub-parent-tr'>";
$html_out .= "<td colspan=\"3\" rowspan=\"1\"><b><i class=\"fa fa-".$icon."\"></i> ".$title ." </b><i class=\"fa fa-long-arrow-right\"></i> </td>";
$html_out .= "<td colspan=\"1\" align=\"center\"><input name=\"module[".$id."][can_access]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_access) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"show\" data-access-id=\"".$role_id."\" ".$disabled."></td>";
$html_out .= "<td align=\"center\" colspan=\"8\">";
$html_out .= "</td>";
$html_out .= "</tr>";
$html_out .= $this->get_childs2($id, $role_id, $disabled);
} else {
$html_out .= "<tr valign='top' class='sub-parent-tr'>";
$html_out .= "<td colspan=\"3\" width=\"20%\" style='white-space:nowrap;'><i class=\"fa fa-".$icon."\"></i> ".$title."</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
$html_out .= "<label><input name=\"module[".$id."][can_access]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_access) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"show\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_create) {
$html_out .= "<label><input name=\"module[".$id."][can_create]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_create) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"create\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_update) {
$html_out .= "<label><input name=\"module[".$id."][can_update]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_update) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"update\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_delete) {
$html_out .= "<label><input name=\"module[".$id."][can_delete]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_delete) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"delete\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_export) {
$html_out .= "<label><input name=\"module[".$id."][can_export]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_export) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"export\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_import) {
$html_out .= "<label><input name=\"module[".$id."][can_import]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_import) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"import\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_print) {
$html_out .= "<label><input name=\"module[".$id."][can_print]\" value=\"1\" class=\"sub-menu\" type=\"checkbox\" ".(($can_print) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"print\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= '</tr>';
}
}
return ($has_subcats) ? $html_out : FALSE;
}
function get_childs2($id, $role_id, $disabled)
{
$has_subcats = FALSE;
$html_out = '';
$modules = $this->module->select('modules.*','ups.can_access','ups.can_delete','ups.can_update', 'ups.can_create', 'ups.can_export', 'ups.can_import', 'ups.can_print')
->where('modules.parent_id', $id)
->where('modules.menu_group_id', 1)
->where('modules.show_menu', 1)
->leftJoin(DB::raw('(SELECT * FROM role_permissions where role_id = '.$role_id.' ) as ups'), 'modules.id', '=', 'ups.menu_id')
->orderBy('modules.title', 'ASC')
->get();
foreach ($modules as $module_key => $row )
{
$id = $row->id;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$dyn_group_id = $row->menu_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
$has_read = $row->has_read;
$has_create = $row->has_create;
$has_update = $row->has_update;
$has_delete = $row->has_delete;
$has_export = $row->has_export;
$has_import = $row->has_import;
$has_print = $row->has_print;
$can_access = $row->can_access;
$can_create = $row->can_create;
$can_update = $row->can_update;
$can_delete = $row->can_delete;
$can_export = $row->can_export;
$can_import = $row->can_import;
$can_print = $row->can_print;
$has_subcats = TRUE;
$html_out .= "<tr valign='top' class='sub-child-tr sub-table'>";
if($module_key == 0) {
$html_out .= "<td rowspan=\"".count($modules)."\" colspan=\"2\" width=\"20%\" style='white-space:nowrap;background-color:#E1E1E1'></td>";
}
$html_out .= "<td nowrap=\"nowrap\"> ".$title."- ID: ".$id."</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
$html_out .= "<label><input name=\"module[".$id."][can_access]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_access) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"show\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_create) {
$html_out .= "<label><input name=\"module[".$id."][can_create]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_create) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"create\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_update) {
$html_out .= "<label><input name=\"module[".$id."][can_update]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_update) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"update\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_delete) {
$html_out .= "<label><input name=\"module[".$id."][can_delete]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_delete) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"delete\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_export) {
$html_out .= "<label><input name=\"module[".$id."][can_export]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_export) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"export\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_import) {
$html_out .= "<label><input name=\"module[".$id."][can_import]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_import) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"import\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= "<td width=\"10%\" align=\"center\">";
if ($has_print) {
$html_out .= "<label><input name=\"module[".$id."][can_print]\" value=\"1\" class=\"icheck\" type=\"checkbox\" ".(($can_print) ? 'checked':'')." data-id=\"".$id."\" data-permission=\"print\" data-access-id=\"".$role_id."\" ".$disabled."></label>";
} else {
$html_out .= "<span class=\"fa fa-times text-red\"></span>";
}
$html_out .= "</td>";
$html_out .= '</tr>';
}
return ($has_subcats) ? $html_out : FALSE;
}
function build_menu($role_id = 0)
{
$modules = $this->module->select('modules.*','ups.can_access','ups.can_delete','ups.can_update', 'ups.can_create', 'ups.can_export', 'ups.can_import', 'ups.can_print')
->where('modules.parent_id', 0)
->where('modules.menu_group_id', 1)
->where('modules.show_menu', 1)
->where('ups.can_access', 1)
->leftJoin(DB::raw('(SELECT * FROM role_permissions where role_id = '.$role_id.' ) as ups'), 'modules.id', '=', 'ups.menu_id')
->where('ups.can_access', 1)
->orderBy('id', 'ASC')
->get();
$html_out = "\t"."<ul class=\"nav navbar-nav\">"."\n";
$html_out .= "\t\t"."<li>"."\n";
$html_out .= "\t\t\t"."<a href=\"#\"><i class=\"fa fa-cogs\"></i> Developer <span class=\"caret\"></span></a>"."\n";
$html_out .= "\t\t\t\t"."<ul class=\"dropdown-menu\" role=\"menu\">"."\n";
$html_out .= "\t\t\t\t\t"."<li><a href=\"".url(config('modules.likod').'/developer/modules')."\"><i class=\"fa fa-cubes\"></i> Module Manager</a></li>"."\n";
$html_out .= "\t\t\t\t\t"."<li><a href=\"".url(config('modules.likod').'/developer/client-modules')."\"><i class=\"fa fa-cubes\"></i> QxCMS Module Manager</a></li>"."\n";
$html_out .= "\t\t\t\t"."</ul>"."\n";
$html_out .= "\t\t"."</li>"."\n";
foreach ($modules as $menu_key => $row )
{
$id = $row->id;
$hashid = $row->hashid;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$icon = $row->icon;
$menu_group_id = $row->menu_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
if ($show_menu && $parent_id == 0) {
if ($is_parent == TRUE) {
$html_out .= "\t\t"."<li>"."\n";
$html_out .= "\t\t\t"."<a href=\"#\"><i class=\"fa fa-".$icon."\"></i> ".$title." <span class=\"caret\"></span></a>"."\n";
} else {
$html_out .= "\t\t"."<li>"."\n";
$html_out .= "\t\t\t".'<a href="'.url(config('modules.likod').'/'.$url).'" title="'.$title.'" id="menu'.$id.'" target="'.$target.'"><span class="fa fa-'.$icon.'"></span> '.$title.'</a>'."\n";
}
$html_out .= $this->get_menu_childs($role_id, $id);
}
$html_out .= '</li>'."\n";
}
$html_out .= "\t\t".'</ul>' . "\n";
return $html_out;
}
function get_menu_childs($role_id, $id)
{
$has_subcats = FALSE;
$html_out = '';
$html_out .= "\t\t\t\t\t".'<ul class="dropdown-menu">'."\n";
$modules = $this->module->select('modules.*','ups.can_access','ups.can_delete','ups.can_update', 'ups.can_create', 'ups.can_export', 'ups.can_import', 'ups.can_print')
->where('modules.parent_id', $id)
->where('modules.menu_group_id', 1)
->where('modules.show_menu', 1)
->where('ups.can_access', 1)
->leftJoin(DB::raw('(SELECT * FROM role_permissions where role_id = '.$role_id.' ) as ups'), 'modules.id', '=', 'ups.menu_id')
->orderBy('modules.title', 'ASC')
->get();
foreach ($modules as $menu_key => $row )
{
$id = $row->id;
$hashid = $row->hashid;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$icon = $row->icon;
$menu_group_id = $row->menu_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->can_access;
$has_subcats = TRUE;
if($show_menu) {
if ($is_parent == TRUE) {
$html_out .= "\t\t\t".'<li class="dropdown"><a href="'.(($uri != "") ? $uri:'#').'" title="'.$title.'" id="'.$id.'" target="'.$target.'"><i class="fa fa-'.$icon.'"></i> '.$title.' ></a>';
} else {
$html_out .= "\t\t\t".'<li><a href="'.url(config('modules.likod').'/'.$url).'" title="'.$title.'" id="'.$id.'" target="'.$target.'"><i class="fa fa-'.$icon.'"></i> '.$title.'</a>';
}
}
$html_out .= $this->get_menu_childs($role_id, $id);
$html_out .= '</li>' . "\n";
}
$html_out .= "\t\t\t\t\t".'</ul>' . "\n";
return ($has_subcats) ? $html_out : FALSE;
}
function write_menu($role_id)
{
$view_path = realpath(app_path()).'/Modules/Likod/Views';
$realpath = $view_path."/cache/menus/";
File::makeDirectory($realpath, 0775, true, true);
$filename = "custom_".$role_id.".blade.php";
$contents = $this->build_menu($role_id);
$created = File::put($realpath.$filename, $contents);
}
}