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

780 lines
31 KiB
PHP
Raw Permalink 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;
2018-08-29 15:18:15 +00:00
use App\Http\Controllers\Controller;
use App\Models\Coverage;
use App\Models\Dataset;
use App\Models\DatasetReference;
use App\Models\Description;
2018-08-29 15:18:15 +00:00
use App\Models\File;
use App\Models\License;
use App\Models\Page;
use App\Models\Person;
2018-09-10 13:09:10 +00:00
use App\Models\Project;
use App\Models\Subject;
2018-08-29 15:18:15 +00:00
use App\Models\Title;
use App\Rules\RdrFilesize;
use App\Rules\RdrFiletypes;
2018-08-29 15:18:15 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
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\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
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()
{
2018-10-18 14:51:46 +00:00
$builder = Dataset::query();
$datasets = $builder
2018-10-19 10:54:40 +00:00
//->where('server_state', 'inprogress')
->whereIn('server_state', ['inprogress', 'unpublished'])
->get();
2018-10-18 14:51:46 +00:00
return view('publish.index', compact('datasets'));
2018-08-06 12:30:51 +00:00
}
/**
* 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::select('id', 'name_long', 'link_licence')
2018-11-08 16:47:27 +00:00
->orderBy('sort_order')
->get();
2018-08-29 15:18:15 +00:00
$languages = DB::table('languages')
->where('active', true)
2019-01-22 17:24:18 +00:00
->pluck('part1', 'part1');
2018-08-29 15:18:15 +00:00
// ->toArray();
$messages = DB::table('messages')
->pluck('help_text', 'metadata_element');
2020-01-23 16:52:26 +00:00
$projects = Project::pluck('label', 'id');
// $relatedIdentifierTypes = ["doi", "handle", "isbn", "issn", "url", "urn"];
$relatedIdentifierTypes = ["DOI", "Handle", "ISBN", "ISSN", "URL", "URN"];
2019-01-22 17:24:18 +00:00
$relatedIdentifierTypes = array_combine($relatedIdentifierTypes, $relatedIdentifierTypes);
$relationTypes = ["IsSupplementTo", "IsSupplementedBy", "IsContinuedBy", "Continues",
"IsNewVersionOf", "IsPartOf", "HasPart", "Compiles", "IsVariantFormOf"];
2019-01-22 17:24:18 +00:00
$relationTypes = array_combine($relationTypes, $relationTypes);
2018-08-29 15:18:15 +00:00
$titleTypes = ['Sub' => 'Sub', 'Alternative' => 'Alternative', 'Translated' => 'Translated', 'Other' => 'Other'];
2019-01-24 16:33:04 +00:00
2019-03-18 13:32:29 +00:00
$keywordTypes = ['uncontrolled' => 'uncontrolled'];
$descriptionTypes = ['Methods' => 'Methods', 'Series_information' => 'Series_information',
'Technical_info' => 'Technical_info', 'Translated' => 'Translated', 'Other' => 'Other'];
2019-01-24 16:33:04 +00:00
2019-01-30 16:46:12 +00:00
$page = Page::query()->where('page_slug', 'terms-and-conditions')->firstOrFail();
$nameTypes = Config::get('enums.name_types');
$contributorTypes = Config::get('enums.contributor_types');
2019-01-22 17:24:18 +00:00
//$relationTypes = array('updates' => 'updates', 'updated-by' => 'updated-by', 'other' => 'other');
2019-03-18 13:32:29 +00:00
return view(
'publish.create-step1',
compact(
'licenses',
'languages',
'messages',
'projects',
'relatedIdentifierTypes',
'relationTypes',
'titleTypes',
'keywordTypes',
'descriptionTypes',
'nameTypes',
'contributorTypes',
'page'
)
2019-03-18 13:32:29 +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
*/
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->fails()) {
2018-08-29 15:18:15 +00:00
//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 = [
2019-01-24 15:56:39 +00:00
// 'server_state' => 'required',
'type' => 'required|min:3',
2018-08-29 15:18:15 +00:00
'rights' => 'required|boolean|in:1',
'belongs_to_bibliography' => 'required|boolean',
2020-01-23 16:52:26 +00:00
'title_main.value' => 'required|min:4|max:255',
2018-08-29 15:18:15 +00:00
'title_main.language' => 'required',
2020-01-23 16:52:26 +00:00
'titles.*.value' => 'required|min:4|max:255',
'abstract_main.value' => 'required|min:4|max:2500',
2018-08-31 14:47:04 +00:00
'abstract_main.language' => 'required',
2020-01-23 16:52:26 +00:00
'descriptions.*.value' => 'required|min:4|max:2500',
'coverage.x_min' => [
2020-01-23 16:52:26 +00:00
'required',
'regex:/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/',
],
'coverage.y_min' => [
2020-01-23 16:52:26 +00:00
'required',
'regex:/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/',
],
'coverage.x_max' => [
2020-01-23 16:52:26 +00:00
'required',
'regex:/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/',
],
'coverage.y_max' => [
2020-01-23 16:52:26 +00:00
'required',
'regex:/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/',
],
'keywords' => 'required|array|min:3',
2020-01-23 16:52:26 +00:00
'keywords.*.value' => 'required|string',
'keywords.*.type' => 'required|string',
'files' => 'required|array|min:1',
2020-01-23 16:52:26 +00:00
'files.*.label' => 'required|string',
// 'authors.*.email' => 'required|email|max:50|unique:persons,email',
2020-01-23 16:52:26 +00:00
];
$customMessages = [
'keywords.required' => 'Minimal three keywords are required.',
'keywords.*.type.required' => 'The types of all keywords are required.',
'files.min' => 'Minimal one file is required.',
'authors.*.email.unique' => 'email of new author is not unique in database.',
2018-08-29 15:18:15 +00:00
];
if (isset($data['authors'])) {
foreach ($request->get('authors') as $index => $person) {
// echo "Item at index {$index} has sm_id value {$item->sm_id}";
if ($person['status'] == false) {
$rules['authors.' . $index . '.email'] = ['email', 'required', 'unique:persons,email'];
}
}
}
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';
2019-07-04 16:46:03 +00:00
$rules['files.' . $index . '.file'] = [new RdrFilesize($index + 1), 'file', 'required', new RdrFiletypes()];
2018-08-29 15:18:15 +00:00
}
}
2020-01-23 16:52:26 +00:00
$validator = Validator::make($request->all(), $rules, $customMessages);
if (!$validator->fails()) {
2018-08-29 15:18:15 +00:00
//store dataset todo
//$data = $request->all();
2019-01-24 15:56:39 +00:00
$input = $request->except('files', 'licenses', 'abstract_main', 'title_main', 'references', 'titles');
2018-08-29 15:18:15 +00:00
// array_push($input, "Himbeere");
2019-01-24 15:56:39 +00:00
// $input += ['server_state' => 'created' ];
if (isset($data['server_state'])) {
$input['server_state'] = $data['server_state'];
} else {
$input['server_state'] = 'inprogress';
}
2018-08-29 15:18:15 +00:00
$dataset = new Dataset($input);
DB::beginTransaction(); //Start transaction!
try {
$dataset->save();
2018-08-29 15:18:15 +00:00
//store related files
2018-10-10 16:28:51 +00:00
if (isset($data['files'])) {
2018-08-29 15:18:15 +00:00
foreach ($data['files'] as $uploadedFile) {
$file = $uploadedFile['file'];
$label = urldecode($uploadedFile['label']);
$sorting = $uploadedFile['sorting'];
// $fileName = "file-" . time() . '.' . $file->getClientOriginalExtension();
$fileName = "file-" . Str::uuid()->toString() . '.' . $file->getClientOriginalExtension();
2018-08-29 15:18:15 +00:00
$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,
2018-08-29 15:18:15 +00:00
]);
//$test = $file->path_name;
$dataset->files()->save($file);
$file->createHashValues();
}
}
//store licenses:
$licenses = $request->input('licenses');
$dataset->licenses()->sync($licenses);
2018-08-29 15:18:15 +00:00
$data_to_sync = [];
2018-10-19 10:54:40 +00:00
//store authors
2018-10-29 13:24:41 +00:00
if (isset($data['authors'])) {
2019-03-19 17:04:41 +00:00
//$data_to_sync = [];
foreach ($request->get('authors') as $key => $person) {
2018-10-29 13:24:41 +00:00
$pivot_data = ['role' => 'author', 'sort_order' => $key + 1];
// if ($galery_id == $request->get('mainPicture')) $pivot_data = ['main' => 1];
if (isset($person['id'])) {
//$data_to_sync[$person['id']] = $pivot_data;
$dataset->persons()->attach($person['id'], $pivot_data);
} else {
$dataPerson = new Person($person);
$dataPerson->status = true;
// $dataPerson->name_type = "Personal";
$dataset->persons()->save($dataPerson, $pivot_data);
}
2018-10-29 13:24:41 +00:00
}
2019-03-19 17:04:41 +00:00
//$dataset->persons()->sync($data_to_sync);
2018-10-18 14:51:46 +00:00
}
2018-10-19 10:54:40 +00:00
//store contributors
// if (isset($data['contributors'])) {
// //$data_to_sync = [];
// foreach ($request->get('contributors') as $key => $contributor_id) {
// $pivot_data = ['role' => 'contributor', 'sort_order' => $key + 1];
// //$data_to_sync[$contributor_id] = $pivot_data;
// $dataset->persons()->attach($contributor_id, $pivot_data);
// }
// //$dataset->persons()->sync($data_to_sync);
// }
2018-10-29 13:24:41 +00:00
if (isset($data['contributors'])) {
2019-03-19 17:04:41 +00:00
//$data_to_sync = [];
foreach ($request->get('contributors') as $key => $contributor) {
$pivot_data = [
'role' => 'contributor',
'sort_order' => $key + 1
];
$pivot_data = array_merge($pivot_data, $contributor['pivot']);
if (isset($contributor['id'])) {
//$data_to_sync[$person['id']] = $pivot_data;
$dataset->persons()->attach($contributor['id'], $pivot_data);
} else {
$dataContributor = new Person($contributor);
$dataContributor->status = true;
// $dataContributor->name_type = "Organizational";
$dataset->persons()->save($dataContributor, $pivot_data);
}
2018-10-29 13:24:41 +00:00
}
2018-10-19 10:54:40 +00:00
}
2018-10-29 13:24:41 +00:00
//store submitters
// if (isset($data['submitters'])) {
// //$data_to_sync = [];
// foreach ($request->get('submitters') as $key => $submitter_id) {
// $pivot_data = ['role' => 'submitter', 'sort_order' => $key + 1];
// $data_to_sync[$submitter_id] = $pivot_data;
// }
// //$dataset->persons()->sync($data_to_sync);
// }
//$dataset->persons()->sync($data_to_sync);
2018-10-29 13:24:41 +00:00
2018-08-29 15:18:15 +00:00
//save main title:
if (isset($data['title_main'])) {
$formTitle = $request->input('title_main');
$title = new Title();
$title->value = $formTitle['value'];
$title->language = $formTitle['language'];
2019-09-16 12:07:10 +00:00
$title->type = 'Main';
//$dataset->addMainTitle($title);
$dataset->titles()->save($title);
2018-08-29 15:18:15 +00:00
}
//save additional titles
2019-01-24 15:56:39 +00:00
if (isset($data['titles'])) {
foreach ($request->get('titles') as $key => $title) {
$titleReference = new Title($title);
$dataset->titles()->save($titleReference);
}
}
2018-08-29 15:18:15 +00:00
//save main abstract:
if (isset($data['abstract_main'])) {
$formAbstract = $request->input('abstract_main');
2019-01-24 15:56:39 +00:00
$abstract = new Description();
2018-08-29 15:18:15 +00:00
$abstract->value = $formAbstract['value'];
$abstract->language = $formAbstract['language'];
2019-09-16 12:07:10 +00:00
$abstract->type = 'Abstract';
//$dataset->addMainAbstract($abstract);
$dataset->abstracts()->save($abstract);
2018-08-29 15:18:15 +00:00
}
2018-10-29 13:24:41 +00:00
2019-01-24 16:33:04 +00:00
//save additional descriptions
if (isset($data['descriptions'])) {
foreach ($request->get('descriptions') as $key => $description) {
$descriptionReference = new Description($description);
$dataset->abstracts()->save($descriptionReference);
}
}
2018-10-29 13:24:41 +00:00
//save references
if (isset($data['references'])) {
foreach ($request->get('references') as $key => $reference) {
$dataReference = new DatasetReference($reference);
$dataset->references()->save($dataReference);
}
}
2019-03-18 13:32:29 +00:00
//save keywords
if (isset($data['keywords'])) {
foreach ($request->get('keywords') as $key => $keyword) {
// $dataKeyword = new Subject($keyword);
// $dataset->subjects()->save($dataKeyword);
$keyword = Subject::firstOrCreate(
['value' => $keyword['value'], 'language' => $keyword['language']],
$keyword
);
$dataset->subjects()->attach($keyword);
2019-03-18 13:32:29 +00:00
}
}
2019-03-29 17:29:20 +00:00
// if (isset($data['geolocation'])) {
// $formGeolocation = $request->input('geolocation');
// if ($formGeolocation['xmin'] !== null && $formGeolocation['ymin'] !== null &&
// $formGeolocation['xmax'] !== null && $formGeolocation['ymax'] !== null) {
// $geolocation = new GeolocationBox($formGeolocation);
// $dataset->geolocation()->save($geolocation);
// //$geolocation->dataset()->associate($dataset)->save();
// }
// }
2019-03-20 17:40:14 +00:00
if (isset($data['coverage'])) {
$formCoverage = (array) $request->input('coverage');
2019-03-20 17:40:14 +00:00
$coverage = new Coverage($formCoverage);
$dataset->coverage()->save($coverage);
//$coverage->dataset()->associate($dataset)->save();
}
// Create relation between Dataset and actual User.
$user = Auth::user();
$dataset->user()->associate($user)->save();
// $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();
2018-10-10 16:28:51 +00:00
if (isset($datasetFolder)) {
Storage::deleteDirectory($datasetFolder);
}
2018-08-29 15:18:15 +00:00
return response()->json([
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
], 422);
//throw $e;
} catch (\Throwable $e) {
DB::rollback();
2018-10-10 16:28:51 +00:00
if (isset($datasetFolder)) {
Storage::deleteDirectory($datasetFolder);
}
2018-08-29 15:18:15 +00:00
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,
'edit' => route('publish.workflow.submit.edit', ['id' => $dataset->id]),
'release' => route('publish.workflow.submit.release', ['id' => $dataset->id]),
'delete' => route('publish.workflow.submit.delete', ['id' => $dataset->id]),
2018-11-05 15:44:25 +00:00
));
} else {
//TODO Handle validation error
//pass validator errors as errors object for ajax response
2019-07-04 16:46:03 +00:00
$errors = $validator->errors();
2018-11-05 15:44:25 +00:00
return response()->json([
'success' => false,
2020-01-23 16:52:26 +00:00
'errors' => $errors,
2018-11-05 15:44:25 +00:00
], 422);
}
}
public function storeTest1(Request $request)
{
$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',
'abstract_main.value' => 'required|min:5',
'abstract_main.language' => 'required',
];
if (null != $request->file('files')) {
$files = count($request->file('files')) - 1;
2018-11-05 15:44:25 +00:00
foreach (range(0, $files) as $index) {
// $rules['files.' . $index] = 'image|max:2048';
2019-02-22 17:07:00 +00:00
$rules['files.' . $index . '.file'] = ['required', 'file', new RdrFiletypes(), new RdrFilesize($index)];
2018-11-05 15:44:25 +00:00
}
}
$validator = Validator::make($request->all(), $rules);
if (!$validator->fails()) {
2018-11-05 15:44:25 +00:00
//store dataset todo
//$data = $request->all();
$input = $request->except('files', 'licenses', 'abstract_main', 'title_main', 'references');
// array_push($input, "Himbeere");
$dataset = new Dataset($input);
DB::beginTransaction(); //Start transaction!
try {
// $dataset->save();
2018-11-05 15:44:25 +00:00
//store related files
if (isset($data['files'])) {
foreach ($data['files'] as $uploadedFile) {
$file = $uploadedFile['file'];
$label = urldecode($uploadedFile['label']);
$sorting = $uploadedFile['sorting'];
$fileName = "file-" . 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');
2018-11-05 15:44:25 +00:00
// $dataset->licenses()->sync($licenses);
//store authors
if (isset($data['authors'])) {
$data_to_sync = [];
foreach ($request->get('authors') as $key => $person_id) {
$pivot_data = ['role' => 'author', 'sort_order' => $key + 1];
// if ($galery_id == $request->get('mainPicture')) $pivot_data = ['main' => 1];
$data_to_sync[$person_id] = $pivot_data;
}
// $dataset->persons()->sync($data_to_sync);
}
//store contributors
if (isset($data['contributors'])) {
$data_to_sync = [];
foreach ($request->get('contributors') as $key => $contributor_id) {
$pivot_data = ['role' => 'contributor', 'sort_order' => $key + 1];
$data_to_sync[$contributor_id] = $pivot_data;
}
// $dataset->persons()->sync($data_to_sync);
}
2018-11-05 15:44:25 +00:00
//store submitters
if (isset($data['submitters'])) {
$data_to_sync = [];
foreach ($request->get('submitters') as $key => $submitter_id) {
$pivot_data = ['role' => 'submitter', 'sort_order' => $key + 1];
$data_to_sync[$submitter_id] = $pivot_data;
}
// $dataset->persons()->sync($data_to_sync);
}
//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);
}
//save references
if (isset($data['references'])) {
foreach ($request->get('references') as $key => $reference) {
$dataReference = new DatasetReference($reference);
// $dataset->references()->save($dataReference);
}
}
2018-11-05 15:44:25 +00:00
// $error = 'Always throw this error';
// throw new \Exception($error);
// all good//commit everything
// DB::commit();
} catch (\Exception $e) {
DB::rollback();
if (isset($datasetFolder)) {
Storage::deleteDirectory($datasetFolder);
}
return response()->json([
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
], 422);
//throw $e;
} catch (\Throwable $e) {
DB::rollback();
if (isset($datasetFolder)) {
Storage::deleteDirectory($datasetFolder);
}
return response()->json([
'success' => false,
'error' => [
'code' => $e->getCode(),
'message' => $e->getMessage(),
],
], 422);
//throw $e;
}
return response()->json(array(
'success' => true,
'redirect' => route('settings.document.edit', ['id' => $dataset->server_state]),
2018-08-29 15:18:15 +00:00
));
} 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)
{
//
}
}