3 namespace Mixstore\StoreBundle\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 use Mixstore\StoreBundle\Entity\Package
;
7 use Mixstore\StoreBundle\Entity\Language
;
8 use Mixstore\StoreBundle\Entity\PackageRepository
;
9 use Mixstore\StoreBundle\Entity\Usecase
;
10 use Mixstore\StoreBundle\Entity\UsecaseRepository
;
12 class PackageController
extends Controller
14 public function viewAction($id)
21 ->getRepository('MixstoreStoreBundle:Package')
24 //pass usecases count + rating (to link with feedbacks)
26 ->getRepository('MixstoreStoreBundle:Usecase')
28 if ($countAndRating['rating'] == '')
29 $countAndRating['rating'] = 'NA';
30 $package->rating
= $countAndRating['rating'];
31 $package->ucscount
= $countAndRating['ucscount'];
33 $user = $this->getUser();
34 $owner = (!is_null($user) && $package->getUser()->getId() == $user->getId());
36 return $this->render('MixstoreStoreBundle:Package:view.html.twig', array(
42 public function listAction()
49 ->getRepository('MixstoreStoreBundle:Package')
53 ->getRepository('MixstoreStoreBundle:Usecase');
55 for ($i=0; $i<count($packages); $i++
)
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'];
64 return $this->render('MixstoreStoreBundle:Package:list.html.twig', array(
65 'packages' => $packages,
69 public function upsertAction($id)
76 ->getRepository('MixstoreStoreBundle:Package')
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()))
86 //TODO: nice error page
87 return $this->redirect($this->generateUrl('mixstore_static_home'));
90 $formBuilder = $this->createFormBuilder($package);
93 ->add('headline', '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');
106 ->add('bannerfile', 'file', array('label' => 'Banner (1000x300)', 'required' => false))
107 ->add('removebanner', 'checkbox', array('label' => 'Remove banner', 'required' => false));
108 $form = $formBuilder->getForm();
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'));
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('');
123 $bannerfile = $form['bannerfile']->getData();
124 $extension = (is_null($bannerfile) ? null : $bannerfile->guessExtension());
126 $bannersDir = $this->get('kernel')->getRootDir().'/../web/mixstore/images/pkg_banners';
128 if (!is_null($extension) && ($extension == 'jpeg' || $extension == 'png'))
130 if (is_null($package->getBannerPath()))
132 $filename = uniqid(rand(), true);
133 $package->setBannerpath($filename);
136 $filename = $package->getBannerpath();
137 $bannerfile->move($bannersDir, $filename);
139 //HACK: currently hard-coded
143 $source = ($extension == 'jpeg'
144 ? imagecreatefromjpeg($bannersDir.'/'.$filename)
145 : imagecreatefrompng($bannersDir.'/'.$filename));
147 $resizedFile = imagecreatetruecolor($newwidth, $newheight);
148 list($width, $height) = getimagesize($bannersDir.'/'.$filename);
149 imagecopyresized($resizedFile, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
151 ($extension == 'jpeg'
152 ? imagejpeg($resizedFile, $bannersDir.'/'.$filename)
153 : imagepng($resizedFile, $bannersDir.'/'.$filename));
155 imagedestroy($resizedFile);
157 else if ($package->removebanner
== TRUE)
159 unlink($bannersDir.'/'.$package->getBannerpath());
160 $package->setBannerpath(null);
163 $em->persist($package);
165 return $this->redirect($this->generateUrl('mixstore_store_package_view', array('id' => $package->getId())));
169 //Email notifications [temporary ??!!]
171 ->getRepository('MixstoreUserBundle:User')
173 $receivers = array();
174 foreach ($users as $user)
176 if ($user->getEmailnotif1() || ($id <= 0 && $user->getEmailNotif0()))
177 $receivers[] = $user->getEmail();
179 if (count($receivers) > 0)
181 $message = \Swift_Message
::newInstance()
182 ->setSubject('Package '.($id > 0 ? 'update' : 'creation'))
183 ->setFrom('contact@mixstore.org')
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(),
190 'id' => $package->getId(),
192 $this->get('mailer')->send($message);
195 return $this->render('MixstoreStoreBundle::upsert.html.twig', array(
196 'title' => ($id > 0 ? 'Edit package' : 'New package'),
197 'form' => $form->createView(),
201 public function deleteAction($id)
203 $em = $this->getDoctrine()->getManager();
206 ->getRepository('MixstoreStoreBundle:Package')
209 $user = $this->getUser();
210 if (is_null($user) || (
211 !in_array('ROLE_ADMIN', $user->getRoles()) &&
212 $package->getUser()->getId() != $this->getUser()->getId()))
214 //TODO: nice error page
215 return $this->redirect($this->generateUrl('mixstore_static_home'));
218 $em->remove($package);
220 return $this->redirect($this->generateUrl('mixstore_user_board'));