From d27ad8e9d7866d994d8b4cf8eb46ea79937f90f3 Mon Sep 17 00:00:00 2001
From: Sam Wilson
Date: Fri, 7 Jul 2023 18:09:02 +0800
Subject: [PATCH] Add default group setting
---
src/Controller/PostController.php | 6 +++---
src/Controller/SettingController.php | 10 ++++++----
src/Entity/Post.php | 23 +++++------------------
src/Repository/PostRepository.php | 18 ++++++++++++++++--
src/Settings.php | 7 +++++++
templates/post/form.html.twig | 2 +-
templates/setting/index.html.twig | 14 ++++++++++++--
tests/Controller/MapControllerTest.php | 2 +-
translations/messages.en.yaml | 1 +
9 files changed, 52 insertions(+), 31 deletions(-)
diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php
index c1ab214..80be5ef 100644
--- a/src/Controller/PostController.php
+++ b/src/Controller/PostController.php
@@ -20,14 +20,14 @@
use Doctrine\ORM\EntityManagerInterface;
use IntlDateFormatter;
use PHPUnit\Util\Json;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
-use Symfony\Component\Routing\Annotation\Route;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PostController extends ControllerBase
@@ -84,7 +84,7 @@ public function editPost(
CommonsRepository $commonsRepository,
$id = null
) {
- $post = $id ? $postRepository->find($id) : new Post();
+ $post = $id ? $postRepository->find($id) : $postRepository->factory();
if ($request->get('in_reply_to')) {
$post->setInReplyTo($postRepository->find($request->get('in_reply_to')));
}
diff --git a/src/Controller/SettingController.php b/src/Controller/SettingController.php
index e5e0692..fd4fe92 100644
--- a/src/Controller/SettingController.php
+++ b/src/Controller/SettingController.php
@@ -2,13 +2,14 @@
namespace App\Controller;
+use App\Repository\UserGroupRepository;
use App\Settings;
+use OAuth\Common\Storage\Session;
use Samwilson\PhpFlickr\PhpFlickr;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
+use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
-use OAuth\Common\Storage\Session;
+use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SettingController extends ControllerBase
@@ -31,10 +32,11 @@ public function save(Request $request, Settings $settings)
* @Route("/settings", name="settings")
* @isGranted("ROLE_ADMIN")
*/
- public function index()
+ public function index(UserGroupRepository $userGroupRepository)
{
return $this->render('setting/index.html.twig', [
'controller_name' => 'SettingController',
+ 'user_groups' => $userGroupRepository->findAll(),
]);
}
diff --git a/src/Entity/Post.php b/src/Entity/Post.php
index 66a0c54..06ee57c 100644
--- a/src/Entity/Post.php
+++ b/src/Entity/Post.php
@@ -3,7 +3,6 @@
namespace App\Entity;
use App\Repository\PostRepository;
-use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
@@ -12,6 +11,7 @@
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\Table;
+use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
/**
* @ORM\Entity(repositoryClass=PostRepository::class)
@@ -237,6 +237,10 @@ public function getInReplyTo(): ?self
public function setInReplyTo(?self $in_reply_to): self
{
$this->in_reply_to = $in_reply_to;
+ if (!$this->getId()) {
+ // For new posts, set the view group to match the parent post.
+ $this->setViewGroup($in_reply_to->getViewGroup());
+ }
return $this;
}
@@ -329,21 +333,4 @@ public function canBeViewedBy(?User $user = null): bool
}
return $user && $user->isInGroup($this->getViewGroup());
}
-
- /**
- * Find out whether a given group should be selected for this post.
- * @param UserGroup $group
- * @return bool
- */
- public function isSelectedGroup(UserGroup $group): bool
- {
- $g = false;
- if ($this->getViewGroup()) {
- $g = $this->getViewGroup();
- }
- if ($this->getInReplyTo()) {
- $g = $this->getInReplyTo()->getViewGroup();
- }
- return $g && $g->getId() == $group->getId();
- }
}
diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php
index 04c60cd..7ecfd2f 100644
--- a/src/Repository/PostRepository.php
+++ b/src/Repository/PostRepository.php
@@ -6,7 +6,7 @@
use App\Entity\Syndication;
use App\Entity\User;
use App\Entity\UserGroup;
-use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
+use App\Settings;
use DateTime;
use DateTimeZone;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
@@ -16,6 +16,7 @@
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use IntlDateFormatter;
+use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\Process;
@@ -42,13 +43,17 @@ class PostRepository extends ServiceEntityRepository
/** @var UserGroupRepository */
private $userGroupRepository;
+ /** @var Settings */
+ private $settings;
+
public function __construct(
ManagerRegistry $registry,
ContactRepository $contactRepository,
TagRepository $tagRepository,
FileRepository $fileRepository,
SyndicationRepository $syndicationRepository,
- UserGroupRepository $userGroupRepository
+ UserGroupRepository $userGroupRepository,
+ Settings $settings
) {
parent::__construct($registry, Post::class);
$this->contactRepository = $contactRepository;
@@ -56,6 +61,7 @@ public function __construct(
$this->fileRepository = $fileRepository;
$this->syndicationRepository = $syndicationRepository;
$this->userGroupRepository = $userGroupRepository;
+ $this->settings = $settings;
}
private function createQueryBuilderForPosts(?User $user = null): QueryBuilder
@@ -68,6 +74,14 @@ private function createQueryBuilderForPosts(?User $user = null): QueryBuilder
->andWhere("p.view_group IN ($groupList)");
}
+ public function factory(): Post
+ {
+ $post = new Post();
+ $ug = $this->userGroupRepository->find($this->settings->defaultGroup());
+ $post->setViewGroup($ug);
+ return $post;
+ }
+
/**
* @inheritDoc
*/
diff --git a/src/Settings.php b/src/Settings.php
index 2c10a2b..fda7e76 100644
--- a/src/Settings.php
+++ b/src/Settings.php
@@ -4,6 +4,8 @@
use App\Entity\Contact;
use App\Entity\Setting;
+use App\Entity\User;
+use App\Entity\UserGroup;
use App\Repository\ContactRepository;
use App\Repository\SettingRepository;
use Doctrine\ORM\EntityManagerInterface;
@@ -126,6 +128,11 @@ public function getMainContact(): ?Contact
return $this->mainContact;
}
+ public function defaultGroup(): int
+ {
+ return (int)($this->getData()['default_group'] ?? UserGroup::PUBLIC);
+ }
+
public function getSiteJs(): string
{
return $this->getData()['site_js'] ?? '';
diff --git a/templates/post/form.html.twig b/templates/post/form.html.twig
index 51ca4fe..0de30af 100644
--- a/templates/post/form.html.twig
+++ b/templates/post/form.html.twig
@@ -59,7 +59,7 @@
diff --git a/templates/setting/index.html.twig b/templates/setting/index.html.twig
index 2d2b06a..1fe64b2 100644
--- a/templates/setting/index.html.twig
+++ b/templates/setting/index.html.twig
@@ -18,8 +18,18 @@
-
-
+
+
+
+
+
+
+
+
diff --git a/tests/Controller/MapControllerTest.php b/tests/Controller/MapControllerTest.php
index 4ce51db..604c309 100644
--- a/tests/Controller/MapControllerTest.php
+++ b/tests/Controller/MapControllerTest.php
@@ -2,8 +2,8 @@
namespace App\Tests;
-use App\Tests\Controller\ControllerTestBase;
use App\Repository\TrackPointRepository;
+use App\Tests\Controller\ControllerTestBase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
class MapControllerTest extends ControllerTestBase
diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml
index 3a01553..a243c3e 100644
--- a/translations/messages.en.yaml
+++ b/translations/messages.en.yaml
@@ -28,6 +28,7 @@ posts:
is_family: 'Famiy'
settings:
title: 'Site Settings'
+ default_group: 'Default group:'
location_legend: 'GPS tracking'
overland_key: 'Overland key:'
overland_help: "Enter the following URL in %overland_link% as the 'Receiver Endpoint':"