Allow to add a part to a project from a parts info page

This commit is contained in:
Jan Böhmer 2022-12-29 13:15:26 +01:00
parent 15677937a2
commit 3f82d0967a
4 changed files with 42 additions and 10 deletions

View file

@ -29,11 +29,13 @@ use App\Form\Type\StructuralEntityType;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Omines\DataTablesBundle\DataTableFactory; use Omines\DataTablesBundle\DataTableFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\NotNull;
/** /**
* @Route("/project") * @Route("/project")
@ -48,7 +50,7 @@ class ProjectController extends AbstractController
} }
/** /**
* @Route("/{id}", name="project_info") * @Route("/{id}", name="project_info", requirements={"id"="\d+"})
*/ */
public function info(Project $project, Request $request) public function info(Project $project, Request $request)
{ {
@ -68,20 +70,28 @@ class ProjectController extends AbstractController
} }
/** /**
* @Route("/{id}/add_parts", name="project_add_parts") * @Route("/add_parts", name="project_add_parts_no_id")
* @Route("/{id}/add_parts", name="project_add_parts", requirements={"id"="\d+"})
* @param Request $request * @param Request $request
* @param Project|null $project * @param Project|null $project
*/ */
public function addPart(Request $request, Project $project, EntityManagerInterface $entityManager): Response public function addPart(Request $request, EntityManagerInterface $entityManager, ?Project $project): Response
{ {
$this->denyAccessUnlessGranted('edit', $project); if($project) {
$this->denyAccessUnlessGranted('edit', $project);
} else {
$this->denyAccessUnlessGranted('@devices.edit');
}
$builder = $this->createFormBuilder(); $builder = $this->createFormBuilder();
$builder->add('project', StructuralEntityType::class, [ $builder->add('project', StructuralEntityType::class, [
'class' => Project::class, 'class' => Project::class,
'required' => true, 'required' => true,
'disabled' => true, 'disabled' => $project !== null, //If a project is given, disable the field
'data' => $project, 'data' => $project,
'constraints' => [
new NotNull()
]
]); ]);
$builder->add('bom_entries', ProjectBOMEntryCollectionType::class); $builder->add('bom_entries', ProjectBOMEntryCollectionType::class);
$builder->add('submit', SubmitType::class, ['label' => 'save']); $builder->add('submit', SubmitType::class, ['label' => 'save']);
@ -111,10 +121,15 @@ class ProjectController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$target_project = $project ?? $form->get('project')->getData();
//Ensure that we really have acces to the selected project
$this->denyAccessUnlessGranted('edit', $target_project);
$data = $form->getData(); $data = $form->getData();
$bom_entries = $data['bom_entries']; $bom_entries = $data['bom_entries'];
foreach ($bom_entries as $bom_entry){ foreach ($bom_entries as $bom_entry){
$project->addBOMEntry($bom_entry); $target_project->addBOMEntry($bom_entry);
} }
$entityManager->flush(); $entityManager->flush();
@ -123,7 +138,7 @@ class ProjectController extends AbstractController
return $this->redirect($request->query->get('_redirect')); return $this->redirect($request->query->get('_redirect'));
} }
//Otherwise just show the project info page //Otherwise just show the project info page
return $this->redirectToRoute('project_info', ['id' => $project->getID()]); return $this->redirectToRoute('project_info', ['id' => $target_project->getID()]);
} }
return $this->renderForm('Projects/add_parts.html.twig', [ return $this->renderForm('Projects/add_parts.html.twig', [

View file

@ -25,6 +25,11 @@
{% endfor %}</td> {% endfor %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<a class="btn btn-success" {% if not is_granted('@devices.edit') %}disabled{% endif %}
href="{{ path('project_add_parts_no_id', {"parts": part.id, "_redirect": app.request.requestUri}) }}">
<i class="fa-solid fa-magnifying-glass-plus fa-fw"></i>
{% trans %}part.info.add_part_to_project{% endtrans %}
</a>

View file

@ -51,4 +51,10 @@
</div> </div>
</form> </form>
{{ dropdown.profile_dropdown('part', part.id) }} {{ dropdown.profile_dropdown('part', part.id) }}
<a class="btn btn-success mt-2" {% if not is_granted('@devices.edit') %}disabled{% endif %}
href="{{ path('project_add_parts_no_id', {"parts": part.id, "_redirect": app.request.requestUri}) }}">
<i class="fa-solid fa-magnifying-glass-plus fa-fw"></i>
{% trans %}part.info.add_part_to_project{% endtrans %}
</a>

View file

@ -10007,5 +10007,11 @@ Element 3</target>
<target>Add parts to project BOM</target> <target>Add parts to project BOM</target>
</segment> </segment>
</unit> </unit>
<unit id="02xC690" name="part.info.add_part_to_project">
<segment>
<source>part.info.add_part_to_project</source>
<target>Add this part to a project</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>