/**
  * @Route("/upload", name="upload_index")
  * @Template()
  */
 public function indexAction()
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     $directory = $this->getKernel()->getRootDir() . '/../web/uploads/users_files/' . $this->getUser()->getLogin();
     if (!file_exists($directory)) {
         mkdir($directory, 0777, true);
     }
     $iterator = new \DirectoryIterator($directory);
     $images = array();
     /** @var $file \SplFileInfo */
     foreach ($iterator as $file) {
         if ($file->isFile() && in_array($file->getExtension(), array('png', 'jpg', 'jpeg', 'gif', 'bmp'))) {
             $images[] = array('id' => substr(md5($file->getBasename()), 0, 10), 'name' => $file->getBasename());
         }
     }
     $form = $this->createFormBuilder()->add('file', 'file', array('required' => true))->getForm();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST' && $form->submit($request)->isValid()) {
         /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */
         $file = $form->getData()['file'];
         if (!in_array(pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION), array('png', 'jpg', 'jpeg', 'gif', 'bmp'))) {
             $this->get('session')->getFlashBag()->set('message', array('type' => 'error', 'message' => 'upload.main.index.error_type'));
             return $this->redirect($this->generateUrl('upload_index'));
         }
         if ($file->getSize() > 2000000) {
             $this->get('session')->getFlashBag()->set('message', array('type' => 'error', 'message' => 'upload.main.index.error_size'));
             return $this->redirect($this->generateUrl('upload_index'));
         }
         $name = StringManipulationExtension::slugify(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
         $extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
         if (file_exists($directory . '/' . $name . '.' . $extension)) {
             $name .= '-' . substr(md5(uniqid(true)), 0, 4);
         }
         $name .= '.' . $extension;
         $file->move($directory, $name);
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'upload.main.index.confirm'));
         return $this->redirect($this->generateUrl('upload_index'));
     }
     return array('images' => $images, 'form' => $form->createView());
 }
Exemple #2
0
 /**
  * Set code
  *
  * @param string $code
  * @return UV
  */
 public function setCode($code)
 {
     $this->code = $code;
     $this->slug = StringManipulationExtension::slugify($this->code);
     return $this;
 }
 /**
  * @Route("/page/create", name="admin_page_create")
  * @Template()
  */
 public function pageCreateAction()
 {
     if (!$this->getUserLayer()->isUser() || !$this->getUser()->hasPermission('pages.admin')) {
         return $this->createAccessDeniedResponse();
     }
     /** @var $em EntityManager */
     $em = $this->getDoctrine()->getManager();
     $page = new Page();
     $form = $this->createFormBuilder($page)->add('title')->add('content', 'redactor')->getForm();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST' && $form->bind($request)->isValid()) {
         $page->setSlug(StringManipulationExtension::slugify($page->getTitle()));
         $em->persist($page);
         $em->flush();
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'core.admin.pageCreate.confirm'));
         return $this->redirect($this->generateUrl('admin_pages'));
     }
     return array('form' => $form->createView());
 }
 /**
  * @Route("/goto/{code}", name="uvs_goto")
  * @Template()
  */
 public function goToAction($code)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     /** @var UV $uv */
     $uv = $em->getRepository('EtuModuleUVBundle:UV')->findOneBy(array('code' => $code));
     if (!$uv) {
         throw $this->createNotFoundException(sprintf('UV for code %s not found', $code));
     }
     return $this->redirect($this->generateUrl('uvs_view', array('slug' => $uv->getSlug(), 'name' => StringManipulationExtension::slugify($uv->getName()))), 301);
 }
 /**
  * @Route("/forum/post/{id}-{slug}", name="forum_post")
  * @Template()
  */
 public function postAction($id, $slug)
 {
     if (!$this->getUser()) {
         return $this->createAccessDeniedResponse();
     }
     $em = $this->getDoctrine()->getManager();
     $category = $em->getRepository('EtuModuleForumBundle:Category')->find($id);
     $checker = new PermissionsChecker($this->getUser());
     if (!$checker->canPost($category)) {
         return $this->createAccessDeniedResponse();
     }
     $parents = $em->createQueryBuilder()->select('c')->from('EtuModuleForumBundle:Category', 'c')->where('c.left <= :left')->andWhere('c.right >= :right')->setParameter('left', $category->getLeft())->setParameter('right', $category->getRight())->orderBy('c.depth')->getQuery()->getResult();
     $thread = new Thread();
     if ($checker->canSticky($category)) {
         $form = $this->createForm(new ThreadType(), $thread);
     } else {
         $form = $this->createForm(new ThreadTypeNoSticky(), $thread);
     }
     $request = $this->get('request');
     if ($request->getMethod() == 'POST') {
         $form->bind($request);
         if ($form->isValid()) {
             if ($thread->getWeight() != 100 && !$checker->canSticky($category)) {
                 $thread->setWeight(100);
             }
             $thread->setAuthor($this->getUser())->setCategory($category)->setCountMessages(1)->setSlug(StringManipulationExtension::slugify($thread->getTitle()));
             $message = $thread->getLastMessage();
             $message->setAuthor($this->getUser())->setCategory($category)->setThread($thread)->setState(100)->setCreatedAt($thread->getCreatedAt());
             $thread->setLastMessage($message);
             foreach ($parents as $parent) {
                 $parent->setLastMessage($message)->setCountMessages($parent->getCountMessages() + 1)->setCountThreads($parent->getCountThreads() + 1);
                 $em->persist($parent);
             }
             $em->persist($thread);
             $cviews = $em->getRepository('EtuModuleForumBundle:CategoryView')->findByCategory($category);
             foreach ($cviews as $cview) {
                 $em->remove($cview);
             }
             $em->flush();
             $this->giveBadges();
             $this->getSubscriptionsManager()->subscribe($this->getUser(), 'message', $thread->getId());
             return $this->redirect($this->generateUrl('forum_thread', array('id' => $thread->getId(), 'slug' => $thread->getSlug())));
         } else {
             return array('errors' => $form->getErrors(), 'category' => $category, 'parents' => $parents, 'form' => $form->createView());
         }
     }
     return array('category' => $category, 'parents' => $parents, 'form' => $form->createView());
 }
 /**
  * @Route("/{id}-{slug}/delete/confirm", requirements = {"id" = "\d+"}, name="bugs_admin_delete_confirm")
  * @Template()
  */
 public function deleteConfirmAction($id, $slug)
 {
     if (!$this->getUserLayer()->isUser() || !$this->getUser()->hasPermission('bugs.admin')) {
         return $this->createAccessDeniedResponse();
     }
     /** @var $em EntityManager */
     $em = $this->getDoctrine()->getManager();
     /** @var $bug Issue */
     $bug = $em->createQueryBuilder()->select('i, u, a')->from('EtuModuleBugsBundle:Issue', 'i')->leftJoin('i.user', 'u')->leftJoin('i.assignee', 'a')->where('i.id = :id')->setParameter('id', $id)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     if (!$bug) {
         throw $this->createNotFoundException('Issue #' . $id . ' not found');
     }
     if (StringManipulationExtension::slugify($bug->getTitle()) != $slug) {
         throw $this->createNotFoundException('Invalid slug');
     }
     /** @var $comments Comment[] */
     $comments = $em->createQueryBuilder()->select('c, u')->from('EtuModuleBugsBundle:Comment', 'c')->leftJoin('c.user', 'u')->where('c.issue = :issue')->setParameter('issue', $bug->getId())->getQuery()->getResult();
     $em->remove($bug);
     foreach ($comments as $comment) {
         $em->remove($comment);
     }
     $em->flush();
     $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'bugs.admin.delete.success'));
     return $this->redirect($this->generateUrl('bugs_index'));
 }
 /**
  * @Route("/{slug}-{name}/send-review", name="uvs_view_send_review")
  * @Template()
  */
 public function sendReviewAction(Request $request, $slug, $name)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     /** @var UV $uv */
     $uv = $em->getRepository('EtuModuleUVBundle:UV')->findOneBy(array('slug' => $slug));
     if (!$uv) {
         throw $this->createNotFoundException(sprintf('UV for slug %s not found', $slug));
     }
     if (StringManipulationExtension::slugify($uv->getName()) != $name) {
         return $this->redirect($this->generateUrl('uvs_view_send_review', array('slug' => $uv->getSlug(), 'name' => StringManipulationExtension::slugify($uv->getName()))), 301);
     }
     $review = new Review();
     $review->setUv($uv)->setSender($this->getUser())->setSemester(User::currentSemester());
     $form = $this->createFormBuilder($review)->add('type', 'choice', array('choices' => Review::$types, 'required' => true))->add('semester', 'choice', array('choices' => Review::availableSemesters(), 'required' => true))->add('file', null, array('required' => true))->getForm();
     if ($request->getMethod() == 'POST' && $form->submit($request)->isValid()) {
         $review->upload();
         $em->persist($review);
         $em->flush();
         // Notify subscribers
         $notif = new Notification();
         $review->file = null;
         $notif->setModule($this->getCurrentBundle()->getIdentifier())->setHelper('uv_new_review')->setAuthorId($this->getUser()->getId())->setEntityType('uv')->setEntityId($uv->getId())->addEntity($review);
         $this->getNotificationsSender()->send($notif);
         // Add badges
         $count = $em->createQueryBuilder()->select('COUNT(r) as nb')->from('EtuModuleUVBundle:Review', 'r')->where('r.sender = :user')->setParameter('user', $this->getUser()->getId())->getQuery()->getSingleScalarResult();
         $user = $this->getUser();
         if ($count >= 1) {
             BadgesManager::userAddBadge($user, 'uvs_reviews', 1);
         } else {
             BadgesManager::userRemoveBadge($user, 'uvs_reviews', 1);
         }
         if ($count >= 2) {
             BadgesManager::userAddBadge($user, 'uvs_reviews', 2);
         } else {
             BadgesManager::userRemoveBadge($user, 'uvs_reviews', 2);
         }
         if ($count >= 4) {
             BadgesManager::userAddBadge($user, 'uvs_reviews', 3);
         } else {
             BadgesManager::userRemoveBadge($user, 'uvs_reviews', 3);
         }
         if ($count >= 10) {
             BadgesManager::userAddBadge($user, 'uvs_reviews', 4);
         } else {
             BadgesManager::userRemoveBadge($user, 'uvs_reviews', 4);
         }
         BadgesManager::userPersistBadges($user);
         $em->persist($user);
         $em->flush();
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'uvs.main.sendReview.confirm'));
         return $this->redirect($this->generateUrl('uvs_view', array('slug' => $slug, 'name' => $name)));
     }
     return array('uv' => $uv, 'form' => $form->createView());
 }
 /**
  * @Route(
  *      "/user/membership/{login}/event/{id}-{slug}/delete/{confirm}",
  *      defaults={"confirm"=false},
  *      name="memberships_orga_events_delete"
  * )
  * @Template()
  */
 public function deleteAction(Request $request, $login, $id, $slug, $confirm = false)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var $em EntityManager */
     $em = $this->getDoctrine()->getManager();
     /** @var $memberships Member[] */
     $memberships = $em->createQueryBuilder()->select('m, o')->from('EtuUserBundle:Member', 'm')->leftJoin('m.organization', 'o')->andWhere('m.user = :user')->setParameter('user', $this->getUser()->getId())->orderBy('m.role', 'DESC')->addOrderBy('o.name', 'ASC')->getQuery()->getResult();
     $membership = null;
     foreach ($memberships as $m) {
         if ($m->getOrganization()->getLogin() == $login) {
             $membership = $m;
             break;
         }
     }
     if (!$membership) {
         throw $this->createNotFoundException('Membership or organization not found for login ' . $login);
     }
     if (!$membership->hasPermission('events')) {
         return $this->createAccessDeniedResponse();
     }
     $orga = $membership->getOrganization();
     /** @var $event Event */
     $event = $em->createQueryBuilder()->select('e, o')->from('EtuModuleEventsBundle:Event', 'e')->leftJoin('e.orga', 'o')->where('e.uid = :id')->setParameter('id', $id)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     if (!$event) {
         throw $this->createNotFoundException('Event #' . $id . ' not found');
     }
     if (StringManipulationExtension::slugify($event->getTitle()) != $slug) {
         return $this->redirect($this->generateUrl('events_view', array('id' => $id, 'slug' => StringManipulationExtension::slugify($event->getTitle()))), 301);
     }
     if ($event->getOrga()->getId() != $orga->getId()) {
         return $this->createAccessDeniedResponse();
     }
     if ($confirm) {
         $entity = array('id' => $event->getId(), 'title' => $event->getTitle(), 'location' => $event->getLocation(), 'begin' => $event->getBegin(), 'end' => $event->getEnd(), 'orga' => array('id' => $event->getOrga()->getId(), 'name' => $event->getOrga()->getName()));
         // Send notifications to subscribers
         $notif = new Notification();
         $notif->setModule($this->getCurrentBundle()->getIdentifier())->setHelper('event_deleted')->setAuthorId($this->getUser()->getId())->setEntityType('event')->setEntityId($event->getId())->addEntity($entity);
         $this->getNotificationsSender()->send($notif);
         $em->createQueryBuilder()->delete()->from('EtuModuleEventsBundle:Answer', 'a')->where('a.event = :id')->setParameter('id', $event->getId())->getQuery()->execute();
         $em->remove($event);
         $em->flush();
         // Confirmation
         $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'events.memberships.delete.confirm'));
         return $this->redirect($this->generateUrl('memberships_orga_events', array('login' => $login)));
     }
     return array('memberships' => $memberships, 'membership' => $membership, 'orga' => $orga, 'event' => $event);
 }
 /**
  * @Route(
  *      "/{issueId}-{slug}/edit/comment/{id}",
  *      requirements = {"issueId" = "\d+", "id" = "\d+"},
  *      name="bugs_edit_comment"
  * )
  * @Template()
  */
 public function editCommentAction($slug, $id)
 {
     if (!$this->getUserLayer()->isUser()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var $em EntityManager */
     $em = $this->getDoctrine()->getManager();
     /** @var $comment Comment */
     $comment = $em->createQueryBuilder()->select('c, i, u')->from('EtuModuleBugsBundle:Comment', 'c')->leftJoin('c.issue', 'i')->leftJoin('c.user', 'u')->where('c.id = :id')->setParameter('id', $id)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     if (!$comment) {
         throw $this->createNotFoundException('Comment #' . $id . ' not found');
     }
     if (StringManipulationExtension::slugify($comment->getIssue()->getTitle()) != $slug) {
         return $this->redirect($this->generateUrl('bugs_edit_comment', array('id' => $id, 'slug' => StringManipulationExtension::slugify($comment->getIssue()->getTitle()))), 301);
     }
     if ($comment->getUser()->getId() != $this->getUser()->getId() && !$this->getUser()->getIsAdmin()) {
         throw new AccessDeniedHttpException('Vous n\'avez pas le droit de modifier ce commentaire.');
     }
     $form = $this->createFormBuilder($comment)->add('body')->getForm();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST' && $form->bind($request)->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->persist($comment);
         $em->flush();
         $em->persist($comment);
         $em->flush();
         return $this->redirect($this->generateUrl('bugs_view', array('id' => $comment->getIssue()->getId(), 'slug' => StringManipulationExtension::slugify($comment->getIssue()->getTitle()))));
     }
     return array('form' => $form->createView());
 }
 /**
  * @Route("/event/{id}-{slug}/members", name="events_members")
  * @Template()
  */
 public function membersAction($id, $slug)
 {
     if (!$this->getUserLayer()->isStudent()) {
         return $this->createAccessDeniedResponse();
     }
     /** @var $em EntityManager */
     $em = $this->getDoctrine()->getManager();
     /** @var $event Event */
     $event = $em->createQueryBuilder()->select('e, o')->from('EtuModuleEventsBundle:Event', 'e')->leftJoin('e.orga', 'o')->where('e.uid = :id')->setParameter('id', $id)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     if (!$event) {
         throw $this->createNotFoundException('Event #' . $id . ' not found');
     }
     if (StringManipulationExtension::slugify($event->getTitle()) != $slug) {
         return $this->redirect($this->generateUrl('events_view', array('id' => $id, 'slug' => StringManipulationExtension::slugify($event->getTitle()))), 301);
     }
     /** @var $answers Answer[] */
     $answers = $em->createQueryBuilder()->select('a, u')->from('EtuModuleEventsBundle:Answer', 'a')->leftJoin('a.user', 'u')->where('a.event = :id')->setParameter('id', $event->getId())->getQuery()->getResult();
     $answersYes = array();
     $answersProbably = array();
     $answersNo = array();
     foreach ($answers as $answer) {
         if ($answer->getAnswer() == Answer::ANSWER_YES) {
             $answersYes[] = $answer;
         } elseif ($answer->getAnswer() == Answer::ANSWER_PROBABLY) {
             $answersProbably[] = $answer;
         } else {
             $answersNo[] = $answer;
         }
     }
     return array('event' => $event, 'answersYesCount' => count($answersYes), 'answersProbablyCount' => count($answersProbably), 'answersNoCount' => count($answersNo), 'answersYes' => $answersYes, 'answersProbably' => $answersProbably, 'answersNo' => $answersNo);
 }