50
common/Database/Seeds/CssThemesTableSeeder.php
Executable file
50
common/Database/Seeds/CssThemesTableSeeder.php
Executable file
@@ -0,0 +1,50 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Admin\Appearance\Themes\CssTheme;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class CssThemesTableSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$dark = config('common.themes.dark');
|
||||
$light = config('common.themes.light');
|
||||
|
||||
$admin = User::whereHas('permissions', function (Builder $builder) {
|
||||
$builder->where('name', 'admin');
|
||||
})->first();
|
||||
|
||||
$darkTheme = CssTheme::where('default_dark', true)
|
||||
->orWhere('name', 'Dark')
|
||||
->first();
|
||||
if (!$darkTheme || !$darkTheme->getRawOriginal('values')) {
|
||||
if ($darkTheme) {
|
||||
$darkTheme->delete();
|
||||
}
|
||||
CssTheme::create([
|
||||
'name' => 'Dark',
|
||||
'is_dark' => true,
|
||||
'default_dark' => true,
|
||||
'values' => $dark,
|
||||
'user_id' => $admin ? $admin->id : 1,
|
||||
]);
|
||||
}
|
||||
|
||||
$lightTheme = CssTheme::where('default_light', true)
|
||||
->orWhere('name', 'Light')
|
||||
->first();
|
||||
if (!$lightTheme || !$lightTheme->getRawOriginal('values')) {
|
||||
if ($lightTheme) {
|
||||
$lightTheme->delete();
|
||||
}
|
||||
CssTheme::create([
|
||||
'name' => 'Light',
|
||||
'default_light' => true,
|
||||
'user_id' => $admin ? $admin->id : 1,
|
||||
'values' => $light,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
common/Database/Seeds/DefaultPagesSeeder.php
Executable file
79
common/Database/Seeds/DefaultPagesSeeder.php
Executable file
@@ -0,0 +1,79 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use Common\Pages\CustomPage;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DefaultPagesSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
CustomPage::firstOrCreate(
|
||||
[
|
||||
'slug' => 'privacy-policy',
|
||||
],
|
||||
[
|
||||
'title' => 'Example Privacy Policy',
|
||||
'slug' => 'privacy-policy',
|
||||
'body' => $this->replacePlaceholders(
|
||||
file_get_contents(
|
||||
base_path(
|
||||
'common/resources/defaults/privacy-policy.html',
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'default',
|
||||
],
|
||||
);
|
||||
|
||||
CustomPage::firstOrCreate(
|
||||
[
|
||||
'slug' => 'terms-of-service',
|
||||
],
|
||||
[
|
||||
'title' => 'Example Terms of Service',
|
||||
'slug' => 'terms-of-service',
|
||||
'body' => $this->replacePlaceholders(
|
||||
file_get_contents(
|
||||
base_path(
|
||||
'common/resources/defaults/terms-of-service.html',
|
||||
),
|
||||
),
|
||||
),
|
||||
'type' => 'default',
|
||||
],
|
||||
);
|
||||
|
||||
CustomPage::firstOrCreate(
|
||||
[
|
||||
'slug' => 'about-us',
|
||||
],
|
||||
[
|
||||
'title' => 'Example About Us',
|
||||
'slug' => 'about-us',
|
||||
'body' => file_get_contents(
|
||||
base_path('common/resources/lorem.html'),
|
||||
),
|
||||
'type' => 'default',
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
protected function replacePlaceholders(string $text): string
|
||||
{
|
||||
return str_replace(
|
||||
[
|
||||
'[Website Name]',
|
||||
'[Website URL]',
|
||||
'[Contact Email]',
|
||||
'[Your Country/State]',
|
||||
],
|
||||
[
|
||||
config('app.name'),
|
||||
url('/'),
|
||||
settings('mail.contact_page_address'),
|
||||
'United States',
|
||||
],
|
||||
$text,
|
||||
);
|
||||
}
|
||||
}
|
||||
45
common/Database/Seeds/LocalizationsTableSeeder.php
Executable file
45
common/Database/Seeds/LocalizationsTableSeeder.php
Executable file
@@ -0,0 +1,45 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use Common\Localizations\Localization;
|
||||
use Common\Localizations\LocalizationsRepository;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class LocalizationsTableSeeder extends Seeder
|
||||
{
|
||||
public function __construct(protected LocalizationsRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$localizations = Localization::all();
|
||||
|
||||
if ($localizations->isNotEmpty()) {
|
||||
$this->mergeExistingTranslationLines($localizations);
|
||||
} else {
|
||||
$this->repository->create([
|
||||
'name' => 'English',
|
||||
'language' => 'en',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge existing localization translation lines with default ones.
|
||||
*/
|
||||
private function mergeExistingTranslationLines(Collection $localizations)
|
||||
{
|
||||
$defaultLines = $this->repository->getDefaultTranslationLines();
|
||||
|
||||
$localizations->each(function ($localization) use ($defaultLines) {
|
||||
$this->repository->storeLocalizationLines(
|
||||
$localization,
|
||||
array_merge(
|
||||
$defaultLines,
|
||||
$this->repository->getLocalizationLines($localization),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
14
common/Database/Seeds/MailTemplatesSeeder.php
Executable file
14
common/Database/Seeds/MailTemplatesSeeder.php
Executable file
@@ -0,0 +1,14 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class MailTemplatesSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
29
common/Database/Seeds/PermissionTableSeeder.php
Executable file
29
common/Database/Seeds/PermissionTableSeeder.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use Common\Auth\Permissions\Permission;
|
||||
use Common\Core\Values\GetStaticPermissions;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class PermissionTableSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$allPermissions = app(GetStaticPermissions::class)->execute();
|
||||
$allPermissions['admin'][] = [
|
||||
'name' => 'admin',
|
||||
'display_name' => 'Super Admin',
|
||||
'description' => 'Give all permissions to user.',
|
||||
'group' => 'admin',
|
||||
];
|
||||
|
||||
foreach ($allPermissions as $groupName => $group) {
|
||||
foreach ($group as $permission) {
|
||||
$permission['group'] = $groupName;
|
||||
app(Permission::class)->updateOrCreate(['name' => $permission['name']], $permission);
|
||||
}
|
||||
}
|
||||
|
||||
// delete legacy permissions
|
||||
app(Permission::class)->whereNull('group')->delete();
|
||||
}
|
||||
}
|
||||
128
common/Database/Seeds/RolesTableSeeder.php
Executable file
128
common/Database/Seeds/RolesTableSeeder.php
Executable file
@@ -0,0 +1,128 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Auth\Permissions\Permission;
|
||||
use Common\Auth\Permissions\Traits\SyncsPermissions;
|
||||
use Common\Auth\Roles\Role;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RolesTableSeeder extends Seeder
|
||||
{
|
||||
use SyncsPermissions;
|
||||
|
||||
private array $commonConfig = [];
|
||||
private array $appConfig = [];
|
||||
|
||||
public function __construct(
|
||||
protected Role $role,
|
||||
protected User $user,
|
||||
protected Permission $permission,
|
||||
protected Filesystem $fs,
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->commonConfig = File::getRequire(
|
||||
app('path.common') . '/resources/defaults/permissions.php',
|
||||
);
|
||||
$this->appConfig = File::getRequire(
|
||||
resource_path('defaults/permissions.php'),
|
||||
);
|
||||
|
||||
foreach ($this->appConfig['roles'] as $appRole) {
|
||||
if ($commonRoleName = Arr::get($appRole, 'extends')) {
|
||||
$commonRole = $this->findRoleConfig($commonRoleName);
|
||||
$appRole = array_merge($commonRole, $appRole);
|
||||
$appRole['permissions'] = array_merge(
|
||||
$commonRole['permissions'],
|
||||
$appRole['permissions'],
|
||||
);
|
||||
}
|
||||
|
||||
// skip billing permissions if billing is not integrated
|
||||
$appRole['permissions'] = array_filter(
|
||||
$appRole['permissions'],
|
||||
function ($permission) {
|
||||
if (is_array($permission)) {
|
||||
$permission = $permission['name'];
|
||||
}
|
||||
return config('common.site.billing_integrated') ||
|
||||
!Str::contains($permission, ['invoice.', 'plans.']);
|
||||
},
|
||||
);
|
||||
|
||||
$this->createOrUpdateRole($appRole);
|
||||
}
|
||||
}
|
||||
|
||||
private function findRoleConfig(string $roleName): array
|
||||
{
|
||||
$roleConfig = Arr::first($this->commonConfig['roles'], function (
|
||||
$role,
|
||||
) use ($roleName) {
|
||||
return $role['name'] === $roleName;
|
||||
});
|
||||
if (!$roleConfig) {
|
||||
$roleConfig = Arr::first($this->appConfig['roles'], function (
|
||||
$role,
|
||||
) use ($roleName) {
|
||||
return $role['name'] === $roleName;
|
||||
});
|
||||
}
|
||||
return $roleConfig;
|
||||
}
|
||||
|
||||
private function createOrUpdateRole(array $appRole): Role
|
||||
{
|
||||
$defaultPermissions = collect($appRole['permissions'])->map(
|
||||
fn($permission) => is_string($permission)
|
||||
? ['name' => $permission]
|
||||
: $permission,
|
||||
);
|
||||
|
||||
$dbPermissions = Permission::whereIn(
|
||||
'name',
|
||||
$defaultPermissions->pluck('name'),
|
||||
)->get();
|
||||
$dbPermissions->map(function (Permission $permission) use (
|
||||
$defaultPermissions,
|
||||
) {
|
||||
$defaultPermission = $defaultPermissions
|
||||
->where('name', $permission['name'])
|
||||
->first();
|
||||
$permission['restrictions'] =
|
||||
Arr::get($defaultPermission, 'restrictions') ?: [];
|
||||
return $permission;
|
||||
});
|
||||
|
||||
if (Arr::get($appRole, 'default')) {
|
||||
$attributes = ['default' => true];
|
||||
Role::where('name', $appRole['name'])->update(['default' => true]);
|
||||
} elseif (Arr::get($appRole, 'guests')) {
|
||||
$attributes = ['guests' => true];
|
||||
Role::where('name', $appRole['name'])->update(['guests' => true]);
|
||||
} else {
|
||||
$attributes = ['name' => $appRole['name']];
|
||||
}
|
||||
|
||||
if ($role = Role::where($attributes)->first()) {
|
||||
return $role;
|
||||
} else {
|
||||
$role = $this->role->create(
|
||||
Arr::except($appRole, ['permissions', 'extends']),
|
||||
);
|
||||
$this->syncPermissions(
|
||||
$role,
|
||||
$role->permissions->concat($dbPermissions),
|
||||
);
|
||||
$role->save();
|
||||
|
||||
return $role;
|
||||
}
|
||||
}
|
||||
}
|
||||
109
common/Database/Seeds/SettingsTableSeeder.php
Executable file
109
common/Database/Seeds/SettingsTableSeeder.php
Executable file
@@ -0,0 +1,109 @@
|
||||
<?php namespace Common\Database\Seeds;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Common\Settings\Setting;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SettingsTableSeeder extends Seeder
|
||||
{
|
||||
public function __construct(protected Setting $setting)
|
||||
{
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$defaultSettings = config('common.default-settings');
|
||||
|
||||
$names = [];
|
||||
|
||||
$defaultSettings = array_map(function ($setting) use (&$names) {
|
||||
$names[] = $setting['name'];
|
||||
|
||||
$setting['created_at'] = Carbon::now();
|
||||
$setting['updated_at'] = Carbon::now();
|
||||
|
||||
//make sure all settings have "private" field to
|
||||
//avoid db errors due to different column count
|
||||
if (!array_key_exists('private', $setting)) {
|
||||
$setting['private'] = 0;
|
||||
}
|
||||
|
||||
// cast booleans to string as "insert"
|
||||
// method will not use Setting model setters
|
||||
if ($setting['value'] === true) {
|
||||
$setting['value'] = 'true';
|
||||
} elseif ($setting['value'] === false) {
|
||||
$setting['value'] = 'false';
|
||||
}
|
||||
$setting['value'] = (string) $setting['value'];
|
||||
|
||||
// add ids to menus and menu items, if don't have one already
|
||||
if ($setting['name'] === 'menus') {
|
||||
$value = json_decode($setting['value'], true);
|
||||
foreach ($value as &$menu) {
|
||||
if (!isset($menu['id'])) {
|
||||
$menu['id'] = Str::random(6);
|
||||
}
|
||||
foreach ($menu['items'] as &$item) {
|
||||
if (!isset($item['id'])) {
|
||||
$item['id'] = Str::random(6);
|
||||
}
|
||||
}
|
||||
}
|
||||
$setting['value'] = json_encode($value);
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}, $defaultSettings);
|
||||
|
||||
$existing = $this->setting->whereIn('name', $names)->pluck('name');
|
||||
|
||||
//only insert settings that don't already exist in database
|
||||
$new = array_filter($defaultSettings, function ($setting) use (
|
||||
$existing,
|
||||
) {
|
||||
return !$existing->contains($setting['name']);
|
||||
});
|
||||
|
||||
$this->setting->insert($new);
|
||||
|
||||
$this->mergeMenusSetting($defaultSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge existing menus setting json with new one.
|
||||
*/
|
||||
private function mergeMenusSetting(array $defaultSettings): void
|
||||
{
|
||||
$existing =
|
||||
$this->setting->where('name', 'menus')->first()->value ?? [];
|
||||
$new = json_decode(
|
||||
Arr::first(
|
||||
$defaultSettings,
|
||||
fn($value) => $value['name'] === 'menus',
|
||||
)['value'],
|
||||
true,
|
||||
);
|
||||
|
||||
foreach ($new as $newMenu) {
|
||||
$alreadyHas = Arr::first(
|
||||
$existing,
|
||||
fn($value) => $value['name'] === $newMenu['name'],
|
||||
);
|
||||
|
||||
foreach ($newMenu['items'] as $index => $item) {
|
||||
$newMenu['items'][$index]['order'] = $index;
|
||||
}
|
||||
|
||||
if (!$alreadyHas) {
|
||||
$existing[] = $newMenu;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setting
|
||||
->where('name', 'menus')
|
||||
->update(['value' => json_encode($existing)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user