Skip to content

Commit

Permalink
Add default group setting
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson authored Aug 14, 2023
1 parent 703c875 commit 22416a3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')));
}
Expand Down
10 changes: 6 additions & 4 deletions src/Controller/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
]);
}

Expand Down
23 changes: 5 additions & 18 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Entity;

use App\Repository\PostRepository;
use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
18 changes: 16 additions & 2 deletions src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -42,20 +43,25 @@ 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;
$this->tagRepository = $tagRepository;
$this->fileRepository = $fileRepository;
$this->syndicationRepository = $syndicationRepository;
$this->userGroupRepository = $userGroupRepository;
$this->settings = $settings;
}

private function createQueryBuilderForPosts(?User $user = null): QueryBuilder
Expand All @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ?? '';
Expand Down
2 changes: 1 addition & 1 deletion templates/post/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<label for="view_group">Viewers:</label>
<select name="view_group" id="view_group" tabindex="50">
{% for g in user_groups %}
<option value="{{ g.id }}" {% if post.isSelectedGroup(g) %}selected{% endif %}>{{ g.name }}</option>
<option value="{{ g.id }}" {% if post.getViewGroup.id == g.id %}selected{% endif %}>{{ g.name }}</option>
{% endfor %}
</select>
</span>
Expand Down
14 changes: 12 additions & 2 deletions templates/setting/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@
</p>

<p class="fields">
<label for="user_registrations">Allow user registrations?</label>
<input id="user_registrations" type="checkbox" {% if settings.userRegistrations %}checked{% endif %} name="settings[user_registrations]" />
<span class="field size-1">
<label for="user_registrations">Allow user registrations?</label>
<input id="user_registrations" type="checkbox" {% if settings.userRegistrations %}checked{% endif %} name="settings[user_registrations]" />
</span>
<span class="field size-4">
<label for="default_group">{{ 'settings.default_group'|trans }}</label>
<select id="default_group" name="settings[default_group]">
{% for group in user_groups %}
<option value="{{ group.id }}" {% if group.id == settings.defaultGroup %}selected{% endif %}>{{ group.name }}</option>
{% endfor %}
</select>
</span>
</p>

<p class="fields">
Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/MapControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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':"
Expand Down

0 comments on commit 22416a3

Please sign in to comment.