Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ security:
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/task/*, roles: ROLE_USER }
- { path: ^/project/*, roles: ROLE_USER }

# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
98 changes: 98 additions & 0 deletions src/Controller/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Controller;

use App\Entity\Project;
use App\Entity\Task;
use App\Entity\User;
use App\Type\ProjectType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProjectController extends AbstractController
{
/**
* @Route("/project/list", name="project_list")
*/
public function index(): Response
{
$projects = $this->getDoctrine()
->getManager()
->getRepository(Project::class)
->getAvailableProject($this->getUser()->getId(),$this->isGranted('ROLE_ADMIN'));

return $this->render('project/index.html.twig', [
'projects' => $projects
]);
}

/**
* @Route("/project/create/form", name="project_create_form")
*/
public function createProjectForm(Request $request): Response
{
$project = new Project();
$form = $this->createForm(ProjectType::class, $project);
$form->handleRequest($request);

if($form->isSubmitted() && $form->isValid())
{
$project->setAuthor($this->getUser());
$project->setToken();
$this->getDoctrine()->getManager()->persist($project);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute("project_list");
}

return $this->render('project/create_project_form.html.twig', [
'form' => $form->createView()
]);
}

/**
* @Route("/project/{id}", name="project_byId")
*/
public function projectById($id): Response
{
$project = $this->getDoctrine()->getManager()->find(Project::class, $id);
if($project === null)
throw $this->createNotFoundException("Project with %s not found", $id);

$this->denyAccessUnlessGranted('project_view', $project);


$tasks = $this->getDoctrine()->getRepository(Task::class)
->findBy(['project' => $project]);
return $this->render('project/project.html.twig', [
'project' => $project,
'tasks' => $tasks
]);
}

/**
* @Route("/project/{id}/edit", name="project_byId_edit")
*/
public function projectEdit($id, Request $request): Response
{
$project = $this->getDoctrine()->getManager()->find(Project::class, $id);
if($project === null)
throw $this->createNotFoundException("Project with %s not found", $id);
$this->denyAccessUnlessGranted('project_edit', $project);

$form = $this->createForm(ProjectType::class, $project);
$form->handleRequest($request);

if($form->isSubmitted() && $form->isValid())
{
$this->getDoctrine()->getManager()->persist($project);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute("project_list");
}

return $this->render('project/project_edit_form.html.twig', [
'form' => $form->createView()
]);
}
}
36 changes: 17 additions & 19 deletions src/Controller/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class TaskController extends AbstractController
public function create(Request $request): Response
{
$task = new Task();
$form = $this->createForm(TaskType::class, $task);
$option = ['userId' => $this->getUser()->getId(), 'hasAdmin' => $this->isGranted('ROLE_ADMIN')];
$form = $this->createForm(TaskType::class, $task, $option);

$form->handleRequest($request);

Expand Down Expand Up @@ -53,24 +54,14 @@ public function list(Request $request): Response
$taskFilterForm->handleRequest($request);

if ($taskFilterForm->isSubmitted() && $taskFilterForm->isValid()) {

$filter = $taskFilterForm->getData();
if ($filter['isCompleted'] === null) {
unset($filter['isCompleted']);
}

$tasks = $this->getDoctrine()->getRepository(Task::class)
->findBy($filter, [
'dueDate' => 'DESC'
]);

$tasks = $this->getDoctrine()
->getRepository(Task::class)
->getAvaiableTaskWithFilter($this->getUser()->getId(), $filter, $this->isGranted('ROLE_ADMIN'));
} else {
/** @var $tasks */
$tasks = $this->getDoctrine()->getManager()
$tasks = $this->getDoctrine()
->getRepository(Task::class)
->findBy([], [
'dueDate' => 'DESC'
]);
->getAvaiableTask($this->getUser()->getId(), $this->isGranted('ROLE_ADMIN'));
}


Expand All @@ -91,13 +82,20 @@ public function complete($id): Response
/** @var Task $task */
$task = $this->getDoctrine()->getManager()->find(Task::class, $id);

$this->denyAccessUnlessGranted('complete', $task);

if ($task === null) {
throw $this->createNotFoundException(sprintf("Task with id %s not found", $id));
}
$this->denyAccessUnlessGranted('complete', $task);

if($task->isCompleted())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше вынести в метод сущности, например, $task->toggleComplete()
$this->complete = $this->complete ? false : true;

{
$task->setIsCompleted();
}
else
{
$task->setIsCompleted(true);
}

$task->setIsCompleted(true);

$this->getDoctrine()->getManager()->persist($task);
$this->getDoctrine()->getManager()->flush();
Expand Down
92 changes: 92 additions & 0 deletions src/Entity/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Entity;

use App\Entity\User;
use App\Repository\ProjectRepository;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass=ProjectRepository::class)
*/
class Project
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=5)
*/
private $token;

/**
* @ORM\Column(type="string", length=255)
*/
private $name;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="user")
* @var User
*/
private $author;

public function getId(): ?int
{
return $this->id;
}

public function getToken(): ?string
{
return $this->token;
}

public function setToken(string $token = null): self
{
if ($token == null)
{
$this->token = $this->generateToken();
return $this;
}
$this->token = $token;
return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

/**
* @return User
*/
public function getAuthor(): user
{
return $this->author;
}

/**
* @param User $user
*/
public function setAuthor(user $author): void
{
$this->author = $author;
}



private function generateToken($length = 5): ?string
{
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
}
27 changes: 24 additions & 3 deletions src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Task

/**
* @ORM\Column(type="boolean", nullable=false, options={"default" : 0})
* @var boolean
* @var bool
*/
protected $isCompleted = false;

Expand All @@ -55,6 +55,12 @@ class Task
*/
protected $author;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="project")
* @var Project
*/
protected $project;

/**
* Create empty task
*/
Expand Down Expand Up @@ -85,6 +91,22 @@ public function getAuthor()
return $this->author;
}

/**
* @return Project|null
*/
public function getProject(): ?Project
{
return $this->project;
}

/**
* @param Project $project
*/
public function setProject(Project $project = null)
{
$this->project = $project;
}


/**
* @return mixed
Expand Down Expand Up @@ -148,7 +170,7 @@ public function setDueDate($dueDate): void
*/
public function isCompleted() : bool
{
return (boolean) $this->isCompleted;
return $this->isCompleted;
}

/**
Expand All @@ -163,5 +185,4 @@ public function setIsCompleted(bool $isCompleted = false)




}
Loading