fix lists in readme
[mixstore.git] / src / Mixstore / StoreBundle / Controller / PackageController.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\Language;
8use Mixstore\StoreBundle\Entity\PackageRepository;
9use Mixstore\StoreBundle\Entity\Usecase;
10use Mixstore\StoreBundle\Entity\UsecaseRepository;
11
12class PackageController extends Controller
13{
14 public function viewAction($id)
15 {
16 $em = $this
17 ->getDoctrine()
18 ->getManager();
19
20 $package = $em
21 ->getRepository('MixstoreStoreBundle:Package')
22 ->find($id);
23
24 //pass usecases count + rating (to link with feedbacks)
25 $countAndRating = $em
26 ->getRepository('MixstoreStoreBundle:Usecase')
27 ->countByPkgId($id);
28 if ($countAndRating['rating'] == '')
29 $countAndRating['rating'] = 'NA';
30 $package->rating = $countAndRating['rating'];
31 $package->ucscount = $countAndRating['ucscount'];
32
33 $user = $this->getUser();
34 $owner = (!is_null($user) && $package->getUser()->getId() == $user->getId());
35
36 return $this->render('MixstoreStoreBundle:Package:view.html.twig', array(
37 'pkg' => $package,
38 'owner' => $owner,
39 ));
40 }
41
42 public function listAction()
43 {
44 $em = $this
45 ->getDoctrine()
46 ->getManager();
47
48 $packages = $em
49 ->getRepository('MixstoreStoreBundle:Package')
50 ->findAll();
51
52 $ucsRepository = $em
53 ->getRepository('MixstoreStoreBundle:Usecase');
54
55 for ($i=0; $i<count($packages); $i++)
56 {
57 $countAndRating = $ucsRepository->countByPkgId($packages[$i]->getId());
58 if ($countAndRating['rating'] == '')
59 $countAndRating['rating'] = 'NA';
60 $packages[$i]->ucscount = $countAndRating['ucscount'];
61 $packages[$i]->rating = $countAndRating['rating'];
62 }
63
64 return $this->render('MixstoreStoreBundle:Package:list.html.twig', array(
65 'packages' => $packages,
66 ));
67 }
68
69 public function upsertAction($id)
70 {
71 $em = $this
72 ->getDoctrine()
73 ->getManager();
74
75 $package = $em
76 ->getRepository('MixstoreStoreBundle:Package')
77 ->safeFindById($id);
78
79 //TODO: place this at another location to avoid code redundancy
80 $user = $this->getUser();
81 if (is_null($user) || (
82 !in_array('ROLE_ADMIN', $user->getRoles()) &&
83 !is_null($package->getUser()) &&
84 $package->getUser()->getId() != $this->getUser()->getId()))
85 {
86 //TODO: nice error page
87 return $this->redirect($this->generateUrl('mixstore_static_home'));
88 }
89
90 $formBuilder = $this->createFormBuilder($package);
91 $formBuilder
92 ->add('name', 'text')
93 ->add('headline', 'text')
94 ->add('url', 'text')
95 ->add('description', 'textarea', array('required' => false))
96 ->add('dependencies', 'text', array('required' => false))
97 ->add('authors', 'text')
98 ->add('contact', 'text')
99 ->add('language', 'entity', array(
100 'class' => 'MixstoreStoreBundle:Language',
101 'property' => 'name',
102 'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
103 return $er->createQueryBuilder('l')
104 ->orderBy('l.name', 'ASC');
105 }))
106 ->add('bannerfile', 'file', array('label' => 'Banner (1000x300)', 'required' => false))
107 ->add('removebanner', 'checkbox', array('label' => 'Remove banner', 'required' => false));
108 $form = $formBuilder->getForm();
109
110 $request = $this->get('request');
111 if ($request->getMethod() == 'POST') {
112 // Link request <-> form ; $package contains data entered in the form
113 $form->bind($request);
114 if ($form->isValid()) {
115 $package->setUser($this->getUser());
116 $package->setModified(new \DateTime('now'));
117
118 //HACK: text editor insert extra <p></p><br> tags for an empty description
119 $description = $package->getDescription();
120 if (trim(str_replace('<br>','', str_replace('<p>','', str_replace('</p>','', $description)))) == '')
121 $package->setDescription('');
122
123 $bannerfile = $form['bannerfile']->getData();
124 $extension = (is_null($bannerfile) ? null : $bannerfile->guessExtension());
125
126 $bannersDir = $this->get('kernel')->getRootDir().'/../web/mixstore/images/pkg_banners';
127
128 if (!is_null($extension) && ($extension == 'jpeg' || $extension == 'png'))
129 {
130 if (is_null($package->getBannerPath()))
131 {
132 $filename = uniqid(rand(), true);
133 $package->setBannerpath($filename);
134 }
135 else
136 $filename = $package->getBannerpath();
137 $bannerfile->move($bannersDir, $filename);
138
139 //HACK: currently hard-coded
140 $newwidth = 1000;
141 $newheight = 300;
142
143 $source = ($extension == 'jpeg'
144 ? imagecreatefromjpeg($bannersDir.'/'.$filename)
145 : imagecreatefrompng($bannersDir.'/'.$filename));
146
147 $resizedFile = imagecreatetruecolor($newwidth, $newheight);
148 list($width, $height) = getimagesize($bannersDir.'/'.$filename);
149 imagecopyresized($resizedFile, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
150
151 ($extension == 'jpeg'
152 ? imagejpeg($resizedFile, $bannersDir.'/'.$filename)
153 : imagepng($resizedFile, $bannersDir.'/'.$filename));
154
155 imagedestroy($resizedFile);
156 }
157 else if ($package->removebanner == TRUE)
158 {
159 unlink($bannersDir.'/'.$package->getBannerpath());
160 $package->setBannerpath(null);
161 }
162
163 $em->persist($package);
164 $em->flush();
165 return $this->redirect($this->generateUrl('mixstore_store_package_view', array('id' => $package->getId())));
166 }
167 }
168
169 //Email notifications [temporary ??!!]
170 $users = $em
171 ->getRepository('MixstoreUserBundle:User')
172 ->findAll();
173 $receivers = array();
174 foreach ($users as $user)
175 {
176 if ($user->getEmailnotif1() || ($id <= 0 && $user->getEmailNotif0()))
177 $receivers[] = $user->getEmail();
178 }
179 if (count($receivers) > 0)
180 {
181 $message = \Swift_Message::newInstance()
182 ->setSubject('Package '.($id > 0 ? 'update' : 'creation'))
183 ->setFrom('contact@mixstore.org')
184 ->setTo($receivers)
185 ->setBody($this->renderView('MixstoreStoreBundle::upsert-email.txt.twig', array(
186 'name' => $this->getUser()->getName(),
187 'surname' => $this->getUser()->getSurname(),
188 'email' => $this->getUser()->getEmail(),
189 'type' => 'Package',
190 'id' => $package->getId(),
191 )));
192 $this->get('mailer')->send($message);
193 }
194
195 return $this->render('MixstoreStoreBundle::upsert.html.twig', array(
196 'title' => ($id > 0 ? 'Edit package' : 'New package'),
197 'form' => $form->createView(),
198 ));
199 }
200
201 public function deleteAction($id)
202 {
203 $em = $this->getDoctrine()->getManager();
204
205 $package = $em
206 ->getRepository('MixstoreStoreBundle:Package')
207 ->findById($id)[0];
208
209 $user = $this->getUser();
210 if (is_null($user) || (
211 !in_array('ROLE_ADMIN', $user->getRoles()) &&
212 $package->getUser()->getId() != $this->getUser()->getId()))
213 {
214 //TODO: nice error page
215 return $this->redirect($this->generateUrl('mixstore_static_home'));
216 }
217
218 $em->remove($package);
219 $em->flush();
220 return $this->redirect($this->generateUrl('mixstore_user_board'));
221 }
222}