Skip to content

Commit

Permalink
Merge branch 'feature/ajax_crud_methods_calls_refactor' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Volmarg committed Feb 1, 2020
2 parents c44735e + d2ef023 commit deffedf
Show file tree
Hide file tree
Showing 48 changed files with 888 additions and 500 deletions.
24 changes: 12 additions & 12 deletions public/assets/app.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/Controller/Files/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace App\Controller\Files;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\Form\Files\UploadSubdirectoryCreateType;
use App\Services\DirectoriesHandler;
Expand Down Expand Up @@ -58,12 +59,16 @@ public function __construct(FilesHandler $files_handler, DirectoriesHandler $dir
/**
* @Route("/files/action/remove-file", name="files_remove_file", methods="POST")
* @param Request $request
* @return JsonResponse
* @return Response
* @throws \Exception
*/
public function removeFileViaPost(Request $request) {
$response = $this->files_handler->removeFile($request);
return $response;

$code = $response->getStatusCode();
$message = $response->getContent();

return AjaxResponse::buildResponseForAjaxCall($code, $message);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/Files/FilesUploadSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace App\Controller\Files;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\Controller\Utils\Env;
use App\Controller\Utils\Utils;
Expand Down Expand Up @@ -70,7 +71,8 @@ public function displaySettings(Request $request) {
return $this->renderSettingsPage(false, $request);
}

return $this->renderSettingsPage(true, $request);
$template_content = $this->renderSettingsPage(true, $request)->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, "", $template_content);
}

/**
Expand Down
34 changes: 23 additions & 11 deletions src/Controller/Modules/Achievements/AchievementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Controller\Modules\Achievements;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\Controller\Utils\Repositories;
use App\Entity\Modules\Achievements\Achievement;
use App\Services\Exceptions\ExceptionDuplicatedTranslationKey;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -36,21 +38,22 @@ public function __construct(Application $app) {
* @return Response
*/
public function display(Request $request) {
$form = $this->app->forms->achievementForm(['enum_types' => $this->enum_types]);
$this->addFormDataToDB($form, $request);
$this->addFormDataToDB($request);

if (!$request->isXmlHttpRequest()) {
return $this->renderTemplate($form, false);
return $this->renderTemplate(false);
}
return $this->renderTemplate($form, true);

$template_content = $this->renderTemplate(true)->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, "", $template_content);
}

