public function __construct($userID)
 {
     $this->SQLData = BackendMainMenu::getBackendMainMenuTree($userID);
     $this->model = 'App\\Models\\System\\BackendMainMenu';
     $this->primaryKey = "backend_main_menu_id";
 }
 public function run()
 {
     $handle = fopen($this->baseDir . 'users_groups_permissions.tsv', 'r');
     $headers = fgetcsv($handle, 1024, '	');
     $entityPermissions = array_fill_keys($headers, '');
     while (($data = fgetcsv($handle, 2048, '	')) != false) {
         reset($headers);
         foreach ($data as $val) {
             $entityPermissions[$headers[key($headers)]][] = $val;
             next($headers);
         }
     }
     fclose($handle);
     $groupList = Models\Group::getConstants();
     $permissionCategories = array();
     $permissionCategoryList = array();
     $permissionCategoryID = 1;
     $permissionEntities = array();
     $permissionEntityList = array();
     $permissionEntityID = 1;
     $permissionActions = array();
     $permissionActionList = array();
     $permissionActionID = 1;
     $permissionManagementActions = array();
     $permissionManagementActionList = array();
     $permissionManagementActionID = 1;
     $handle = fopen($this->baseDir . 'permission_entities_actions.tsv', 'r');
     fgetcsv($handle, 1024, '	');
     while (($data = fgetcsv($handle, 1024, '	')) != false) {
         if (!empty($data[3])) {
             if (!isset($permissionCategoryList[$data[3]])) {
                 $permissionCategoryList[$data[3]] = $permissionCategoryID++;
                 $permissionCategories[] = ['name' => $data[3]];
             }
         }
         $permissionEntities[] = ['name' => $data[0], 'permission_category_id' => $permissionCategoryList[$data[3]]];
         $permissionEntityList[$data[0]] = $permissionEntityID;
         $tmp = explode(';', $data[1]);
         foreach ($tmp as $item) {
             $permissionActions[] = array('name' => $item, 'permission_entity_id' => $permissionEntityID);
             $permissionActionList[$data[0]][$item] = $permissionActionID++;
         }
         $tmp = explode(';', $data[2]);
         foreach ($tmp as $item) {
             $permissionManagementActions[] = array('name' => $item, 'permission_entity_id' => $permissionEntityID);
             $permissionManagementActionList[$data[0]][$item] = $permissionManagementActionID++;
         }
         $permissionEntityID++;
     }
     fclose($handle);
     $permissionGroup = array();
     $permissionManagementGroup = array();
     $permissionManagementID = 1;
     $permissionManagement = array();
     $permissionID = 1;
     $permission = array();
     $permissionListForUserInGroup = array();
     $permissionManagementListForUserInGroup = array();
     reset($entityPermissions);
     $permissionGroupList = $entityPermissions[key($entityPermissions)];
     next($entityPermissions);
     while (list($entity, $val) = each($entityPermissions)) {
         if (strpos($entity, '_management') !== false) {
             foreach ($val as $groupKey => $actions) {
                 $tmp = explode(';', $actions);
                 foreach ($tmp as $item) {
                     if (!empty($item)) {
                         $permissionManagement[] = array('permission_management_action_id' => $permissionManagementActionList[str_replace('_management', '', $entity)][$item], 'permission_recipient_type_id' => Models\PermissionRecipientType::group);
                         $permissionManagementGroup[] = array('type_id' => $groupList[$permissionGroupList[$groupKey]], 'permission_management_id' => $permissionManagementID++);
                         $permissionManagementListForUserInGroup[$groupList[$permissionGroupList[$groupKey]]][] = array('permission_management_action_id' => $permissionManagementActionList[str_replace('_management', '', $entity)][$item], 'permission_recipient_type_id' => Models\PermissionRecipientType::user);
                     }
                 }
             }
         } else {
             foreach ($val as $groupKey => $actions) {
                 $tmp = explode(';', $actions);
                 foreach ($tmp as $item) {
                     if (!empty($item)) {
                         $permission[] = array('permission_action_id' => $permissionActionList[$entity][$item], 'permission_recipient_type_id' => Models\PermissionRecipientType::group);
                         $permissionGroup[] = array('type_id' => $groupList[$permissionGroupList[$groupKey]], 'permission_id' => $permissionID++);
                         $permissionListForUserInGroup[$groupList[$permissionGroupList[$groupKey]]][] = array('permission_action_id' => $permissionActionList[$entity][$item], 'permission_recipient_type_id' => Models\PermissionRecipientType::user);
                     }
                 }
             }
         }
     }
     $permissionUser = array();
     $permissionManagementUser = array();
     $alreadyGivenPermissions = $alreadyGivenManagementPermissions = [];
     $userGroups = Models\UserGroup::all();
     foreach ($userGroups as $userGroup) {
         if (isset($permissionListForUserInGroup[$userGroup->group_id])) {
             foreach ($permissionListForUserInGroup[$userGroup->group_id] as $p) {
                 if (!isset($alreadyGivenPermissions[$userGroup->user_id][$p['permission_action_id']])) {
                     $alreadyGivenPermissions[$userGroup->user_id][$p['permission_action_id']] = true;
                     $permission[] = $p;
                     $permissionUser[] = ['type_id' => $userGroup->user_id, 'permission_id' => $permissionID++];
                 }
             }
         }
         if (isset($permissionManagementListForUserInGroup[$userGroup->group_id])) {
             foreach ($permissionManagementListForUserInGroup[$userGroup->group_id] as $p) {
                 if (!isset($alreadyGivenManagementPermissions[$userGroup->user_id][$p['permission_management_action_id']])) {
                     $alreadyGivenManagementPermissions[$userGroup->user_id][$p['permission_management_action_id']] = true;
                     $permissionManagement[] = $p;
                     $permissionManagementUser[] = ['type_id' => $userGroup->user_id, 'permission_management_id' => $permissionManagementID++];
                 }
             }
         }
     }
     DB::table('permission_recipient_types')->insert([['name' => 'group'], ['name' => 'user']]);
     DB::table('permission_categories')->insert($permissionCategories);
     DB::table('permission_entities')->insert($permissionEntities);
     DB::table('permission_actions')->insert($permissionActions);
     DB::table('permission_management_actions')->insert($permissionManagementActions);
     DB::table('permission_management')->insert($permissionManagement);
     DB::table('permission_management_groups')->insert($permissionManagementGroup);
     DB::table('permission_management_users')->insert($permissionManagementUser);
     DB::table('permissions')->insert($permission);
     DB::table('permission_groups')->insert($permissionGroup);
     DB::table('permission_users')->insert($permissionUser);
     $this->command->info('Permissions seeded');
     /**
      * Entitites for which permissions don't need to be checked have a permission_entity_id of 0.
      *
      */
     $dashboard = ['name' => 'entities.dashboard', 'icon' => 'fa fa-dashboard fa-fw', 'route' => 'admin.dashboard'];
     $sentinel = ['name' => 'entities.sentinel', 'icon' => 'fa fa-key fa-fw', 'children' => [['name' => 'entities.users', 'permission_entity_id' => isset($permissionEntityList['user']) ? $permissionEntityList['user'] : 0, 'route' => 'admin.user.index'], ['name' => 'entities.groups', 'permission_entity_id' => isset($permissionEntityList['group']) ? $permissionEntityList['group'] : 0, 'route' => 'admin.group.index']]];
     $spirit = ['name' => 'entities.spirit', 'icon' => 'fa fa-bullhorn fa-fw', 'children' => [['name' => 'entities.people', 'permission_entity_id' => isset($permissionEntityList['person']) ? $permissionEntityList['person'] : 0, 'route' => 'admin.person.index']]];
     Models\System\BackendMainMenu::create($dashboard);
     Models\System\BackendMainMenu::create($sentinel);
     Models\System\BackendMainMenu::create($spirit);
     $this->command->info('Menu items seeded');
 }