tethys/app/Http/Controllers/Settings/DatasetController.php

220 lines
7.1 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
namespace App\Http\Controllers\Settings;
2019-02-22 17:07:00 +00:00
use App\Exceptions\GeneralException;
2018-08-06 12:30:51 +00:00
use App\Http\Controllers\Controller;
2019-02-22 17:07:00 +00:00
use App\Http\Requests\DocumentRequest;
2018-09-10 13:09:10 +00:00
use App\Models\Dataset;
2019-02-22 17:07:00 +00:00
use App\Models\Description;
2018-09-10 13:09:10 +00:00
use App\Models\License;
2019-02-22 17:07:00 +00:00
use App\Models\Project;
2018-08-29 15:18:15 +00:00
use App\Models\Title;
2018-08-06 12:30:51 +00:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2019-02-22 17:07:00 +00:00
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
2018-08-06 12:30:51 +00:00
class DatasetController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
2019-02-22 17:07:00 +00:00
public function index(Request $request): View
2018-08-06 12:30:51 +00:00
{
$searchType = $request->input('searchtype');
$builder = Dataset::query();
//$registers = array();
2019-02-12 11:21:35 +00:00
$filter = $request->input('filter');
2019-02-22 17:07:00 +00:00
2018-08-29 15:18:15 +00:00
if (null !== ($request->input('state'))) {
$state = $request->input('state');
} else {
2019-02-22 17:07:00 +00:00
$state = "published";
2018-08-29 15:18:15 +00:00
}
2019-02-22 17:07:00 +00:00
$data = $request->all();
2018-08-06 12:30:51 +00:00
if ($searchType == "simple") {
if (strlen($filter) > 0) {
2019-02-22 17:07:00 +00:00
$closure = function ($query) use ($filter) {
2018-08-06 12:30:51 +00:00
$query->where('value', 'LIKE', '%' . $filter . '%');
2019-02-22 17:07:00 +00:00
};
$builder->whereHas('titles', $closure);
2018-08-06 12:30:51 +00:00
// ::with(['titles' => function ($query) use($filter) {
// $query->where('value', 'LIKE', '%' . $filter . '%');
// }])
}
}
$builder->where('server_state', $state);
2019-02-22 17:07:00 +00:00
2018-08-06 12:30:51 +00:00
//$perPage = $request->get('perPage', 20);
$documents = $builder
2019-02-22 17:07:00 +00:00
->paginate(8);
2019-02-12 11:21:35 +00:00
return view('settings.document.document', compact('documents', 'state', 'filter'));
2018-08-06 12:30:51 +00:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id): View
{
$document = Dataset::findOrFail($id);
$document->load('titles');
$document->load('abstracts');
return view('settings.document.show', compact('document'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
// $categories = Category::lists('category', 'id');
// $shelves = Shelf::lists('shelf', 'id');
// $tanggal = date('Y-m-d');
// $ambilTahun = substr($tanggal, 0, 4);
// for($tahun = 1990; $tahun <= $ambilTahun; $tahun++){
// $years[] = $tahun;
// }
// return view('lms.settings.book.add', compact('categories', 'shelves', 'years'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(DocumentRequest $request)
{
// $input = $request->all();
// $book = Book::create($input);
// session()->flash('flash_message', 'You have been addded 1 dataset!');
// return redirect()->route('settings.book');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-02-22 17:07:00 +00:00
public function edit($id): View
2018-08-06 12:30:51 +00:00
{
$document = Dataset::findOrFail($id);
2018-10-11 14:49:08 +00:00
$document->load('licenses', 'titles', 'abstracts', 'files');
2018-08-06 12:30:51 +00:00
$projects = Project::pluck('label', 'id');
2019-02-22 17:07:00 +00:00
2018-08-06 12:30:51 +00:00
$datum = date('Y-m-d');
$nowYear = substr($datum, 0, 4);
$years = array();
for ($jahr = 1990; $jahr <= $nowYear; $jahr++) {
$years[$jahr] = $jahr;
}
2019-02-22 17:07:00 +00:00
2018-08-06 12:30:51 +00:00
$languages = DB::table('languages')
2019-01-24 15:56:39 +00:00
->where('active', true)
->pluck('part1', 'part1');
2018-08-06 12:30:51 +00:00
//$options = License::all();
$options = License::all('id', 'name_long');
$checkeds = $document->licenses->pluck('id')->toArray();
//$shelves = Shelf::pluck('shelf', 'id');
// $datum = date('Y-m-d');
// $nowYear = substr($datum, 0, 4);
// $years = array();
// for($jahr = 1990; $jahr <= $nowYear; $jahr++){
// $years[$jahr] = $jahr;
// }
2018-08-29 15:18:15 +00:00
return view(
'settings.document.edit',
compact('document', 'projects', 'options', 'checkeds', 'years', 'languages')
);
2018-08-06 12:30:51 +00:00
}
//https://stackoverflow.com/questions/17480200/using-laravel-how-do-i-create-a-view-that-will-update-a-one-to-many-relationshi?rq=1
// https://laravel.io/forum/06-11-2014-how-to-save-eloquent-model-with-relations-in-one-go
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-02-22 17:07:00 +00:00
public function update(DocumentRequest $request, $id): RedirectResponse
2018-08-06 12:30:51 +00:00
{
2018-08-29 15:18:15 +00:00
$dataset = Dataset::findOrFail($id);
2018-08-06 12:30:51 +00:00
//$input = $request->all();
2019-02-14 14:09:11 +00:00
$input = $request->except('abstracts', 'licenses', 'titles', '_method', '_token');
// foreach ($input as $key => $value) {
// $dataset[$key] = $value;
// }
//$dataset->update($input);
2018-08-29 15:18:15 +00:00
// $dataset->type = $input['type'];
// $dataset->thesis_year_accepted = $input['thesis_year_accepted'];
// $dataset->project_id = $input['project_id'];
// $dataset->save();
2018-08-06 12:30:51 +00:00
$licenses = $request->input('licenses');
//$licenses = $input['licenses'];
2018-08-29 15:18:15 +00:00
$dataset->licenses()->sync($licenses);
2018-08-06 12:30:51 +00:00
//save the titles:
$titles = $request->input('titles');
if (is_array($titles) && count($titles) > 0) {
foreach ($titles as $key => $formTitle) {
$title = Title::findOrFail($key);
$title->value = $formTitle['value'];
$title->language = $formTitle['language'];
$title->save();
}
}
//save the abstracts:
$abstracts = $request->input('abstracts');
if (is_array($abstracts) && count($abstracts) > 0) {
foreach ($abstracts as $key => $formAbstract) {
2019-02-12 11:21:35 +00:00
$abstract = Description::findOrFail($key);
2018-08-06 12:30:51 +00:00
$abstract->value = $formAbstract['value'];
$abstract->language = $formAbstract['language'];
$abstract->save();
}
}
2019-02-22 17:07:00 +00:00
if (!$dataset->isDirty(dataset::UPDATED_AT)) {
2019-02-14 14:09:11 +00:00
$time = new \Illuminate\Support\Carbon();
$dataset->setUpdatedAt($time);
}
// $dataset->save();
if ($dataset->update($input)) {
//event(new DatasetUpdated($dataset));
session()->flash('flash_message', 'You have updated 1 dataset!');
return redirect()->route('settings.document');
}
throw new GeneralException(trans('exceptions.backend.dataset.update_error'));
2018-08-06 12:30:51 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-02-22 17:07:00 +00:00
public function destroy($id): RedirectResponse
2018-08-06 12:30:51 +00:00
{
// $document = Document::findOrFail($id);
// $document->delete();
// session()->flash('flash_message', 'You have been deleted 1 dataset!');
return redirect()->route('settings.document');
}
}