function test3()
 {
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userOwner = new UserAccountModel();
     $userOwner->setEmail("*****@*****.**");
     $userOwner->setUsername("test2");
     $userOwner->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userOwner);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $userOwner, array(), $this->getSiteQuotaUsedForTesting());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $userOwner);
     $userWatchesgroupRepo = new UserWatchesGroupRepository();
     # Part 1: User does not watch group
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertNull($t);
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
     # Part 2: Watches if not watched before!
     $userWatchesgroupRepo->startUserWatchingGroupIfNotWatchedBefore($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals($user->getId(), $t->getUserAccountId());
     $this->assertEquals(true, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
     # Part 3: Stops Watching!
     $userWatchesgroupRepo->stopUserWatchingGroup($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
     # Part 4: Watches if not watched before! As they have watched before, nothing happens
     $userWatchesgroupRepo->startUserWatchingGroupIfNotWatchedBefore($user, $group);
     $t = $userWatchesgroupRepo->loadByUserAndGroup($user, $group);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesGroupRepositoryBuilder();
     $b->setGroup($group);
     $b->setUser($user);
     $t = $b->fetchAll();
     $this->assertEquals(0, count($t));
 }
 function recur($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Event does not exist.");
     }
     if ($this->parameters['event']->getIsDeleted()) {
         die("No");
         // TODO
     }
     if ($this->parameters['event']->getIsImported()) {
         die("No");
         // TODO
     }
     if (!$this->parameters['group']) {
         // Existing Group
         // TODO csfr
         if ($request->request->get('intoGroupSlug')) {
             $groupRepo = new GroupRepository();
             $group = $groupRepo->loadBySlug($app['currentSite'], $request->request->get('intoGroupSlug'));
             if ($group) {
                 $groupRepo->addEventToGroup($this->parameters['event'], $group);
                 $repo = new UserWatchesGroupRepository();
                 $repo->startUserWatchingGroupIfNotWatchedBefore($app['currentUser'], $group);
                 return $app->redirect("/event/" . $this->parameters['event']->getSlugForURL() . "/recur/");
             }
         }
         // New group
         if ($request->request->get('NewGroupTitle') && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $group = new GroupModel();
             $group->setTitle($request->request->get('NewGroupTitle'));
             $groupRepo = new GroupRepository();
             $groupRepo->create($group, $app['currentSite'], $app['currentUser']);
             $groupRepo->addEventToGroup($this->parameters['event'], $group);
             return $app->redirect("/event/" . $this->parameters['event']->getSlugForURL() . "/recur/");
         }
         return $app['twig']->render('site/event/recur.groupneeded.html.twig', $this->parameters);
     } else {
         $eventRecurSet = new EventRecurSetModel();
         $eventRecurSet->setTimeZoneName($this->parameters['event']->getTimezone());
         $data = $eventRecurSet->getEventPatternData($this->parameters['event']);
         if ($data['weekInMonth'] < 5) {
             $this->parameters['recurMonthlyOnWeekNumber'] = $data['weekInMonth'];
             $ordinal = array(1 => 'st', 2 => 'nd', 3 => 'rd', 4 => 'th');
             $this->parameters['recurMonthlyOnWeekNumberOrdinal'] = $ordinal[$data['weekInMonth']];
         } else {
             $this->parameters['recurMonthlyOnWeekNumber'] = null;
             $this->parameters['recurMonthlyOnWeekNumberOrdinal'] = null;
         }
         $this->parameters['recurMonthlyOnLastWeek'] = $data['isLastWeekInMonth'];
         return $app['twig']->render('site/event/recur.html.twig', $this->parameters);
     }
 }
 public function markDuplicateWithMetaData(GroupModel $duplicateGroup, GroupModel $originalGroup, GroupEditMetaDataModel $groupEditMetaDataModel)
 {
     global $DB;
     if ($duplicateGroup->getId() == $originalGroup->getId()) {
         return;
     }
     try {
         $DB->beginTransaction();
         $duplicateGroup->setIsDeleted(true);
         $duplicateGroup->setIsDuplicateOfId($originalGroup->getId());
         $this->groupDBAccess->update($duplicateGroup, array('is_deleted', 'is_duplicate_of_id'), $groupEditMetaDataModel);
         // Users Watching Group
         $ufgr = new UserWatchesGroupRepository();
         $usersRepo = new UserAccountRepositoryBuilder();
         $usersRepo->setWatchesGroup($duplicateGroup);
         foreach ($usersRepo->fetchAll() as $user) {
             $ufgr->startUserWatchingGroupIfNotWatchedBefore($user, $originalGroup);
         }
         // Events in Group
         $statCheck = $DB->prepare("SELECT * FROM event_in_group WHERE group_id=:group_id AND " . " event_id=:event_id AND removed_at IS NULL ");
         $statAdd = $DB->prepare("INSERT INTO event_in_group (group_id,event_id,added_by_user_account_id,added_at,addition_approved_at,is_main_group) " . "VALUES (:group_id,:event_id,:added_by_user_account_id,:added_at,:addition_approved_at,:is_main_group)");
         $erb = new EventRepositoryBuilder();
         $erb->setGroup($duplicateGroup);
         foreach ($erb->fetchAll() as $event) {
             // check event not already in list
             $statCheck->execute(array('group_id' => $originalGroup->getId(), 'event_id' => $event->getId()));
             if ($statCheck->rowCount() == 0) {
                 // TODO is_main_group ??????????????????
                 $statAdd->execute(array('group_id' => $originalGroup->getId(), 'event_id' => $event->getId(), 'is_main_group' => 0, 'added_by_user_account_id' => $user ? $user->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase()));
             }
         }
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }