Skip to content

Commit

Permalink
Resolved various bugs in project creation & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosIsaris committed Dec 12, 2024
1 parent 1d945ad commit cb62d5a
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected function setDefaultValuesForCommonProjectFields(array $attributes, ?Cr
}

if (!isset($attributes['about']) || !$attributes['about']) {
$attributes['about'] = $attributes['description'];
$attributes['about'] = trim($attributes['description']);
}

if ((!isset($attributes['img_path']) || !$attributes['img_path']) && (!$project || !$project->img_path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function getTranslationsForProject(CrowdSourcingProject $project): Collec
public function storeOrUpdateDefaultTranslationForProject(array $attributes, int $project_id): void {
$allowedKeys = (new CrowdSourcingProjectTranslation)->getFillable();
$filtered = Helpers::getFilteredAttributes($attributes, $allowedKeys);
// for each of the filtered attributes, if the value is empty, set it to null
// for the WYSIWYG editor, we need to also regard as "empty" the value '<p><br></p>'
foreach ($filtered as $key => $value) {
$filtered[$key] = Helpers::HTMLValueIsNotEmpty($value) ? $value : null;
}
$this->crowdSourcingProjectTranslationRepository->updateOrCreate(
['project_id' => $project_id, 'language_id' => $filtered['language_id']],
$filtered
Expand All @@ -58,8 +63,9 @@ public function storeOrUpdateExtraTranslationsForProject(array $extraTranslation
$extraTranslation = json_decode(json_encode($extraTranslation), true);
foreach ($extraTranslation as $key => $value) {
if (!$value) {
$extraTranslation[$key] = $defaultLanguageContentForProject[$key] && $defaultLanguageContentForProject[$key] !== '<p><br></p>' ?
$defaultLanguageContentForProject[$key] : null;
$extraTranslation[$key] =
Helpers::HTMLValueIsNotEmpty($defaultLanguageContentForProject[$key]) ?
$defaultLanguageContentForProject[$key] : null;
}
}
$filtered = Helpers::getFilteredAttributes($extraTranslation, $allowedKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function store(Request $request): RedirectResponse {
'status_id' => 'required|numeric|exists:crowd_sourcing_project_statuses_lkp,id',
'slug' => 'nullable|string|alpha_dash|unique:crowd_sourcing_projects,slug|max:100',
'language_id' => 'required|numeric|exists:languages_lkp,id',
'motto_title' => 'required|string',
'motto_subtitle' => 'required|string',
'about' => 'nullable|string',
]);
$project = $this->crowdSourcingProjectManager->storeProject($request->all());

Expand All @@ -75,6 +78,9 @@ public function update(Request $request, string $locale, int $id): RedirectRespo
'description' => 'required|string',
'slug' => 'nullable|string|alpha_dash|unique:crowd_sourcing_projects,slug,' . $id . '|max:100',
'language_id' => 'required|numeric|exists:languages_lkp,id',
'motto_title' => 'required|string',
'motto_subtitle' => 'required|string',
'about' => 'nullable|string',
]);
$attributes = $request->all();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,24 @@ public function project(): BelongsTo {
public function language(): BelongsTo {
return $this->belongsTo(Language::class, 'language_id', 'id');
}

protected static function boot() {
parent::boot();

// Trim all string attributes before saving
static::saving(function ($model) {
$model->trimAttributes();
});
}

/**
* Trim all string attributes
*/
protected function trimAttributes() {
foreach ($this->attributes as $key => $value) {
if (is_string($value) && $value !== '') {
$this->attributes[$key] = trim($value);
}
}
}
}
11 changes: 11 additions & 0 deletions app/Utils/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ function ($key) use ($allowedKeys) {
ARRAY_FILTER_USE_KEY
);
}

/**
* Checks if the value is not empty
* @param $value mixed
* @return bool whether the value is not empty
*/
public static function HTMLValueIsNotEmpty(mixed $value): bool {
return $value && $value !== '<p><br></p>'
&& $value !== '<p><br></p><p><br></p>'
&& $value !== '<p>&nbsp;</p>';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace App\ViewModels\CrowdSourcingProject;

use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class CrowdSourcingProjectForLandingPage {
public $project;
class CrowdSourcingProjectForLandingPage extends CrowdSourcingProjectLayoutPage {
public $questionnaire;
public $feedbackQuestionnaire;
public $projectHasPublishedProblems;
Expand All @@ -22,7 +21,7 @@ class CrowdSourcingProjectForLandingPage {
public $moderator;

public function __construct(
$project,
CrowdSourcingProject $project,
$questionnaire,
$feedbackQuestionnaire,
$projectHasPublishedProblems,
Expand All @@ -34,7 +33,7 @@ public function __construct(
Collection $languages,
$shareUrlForFacebook,
$shareUrlForTwitter) {
$this->project = $project;
parent::__construct($project);
$this->questionnaire = $questionnaire;
$this->feedbackQuestionnaire = $feedbackQuestionnaire;
$this->projectHasPublishedProblems = $projectHasPublishedProblems;
Expand All @@ -50,32 +49,12 @@ public function __construct(
$this->moderator = false;
}

public function getSignInURLWithParameters(): string {
$url = '/login?submitQuestionnaire=1&redirectTo=' . urlencode($this->project->slug . '?open=1');
if (Request()->referrerId) {
$url .= urlencode('&referrerId=') . Request()->referrerId;
}
if (Request()->questionnaireId) {
$url .= urlencode('&questionnaireId=') . Request()->questionnaireId;
}

return $url;
}

public function displayFeedbackQuestionnaire(): bool {
// if user has responded to the main questionnaire,
// and a feedback questionnaire exists
// and the feedback questionnare has not been answered
// and the feedback questionnaire has not been answered
return $this->userResponse != null &&
$this->feedbackQuestionnaire != null
&& $this->userFeedbackQuestionnaireResponse == null;
}

public function shouldShowQuestionnaireStatisticsLink(): bool {
return true;
}

public function getLoggedInUser() {
return Auth::user();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\ViewModels\CrowdSourcingProject;

use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\Utils\Helpers;

abstract class CrowdSourcingProjectLayoutPage {
public CrowdSourcingProject $project;

public function __construct(CrowdSourcingProject $crowdSourcingProject) {
$this->project = $crowdSourcingProject;
}

public function projectHasExternalURL(): bool {
return isset($this->project)
&& !empty($this->project->external_url)
&& $this->project->external_url !== null
&& starts_with($this->project->external_url, ['http', 'https'])
&& filter_var($this->project->external_url, FILTER_VALIDATE_URL);
}

public function projectHasCustomFooter(): bool {
return Helpers::HTMLValueIsNotEmpty($this->project->defaultTranslation->footer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use Illuminate\Support\Collection;

class CrowdSourcingProjectUnavailable {
public $project;
class CrowdSourcingProjectUnavailable extends CrowdSourcingProjectLayoutPage {
public $projects;
public $statusMessage;

public function __construct(CrowdSourcingProject $project, Collection $projects, string $statusMessage) {
$this->project = $project;
parent::__construct($project);
$this->projects = $projects;
$this->statusMessage = $statusMessage;
}

public function getProjectStatusMessage() {
public function getProjectStatusMessage(): string {
return $this->statusMessage;
}
}
6 changes: 3 additions & 3 deletions app/ViewModels/Problem/ProblemPublicPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\Models\Problem\Problem;
use App\ViewModels\CrowdSourcingProject\CrowdSourcingProjectLayoutPage;

class ProblemPublicPage {
class ProblemPublicPage extends CrowdSourcingProjectLayoutPage {
public Problem $problem;
public CrowdSourcingProject $project;
public string $page_title;

public function __construct(Problem $problem, CrowdSourcingProject $project) {
parent::__construct($project);
$this->problem = $problem;
$this->project = $project;
$this->page_title = $project->currentTranslation->name . ' | ' . $problem->currentTranslation->title;
}
}
10 changes: 2 additions & 8 deletions app/ViewModels/Problem/ProblemsLandingPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

namespace App\ViewModels\Problem;

use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\ViewModels\CrowdSourcingProject\CrowdSourcingProjectLayoutPage;

class ProblemsLandingPage {
public CrowdSourcingProject $project;

public function __construct(CrowdSourcingProject $crowdSourcingProject) {
$this->project = $crowdSourcingProject;
}
}
class ProblemsLandingPage extends CrowdSourcingProjectLayoutPage {}
5 changes: 3 additions & 2 deletions app/ViewModels/Questionnaire/QuestionnairePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
use App\Models\Questionnaire\Questionnaire;
use App\Models\Questionnaire\QuestionnaireResponse;
use App\Models\User\User;
use App\ViewModels\CrowdSourcingProject\CrowdSourcingProjectLayoutPage;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class QuestionnairePage {
class QuestionnairePage extends CrowdSourcingProjectLayoutPage {
public Questionnaire $questionnaire;
public ?QuestionnaireResponse $userResponse;
public CrowdSourcingProject $project;
public Collection $languages;
public bool $moderator;

public function __construct(Questionnaire $questionnaire, ?QuestionnaireResponse $userResponse, CrowdSourcingProject $project, Collection $languages, bool $moderator) {
parent::__construct($project);
$this->questionnaire = $questionnaire;
$this->userResponse = $userResponse;
$this->project = $project;
$this->languages = $languages;
$this->moderator = $moderator;
}
Expand Down
8 changes: 8 additions & 0 deletions app/ViewModels/Questionnaire/QuestionnaireStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public function __construct(Questionnaire $questionnaire,
public function userCanViewFileTypeQuestionsStatistics(): int {
return ($this->userCanAnnotateAnswers || $this->questionnaire->show_file_type_questions_to_statistics_page_audience) ? 1 : -1;
}

public function projectHasExternalURL(): bool {
return false;
}

public function projectHasCustomFooter(): bool {
return false;
}
}
6 changes: 3 additions & 3 deletions app/ViewModels/Solution/ProposeSolutionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\Models\Language;
use App\Models\Problem\Problem;
use App\ViewModels\CrowdSourcingProject\CrowdSourcingProjectLayoutPage;

class ProposeSolutionPage {
public CrowdSourcingProject $project;
class ProposeSolutionPage extends CrowdSourcingProjectLayoutPage {
public Problem $problem;
public Language $language;
public string $page_title;
Expand All @@ -17,7 +17,7 @@ public function __construct(
Problem $problem,
Language $language,
) {
$this->project = $project;
parent::__construct($project);
$this->problem = $problem;
$this->language = $language;
$this->page_title = $project->currentTranslation->name . ' | ' . $problem->currentTranslation->title . '' . __('solution.propose_solution');
Expand Down
6 changes: 3 additions & 3 deletions app/ViewModels/Solution/SolutionSubmitted.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
use App\Models\CrowdSourcingProject\CrowdSourcingProject;
use App\Models\Problem\Problem;
use App\Models\Solution\Solution;
use App\ViewModels\CrowdSourcingProject\CrowdSourcingProjectLayoutPage;

class SolutionSubmitted {
class SolutionSubmitted extends CrowdSourcingProjectLayoutPage {
public Solution $solution;
public Problem $problem;
public CrowdSourcingProject $project;
public string $page_title;

public function __construct(Solution $solution, Problem $problem, CrowdSourcingProject $project) {
parent::__construct($project);
$this->solution = $solution;
$this->problem = $problem;
$this->project = $project;
$this->page_title = $project->currentTranslation->name . ' | ' . $problem->currentTranslation->title . '' . __('solution.proposal_submitted_title');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void {
Schema::table('crowd_sourcing_project_translations', function (Blueprint $table) {
$table->mediumText('about')->nullable()->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void {
Schema::table('crowd_sourcing_project_translations', function (Blueprint $table) {
//
});
}
};
2 changes: 1 addition & 1 deletion resources/assets/js/common-backoffice.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const MOBILE_WIDTH = 768;
.slideUp(500, function () {
window.$(".alert-dismissable").alert("close");
});
}, 3000);
}, 3000000);
};

const initClipboardElements = function () {
Expand Down
9 changes: 9 additions & 0 deletions resources/assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ if (import.meta.env.VITE_SENTRY_DSN_PUBLIC) {
});
};

const trimTextareaInputFields = function () {
// get all textarea elements
// and trim their values
$("textarea").each(function () {
$(this).val($.trim($(this).val()));
});
};

const init = function () {
$(".dropdown-toggle").dropdown();
handleLogoutBtnClick();
$(".smooth-goto").on("click", function () {
$("html, body").animate({ scrollTop: $(this.hash).offset().top - 100 }, 1000);
return false;
});
trimTextareaInputFields();
};

$(document).ready(function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ export default {
const filteredTranslations = (translation) => {
// return an object with only the properties that exist in the model metadata
return Object.keys(translation).reduce((acc, key) => {
if (propertyExistsInMetadata(translation[key], key) && translation[key] !== "<p><br></p>") {
if (propertyExistsInMetadata(translation[key], key)
&& translation[key] !== "<p><br></p>"
&& translation[key] !== "<p>&nbsp;</p>") {
acc[key] = translation[key];
}
return acc;
Expand Down
Loading

0 comments on commit cb62d5a

Please sign in to comment.