Skip to content

Commit

Permalink
Merge pull request #38 from yajra/upload
Browse files Browse the repository at this point in the history
Add support for handling field type upload and uploadMany.
  • Loading branch information
yajra committed Sep 10, 2019
2 parents 33359f4 + 65ffefc commit 302cd5e
Showing 1 changed file with 92 additions and 4 deletions.
96 changes: 92 additions & 4 deletions src/DataTablesEditor.php
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class DataTablesEditor
Expand All @@ -18,7 +20,7 @@ abstract class DataTablesEditor
*
* @var array
*/
protected $actions = ['create', 'edit', 'remove'];
protected $actions = ['create', 'edit', 'remove', 'upload'];

/**
* @var \Illuminate\Database\Eloquent\Model
Expand All @@ -32,6 +34,20 @@ abstract class DataTablesEditor
*/
protected $unguarded = false;

/**
* Upload directory relative to storage path.
*
* @var string
*/
protected $uploadDir = 'editor';

/**
* Filesystem disk config to use for upload.
*
* @var string
*/
protected $disk = 'public';

/**
* Process dataTables editor action request.
*
Expand Down Expand Up @@ -66,7 +82,7 @@ public function create(Request $request)
$connection->beginTransaction();
foreach ($request->get('data') as $data) {
$validator = $this->getValidationFactory()
->make($data, $this->createRules(), $this->createMessages(), $this->attributes());
->make($data, $this->createRules(), $this->messages() + $this->createMessages(), $this->attributes());
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error;
Expand Down Expand Up @@ -132,6 +148,7 @@ abstract public function createRules();
/**
* Get create validation messages.
*
* @deprecated deprecated since v1.12.0, please use messages() instead.
* @return array
*/
protected function createMessages()
Expand Down Expand Up @@ -200,7 +217,7 @@ public function edit(Request $request)
foreach ($request->get('data') as $key => $data) {
$model = $this->getBuilder()->findOrFail($key);
$validator = $this->getValidationFactory()
->make($data, $this->editRules($model), $this->editMessages(), $this->attributes());
->make($data, $this->editRules($model), $this->messages() + $this->editMessages(), $this->attributes());
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error;
Expand Down Expand Up @@ -267,6 +284,7 @@ abstract public function editRules(Model $model);
/**
* Get edit validation messages.
*
* @deprecated deprecated since v1.12.0, please use messages() instead.
* @return array
*/
protected function editMessages()
Expand All @@ -290,7 +308,7 @@ public function remove(Request $request)
foreach ($request->get('data') as $key => $data) {
$model = $this->getBuilder()->findOrFail($key);
$validator = $this->getValidationFactory()
->make($data, $this->removeRules($model), $this->removeMessages(), $this->attributes());
->make($data, $this->removeRules($model), $this->messages() + $this->removeMessages(), $this->attributes());
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error['status'];
Expand Down Expand Up @@ -346,6 +364,7 @@ abstract public function removeRules(Model $model);
/**
* Get remove validation messages.
*
* @deprecated deprecated since v1.12.0, please use messages() instead.
* @return array
*/
protected function removeMessages()
Expand Down Expand Up @@ -401,6 +420,75 @@ public function unguard($state = true)
return $this;
}

/**
* Handle uploading of file.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function upload(Request $request)
{
$field = $request->input('uploadField');
$storage = Storage::disk($this->disk);

try {
$rules = $this->uploadRules();
$fieldRules = ['upload' => data_get($rules, $field, [])];

$this->validate($request, $fieldRules, $this->uploadMessages(), $this->attributes());

$id = $storage->putFile($this->uploadDir, $request->file('upload'));

if (method_exists($this, 'uploaded')) {
$id = $this->uploaded($id);
}

return response()->json([
'data' => [],
'file' => [
'filename' => $id,
'size' => $request->file('upload')->getSize(),
'directory' => $this->uploadDir,
'disk' => $this->disk,
'url' => $storage->url($id),
],
'upload' => [
'id' => $id,
],
]);
} catch (ValidationException $exception) {
return response()->json([
'data' => [],
'fieldErrors' => [
[
'name' => $field,
'status' => str_replace('upload', $field, $exception->errors()['upload'][0]),
],
],
]);
}
}

/**
* Upload validation rules.
*
* @return array
*/
public function uploadRules()
{
return [];
}

/**
* Get validation messages.
*
* @return array
*/
protected function messages()
{
return [];
}

/**
* Display dataTables editor validation errors.
*
Expand Down

0 comments on commit 302cd5e

Please sign in to comment.