Skip to content

Commit

Permalink
IP-188: quick 'n' dirty problem creation form
Browse files Browse the repository at this point in the history
  • Loading branch information
papandrk committed Oct 24, 2024
1 parent 87c106f commit 09f607e
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use App\BusinessLogicLayer\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemManager;
use App\Http\Controllers\Controller;
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblem;
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemTranslation;
use App\ViewModels\CrowdSourcingProject\Problem\CreateEditProblem;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -33,4 +36,79 @@ public function showProblemsPage(Request $request) {
abort(ResponseAlias::HTTP_NOT_FOUND);
}
}

/**
* Display a listing of the resource.
*/
public function index()
{
$viewModel = [];
$viewModel['problems'] = CrowdSourcingProjectProblem::with('translations')->latest()->get();
return view('loggedin-environment.management.problem.index', ['viewModel' => $viewModel]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
$newProblem = new CrowdSourcingProjectProblem();
$newProblem->default_language_id = 6; // @todo change with lookuptable value - bookmark2
$newProblem->setRelation('defaultTranslation', new CrowdSourcingProjectProblemTranslation()); // bookmark2 - is this an "empty" relationship?
$viewModel = new CreateEditProblem($newProblem);
return view('loggedin-environment.management.problem.create-edit.form-page');
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
request()->validate([ // bookmark2
'problem-title' => ['required'],
'problem-description' => ['required'],
'problem-status' => ['required'], // bookmark2
'problem-default-language' => ['required'], // bookmark2
]);

$crowdSourcingProjectProblem = CrowdSourcingProjectProblem::create([
'project_id' => '3', // bookmark2
'user_creator_id' => '2', // bookmark2
'slug' => 'test-slug-1', // bookmark2
'status_id' => request('problem-status'), // bookmark2
'img_url' => 'zxcv', // bookmark2
'default_language_id' => request('problem-default-language'), // bookmark2 - default or generally another translation language?
]);

$crowdSourcingProjectProblemTranslation = $crowdSourcingProjectProblem->defaultTranslation()->create([ // bookmark2 - default or regular translation?
'title' => request('problem-title'),
'description' => request('problem-description'),
]);

return redirect()->route('problems.index');
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CrowdSourcingProjectProblem extends Model {
use SoftDeletes;

protected $table = 'crowd_sourcing_project_problems';
protected $fillable = ['id', 'project_id', 'slug', 'status_id', 'img_url', 'default_language_id'];
protected $fillable = ['id', 'project_id', 'user_creator_id', 'slug', 'status_id', 'img_url', 'default_language_id'];

public function defaultTranslation(): HasOne {
return $this->hasOne(CrowdSourcingProjectProblemTranslation::class,
Expand Down
13 changes: 13 additions & 0 deletions app/ViewModels/CrowdSourcingProject/Problem/CreateEditProblem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\ViewModels\CrowdSourcingProject\Problem;

use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblem;

class CreateEditProblem {
public CrowdSourcingProjectProblem $crowdSourcingProjectProblem;
public function __construct(CrowdSourcingProjectProblem $crowdSourcingProjectProblem) {
$this->crowdSourcingProjectProblem = $crowdSourcingProjectProblem;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="status_id">Project Default Language</label>
<label for="default_lang_id">Project Default Language</label>
<select id="default_lang_id" class="form-control" name="language_id">
@foreach ($viewModel->languagesLkp as $language)
<option
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
Problems
<pre>
0 => "id"
1 => "project_id"
2 => "slug"
3 => "status_id"
4 => "img_url"
5 => "default_language_id"

?? => "user_creator_id"

defaultTranslation(): HasOne
translations(): HasMany
</pre>

ProblemTranslations
<pre>
0 => "problem_id"
1 => "language_id"
2 => "title"
3 => "description"

problem(): BelongsTo
language(): BelongsTo
</pre>

{{--
<form id="problem-form" enctype="multipart/form-data" method="POST"
action="{{ $viewModel->isEditMode() ? route('problems.update', $viewModel->problem) : route('problems.store') }}">
--}}

<form id="problem-form" enctype="multipart/form-data"{{-- bookmark2 - enctype? --}} method="POST"
action="{{ route('problems.store') }}">

{{--
@if($viewModel->isEditMode())
@method('PUT')
@endif
--}}

@csrf

<div class="container">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">

<div class="row">
<label class="col-sm-12 control-label" for="problem-title">Problem Title (<span class="red">*</span>)</label>
<div class="col-sm-12">
<div class="form-group has-feedback {{ $errors->has('problem-title') ? 'has-error' : '' }}">
<input type="text"
id="problem-title"
name="problem-title"
class="form-control"
required
placeholder="Problem Title"
{{-- value="{{ old('problem-title') ? old('problem-title') : $viewModel->problem->defaultTranslation->title }}" bookmark2 --}}
value="{{ old('problem-title') ? old('problem-title') : '' }}"
>
<span class="help-block"><strong>{{ $errors->first('problem-title') }}</strong></span>
</div>
</div>
</div>

<div class="row">
<label class="col-sm-12 control-label" for="problem-description">Problem Description (<span class="red">*</span>)</label>
<div class="col-sm-12">
<div class="form-group has-feedback {{ $errors->has('problem-description') ? 'has-error' : '' }}">
<textarea
id="problem-description"
name="problem-description"
class="form-control"
required
rows="6"
placeholder="Problem Description"
>{{-- {{ old('problem-description') ? old('problem-description') : $viewModel->problem->defaultTranslation->description }} bookmark2 --}}{{ old('problem-description') ? old('problem-description') : '' }}</textarea>
<span class="help-block"><strong>{{ $errors->first('problem-description') }}</strong></span>
</div>
</div>
</div>

<div class="row">
<label class="col-sm-12 control-label" for="problem-status">Problem Status (<span class="red">*</span>)</label>
<div class="col-sm-12">
<div class="form-group has-feedback {{ $errors->has('problem-status') ? 'has-error' : '' }}">
<input type="text"
id="problem-status"
name="problem-status"
class="form-control"
required
placeholder="Problem Status"
{{-- value="{{ old('problem-status') ? old('problem-status') : $viewModel->problem->status_id }}" bookmark2 --}}
value="{{ old('problem-status') ? old('problem-status') : '' }}"
>
<span class="help-block"><strong>{{ $errors->first('problem-status') }}</strong></span>
</div>
</div>
</div>

<div class="row" style="display: none;{{-- bookmark2 --}}">
<div class="col-md-12">
<div class="form-group">
<label for="problem-status-2">Problem Status</label>
@if(!Gate::check('manage-platform-content'))
<small class="text-blue">(The problem status can only be changed by a platform administrator.)</small>{{-- bookmark2 - is this what we want? --}}
@endif
<select id="problem-status-2" class="form-control" name="problem-status-2">
{{-- @foreach ($viewModel->problemStatusesLkp as $status) --}}
<option
@if(!Gate::allows('manage-platform-content'))
disabled{{-- bookmark2 - is this what we want? --}}
@endif
{{-- @if ($viewModel->problem->status_id == $status->id || old('problem-status-2') == $status->id) --}}
selected
{{-- @endif --}}
{{-- value="{{ $status->id }}"> --}}
value="1">
{{-- {{ $status->title }} --}}
Draft
</option>
{{-- @endforeach --}}
</select>
</div>
</div>
</div>

<div class="row">
<label class="col-sm-12 control-label" for="problem-default-language">Problem Default Language (<span class="red">*</span>)</label>
<div class="col-sm-12">
<div class="form-group has-feedback {{ $errors->has('problem-default-language') ? 'has-error' : '' }}">
<input type="text"
id="problem-default-language"
name="problem-default-language"
class="form-control"
required
placeholder="Problem Default Language"
{{-- value="{{ old('problem-default-language') ? old('problem-default-language') : $viewModel->problem->default_language_id }}" bookmark2 --}}
value="{{ old('problem-default-language') ? old('problem-default-language') : '' }}"
>
<span class="help-block"><strong>{{ $errors->first('problem-default-language') }}</strong></span>
</div>
</div>
</div>

<div class="row" style="display: none;{{-- bookmark2 --}}">
<div class="col-md-12">
<div class="form-group">
<label for="problem-default-language-2">Problem Default Language</label>
<select id="problem-default-language-2" class="form-control" name="problem-default-language-2">
{{-- @foreach ($viewModel->languagesLkp as $language) --}}
<option
{{-- @if ($viewModel->shouldLanguageBeSelected($language)) --}}
selected
{{-- @endif --}}
{{-- value="{{ $language->id }}"> --}}
value="1">
{{-- {{ $language->language_name }} --}}
English
</option>
{{-- @endforeach --}}
</select>
</div>
</div>
</div>

{{-- <div class="row">
<label class="col-md-12 control-label" for="slug">Project Slug
<br>(it defines the project's url, for example:
<br><i>For english | https://crowdsourcing.ecas.org/en/your-project-slug</i>)
<br><i>For greek | https://crowdsourcing.ecas.org/gr/your-project-slug</i>)
<br><i>For dutch | https://crowdsourcing.ecas.org/nl/your-project-slug</i>)
<br>The url can contain only letters, numbers, and dashes.
<br>If left empty, we will take care of creating the URL, based on the project name.
<br>Please note that once you publish the project you <i>cannot</i> change the slug.
</label>
<div class="col-sm-12">
<div class="form-group has-feedback {{ $errors->has('slug') ? 'has-error' : '' }}">
<input id="slug" type="text" class="form-control" name="slug"
value="{{ old('slug') ? old('slug') : $viewModel->project->slug }}"
placeholder="Project Slug">
<span class="help-block"><strong>{{ $errors->first('slug') }}</strong></span>
</div>
</div>
</div> --}}

<input type="submit">

</div>
</div>
</div>
</div>
</div>

</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@php
// var_dump($viewModel);
@endphp

<ol>
@foreach ($viewModel['problems'] as $problem)
<li>
id: {{ $problem->id }} <br>
slug: {{ $problem->slug }} <br>
default_language_id: {{ $problem->default_language_id }} <br>
@foreach ($problem->translations as $translation)
@if ($translation->language_id === $problem->default_language_id)
default_lang_title: {{ $translation->title }} <br>
default_lang_desc: {{ $translation->description }} <br>
@endif
@endforeach
</li>
<hr>
@endforeach
</ol>
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
Route::post('/questionnaire/update-status', [QuestionnaireController::class, 'saveQuestionnaireStatus'])->name('questionnaire.update-status');
Route::get('/questionnaires/{questionnaire}/colors', [QuestionnaireStatisticsController::class, 'showEditStatisticsColorsPage'])->name('questionnaire.statistics-colors');
Route::post('/questionnaires/{questionnaire}/colors', [QuestionnaireStatisticsController::class, 'saveStatisticsColors'])->name('questionnaire.statistics-colors.store');
Route::resource('problems', CrowdSourcingProjectProblemController::class)->except(['show']);
});

Route::group(['middleware' => ['auth', 'can:moderate-content-by-users']], function () {
Expand Down

0 comments on commit 09f607e

Please sign in to comment.