Skip to content

Commit

Permalink
Neighbourhood.php updated to be compliant with last master changes; V…
Browse files Browse the repository at this point in the history
…iew and Api controllers separated
  • Loading branch information
rapotek committed Oct 16, 2024
1 parent f86debc commit 8083a06
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 218 deletions.
4 changes: 2 additions & 2 deletions public/views/myNbhInteractive/myNeighbourhood.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $(".nbh-hide-toggle").on("click", function() {
let itemId = icon.closest(".nbh-block").attr('id');
let section = icon.closest(".nbh-block").attr('section');
$.ajax({
url : changeDisplayAjaxUri,
url : changeDisplayUri,
type : "post",
data : {
hide : hidden,
Expand All @@ -57,7 +57,7 @@ $(".nbh-size-toggle").on("click", function() {
let sizeClass = icon.closest(".nbh-block").hasClass("nbh-full");
let itemId = icon.closest(".nbh-block").attr('id');
$.ajax({
url : changeSizeAjaxUri,
url : changeSizeUri,
type : "post",
data : {
size : sizeClass,
Expand Down
119 changes: 119 additions & 0 deletions src/Controllers/MyNbhInteractiveApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace src\Controllers;

use src\Controllers\Core\ApiBaseController;
use src\Models\User\UserPreferences\NeighbourhoodPref;
use src\Models\User\UserPreferences\UserPreferences;

class MyNbhInteractiveApiController extends ApiBaseController
{
/**
* Saves changed order of MyNbh sections. Called via Ajax by MyNbh main page
*/
public function changeOrder()
{
$this->checkUserLoggedAjax();
$this->paramCheck('order');
$order = [];
parse_str($_POST['order'], $order);
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$counter = 1;

foreach ($order['item'] as $itemOrder) {
$preferences['items'][$itemOrder]['order'] = $counter;
$counter++;
}

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Saves changed size of MyNbh section. Called via Ajax by MyNbh main page
*/
public function changeSize()
{
$this->checkUserLoggedAjax();
$this->paramCheck('size');
$this->paramCheck('item');
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$itemNr = ltrim($_POST['item'], 'item_');
$preferences['items'][$itemNr]['fullsize'] = filter_var(
$_POST['size'],
FILTER_VALIDATE_BOOLEAN
);

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Saves display status of MyNbh section. Called via Ajax by MyNbh main page
*/
public function changeDisplay()
{
$this->checkUserLoggedAjax();
$this->paramCheck('hide');
$this->paramCheck('item');
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$itemNr = ltrim($_POST['item'], 'item_');
$preferences['items'][$itemNr]['show'] = ! filter_var(
$_POST['hide'],
FILTER_VALIDATE_BOOLEAN
);

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Abstract function implementation,
* (definition has to be compliant with parent class)
*/
public function isCallableFromRouter(string $actionName): bool
{
return true;
}

/**
* Check if $_POST[$paramName] is set. If not - generates 400 AJAX response
*
* @param string $paramName a name of parameter to check
*/
private function paramCheck(string $paramName)
{
if (! isset($_POST[$paramName])) {
$this->ajaxErrorResponse('No parameter: ' . $paramName, 400);

exit();
}
}
}
105 changes: 3 additions & 102 deletions src/Controllers/MyNbhInteractiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace src\Controllers;

use src\Controllers\Core\ViewBaseController;
use src\Models\ChunkModels\InteractiveMap\CacheMarkerModel;
use src\Models\ChunkModels\InteractiveMap\InteractiveMapModel;
use src\Models\ChunkModels\InteractiveMap\LogMarkerModel;
Expand All @@ -15,7 +16,7 @@
use src\Utils\Uri\SimpleRouter;
use src\Utils\Uri\Uri;

class MyNbhInteractiveController extends BaseController
class MyNbhInteractiveController extends ViewBaseController
{
// an URL path to public resources
public const PUBLIC_SRC_PATH = '/views/myNbhInteractive/';
Expand Down Expand Up @@ -480,97 +481,11 @@ public function delete(int $nbhSeq = 0)
exit();
}

/**
* Saves changed order of MyNbh sections. Called via Ajax by MyNbh main page
*/
public function changeOrderAjax()
{
$this->checkUserLoggedAjax();
$this->paramAjaxCheck('order');
$order = [];
parse_str($_POST['order'], $order);
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$counter = 1;

foreach ($order['item'] as $itemOrder) {
$preferences['items'][$itemOrder]['order'] = $counter;
$counter++;
}

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Saves changed size of MyNbh section. Called via Ajax by MyNbh main page
*/
public function changeSizeAjax()
{
$this->checkUserLoggedAjax();
$this->paramAjaxCheck('size');
$this->paramAjaxCheck('item');
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$itemNr = ltrim($_POST['item'], 'item_');
$preferences['items'][$itemNr]['fullsize'] = filter_var(
$_POST['size'],
FILTER_VALIDATE_BOOLEAN
);

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Saves display status of MyNbh section. Called via Ajax by MyNbh main page
*/
public function changeDisplayAjax()
{
$this->checkUserLoggedAjax();
$this->paramAjaxCheck('hide');
$this->paramAjaxCheck('item');
$preferences = UserPreferences::getUserPrefsByKey(
NeighbourhoodPref::KEY
)->getValues();
$itemNr = ltrim($_POST['item'], 'item_');
$preferences['items'][$itemNr]['show'] = ! filter_var(
$_POST['hide'],
FILTER_VALIDATE_BOOLEAN
);

if (
! UserPreferences::savePreferencesJson(
NeighbourhoodPref::KEY,
json_encode($preferences)
)
) {
$this->ajaxErrorResponse('Error saving UserPreferences');
}
$this->ajaxSuccessResponse();
}

/**
* Abstract function implementation,
* (definition has to be compliant with parent class)
*/
public function isCallableFromRouter($actionName)
public function isCallableFromRouter(string $actionName): bool
{
return true;
}
Expand Down Expand Up @@ -717,18 +632,4 @@ private function viewDetails(

$this->view->buildView();
}

/**
* Check if $_POST[$paramName] is set. If not - generates 400 AJAX response
*
* @param string $paramName a name of parameter to check
*/
private function paramAjaxCheck(string $paramName)
{
if (! isset($_POST[$paramName])) {
$this->ajaxErrorResponse('No parameter: ' . $paramName, 400);

exit();
}
}
}
Loading

0 comments on commit 8083a06

Please sign in to comment.