function editTags($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Event does not exist.");
     }
     $tagRepo = new TagRepository();
     if ('POST' == $request->getMethod() && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         if ($request->request->get('addTag')) {
             $tag = $tagRepo->loadBySlug($app['currentSite'], $request->request->get('addTag'));
             if ($tag) {
                 $tagRepo->addTagToEvent($tag, $this->parameters['event'], $app['currentUser']);
             }
         } elseif ($request->request->get('removeTag')) {
             $tag = $tagRepo->loadBySlug($app['currentSite'], $request->request->get('removeTag'));
             if ($tag) {
                 $tagRepo->removeTagFromEvent($tag, $this->parameters['event'], $app['currentUser']);
             }
         }
     }
     $trb = new TagRepositoryBuilder();
     $trb->setSite($app['currentSite']);
     $trb->setIncludeDeleted(false);
     $trb->setTagsForEvent($this->parameters['event']);
     $this->parameters['tags'] = $trb->fetchAll();
     $trb = new TagRepositoryBuilder();
     $trb->setSite($app['currentSite']);
     $trb->setIncludeDeleted(false);
     $trb->setTagsNotForEvent($this->parameters['event']);
     $this->parameters['tagsToAdd'] = $trb->fetchAll();
     return $app['twig']->render('site/event/edit.tags.html.twig', $this->parameters);
 }
 function testAddRemove()
 {
     TimeSource::mock(2013, 7, 1, 7, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2013, 8, 1, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2013, 8, 1, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $tag = new TagModel();
     $tag->setTitle("test");
     $tagRepo = new TagRepository();
     $tagRepo->create($tag, $site, $user);
     ## No tags
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsForEvent($event);
     $this->assertEquals(0, count($tagRepoBuilder->fetchAll()));
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsNotForEvent($event);
     $this->assertEquals(1, count($tagRepoBuilder->fetchAll()));
     $eventRepoBuilder = new EventRepositoryBuilder();
     $eventRepoBuilder->setSite($site);
     $eventRepoBuilder->setTag($tag);
     $this->assertEquals(0, count($eventRepoBuilder->fetchAll()));
     ## Add event to tag, test
     $tagRepo->addTagToEvent($tag, $event, $user);
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsForEvent($event);
     $this->assertEquals(1, count($tagRepoBuilder->fetchAll()));
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsNotForEvent($event);
     $this->assertEquals(0, count($tagRepoBuilder->fetchAll()));
     $eventRepoBuilder = new EventRepositoryBuilder();
     $eventRepoBuilder->setSite($site);
     $eventRepoBuilder->setTag($tag);
     $this->assertEquals(1, count($eventRepoBuilder->fetchAll()));
     ## remove tag
     $tagRepo->removeTagFromEvent($tag, $event, $user);
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsForEvent($event);
     $this->assertEquals(0, count($tagRepoBuilder->fetchAll()));
     $tagRepoBuilder = new TagRepositoryBuilder();
     $tagRepoBuilder->setSite($site);
     $tagRepoBuilder->setTagsNotForEvent($event);
     $this->assertEquals(1, count($tagRepoBuilder->fetchAll()));
     $eventRepoBuilder = new EventRepositoryBuilder();
     $eventRepoBuilder->setSite($site);
     $eventRepoBuilder->setTag($tag);
     $this->assertEquals(0, count($eventRepoBuilder->fetchAll()));
 }