[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: DataTablesEditor.php
<?php declare(strict_types=1); namespace Yajra\DataTables; use Exception; use Illuminate\Contracts\Validation\Validator; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; /** * @template TModelClass of Model */ abstract class DataTablesEditor { use Concerns\WithCreateAction; use Concerns\WithEditAction; use Concerns\WithForceDeleteAction; use Concerns\WithRemoveAction; use Concerns\WithUploadAction; use ValidatesRequests; /** * Action performed by the editor. */ protected ?string $action = null; /** * Allowed dataTables editor actions. * * @var string[] */ protected array $actions = [ 'create', 'edit', 'remove', 'upload', 'forceDelete', 'restore', ]; /** * List of custom editor actions. * * @var string[] */ protected array $customActions = []; /** * @var null|class-string<\Illuminate\Database\Eloquent\Model>|Model */ protected $model = null; /** * Indicates if all mass assignment is enabled on model. */ protected bool $unguarded = false; /** * Upload directory relative to storage path. */ protected string $uploadDir = 'editor'; /** * Flag to force delete a model. */ protected bool $forceDeleting = false; /** * Flag to restore a model from deleted state. */ protected bool $restoring = false; /** * Filesystem disk config to use for upload. */ protected string $disk = 'public'; /** * Current request data that is being processed. */ protected array $currentData = []; /** * Process dataTables editor action request. * * @return JsonResponse * * @throws DataTablesEditorException */ public function process(Request $request): mixed { if ($request->get('action') && is_string($request->get('action'))) { $this->action = $request->get('action'); } else { throw new DataTablesEditorException('Invalid action requested!'); } if (! in_array($this->action, array_merge($this->actions, $this->customActions))) { throw new DataTablesEditorException(sprintf('Requested action (%s) not supported!', $this->action)); } try { return $this->{$this->action}($request); } catch (Exception $exception) { $error = config('app.debug') ? '<strong>Server Error:</strong> '.$exception->getMessage() : $this->getUseFriendlyErrorMessage(); app('log')->error($exception); return $this->toJson([], [], $error); } } protected function getUseFriendlyErrorMessage(): string { return 'An error occurs while processing your request.'; } /** * Display success data in dataTables editor format. */ protected function toJson(array $data, array $errors = [], string|array $error = ''): JsonResponse { $code = 200; $response = [ 'action' => $this->action, 'data' => $data, ]; if ($error) { $code = 422; $response['error'] = $error; } if ($errors) { $code = 422; $response['fieldErrors'] = $errors; } return new JsonResponse($response, $code); } /** * Get custom attributes for validator errors. */ public function attributes(): array { return []; } /** * Get dataTables model. */ public function getModel(): Model|string|null { return $this->model; } /** * Set the dataTables model on runtime. * * @param class-string<Model>|Model $model */ public function setModel(Model|string $model): static { $this->model = $model; return $this; } /** * Get validation messages. */ protected function messages(): array { return []; } protected function formatErrors(Validator $validator): array { $errors = []; collect($validator->errors())->each(function ($error, $key) use (&$errors) { $errors[] = [ 'name' => $key, 'status' => $error[0], ]; }); return $errors; } /** * Get eloquent builder of the model. * * @return \Illuminate\Database\Eloquent\Builder<TModelClass> */ protected function getBuilder(): Builder { $model = $this->resolveModel(); if (in_array(SoftDeletes::class, class_uses($model))) { // @phpstan-ignore-next-line return $model->newQuery()->withTrashed(); } return $model->newQuery(); } /** * Resolve model to used. */ protected function resolveModel(): Model { if (! $this->model instanceof Model) { $this->model = new $this->model; } $this->model->unguard($this->unguarded); return $this->model; } /** * Set model unguard state. * * @return $this */ public function unguard(bool $state = true): static { $this->unguarded = $state; return $this; } protected function dataFromRequest(Request $request): array { return (array) $request->get('data'); } public function saving(Model $model, array $data): array { return $data; } public function saved(Model $model, array $data): Model { return $model; } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.84 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002