Created
October 25, 2017 13:40
-
-
Save gdevdeiv/0688baada64707e630c0bae76b8bac2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Voyager; | |
use TCG\Voyager\Events\BreadDataUpdated; | |
use TCG\Voyager\Facades\Voyager; | |
use TCG\Voyager\Http\Controllers\VoyagerBreadController as BaseVoyagerBreadController; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\DB; | |
class VoyagerBreadController extends BaseVoyagerBreadController | |
{ | |
public function form(Request $request, $id = NULL) | |
{ | |
$slug = $this->getSlug($request); | |
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first(); | |
if ($id === NULL) | |
{ | |
$this->authorize('add', app($dataType->model_name)); | |
$dataTypeContent = (strlen($dataType->model_name) != 0) | |
? new $dataType->model_name() | |
: false; | |
$iterateOn = $dataType->addRows; | |
} | |
else | |
{ | |
// Compatibility with Model binding. | |
$id = $id instanceof Model ? $id->{$id->getKeyName()} : $id; | |
$relationships = $this->getRelationships($dataType); | |
$dataTypeContent = ($id && strlen($dataType->model_name) != 0) | |
? app($dataType->model_name)->with($relationships)->find($id) | |
: DB::table($dataType->name)->where('id', $id)->first(); // If Model doest exist, get data from table name | |
// Check permission | |
$this->authorize('edit', $dataTypeContent); | |
$iterateOn = $dataType->editRows; | |
} | |
foreach ($iterateOn as $key => $row) { | |
$details = json_decode($row->details); | |
$iterateOn[$key]['col_width'] = isset($details->width) ? $details->width : 100; | |
} | |
// If a column has a relationship associated with it, we do not want to show that field | |
$this->removeRelationshipField($dataType, $id === NULL ? 'add' : 'edit'); | |
// Check if BREAD is Translatable | |
$isModelTranslatable = is_bread_translatable($dataTypeContent); | |
$view = 'voyager::bread.form'; | |
if (view()->exists("voyager::$slug.form")) { | |
$view = "voyager::$slug.form"; | |
} | |
return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable')); | |
} | |
public function update(Request $request, $id) | |
{ | |
$slug = $this->getSlug($request); | |
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first(); | |
// Compatibility with Model binding. | |
$id = $id instanceof Model ? $id->{$id->getKeyName()} : $id; | |
$data = call_user_func([$dataType->model_name, 'findOrFail'], $id); | |
// Check permission | |
$this->authorize('edit', $data); | |
// Validate fields with ajax | |
$val = $this->validateBread($request->all(), $dataType->editRows); | |
if ($val->fails()) { | |
return response()->json(['errors' => $val->messages()]); | |
} | |
if (!$request->ajax()) { | |
$this->insertUpdateData($request, $slug, $dataType->editRows, $data); | |
event(new BreadDataUpdated($dataType, $data)); | |
foreach ($request->all() as $key => $value) { | |
if (is_array($value)) { | |
$relDataType = Voyager::model('DataType')->where('name', '=', $key)->first(); | |
$relModel = app($relDataType->model_name); | |
try { | |
$relData = $relModel->findOrFail(key($value)); | |
} catch(ModelNotFoundException $ex) { | |
$relData = $relModel->create([ | |
lcfirst($dataType->display_name_singular) . '_id' => $id | |
]); | |
} | |
foreach ($relDataType->editRows as $row) { | |
$row->field = $key . '.' . key($value) . '.' . $row->field; | |
} | |
$this->insertUpdateData($request, $key, $relDataType->editRows, $relData, TRUE); | |
event(new BreadDataUpdated($relDataType, $relData)); | |
} | |
} | |
return redirect() | |
->route("voyager.{$dataType->slug}.index") | |
->with([ | |
'message' => __('voyager.generic.successfully_updated')." {$dataType->display_name_singular}", | |
'alert-type' => 'success', | |
]); | |
} | |
} | |
public function insertUpdateData($request, $slug, $rows, $data, $isRel = FALSE) | |
{ | |
$multi_select = []; | |
/* | |
* Prepare Translations and Transform data | |
*/ | |
$translations = is_bread_translatable($data) | |
? $data->prepareTranslations($request) | |
: []; | |
foreach ($rows as $row) { | |
$options = json_decode($row->details); | |
if ($row->type == 'relationship' && $options->type != 'belongsToMany') { | |
$row->field = @$options->column; | |
} | |
// if the field for this row is absent from the request, continue | |
// checkboxes will be absent when unchecked, thus they are the exception | |
if (!$request->has($row->field) && $row->type !== 'checkbox') { | |
continue; | |
} | |
$content = $this->getContentBasedOnType($request, $slug, $row); | |
if ($isRel) | |
{ | |
$segments = explode('.', $row->field); | |
$row->field = $segments[count($segments) - 1]; | |
} | |
/* | |
* merge ex_images and upload images | |
*/ | |
if ($row->type == 'multiple_images' && !is_null($content)) { | |
if (isset($data->{$row->field})) { | |
$ex_files = json_decode($data->{$row->field}, true); | |
if (!is_null($ex_files)) { | |
$content = json_encode(array_merge($ex_files, json_decode($content))); | |
} | |
} | |
} | |
if (is_null($content)) { | |
// If the image upload is null and it has a current image keep the current image | |
if ($row->type == 'image' && is_null($request->input($row->field)) && isset($data->{$row->field})) { | |
$content = $data->{$row->field}; | |
} | |
// If the multiple_images upload is null and it has a current image keep the current image | |
if ($row->type == 'multiple_images' && is_null($request->input($row->field)) && isset($data->{$row->field})) { | |
$content = $data->{$row->field}; | |
} | |
// If the file upload is null and it has a current file keep the current file | |
if ($row->type == 'file') { | |
$content = $data->{$row->field}; | |
} | |
if ($row->type == 'password') { | |
$content = $data->{$row->field}; | |
} | |
} | |
if ($row->type == 'relationship' && $options->type == 'belongsToMany') { | |
// Only if select_multiple is working with a relationship | |
$multi_select[] = ['model' => $options->model, 'content' => $content, 'table' => $options->pivot_table]; | |
} else { | |
$data->{$row->field} = $content; | |
} | |
} | |
$data->save(); | |
// Save translations | |
if (count($translations) > 0) { | |
$data->saveTranslations($translations); | |
} | |
foreach ($multi_select as $sync_data) { | |
$data->belongsToMany($sync_data['model'], $sync_data['table'])->sync($sync_data['content']); | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment