tethys/app/Http/Controllers/Publish/IndexController.php

383 lines
13 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
//https://www.5balloons.info/multi-page-step-form-in-laravel-with-validation/
namespace App\Http\Controllers\Publish;
use App\Dataset;
2018-08-29 15:18:15 +00:00
use App\Http\Controllers\Controller;
use App\License;
use App\Models\File;
use App\Person;
2018-08-31 14:47:04 +00:00
use App\Project;
2018-08-29 15:18:15 +00:00
use App\Models\Title;
use App\Rules\RdrFiletypes;
use App\Rules\RdrFilesize;
use Illuminate\Http\Request;
2018-08-06 12:30:51 +00:00
use Illuminate\Support\Facades\DB;
2018-08-29 15:18:15 +00:00
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
2018-08-06 12:30:51 +00:00
class IndexController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function createStep1(Request $request)
{
2018-08-29 15:18:15 +00:00
#$dataset = $request->session()->get('dataset');
$licenses = License::all('id', 'name_long');
$languages = DB::table('languages')
->where('active', true)
->pluck('part2_t', 'part2_t');
// ->toArray();
$persons = Person::where('status', 1)
->pluck('last_name', 'id');
2018-08-31 14:47:04 +00:00
$projects = Project::pluck('label', 'id');
2018-08-29 15:18:15 +00:00
2018-08-31 14:47:04 +00:00
return view('publish.create-step1', compact('licenses', 'languages', 'persons', 'projects'));
2018-08-06 12:30:51 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function storeStep1(Request $request)
{
$validatedData = $this->validate($request, [
'Type' => 'required|min:4',
2018-08-29 15:18:15 +00:00
'rights' => 'required|boolean|in:1',
2018-08-06 12:30:51 +00:00
]);
// $validatedData = $request->validate([
// 'name' => 'required|unique:products',
// 'amount' => 'required|numeric',
// 'company' => 'required',
// 'available' => 'required',
// 'description' => 'required',
// ]);
if (empty($request->session()->get('dataset'))) {
// $dataset = new Dataset();
//$dataset->fill($validatedData);
// $dataset->type = $request->input('type');
$dataset = $request->except('rights', '_token');
$request->session()->put('dataset', $dataset);
} else {
$dataset = $request->session()->get('dataset');
//$dataset->fill($validatedData);
$dataset['Type'] = $request->input('Type');
$request->session()->put('dataset', $dataset);
}
return redirect()->route('dataset.create2');
}
/**
* Show the step 2 Form for creating a new dataset.
*
* @return \Illuminate\Http\Response
*/
public function createStep2(Request $request)
{
//if no dataset is'nt in session variable return to step1
if (empty($request->session()->get('dataset'))) {
return redirect()->route('dataset.create1');
}
$dataset = $request->session()->get('dataset');
//fill select variable
$languages = DB::table('languages')
->where('active', true)
->pluck('part2_t', 'part2_t');
return view('publish.create-step2', compact('dataset', 'languages'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function storeStep2(Request $request)
{
$validatedData = $this->validate($request, [
'Type' => 'required|min:4',
'BelongsToBibliography' => 'required|boolean',
"TitleMain.Value" => 'required|min:5|max:255',
"TitleMain.Language" => 'required|min:3',
"TitleAbstract.Value" => 'required|min:5|max:255',
2018-08-29 15:18:15 +00:00
"TitleAbstract.Language" => 'required|min:3',
2018-08-06 12:30:51 +00:00
]);
$optionalData = $request->all();
2018-08-29 15:18:15 +00:00
2018-08-06 12:30:51 +00:00
// $dataset = $request->except('rights', '_token', 'input_img');
$dataset = $request->session()->get('dataset');
2018-08-29 15:18:15 +00:00
2018-08-06 12:30:51 +00:00
//update dataset with validated data
2018-08-29 15:18:15 +00:00
$dataset['Type'] = $validatedData['Type'];
$dataset['BelongsToBibliography'] = $validatedData['BelongsToBibliography'];
$dataset['TitleMain']['Value'] = $validatedData['TitleMain']['Value'];
$dataset['TitleMain']['Language'] = $validatedData['TitleMain']['Language'];
$dataset['TitleAbstract']['Value'] = $validatedData['TitleAbstract']['Value'];
$dataset['TitleAbstract']['Language'] = $validatedData['TitleAbstract']['Language'];
2018-08-06 12:30:51 +00:00
if (isset($optionalData['CreatingCorporation'])) {
$dataset['CreatingCorporation'] = $optionalData['CreatingCorporation'];
}
if (isset($optionalData['EmbargoDate'])) {
$dataset['EmbargoDate'] = $optionalData['EmbargoDate'];
}
if (!isset($dataset['DatasetFile'])) {
$this->validate($request, [
2018-08-29 15:18:15 +00:00
'dataset_file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
2018-08-06 12:30:51 +00:00
//update session variable
// $dataset = $request->session()->get('dataset');
$image = $request->file('dataset_file');
$fileName = "productImage-" . time() . '.' . $image->getClientOriginalExtension();
$path = $image->storeAs(
'files',
$fileName
);
// $path = Storage::putFile('files', $image, $fileName);
//$dataset = $request->session()->get('dataset');
$dataset['DatasetFile'] = $fileName;
}
$request->session()->put('dataset', $dataset);
return redirect()->route('dataset.create3');
}
/**
* Show the Product Review page
*
* @return \Illuminate\Http\Response
*/
public function createStep3(Request $request)
{
2018-08-29 15:18:15 +00:00
//if no dataset is'nt in session variable return to step1
2018-08-06 12:30:51 +00:00
if (empty($request->session()->get('dataset'))) {
return redirect()->route('dataset.create1');
}
$dataset = $request->session()->get('dataset');
return view('publish.create-step3', compact('dataset'));
}
2018-08-29 15:18:15 +00:00
/**
2018-08-06 12:30:51 +00:00
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2018-08-29 15:18:15 +00:00
public function storeTest(Request $request)
{
//$data = $request->all();
$data = json_decode($request->getContent(), true);
$validator = Validator::make($data, [
'type' => 'required|min:4',
'belongs_to_bibliography' => 'required|boolean',
]);
if ($validator->passes()) {
//TODO Handle your data
return response()->json(array(
'response' => 'success'));
} else {
//TODO Handle your error
//pass validator errors as errors object for ajax response
return response()->json(['errors' => $validator->errors()->all()], 422);
}
}
//https://laravel.io/forum/06-11-2014-how-to-save-eloquent-model-with-relations-in-one-go
//attach vs save https://stackoverflow.com/questions/35756469/laravel-5-many-to-many-attach-versus-save
2018-08-06 12:30:51 +00:00
public function store(Request $request)
{
2018-08-29 15:18:15 +00:00
$data = $request->all();
// $validatedData = $this->validate($request, [
// 'type' => 'required|min:4',
// 'rights' => 'required|boolean|in:1',
// ]);
$rules = [
'server_state' => 'required',
'type' => 'required|min:5',
'rights' => 'required|boolean|in:1',
'belongs_to_bibliography' => 'required|boolean',
'title_main.value' => 'required|min:5',
'title_main.language' => 'required',
2018-08-31 14:47:04 +00:00
'abstract_main.value' => 'required|min:5',
'abstract_main.language' => 'required',
2018-08-29 15:18:15 +00:00
];
if (null != $request->file('files')) {
$files = count($request->file('files')) - 1;
foreach (range(0, $files) as $index) {
// $rules['files.' . $index] = 'image|max:2048';
$rules['files.' . $index . '.file'] = ['required', 'file', new RdrFiletypes(), new RdrFilesize()];
}
}
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
//store dataset todo
//$data = $request->all();
$input = $request->except('files', 'licenses', 'abstract_main', 'title_main');
// array_push($input, "Himbeere");
$dataset = new Dataset($input);
DB::beginTransaction(); //Start transaction!
try {
$dataset->save();
//store related files
if (null != $data['files']) {
foreach ($data['files'] as $uploadedFile) {
$file = $uploadedFile['file'];
$label = urldecode($uploadedFile['label']);
$sorting = $uploadedFile['sorting'];
$fileName = "productImage-" . time() . '.' . $file->getClientOriginalExtension();
$mimeType = $file->getMimeType();
$datasetFolder = 'files/' . $dataset->id;
$path = $file->storeAs($datasetFolder, $fileName);
$size = Storage::size($path);
//$path = Storage::putFile('files', $image, $fileName);
$file = new File([
'path_name' => $path,
'file_size' => $size,
'mime_type' => $mimeType,
'label' => $label,
'sort_order' => $sorting,
'visible_in_frontdoor' => 1,
'visible_in_oai' => 1
]);
//$test = $file->path_name;
$dataset->files()->save($file);
$file->createHashValues();
}
}
//store licenses:
$licenses = $request->input('licenses');
$dataset->licenses()->sync($licenses);
//save main title:
if (isset($data['title_main'])) {
$formTitle = $request->input('title_main');
$title = new Title();
$title->value = $formTitle['value'];
$title->language = $formTitle['language'];
$dataset->addMainTitle($title);
}
//save main abstract:
if (isset($data['abstract_main'])) {
$formAbstract = $request->input('abstract_main');
$abstract = new Title();
$abstract->value = $formAbstract['value'];
$abstract->language = $formAbstract['language'];
$dataset->addMainAbstract($abstract);
}
2018-08-31 14:47:04 +00:00
$error = 'Always throw this error';
throw new \Exception($error);
2018-08-29 15:18:15 +00:00
// all good//commit everything
DB::commit();
} catch (\Exception $e) {
DB::rollback();
Storage::deleteDirectory($datasetFolder);
return response()->json([
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
], 422);
//throw $e;
} catch (\Throwable $e) {
DB::rollback();
Storage::deleteDirectory($datasetFolder);
return response()->json([
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
], 422);
//throw $e;
}
return response()->json(array(
2018-08-31 14:47:04 +00:00
'success' => true,
2018-08-29 15:18:15 +00:00
'redirect' => route('settings.document', ['state' => $dataset->server_state]),
));
} else {
//TODO Handle validation error
//pass validator errors as errors object for ajax response
return response()->json([
'success' => false,
'errors' => $validator->errors()->all(),
], 422);
}
2018-08-06 12:30:51 +00:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}