From 8083a0601ac73b463fa45017a63374623fd0a993 Mon Sep 17 00:00:00 2001
From: rapotek <17698165+rapotek@users.noreply.github.com>
Date: Wed, 16 Oct 2024 09:43:05 +0200
Subject: [PATCH] Neighbourhood.php updated to be compliant with last master
changes; View and Api controllers separated
---
.../views/myNbhInteractive/myNeighbourhood.js | 4 +-
.../MyNbhInteractiveApiController.php | 119 ++++++++++++
.../MyNbhInteractiveController.php | 105 +----------
src/Models/Neighbourhood/Neighbourhood.php | 170 +++++++-----------
.../myNbhInteractive/myNeighbourhood.tpl.php | 10 +-
5 files changed, 190 insertions(+), 218 deletions(-)
create mode 100644 src/Controllers/MyNbhInteractiveApiController.php
diff --git a/public/views/myNbhInteractive/myNeighbourhood.js b/public/views/myNbhInteractive/myNeighbourhood.js
index e87cd1010b..e904abc3ea 100644
--- a/public/views/myNbhInteractive/myNeighbourhood.js
+++ b/public/views/myNbhInteractive/myNeighbourhood.js
@@ -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,
@@ -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,
diff --git a/src/Controllers/MyNbhInteractiveApiController.php b/src/Controllers/MyNbhInteractiveApiController.php
new file mode 100644
index 0000000000..b3f194858d
--- /dev/null
+++ b/src/Controllers/MyNbhInteractiveApiController.php
@@ -0,0 +1,119 @@
+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();
+ }
+ }
+}
diff --git a/src/Controllers/MyNbhInteractiveController.php b/src/Controllers/MyNbhInteractiveController.php
index abc98bb6d3..aa77e46c43 100644
--- a/src/Controllers/MyNbhInteractiveController.php
+++ b/src/Controllers/MyNbhInteractiveController.php
@@ -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;
@@ -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/';
@@ -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;
}
@@ -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();
- }
- }
}
diff --git a/src/Models/Neighbourhood/Neighbourhood.php b/src/Models/Neighbourhood/Neighbourhood.php
index 505fbabd00..5dd1f591d8 100644
--- a/src/Models/Neighbourhood/Neighbourhood.php
+++ b/src/Models/Neighbourhood/Neighbourhood.php
@@ -34,74 +34,40 @@ class Neighbourhood extends BaseObject
self::ITEM_RECOMMENDEDCACHES => 'top_recommended',
];
- /**
- * Id in DB
- *
- * @var int
- */
- private $id;
+ /** Id in DB */
+ private int $id;
- /** @var int */
- private $userid;
+ private int $userid;
- /**
- * User who neighbourhood belongs to
- *
- * @var User
- */
- private $user = null;
+ /** User who neighbourhood belongs to */
+ private ?User $user = null;
- /**
- * Number in neighbourhoods sequence
- *
- * @var int
- */
- private $seq;
+ /** Number in neighbourhoods sequence */
+ private int $seq;
- /**
- * Name of neighbourhood
- *
- * @var string
- */
- private $name;
+ /** Name of neighbourhood */
+ private string $name;
- /**
- * Coords of neighbourhood's center
- *
- * @var Coordinates
- */
- private $coords;
+ /** Coords of neighbourhood's center */
+ private Coordinates $coords;
- /**
- * Radius of neighbourhood
- *
- * @var int
- */
- private $radius;
-
- /**
- * User will receive new cache notifications from this Nbh
- *
- * @var bool
- */
- private $notify = false;
+ /** Radius of neighbourhood */
+ private int $radius;
- public function __construct()
- {
- parent::__construct();
- }
+ /** User will receive new cache notifications from this Nbh */
+ private bool $notify = false;
- public function getId()
+ public function getId(): int
{
return $this->id;
}
- public function getUserid()
+ public function getUserid(): int
{
return $this->userid;
}
- public function getUser()
+ public function getUser(): ?User
{
if (is_null($this->user)) {
$this->user = User::fromUserIdFactory($this->getUserid());
@@ -110,62 +76,62 @@ public function getUser()
return $this->user;
}
- public function getSeq()
+ public function getSeq(): int
{
return $this->seq;
}
- public function getName()
+ public function getName(): string
{
return $this->name;
}
- public function getCoords()
+ public function getCoords(): Coordinates
{
return $this->coords;
}
- public function getRadius()
+ public function getRadius(): int
{
return $this->radius;
}
- public function getNotify()
+ public function getNotify(): bool
{
return $this->notify;
}
- public function setId($id)
+ public function setId(int $id)
{
$this->id = $id;
}
- public function setUserid($userid)
+ public function setUserid(int $userid)
{
$this->userid = $userid;
}
- public function setSeq($seq)
+ public function setSeq(int $seq)
{
$this->seq = $seq;
}
- public function setName($name)
+ public function setName(string $name)
{
$this->name = $name;
}
- public function setCoords($coords)
+ public function setCoords(Coordinates $coords)
{
$this->coords = $coords;
}
- public function setRadius($radius)
+ public function setRadius(int $radius)
{
$this->radius = $radius;
}
- public function setNotify($notify)
+ public function setNotify(bool $notify)
{
$this->notify = $notify;
}
@@ -173,29 +139,26 @@ public function setNotify($notify)
/**
* Factory
*
- * @param int $id - Id of Neighbourhood in DB
- * @return Neighbourhood|null
+ * @param int $id - ID of Neighbourhood in DB
*/
- public static function fromIdFactory($id)
+ public static function fromIdFactory(int $id): ?Neighbourhood
{
$result = new self();
try {
$result->loadById($id);
} catch (Exception $e) {
- return;
+ return null;
}
return $result;
}
/**
- * Returns simple array with Coords obj and radius - for given $user and nbhSeq
- *
- * @param int $seq
- * @return array
+ * Returns simple array with Coords obj and radius - for given $user and
+ * nbhSeq
*/
- public static function getCoordsAndRadius(User $user, $seq)
+ public static function getCoordsAndRadius(User $user, int $seq): array
{
$result = [];
$result['coords'] = null;
@@ -234,11 +197,11 @@ public static function getCoordsAndRadius(User $user, $seq)
/**
* Returns array of all Neighbourhoods of user.
- * HomeCoords & NotifyRadius has Id & Seq set to 0
+ * HomeCoords & NotifyRadius has ID & Seq set to 0
*
* @return Neighbourhood[]
*/
- public static function getNeighbourhoodsList(User $user)
+ public static function getNeighbourhoodsList(User $user): array
{
$result = [];
// Stage 1 - get default neighbourhood stored in user table
@@ -258,7 +221,8 @@ public static function getNeighbourhoodsList(User $user)
$myNgh->prepareForSerialization();
$result[0] = $myNgh;
}
- // Stage 2 - get additional neighbourhoods stored in user_neighbourhoods table
+ // Stage 2 - get additional neighbourhoods stored in user_neighbourhoods
+ // table
$myNghAdd = self::getAdditionalNeighbourhoodsList($user);
foreach ($myNghAdd as $row) {
@@ -268,13 +232,13 @@ public static function getNeighbourhoodsList(User $user)
return $result;
}
- public static function getAdditionalNeighbourhoodsList(User $user)
+ public static function getAdditionalNeighbourhoodsList(User $user): array
{
$query = '
SELECT `id`
FROM `user_neighbourhoods`
WHERE `user_id` = :userid
- ORDER BY `seq` ASC
+ ORDER BY `seq`
';
$params = [];
$params['userid']['value'] = $user->getUserId();
@@ -296,19 +260,16 @@ public static function getAdditionalNeighbourhoodsList(User $user)
/**
* Stores (create lub modify in DB) additional user's neighbourhood
*
- * @param int $radius
- * @param string $name
- * @param int $seq - if null - get first available number
- * @return bool
+ * @param int|null $seq - if null - get first available number
*/
public static function storeUserNeighbourhood(
User $user,
Coordinates $coords,
- $radius,
- $name,
- $seq = null,
- $notify = false
- ) {
+ int $radius,
+ string $name,
+ int $seq = null,
+ bool $notify = false
+ ): bool {
if (is_null($seq)) {
$seq = self::getMaxUserSeq($user) + 1;
}
@@ -332,18 +293,15 @@ public static function storeUserNeighbourhood(
$name,
$coords->getLongitude(),
$coords->getLatitude(),
- (int) $radius,
- boolval($notify)
+ $radius,
+ (int) $notify
) !== null;
}
/**
* Removes additional user neighbourhood from DB
- *
- * @param int $seq
- * @return bool
*/
- public static function removeUserNeighbourhood(User $user, $seq)
+ public static function removeUserNeighbourhood(User $user, int $seq): bool
{
$query = '
DELETE FROM `user_neighbourhoods`
@@ -376,33 +334,25 @@ public static function removeAllUserNeighbourhood(User $user): void
/**
* Changes "Notify" state for $seq Neighbourhood for $user
- *
- * @param int $seq
- * @param bool $state
- * @return bool
*/
- public static function setNeighbourhoodNotify(User $user, $seq, $state)
- {
- return null !== self::db()->multiVariableQuery(
- '
+ public static function setNeighbourhoodNotify(
+ User $user,
+ int $seq,
+ bool $state
+ ): bool {
+ return null !== self::db()->multiVariableQuery('
UPDATE `user_neighbourhoods`
SET `notify` = :1
WHERE `user_id` = :2
AND `seq` = :3
LIMIT 1
- ',
- boolval($state),
- $user->getUserId(),
- $seq
- );
+ ', intval($state), $user->getUserId(), $seq);
}
/**
* Returns max seq number for user's additional nbh
- *
- * @return int
*/
- private static function getMaxUserSeq(User $user)
+ private static function getMaxUserSeq(User $user): int
{
$query = '
SELECT MAX(`seq`)
@@ -437,7 +387,7 @@ private function loadById($id)
}
}
- private function loadFromRow(array $neighbourhoodDbRow)
+ private function loadFromRow(array $neighbourhoodDbRow): self
{
$this->id = $neighbourhoodDbRow['id'];
$this->userid = $neighbourhoodDbRow['user_id'];
diff --git a/src/Views/myNbhInteractive/myNeighbourhood.tpl.php b/src/Views/myNbhInteractive/myNeighbourhood.tpl.php
index 55b84b5698..35fb6350b4 100644
--- a/src/Views/myNbhInteractive/myNeighbourhood.tpl.php
+++ b/src/Views/myNbhInteractive/myNeighbourhood.tpl.php
@@ -1,8 +1,10 @@
= tr('myn_distances'); ?>
= tr('hidden_by'); ?> = $cache->getOwner()->getUserName(); ?>
getType() == GeoCacheLog::LOGTYPE_FOUNDIT && $log->isRecommendedByUser($log->getUser())) { ?>
+ if ($log->getType() == GeoCacheLogCommons::LOGTYPE_FOUNDIT && $log->isRecommendedByUser($log->getUser())) { ?>
|