fix lists in readme
[mixstore.git] / src / Mixstore / StoreBundle / Controller / UsecaseController.php
CommitLineData
929ca066
BA
1<?php
2
3namespace Mixstore\StoreBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Mixstore\StoreBundle\Entity\Package;
7use Mixstore\StoreBundle\Entity\PackageRepository;
8use Mixstore\StoreBundle\Entity\Usecase;
9
10class UsecaseController extends Controller
11{
12 public function viewAction($id)
13 {
14 $usecase = $this
15 ->getDoctrine()
16 ->getManager()
17 ->getRepository('MixstoreStoreBundle:Usecase')
18 ->find($id);
19
20 $user = $this->getUser();
21 $owner = (!is_null($user) && $usecase->getUser()->getId() == $user->getId());
22
23 return $this->render('MixstoreStoreBundle:Usecase:view.html.twig', array(
24 'ucs' => $usecase,
25 'owner' => $owner,
26 ));
27 }
28
29 public function listAction($pkgid)
30 {
31 $em = $this
32 ->getDoctrine()
33 ->getManager();
34
35 $pkgName = $em
36 ->getRepository('MixstoreStoreBundle:Package')
37 ->find($pkgid)
38 ->getName();
39
40 return $this->render('MixstoreStoreBundle:Usecase:list.html.twig', array(
41 'usecases' => $em
42 ->getRepository('MixstoreStoreBundle:Usecase')
43 ->findByPackage($pkgid),
44 'pkgname' => $pkgName,
45 ));
46 }
47
48 public function upsertAction($id, $pkgid)
49 {
50 $em = $this
51 ->getDoctrine()
52 ->getManager();
53
54 $usecase = $em
55 ->getRepository('MixstoreStoreBundle:Usecase')
56 ->safeFindById($id);
57
58 //TODO: place this in an external service (used at several locations)
59 $user = $this->getUser();
60 if (is_null($user) || (
61 !in_array('ROLE_ADMIN', $user->getRoles()) &&
62 !is_null($usecase->getUser()) &&
63 $usecase->getUser()->getId() != $this->getUser()->getId()))
64 {
65 //TODO: nice error page
66 return $this->redirect($this->generateUrl('mixstore_static_home'));
67 }
68
69 //http://symfony.com/doc/current/reference/forms/types/entity.html
70 if ($pkgid > 0)
71 {
72 $package = $em
73 ->getRepository('MixstoreStoreBundle:Package')
74 ->findById($pkgid);
75 }
76
77 $formBuilder = $this->createFormBuilder($usecase);
78 if ($pkgid > 0)
79 {
80 $formBuilder
81 ->add('package', 'entity', array(
82 'class' => 'MixstoreStoreBundle:Package',
83 'choices' => $package,
84 'property' => 'name',
85 'read_only' => true));
86 }
87 else
88 {
89 $formBuilder
90 ->add('package', 'entity', array(
91 'class' => 'MixstoreStoreBundle:Package',
92 'property' => 'name'));
93 }
94 $formBuilder
95 ->add('institution', 'text')
96 ->add('headline', 'text')
97 ->add('description', 'textarea')
98 ->add('contact', 'text')
99 ->add('grade', 'choice', array('choices' => range(0, 10)));
100 $form = $formBuilder->getForm();
101
102 $request = $this->get('request');
103 if ($request->getMethod() == 'POST') {
104 // Link request <-> form ; $usecase contains data entered in the form
105 $form->bind($request);
106 if ($form->isValid()) {
107 $usecase->setUser($this->getUser());
108 $usecase->setModified(new \DateTime('now'));
109 $em->persist($usecase);
110 $em->flush();
111 return $this->redirect($this->generateUrl('mixstore_store_usecase_view', array('id' => $usecase->getId())));
112 }
113 }
114
115 //Email notifications [temporary ??!!]
116 $users = $em
117 ->getRepository('MixstoreUserBundle:User')
118 ->findAll();
119 $receivers = array();
120 foreach ($users as $user)
121 {
122 if ($user->getEmailnotif1() || ($id <= 0 && $user->getEmailNotif0()))
123 $receivers[] = $user->getEmail();
124 }
125 if (count($receivers) > 0)
126 {
127 $message = \Swift_Message::newInstance()
128 ->setSubject('Usecase '.($id > 0 ? 'update' : 'creation'))
129 ->setFrom('contact@mixstore.org')
130 ->setTo($receivers)
131 ->setBody($this->renderView('MixstoreStoreBundle::upsert-email.txt.twig', array(
132 'name' => $this->getUser()->getName(),
133 'surname' => $this->getUser()->getSurname(),
134 'email' => $this->getUser()->getEmail(),
135 'type' => 'Usecase',
136 'id' => $usecase->getId(),
137 )));
138 $this->get('mailer')->send($message);
139 }
140
141 return $this->render('MixstoreStoreBundle::upsert.html.twig', array(
142 'title' => ($id > 0 ? 'Edit usecase' : 'New usecase'),
143 'form' => $form->createView(),
144 ));
145 }
146
147 public function deleteAction($id)
148 {
149 $usecase = $this
150 ->getDoctrine()
151 ->getManager()
152 ->getRepository('MixstoreStoreBundle:Usecase')
153 ->findById($id)[0];
154
155 $user = $this->getUser();
156 if (is_null($user) || (
157 !in_array('ROLE_ADMIN', $user->getRoles()) &&
158 $usecase->getUser()->getId() != $this->getUser()->getId()))
159 {
160 //TODO: nice error page
161 return $this->redirect($this->generateUrl('mixstore_static_home'));
162 }
163
164 $em = $this->getDoctrine()->getManager();
165 $em->remove($usecase);
166 $em->flush();
167 return $this->redirect($this->generateUrl('mixstore_user_board'));
168 }
169}