Skip to content

Commit

Permalink
Add previous and next links to posts (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Aug 28, 2021
1 parent c2615f7 commit 4ae5c14
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
fi
echo DATABASE_URL=$DATABASE_URL > .env.test.local
sudo apt-get update -q
sudo apt-get --fix-broken install
sudo apt-get install -y exiftool
composer install
./bin/console doctrine:migrations:migrate
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function editPost(
'contacts' => $contactRepository->findBy([], ['name' => 'ASC']),
'max_filesize' => UploadedFile::getMaxFilesize(),
'user_groups' => $userGroupRepository->findAll(),
'prev_post' => $postRepository->findPrevByDate($post, $this->getUser()),
'next_post' => $postRepository->findNextByDate($post, $this->getUser()),
]);
}

Expand Down Expand Up @@ -285,6 +287,8 @@ public function viewPost($id, PostRepository $postRepository)
}
return $this->render('post/view.html.twig', [
'post' => $post,
'prev_post' => $postRepository->findPrevByDate($post, $this->getUser()),
'next_post' => $postRepository->findNextByDate($post, $this->getUser()),
]);
}

Expand Down
46 changes: 46 additions & 0 deletions src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use DateTimeZone;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\OrderBy;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
Expand Down Expand Up @@ -162,6 +164,50 @@ public function getMonths($year, ?User $user = null): array
return $months;
}

public function findPrevByDate(Post $post, ?User $user = null): ?Post
{
$groupList = $user ? $user->getGroupIdList() : false;
if (!$groupList) {
$groupList = UserGroup::PUBLIC;
}
$qb = $this->createQueryBuilder('p');
$orderBy = new OrderBy();
$orderBy->add('p.date', 'DESC');
$orderBy->add('p.id', 'DESC');
$out = $qb
->andWhere('p.view_group IN (' . $groupList . ')')
->andWhere($qb->expr()->orX('p.date < :date', 'p.date = :date AND p.id < :id'))
->setParameter('date', $post->getDate())
->setParameter('id', $post->getId())
->orderBy($orderBy)
->setMaxResults(1)
->getQuery()
->getResult();
return $out[0] ?? null;
}

public function findNextByDate(Post $post, ?User $user = null): ?Post
{
$groupList = $user ? $user->getGroupIdList() : false;
if (!$groupList) {
$groupList = UserGroup::PUBLIC;
}
$qb = $this->createQueryBuilder('p');
$orderBy = new OrderBy();
$orderBy->add('p.date', 'ASC');
$orderBy->add('p.id', 'ASC');
$out = $qb
->andWhere('p.view_group IN (' . $groupList . ')')
->andWhere($qb->expr()->orX('p.date > :date', 'p.date = :date AND p.id > :id'))
->setParameter('date', $post->getDate())
->setParameter('id', $post->getId())
->orderBy($orderBy)
->setMaxResults(1)
->getQuery()
->getResult();
return $out[0] ?? null;
}

public function findByDate($year, $month, User $user = null, int $pageNum = 1)
{
$pageSize = 10;
Expand Down
17 changes: 17 additions & 0 deletions templates/post/view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
<span class="url">URL: <a href="{{ post.url }}">{{ post.url }}</a></span>
{% endif %}
</p>
<nav>
<span class="prev-post">
{% if prev_post %}
<a rel="prev" href="{{ path('post_view', {id:prev_post.id}) }}">&larr; Previous</a>
{% else %}
&larr; Previous
{% endif %}
</span>
&middot;
<span class="next-post">
{% if next_post %}
<a rel="prev" href="{{ path('post_view', {id:next_post.id}) }}">Next &rarr;</a>
{% else %}
Next &rarr;
{% endif %}
</span>
</nav>

{% if is_granted('ROLE_ADMIN') %}
<p>
Expand Down
39 changes: 33 additions & 6 deletions tests/Repository/PostRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Repository\UserRepository;
use App\Tests\Repository\RepositoryTestBase;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -29,6 +30,17 @@ protected function setUp(): void
$this->postRepo = self::$container->get(PostRepository::class);
}

private function getTestPost(): Post
{
$post = $this->postRepo->createNew();
$this->entityManager->persist($post);
$author = new Contact();
$author->setName('Bob');
$this->entityManager->persist($author);
$post->setAuthor($author);
return $post;
}

public function testAuthor()
{
// Test author.
Expand Down Expand Up @@ -116,12 +128,7 @@ public function provideSaveFromRequest()

public function testFile()
{
$post = $this->postRepo->createNew();
$this->entityManager->persist($post);
$author = new Contact();
$author->setName('Bob');
$this->entityManager->persist($author);
$post->setAuthor($author);
$post = $this->getTestPost();

$file = new File();
$file->setSize(100);
Expand Down Expand Up @@ -192,4 +199,24 @@ public function testAbortedSave()
}
$this->assertNull($post2->getId());
}

public function testNextAndPrevPosts()
{
$post1 = $this->getTestPost();
$post2 = $this->getTestPost();
$post3 = $this->getTestPost();
$post1->setDate(new DateTime('2021-08-24 12:00:00'));
$post2->setDate(new DateTime('2021-08-24 13:00:00'));
$post3->setDate(new DateTime('2021-08-24 13:00:00'));
$this->entityManager->persist($post1);
$this->entityManager->persist($post2);
$this->entityManager->persist($post3);
$this->entityManager->flush();
$this->assertSame($post2, $this->postRepo->findNextByDate($post1));
$this->assertSame($post3, $this->postRepo->findNextByDate($post2));
$this->assertNull($this->postRepo->findNextByDate($post3));
$this->assertNull($this->postRepo->findPrevByDate($post1));
$this->assertSame($post1, $this->postRepo->findPrevByDate($post2));
$this->assertSame($post2, $this->postRepo->findPrevByDate($post3));
}
}

0 comments on commit 4ae5c14

Please sign in to comment.