add locale manager

This commit is contained in:
2024-03-27 13:56:52 +05:00
parent 7edca39546
commit 7443ff19d6
23 changed files with 845 additions and 497 deletions

View File

@@ -18,6 +18,23 @@ function isLocalIp(string $ip = ''): bool
return ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
}
/**
* Is turkmen ip
* @param string $ip
*/
function isTurkmenIp(string $ip = ''): bool
{
$patterns = ['95.85', '216'];
foreach ($patterns as $pattern) {
if (strpos($ip, $pattern) === 0) {
return true;
}
}
return false;
}
/**
* Un mask phone from "+(993)-xx-xx-xx-xx"
*/
@@ -138,3 +155,19 @@ function storeResourceEvent(string $name, array $data, Request $request): void
Log::error('Error in storeResourceEvent() helpers', ['error' => $e]);
}
}
/**
* Locale app path
*/
function localeAppPath(): string
{
return config('app.locale_app.path');
}
/**
* Locale app url
*/
function localeAppUrl(): string
{
return config('app.locale_app.url');
}

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Http\Controllers;
class LocaleManagerController extends Controller
{
//
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models\System\Locale;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LocaleManager extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Nova\Actions;
use App\Repos\System\Locale\LocaleManagerRepo;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Actions\ActionResponse;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Http\Requests\NovaRequest;
class ExportTranslations extends Action
{
use InteractsWithQueue, Queueable;
/**
* Perform the action on the given models.
*
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models): mixed
{
LocaleManagerRepo::make()->handle();
return ActionResponse::message('It worked!');
}
/**
* Get the fields available on the action.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request): array
{
return [];
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Nova\Resources\System\Locale;
use App\Models\System\Locale\LocaleManager;
use App\Nova\Actions\ExportTranslations;
use App\Nova\Resource;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class LocaleManagerResource extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\App\Models\System\Locale\LocaleManager>
*/
public static $model = LocaleManager::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'name',
];
/**
* Get the fields displayed by the resource.
*/
public function fields(NovaRequest $request): array
{
return [
ID::make()->sortable(),
Text::make(__('Main'), 'name'),
];
}
/**
* Get the cards available for the request.
*
* @return array
*/
public function cards(NovaRequest $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @return array
*/
public function filters(NovaRequest $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @return array
*/
public function lenses(NovaRequest $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @return array
*/
public function actions(NovaRequest $request): array
{
return [
ExportTranslations::make()
->standalone(),
];
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace App\Policies\System\Locale;
use App\Models\System\Locale\LocaleManager;
use App\Models\User;
class LocaleManagerPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, LocaleManager $localeManager): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, LocaleManager $localeManager): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, LocaleManager $localeManager): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, LocaleManager $localeManager): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, LocaleManager $localeManager): bool
{
if ($user->isMe()) {
return true;
}
return false;
}
}

View File

@@ -7,14 +7,17 @@ use App\Models\Order\Card\CardState;
use App\Models\Order\Card\CardType;
use App\Models\Order\Loan\LoanOrder;
use App\Models\Order\Loan\LoanType;
use App\Models\System\Locale\LocaleManager;
use App\Models\System\Location\Province;
use App\Models\System\Roles\Permission;
use App\Models\System\Roles\Role;
use App\Models\User;
use App\Policies\Branch\BranchPolicy;
use App\Policies\Order\Card\CardStatePolicy;
use App\Policies\Order\Card\CardTypePolicy;
use App\Policies\Order\Loan\LoanOrderPolicy;
use App\Policies\Order\Loan\LoanTypePolicy;
use App\Policies\System\Locale\LocaleManagerPolicy;
use App\Policies\System\Location\ProvincePolicy;
use App\Policies\System\Logs\ActionEventPolicy;
use App\Policies\System\Roles\PermissionPolicy;
@@ -53,6 +56,9 @@ class AuthServiceProvider extends ServiceProvider
Branch::class => BranchPolicy::class,
Province::class => ProvincePolicy::class,
// Locale manager...
LocaleManager::class => LocaleManagerPolicy::class,
// ActionsEvents...
ActionEvent::class => ActionEventPolicy::class,
];

View File

@@ -0,0 +1,101 @@
<?php
namespace App\Repos\System\Locale;
use Exception;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Symfony\Component\Filesystem\Filesystem;
class LocaleManagerRepo
{
/**
* Locale app path
*/
protected string $localeAppPath;
/**
* Locale app url
*/
protected string $localeAppUrl;
/**
* Locale app api token
*/
protected string $localeAppApiToken;
/**
* Files system
*/
protected Filesystem $fileSystem;
/**
* Locale manager app
*/
public function __construct()
{
$this->localeAppPath = config('app.locale_app.path');
$this->localeAppUrl = config('app.locale_app.url');
$this->localeAppApiToken = config('app.locale_app.api_token');
$this->fileSystem = new Filesystem();
}
/**
* Magic way of new
*/
public static function make(): self
{
return new self();
}
/**
* Run locale manager
*/
public function handle(): void
{
$this->exportTranslations()
->syncTranslationsWithLocaleApp();
}
/**
* Locale app translations directory
*/
public function localeAppTranslationsDirectory(): string
{
return $this->localeAppPath . DIRECTORY_SEPARATOR . 'lang';
}
/**
* Export translations to translate app
*/
public function exportTranslations(): self
{
$this->fileSystem->mirror(lang_path(), $this->localeAppTranslationsDirectory());
return $this;
}
/**
* Sync translations with locale app
*/
public function syncTranslationsWithLocaleApp(): self
{
$response = Http::acceptJson()->withHeaders([
'Api-Token' => $this->localeAppApiToken,
])
->retry(
times: 3,
sleepMilliseconds: 50,
throw: false,
when: function (Exception $exception, PendingRequest $request) {
return true;
})
->post(
url: $this->localeAppUrl . '/api/import-translations',
data: []
);
return $this;
}
}

View File

@@ -12,6 +12,7 @@ use App\Nova\Resources\Order\Card\Requisite\CardRequisite;
use App\Nova\Resources\Order\Loan\LoanOrder;
use App\Nova\Resources\Order\Loan\LoanPaidOffLetterOrder;
use App\Nova\Resources\Order\Loan\LoanType;
use App\Nova\Resources\System\Locale\LocaleManagerResource;
use App\Nova\Resources\System\Location\Province;
use App\Nova\Resources\System\Roles\Permission;
use App\Nova\Resources\System\Roles\Role;
@@ -64,6 +65,11 @@ class NovaMenuRepo
MenuItem::resource(Province::class),
MenuItem::resource(Branch::class),
])->collapsedByDefault(),
MenuGroup::make(__('Locale'), [
MenuItem::resource(LocaleManagerResource::class),
])->collapsedByDefault(),
])->icon('cog')->collapsedByDefault(),
MenuSection::make(__('Backups'))