/**
* @param bool $ajax_render
* @param $achievement_form
* @return Response
*/
protected function renderTemplate($achievement_form, $ajax_render = false) {
protected function renderTemplate($ajax_render = false) {
$achievement_form = $this->app->forms->achievementForm(['enum_types' => $this->enum_types]);
$achievement_form_view = $achievement_form->createView();

$columns_names = $this->getDoctrine()->getManager()->getClassMetadata(Achievement::class)->getColumnNames();
Expand All @@ -66,10 +69,13 @@ protected function renderTemplate($achievement_form, $ajax_render = false) {
}

/**
* @param FormInterface $achievement_form
* @param Request $request
*/
protected function addFormDataToDB(FormInterface $achievement_form, Request $request) {
protected function addFormDataToDB(Request $request) {
/**
* @var FormInterface $achievement_form
*/
$achievement_form = $this->app->forms->achievementForm(['enum_types' => $this->enum_types]);
$achievement_form->handleRequest($request);

if ($achievement_form->isSubmitted() && $achievement_form->isValid()) {
Expand All @@ -83,6 +89,7 @@ protected function addFormDataToDB(FormInterface $achievement_form, Request $req
* @Route("/achievement/update/",name="achievement-update")
* @param Request $request
* @return Response
* @throws ExceptionDuplicatedTranslationKey
*/
public function update(Request $request) {

Expand All @@ -108,11 +115,16 @@ public function remove(Request $request): Response {
$request->request->get('id')
);

$message = $response->getContent();

if ($response->getStatusCode() == 200) {
$form = $this->app->forms->achievementForm(['enum_types' => $this->enum_types]);
return $this->renderTemplate($form, true);
$rendered_template = $this->renderTemplate(true);

$template_content = $rendered_template->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, $message, $template_content);
}
return $response;

return AjaxResponse::buildResponseForAjaxCall(500, $message);
}

}
15 changes: 12 additions & 3 deletions src/Controller/Modules/Contacts/MyContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller\Modules\Contacts;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\Controller\Utils\Repositories;
use App\Controller\Utils\Utils;
Expand Down Expand Up @@ -51,7 +52,8 @@ public function display(Request $request) {
if (!$request->isXmlHttpRequest()) {
return $this->renderTemplate( false);
}
return $this->renderTemplate( true);
$template_content = $this->renderTemplate( true)->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, "", $template_content);
}

protected function renderTemplate($ajax_render = false) {
Expand Down Expand Up @@ -80,9 +82,16 @@ public function remove(Request $request) {
);

if ($response->getStatusCode() == 200) {
return $this->renderTemplate(true);
$rendered_template = $this->renderTemplate(true);
$template_content = $rendered_template->getContent();
$message = $this->app->translations->ajaxSuccessRecordHasBeenRemoved();

return AjaxResponse::buildResponseForAjaxCall(200, $message, $template_content);
}
return $response;

$message = $this->app->translations->ajaxFailureRecordCouldNotBeenRemoved();

return AjaxResponse::buildResponseForAjaxCall(500, $message);
}

/**
Expand Down
60 changes: 18 additions & 42 deletions src/Controller/Modules/Contacts/MyContactsSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller\Modules\Contacts;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\Controller\Utils\Repositories;
use App\DTO\Modules\Contacts\ContactsTypesDTO;
Expand Down Expand Up @@ -35,26 +36,18 @@ public function __construct(Application $app) {
* @Route("/my-contacts-settings", name="my-contacts-settings")
* @param Request $request
* @return Response
* @throws ExceptionDuplicatedTranslationKey
*/
public function displaySettingsPage(Request $request) {
$response = $this->submitContactTypeForm($request);

if ($response->getStatusCode() != 200) {
return $response;
}

$response = $this->submitContactGroupForm($request);

if ($response->getStatusCode() != 200) {
return $response;
}
$this->submitContactTypeForm($request);
$this->submitContactGroupForm($request);

if (!$request->isXmlHttpRequest()) {
return $this->renderSettingsTemplate(false);
}


return $this->renderSettingsTemplate(true);
$template_content = $this->renderSettingsTemplate(true)->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, "", $template_content);
}

public function renderSettingsTemplate($ajax_render = false) {
Expand Down Expand Up @@ -141,6 +134,7 @@ private function submitContactTypeForm(Request $request):Response {
/**
* @param Request $request
* @return Response
* @throws ExceptionDuplicatedTranslationKey
*/
private function submitContactGroupForm(Request $request):Response {
$form = $this->app->forms->contactGroupForm();
Expand All @@ -156,15 +150,15 @@ private function submitContactGroupForm(Request $request):Response {

if (!is_null($form_data) && $this->app->repositories->myContactGroupRepository->findBy([ 'name' => $name ] )) {
$record_with_this_name_exist = $this->app->translator->translate('db.recordWithThisNameExist');
return new JsonResponse($record_with_this_name_exist, 409);
return new Response($record_with_this_name_exist, 409);
}

$this->app->em->persist($form_data);
$this->app->em->flush();
}

$form_submitted_message = $this->app->translator->translate('forms.general.success');
return new JsonResponse($form_submitted_message, 200);
return new Response($form_submitted_message, 200);
}

/**
Expand All @@ -175,40 +169,22 @@ private function submitContactGroupForm(Request $request):Response {
*/
public function removeContactType(Request $request) {

$record_id = $request->request->get('id');
$are_there_active_contacts_with_contact_type = $this->areThereActiveContactsWithContactType($record_id);

if( $are_there_active_contacts_with_contact_type ){
$message = $this->app->translator->translate('db.foreignKeyViolation');
$response_data = [
self::KEY_MESSAGE => $message,
];
return new JsonResponse($response_data, 500);
}

$response = $this->app->repositories->deleteById(
$record_id = $request->request->get('id');
$response = $this->app->repositories->deleteById(
Repositories::MY_CONTACT_TYPE_REPOSITORY,
$record_id
);

$message = $response->getContent();

if ($response->getStatusCode() == 200) {
return $this->renderSettingsTemplate(true);
}
return $response;
}
$rendered_template = $this->renderSettingsTemplate(true);
$template_content = $rendered_template->getContent();

/**
* This function checks if there are any contacts with deleted = 0 that still use this contact type
* Jsons are not cleared for removal - with this minimal data that there is, it's possible to revert the contact
* @param string $record_id
* @return bool
*/
private function areThereActiveContactsWithContactType(string $record_id):bool {
$removed_record = $this->app->repositories->myContactTypeRepository->find($record_id);
$contact_type_name = $removed_record->getName();
$contacts = $this->app->repositories->myContactRepository->findContactsWithContactTypeByContactTypeName($contact_type_name);
return AjaxResponse::buildResponseForAjaxCall(200, $message, $template_content);
}

return !empty($contacts);
return AjaxResponse::buildResponseForAjaxCall(500, $message);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/Modules/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller\Modules\Dashboard;

use App\Controller\Utils\AjaxResponse;
use App\Controller\Utils\Application;
use App\DTO\Settings\SettingsDashboardDTO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand Down Expand Up @@ -34,7 +35,9 @@ public function display(Request $request) {
if (!$request->isXmlHttpRequest()) {
return $this->renderTemplate(false);
}
return $this->renderTemplate(true);

$template_content = $this->renderTemplate( true)->getContent();
return AjaxResponse::buildResponseForAjaxCall(200, "", $template_content);
}

/**
Expand Down
Loading

0 comments on commit deffedf

Please sign in to comment.