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

210 lines
6.6 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
2018-09-10 13:09:10 +00:00
use App\Models\Dataset;
use App\Models\Project;
use App\Models\License;
2018-08-29 15:18:15 +00:00
use App\Models\Title;
2018-08-06 12:30:51 +00:00
use App\Http\Requests\DocumentRequest;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
2018-10-11 14:49:08 +00:00
use App\Models\File;
2018-08-06 12:30:51 +00:00
class DatasetController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(Request $request) : View
{
$searchType = $request->input('searchtype');
$builder = Dataset::query();
//$registers = array();
$filter = $request->input('search');
2018-08-29 15:18:15 +00:00
if (null !== ($request->input('state'))) {
$state = $request->input('state');
} else {
$state = "published";
}
2018-08-06 12:30:51 +00:00
$data = $request->all();
if ($searchType == "simple") {
if (strlen($filter) > 0) {
//$builder->where('field1', '=', $criteria1);
$builder->whereHas('titles', function ($query) use ($filter) {
$query->where('value', 'LIKE', '%' . $filter . '%');
});
// ::with(['titles' => function ($query) use($filter) {
// $query->where('value', 'LIKE', '%' . $filter . '%');
// }])
}
}
$builder->where('server_state', $state);
//$perPage = $request->get('perPage', 20);
$documents = $builder
->paginate(8);
return view('settings.document.document', compact('documents'));
}
/**
* 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
*/
public function edit($id) : View
{
$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');
$datum = date('Y-m-d');
$nowYear = substr($datum, 0, 4);
$years = array();
for ($jahr = 1990; $jahr <= $nowYear; $jahr++) {
$years[$jahr] = $jahr;
}
$languages = DB::table('languages')
->where('active', true)
->pluck('part2_t', 'part2_t');
//$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
*/
public function update(DocumentRequest $request, $id) : RedirectResponse
{
2018-08-29 15:18:15 +00:00
$dataset = Dataset::findOrFail($id);
2018-08-06 12:30:51 +00:00
//$input = $request->all();
$input = $request->except('licenses', 'titles');
2018-08-29 15:18:15 +00:00
$dataset->update($input);
// $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) {
$abstract = Title::findOrFail($key);
$abstract->value = $formAbstract['value'];
$abstract->language = $formAbstract['language'];
$abstract->save();
}
}
session()->flash('flash_message', 'You have updated 1 dataset!');
return redirect()->route('settings.document');
}
2018-10-11 14:49:08 +00:00
2018-08-06 12:30:51 +00:00
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) : RedirectResponse
{
// $document = Document::findOrFail($id);
// $document->delete();
// session()->flash('flash_message', 'You have been deleted 1 dataset!');
return redirect()->route('settings.document');
}
}