Skip to content

Commit d1aa09f

Browse files
committed
Added pastes list view
1 parent 0493221 commit d1aa09f

File tree

8 files changed

+175
-4
lines changed

8 files changed

+175
-4
lines changed

public/css/main.css

+29-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/main.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/main.scss

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@import "utils";
22
@import "fonts/fonts.css";
33

4+
/* Root view */
45
form {
56
h1 {
67
margin-top: 10px;
@@ -119,6 +120,7 @@ form {
119120
}
120121
}
121122

123+
/* Paste view */
122124
#paste {
123125
margin-left: 4vw;
124126

@@ -174,3 +176,34 @@ form {
174176
}
175177
}
176178
}
179+
180+
/* Pastes list view */
181+
182+
#removed {
183+
color: #C03;
184+
font-size: 25px;
185+
text-align: center;
186+
margin-top: 5px;
187+
margin-bottom: 5px;
188+
}
189+
190+
table {
191+
width: 100%;
192+
193+
th {
194+
color: #C03;
195+
}
196+
197+
a {
198+
color: blue;
199+
}
200+
.remove::before {
201+
@extend .icon;
202+
content: '\f2ed';
203+
color: black;
204+
}
205+
206+
tr {
207+
text-align: center;
208+
}
209+
}

src/Controller/PanelController.php

+43
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App\Controller;
44

55
use App\Repository\PasteRepository;
6+
use Doctrine\ORM\EntityManagerInterface;
67
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
78
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\HttpFoundation\Request;
810
use Symfony\Component\Routing\Annotation\Route;
911

1012
/**
@@ -21,4 +23,45 @@ public function root(PasteRepository $pasteRepository) {
2123
'sidebar' => $pasteRepository->getSidebar()
2224
]);
2325
}
26+
27+
/**
28+
* @IsGranted("ROLE_USER")
29+
* @Route("/pastes", name="app_pastes")
30+
*/
31+
public function pastes(Request $request, PasteRepository $pasteRepository) {
32+
$user = $this->get('security.token_storage')->getToken()->getUser();
33+
34+
return $this->render('panel/pastes.html.twig', [
35+
'sidebar' => $pasteRepository->getSidebar(),
36+
'pastes' => $pasteRepository->findUserPastes($user),
37+
'removed' => $request->get('removed')
38+
]);
39+
}
40+
41+
/**
42+
* @IsGranted("ROLE_USER")
43+
* @Route("/delete/{name}", name="app_delete")
44+
*/
45+
public function delete(string $name, PasteRepository $pasteRepository, EntityManagerInterface $entityManager) {
46+
$paste = $pasteRepository->findOneByName($name);
47+
$user = $this->get('security.token_storage')->getToken()->getUser();
48+
49+
if ($paste == null) {
50+
return $this->redirectToRoute('app_pastes', [
51+
'removed' => false
52+
]);
53+
}
54+
55+
if ($paste->getOwner() != $user->getId()) {
56+
return $this->redirectToRoute('app_pastes', [
57+
'removed' => false,
58+
]);
59+
}
60+
61+
$entityManager->remove($paste);
62+
$entityManager->flush();
63+
return $this->redirectToRoute('app_pastes', [
64+
'removed' => true
65+
]);
66+
}
2467
}

src/Controller/RootController.php

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function root(Request $request, PasteRepository $pasteRepository, EntityM
4545
}
4646

4747
$user = $this->get('security.token_storage')->getToken()->getUser();
48+
if ($paste->getVisibility() == 3 && $user == 'anon.') {
49+
$form->addError(new FormError('You must be logged to create private paste'));
50+
return $this->render('index.html.twig', [
51+
'form' => $form->createView(),
52+
'recaptcha' => getenv('RECAPTCHA_SITEKEY'),
53+
'sidebar' => $pasteRepository->getSidebar()
54+
]);
55+
}
56+
4857
$paste->setOwner($user == 'anon.' ? null : $user->getId());
4958
$paste->setUploadDate(new DateTime());
5059
$paste->setContent(base64_encode(str_replace(array("\r\n", "\r", "\n"), "[new-line]", $paste->getContent())));

src/Repository/PasteRepository.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Repository;
44

55
use App\Entity\Paste;
6+
use App\Entity\User;
67
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
78
use Symfony\Bridge\Doctrine\RegistryInterface;
89

@@ -25,7 +26,15 @@ public function findOneByName($value): ?Paste {
2526
->getOneOrNullResult();
2627
}
2728

28-
public function getSidebar(): array {
29+
public function findUserPastes(User $user) {
30+
return $this->createQueryBuilder('p')
31+
->andWhere('p.owner = :owner')
32+
->setParameter('owner', $user->getId())
33+
->getQuery()
34+
->getResult();
35+
}
36+
37+
public function getSidebar() {
2938
return $this->createQueryBuilder('p')
3039
->andWhere('p.visibility = 1')
3140
->setMaxResults(8)

templates/base.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</div>
2020

2121
<div id="account">
22-
<a href="#"><i class="fas fa-paste"></i></a>
22+
<a href="{{ path('app_pastes') }}"><i class="fas fa-paste"></i></a>
2323
<a href="{{ path('app_panel') }}"><i class="fas fa-cog"></i></a>
2424

2525
{% if app.user %}

templates/panel/pastes.html.twig

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{% extends 'base.html.twig' %}
2+
{% block head %}
3+
<link rel="stylesheet" href="{{ asset('css/main.css') }}" />
4+
<title>Pastes list</title>
5+
{% endblock %}
6+
7+
{% block body %}
8+
{% if removed == true %}
9+
<h1 id="removed">Paste has been removed</h1>
10+
{% endif %}
11+
<table>
12+
13+
<tr>
14+
<th>Title</th>
15+
<th>Upload date</th>
16+
<th>Visibility</th>
17+
<th>Syntax</th>
18+
<th>Actions</th>
19+
</tr>
20+
{% for paste in pastes %}
21+
<tr>
22+
<td class="title">
23+
<a href="{{ path('app_paste', {name: paste.name}) }}">
24+
{% if paste.title != null %}
25+
{{ paste.title }}
26+
{% else %}
27+
Untitled
28+
{% endif %}
29+
</a>
30+
</td>
31+
32+
<td>{{ paste.uploadDate.format('d M Y G:i') }}</td>
33+
34+
<td>
35+
{% if paste.visibility == 1 %}
36+
Public
37+
{% elseif paste.visibility == 2 %}
38+
Unlisted
39+
{% else %}
40+
Private
41+
{% endif %}
42+
</td>
43+
44+
<td>{{ paste.syntax }}</td>
45+
<td><a class="remove" href="{{ path('app_delete', {name: paste.name}) }}"></a></td>
46+
</tr>
47+
{% endfor %}
48+
</table>
49+
{% endblock %}

0 commit comments

Comments
 (0)