function test1()
 {
     TimeSource::mock(2014, 5, 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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $event->setUrl("http://www.info.com");
     $event->setTicketUrl("http://www.tickets.com");
     $eventDupe = new EventModel();
     $eventDupe->setSummary("test");
     $eventDupe->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $eventDupe->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user, $group);
     $eventRepository->create($eventDupe, $site, $user, $group);
     TimeSource::mock(2014, 5, 1, 7, 1, 0);
     $eventRepository->markDuplicate($eventDupe, $event);
     $userAtEvent = new \models\UserAtEventModel();
     $userAtEvent->setEventId($event->getId());
     $userAtEvent->setUserAccountId($user->getId());
     $userAtEvent->setIsPlanAttending(true);
     $userAtEventRepo = new \repositories\UserAtEventRepository();
     $userAtEventRepo->create($userAtEvent);
     $curatedList = new CuratedListModel();
     $curatedList->setTitle("test");
     $curatedList->setDescription("test this!");
     $clRepo = new CuratedListRepository();
     $clRepo->create($curatedList, $site, $user);
     $clRepo->addEventtoCuratedList($event, $curatedList, $user);
     $tag = new TagModel();
     $tag->setTitle("Test");
     $tagRepo = new TagRepository();
     $tagRepo->create($tag, $site, $user);
     $tagRepo->addTagToEvent($tag, $event, $user);
     ## TEST
     $this->assertNotNull($eventRepository->loadBySlug($site, $event->getSlug()));
     ## PURGE!
     $eventRepository->purge($event);
     ## TEST
     $this->assertNull($eventRepository->loadBySlug($site, $event->getSlug()));
 }
 /**
  * @dataProvider dataForTestSet
  */
 function testSet2($slug, $text, $result)
 {
     $group = new GroupModel();
     $group->setSlug($slug);
     $group->setTitle($text);
     $this->assertEquals($result, $group->getSlugForUrl());
 }
 public function postInfoJson($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Does not exist.");
     }
     $ourRequest = new \Request($request);
     $edits = false;
     if ($ourRequest->hasGetOrPost('title') && $this->group->setTitleIfDifferent($ourRequest->getGetOrPostString('title', ''))) {
         $edits = true;
     }
     if ($ourRequest->hasGetOrPost('description') && $this->group->setDescriptionIfDifferent($ourRequest->getGetOrPostString('description', ''))) {
         $edits = true;
     }
     if ($ourRequest->hasGetOrPost('url') && $this->group->setUrlIfDifferent($ourRequest->getGetOrPostString('url', ''))) {
         $edits = true;
     }
     if ($edits) {
         $repo = new GroupRepository();
         $repo->edit($this->group, $app['apiUser']);
         $out = array('edited' => true);
     } else {
         $out = array('edited' => false);
     }
     return json_encode($out);
 }
 function test1()
 {
     \TimeSource::mock(2014, 1, 1, 0, 0, 0);
     $user1 = new UserAccountModel();
     $user1->setEmail("*****@*****.**");
     $user1->setUsername("test");
     $user1->setPassword("password");
     $user2 = new UserAccountModel();
     $user2->setEmail("*****@*****.**");
     $user2->setUsername("test2");
     $user2->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user1);
     $userRepo->create($user2);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user1, array(), $this->getSiteQuotaUsedForTesting());
     $group1 = new GroupModel();
     $group1->setTitle("test1");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("test this looks similar");
     $group2->setDescription("test test");
     $group2->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     \TimeSource::mock(2014, 1, 1, 1, 0, 0);
     $groupRepo->create($group1, $site, $user1);
     $groupRepo->create($group2, $site, $user2);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user1, $group2);
     $uwgr = new UserWatchesGroupRepository();
     // Test before
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(0, count($erb->fetchAll()));
     $this->assertNull($uwgr->loadByUserAndGroup($user2, $group1));
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertFalse($group2->getIsDeleted());
     $this->assertNull($group2->getIsDuplicateOfId());
     // Mark
     \TimeSource::mock(2014, 1, 1, 2, 0, 0);
     $groupRepo->markDuplicate($group2, $group1, $user1);
     // Test Duplicate
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(1, count($erb->fetchAll()));
     $uwg = $uwgr->loadByUserAndGroup($user2, $group1);
     $this->assertNotNull($uwg);
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertTrue($group2->getIsDeleted());
     $this->assertEquals($group1->getId(), $group2->getIsDuplicateOfId());
 }
 public function getLastGroupPromptEmailSent(UserWatchesSiteModel $userWatchesSite, GroupModel $group)
 {
     global $DB;
     $stat = $DB->prepare("SELECT MAX(sent_at) AS c FROM user_watches_site_group_prompt_email WHERE user_account_id=:user_account_id AND group_id=:group_id");
     $stat->execute(array('user_account_id' => $userWatchesSite->getUserAccountId(), 'group_id' => $group->getId()));
     $data = $stat->fetch();
     return $data['c'] ? new \DateTime($data['c'], new \DateTimeZone("UTC")) : null;
 }
 function testIntegration1()
 {
     \TimeSource::mock(2014, 1, 1, 12, 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());
     ## Create group
     \TimeSource::mock(2014, 1, 1, 13, 0, 0);
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     ## Edit group
     \TimeSource::mock(2014, 1, 1, 14, 0, 0);
     $group = $groupRepo->loadById($group->getId());
     $group->setTwitterUsername("testy");
     $groupRepo->edit($group, $user);
     ## Now save changed flags on these .....
     $groupHistoryRepo = new GroupHistoryRepository();
     $stat = $this->app['db']->prepare("SELECT * FROM group_history");
     $stat->execute();
     while ($data = $stat->fetch()) {
         $groupHistory = new GroupHistoryModel();
         $groupHistory->setFromDataBaseRow($data);
         $groupHistoryRepo->ensureChangedFlagsAreSet($groupHistory);
     }
     ## Now load and check
     $historyRepo = new HistoryRepositoryBuilder();
     $historyRepo->setGroup($group);
     $historyRepo->setIncludeEventHistory(false);
     $historyRepo->setIncludeVenueHistory(false);
     $historyRepo->setIncludeGroupHistory(true);
     $histories = $historyRepo->fetchAll();
     $this->assertEquals(2, count($histories));
     #the edit
     $this->assertEquals(FALSE, $histories[0]->getTitleChanged());
     $this->assertEquals(false, $histories[0]->getDescriptionChanged());
     $this->assertEquals(false, $histories[0]->getUrlChanged());
     $this->assertEquals(true, $histories[0]->getTwitterUsernameChanged());
     $this->assertEquals(false, $histories[0]->getIsDeletedChanged());
     #the create
     $this->assertEquals(true, $histories[1]->getTitleChanged());
     $this->assertEquals(true, $histories[1]->getDescriptionChanged());
     $this->assertEquals(true, $histories[1]->getUrlChanged());
     $this->assertEquals(false, $histories[1]->getTwitterUsernameChanged());
     $this->assertEquals(false, $histories[1]->getIsDeletedChanged());
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Lanyrd1.ical');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("State of the Map Scotland 2013", $event->getSummary());
     $this->assertEquals('2013-10-11 00:00:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-14 23:59:59', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals("http://sotms.eventbrite.com/\n\nhttp://lanyrd.com/crkmt", $event->getDescription());
     $this->assertEquals('http://lanyrd.com/2013/sotmscot2013/', $event->getURL());
     $this->assertFalse($event->getIsDeleted());
     // Look for event
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setImportURL($importURL);
     $this->assertEquals(1, count($erb->fetchAll()));
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Eventbrite1.ical');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $importURLRun->setFlag(ImportURLRun::$FLAG_SET_TICKET_URL_AS_URL);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("Computing At School Scotland Conference 2013", $event->getSummary());
     $this->assertEquals('2013-10-26 07:30:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-26 16:00:00', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('For details, click here: https://casscot13.eventbrite.co.uk', $event->getDescription());
     $this->assertEquals('https://casscot13.eventbrite.co.uk', $event->getURL());
     $this->assertEquals('https://casscot13.eventbrite.co.uk', $event->getTicketURL());
     $this->assertFalse($event->getIsDeleted());
 }
 protected function build()
 {
     if ($this->site) {
         $this->where[] = " import_url_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->group) {
         $this->where[] = " import_url_information.group_id = :group_id ";
         $this->params['group_id'] = $this->group->getId();
     }
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Meetup1.ics');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("Talk & Build AngularJS", $event->getSummary());
     $this->assertEquals('2013-10-17 18:00:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-17 21:00:00', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals("AngularJS - Edinburgh\nThursday, October 17 at 7:00 PM\n\nDetails: http://www.meetup.com/AngularJS-Edinburgh/events/141654792/", $event->getDescription());
     $this->assertEquals('http://www.meetup.com/AngularJS-Edinburgh/events/141654792/', $event->getURL());
     $this->assertFalse($event->getIsDeleted());
 }
 public function fetchAll()
 {
     $this->buildStart();
     $this->build();
     $this->buildStat();
     $results = array();
     while ($data = $this->stat->fetch()) {
         $event = new GroupModel();
         $event->setFromDataBaseRow($data);
         $results[] = $event;
     }
     return $results;
 }
 function testEventAndNotEvent1()
 {
     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);
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     ## Test event not in group
     $grb = new GroupRepositoryBuilder();
     $grb->setEvent($event);
     $this->assertEquals(0, count($grb->fetchAll()));
     $grb = new GroupRepositoryBuilder();
     $grb->setNotEvent($event);
     $this->assertEquals(1, count($grb->fetchAll()));
     ## Add event to group, test
     $groupRepo->addEventToGroup($event, $group, $user);
     $grb = new GroupRepositoryBuilder();
     $grb->setEvent($event);
     $this->assertEquals(1, count($grb->fetchAll()));
     $grb = new GroupRepositoryBuilder();
     $grb->setNotEvent($event);
     $this->assertEquals(0, count($grb->fetchAll()));
     ## remove event from group
     $groupRepo->removeEventFromGroup($event, $group, $user);
     $grb = new GroupRepositoryBuilder();
     $grb->setEvent($event);
     $this->assertEquals(0, count($grb->fetchAll()));
     $grb = new GroupRepositoryBuilder();
     $grb->setNotEvent($event);
     $this->assertEquals(1, count($grb->fetchAll()));
 }
 function testMultiple()
 {
     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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupDupe = new GroupModel();
     $groupDupe->setTitle("test DUPE");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $groupRepo->create($groupDupe, $site, $user);
     TimeSource::mock(2013, 7, 1, 7, 1, 0);
     $groupRepo->markDuplicate($groupDupe, $group);
     $ufgr = new UserWatchesGroupRepository();
     $ufgr->startUserWatchingGroupIdIfNotWatchedBefore($user, $group->getId());
     $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, $group);
     ## TEST
     $this->assertNotNull($groupRepo->loadById($group->getId()));
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($event);
     $groups = $groupRB->fetchAll();
     $this->assertEquals(1, count($groups));
     ## PURGE!
     $groupRepo->purge($group);
     ## TEST
     $this->assertNull($groupRepo->loadById($group->getId()));
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($event);
     $groups = $groupRB->fetchAll();
     $this->assertEquals(0, count($groups));
 }
 function test1()
 {
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userOther = new UserAccountModel();
     $userOther->setEmail("*****@*****.**");
     $userOther->setUsername("test2");
     $userOther->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userOther);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, 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, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0, 'Europe/London'));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0, 'Europe/London'));
     $event->setUrl("http://www.info.com");
     $event->setTicketUrl("http://www.tickets.com");
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $curatedList = new CuratedListModel();
     $curatedList->setTitle("test");
     $curatedList->setDescription("test this!");
     $clRepo = new CuratedListRepository();
     $clRepo->create($curatedList, $site, $user);
     $clRepo->addEditorToCuratedList($userOther, $curatedList, $user);
     $clRepo->addEventtoCuratedList($event, $curatedList, $user);
     $clRepo->addGroupToCuratedList($group, $curatedList, $user);
     ## Test
     $this->assertNotNull($clRepo->loadBySlug($site, $curatedList->getSlug()));
     ## Purge!
     $clRepo->purge($curatedList);
     ## Test
     $this->assertNull($clRepo->loadBySlug($site, $curatedList->getSlug()));
 }
 protected function build()
 {
     if ($this->onlyCurrent) {
         $this->joins[] = " JOIN group_information ON group_information.id = user_watches_group_information.group_id  ";
         $this->joins[] = " LEFT JOIN user_watches_site_information ON user_watches_site_information.site_id = group_information.site_id " . "AND user_watches_site_information.user_account_id = user_watches_group_information.user_account_id " . " AND user_watches_site_information.is_watching = '1' ";
         $this->where[] = " user_watches_group_information.is_watching = '1' AND user_watches_site_information.is_watching IS NULL ";
     }
     if ($this->group) {
         $this->where[] = " user_watches_group_information.group_id = :group_id";
         $this->params['group_id'] = $this->group->getId();
     }
     if ($this->user) {
         $this->where[] = " user_watches_group_information.user_account_id = :user_account_id";
         $this->params['user_account_id'] = $this->user->getId();
     }
 }
 /**
  * This will always return something. If one doesn't exist, one will be created.
  * @return UserWatchesSiteStopModel
  */
 public function getForUserAndGroup(UserAccountModel $user, GroupModel $group)
 {
     global $DB;
     $stat = $DB->prepare("SELECT * FROM user_watches_group_stop WHERE user_account_id=:uid AND group_id=:gid");
     $stat->execute(array('uid' => $user->getId(), 'gid' => $group->getId()));
     if ($stat->rowCount() > 0) {
         $uwgs = new UserWatchesGroupStopModel();
         $uwgs->setFromDataBaseRow($stat->fetch());
         return $uwgs;
     }
     $uwgs = new UserWatchesGroupStopModel();
     $uwgs->setUserAccountId($user->getId());
     $uwgs->setGroupId($group->getId());
     $uwgs->setAccessKey(createKey(2, 150));
     // TODO check not already used
     $stat = $DB->prepare("INSERT INTO user_watches_group_stop (user_account_id, group_id, access_key, created_at) " . "VALUES (:user_account_id, :group_id, :access_key, :created_at)");
     $stat->execute(array('user_account_id' => $uwgs->getUserAccountId(), 'group_id' => $uwgs->getGroupId(), 'access_key' => $uwgs->getAccessKey(), 'created_at' => \TimeSource::getFormattedForDataBase()));
     return $uwgs;
 }
 function test1()
 {
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $newImportURL = new ImportURLModel();
     $newImportURL->setIsEnabled(true);
     $newImportURL->setSiteId($site->getId());
     $newImportURL->setGroupId($group->getId());
     $newImportURL->setTitle("Test");
     $newImportURL->setUrl("http://test.com");
     # no clash
     $clash = $importURLRepository->loadClashForImportUrl($newImportURL);
     $this->assertNull($clash);
     # save import, now clash!
     $importURLRepository->create($newImportURL, $site, $user);
     $newImportURL2 = new ImportURLModel();
     $newImportURL2->setIsEnabled(true);
     $newImportURL2->setSiteId($site->getId());
     $newImportURL2->setGroupId($group->getId());
     $newImportURL2->setTitle("Test.com site");
     $newImportURL2->setUrl("http://TEST.com");
     # no clash
     $clash = $importURLRepository->loadClashForImportUrl($newImportURL2);
     $this->assertTrue($clash != null);
 }
 protected function build()
 {
     if ($this->site) {
         $this->where[] = " media_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->group) {
         $this->joins[] = " JOIN media_in_group AS media_in_group ON media_in_group.media_id = media_information.id " . "AND media_in_group.removed_at IS NULL AND media_in_group.group_id = :group_id ";
         $this->params['group_id'] = $this->group->getId();
     } else {
         if ($this->groupNotIn) {
             $this->joins[] = " LEFT JOIN media_in_group AS media_in_group ON media_in_group.media_id = media_information.id " . "AND media_in_group.removed_at IS NULL AND media_in_group.group_id = :group_id ";
             $this->where[] = " media_in_group.added_at IS NULL ";
             $this->params['group_id'] = $this->groupNotIn->getId();
         }
     }
     if ($this->venue) {
         $this->joins[] = " JOIN media_in_venue AS media_in_venue ON media_in_venue.media_id = media_information.id " . "AND media_in_venue.removed_at IS NULL AND media_in_venue.venue_id = :venue_id ";
         $this->params['venue_id'] = $this->venue->getId();
     } else {
         if ($this->venueNotIn) {
             $this->joins[] = " LEFT JOIN media_in_venue AS media_in_venue ON media_in_venue.media_id = media_information.id " . "AND media_in_venue.removed_at IS NULL AND media_in_venue.venue_id = :venue_id ";
             $this->where[] = " media_in_venue.added_at IS NULL ";
             $this->params['venue_id'] = $this->venueNotIn->getId();
         }
     }
     if ($this->event) {
         $this->joins[] = " JOIN media_in_event AS media_in_event ON media_in_event.media_id = media_information.id " . "AND media_in_event.removed_at IS NULL AND media_in_event.event_id = :event_id ";
         $this->params['event_id'] = $this->event->getId();
     } else {
         if ($this->eventNotIn) {
             $this->joins[] = " LEFT JOIN media_in_event AS media_in_event ON media_in_event.media_id = media_information.id " . "AND media_in_event.removed_at IS NULL AND media_in_event.event_id = :event_id ";
             $this->where[] = " media_in_event.added_at IS NULL ";
             $this->params['event_id'] = $this->eventNotIn->getId();
         }
     }
     if (!$this->include_deleted) {
         $this->where[] = " media_information.deleted_at IS NULL ";
     }
 }
 function testMultiple()
 {
     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());
     $group1 = new GroupModel();
     $group1->setTitle("test");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("cat");
     $group2->setDescription("cat cat");
     $group2->setUrl("http://www.cat.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group1, $site, $user);
     $groupRepo->create($group2, $site, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt($this->mktime(2013, 8, 1, 19, 0, 0));
     $event->setEndAt($this->mktime(2013, 8, 1, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user, $group1, array($group1, $group2));
     // Check groups
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($event);
     $groups = $groupRB->fetchAll();
     $this->assertEquals(2, count($groups));
 }
 protected function build()
 {
     $this->select[] = ' curated_list_information.* ';
     if ($this->userAccount) {
         $this->joins[] = " JOIN user_in_curated_list_information ON user_in_curated_list_information.curated_list_id = curated_list_information.id " . "AND user_in_curated_list_information.user_account_id = :user_account_id ";
         $this->params['user_account_id'] = $this->userAccount->getId();
         $this->where[] = " (user_in_curated_list_information.is_owner = '1' OR user_in_curated_list_information.is_editor = '1'  ) ";
     }
     if ($this->site) {
         $this->where[] = " curated_list_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->containsEvent) {
         $this->params['event_id'] = $this->containsEvent->getId();
         // event directly in list?
         $this->joins[] = " LEFT JOIN event_in_curated_list ON event_in_curated_list.curated_list_id = curated_list_information.id AND   " . " event_in_curated_list.event_id = :event_id AND event_in_curated_list.removed_at IS NULL ";
         // event in list via group?
         $this->joins[] = " LEFT JOIN ( SELECT group_in_curated_list.curated_list_id, MAX(group_in_curated_list.group_id) AS group_id FROM group_in_curated_list " . " JOIN event_in_group ON event_in_group.group_id = group_in_curated_list.group_id " . " WHERE event_in_group.event_id = :event_id AND group_in_curated_list.removed_at IS NULL AND event_in_group.removed_at IS NULL " . " GROUP BY group_in_curated_list.curated_list_id ) AS event_in_curated_list_via_group_table ON event_in_curated_list_via_group_table.curated_list_id = curated_list_information.id ";
         $this->where[] = " (event_in_curated_list.added_at IS NOT NULL OR event_in_curated_list_via_group_table.group_id IS NOT NULL) ";
     } else {
         if ($this->eventInfo) {
             $this->params['event_id'] = $this->eventInfo->getId();
             // event directly in list?
             $this->joins[] = " LEFT JOIN event_in_curated_list ON event_in_curated_list.curated_list_id = curated_list_information.id AND   " . " event_in_curated_list.event_id = :event_id AND event_in_curated_list.removed_at IS NULL ";
             $this->select[] = " event_in_curated_list.added_at AS is_event_in_list ";
             // event in list via group?
             $this->joins[] = " LEFT JOIN ( SELECT group_in_curated_list.curated_list_id, MAX(group_in_curated_list.group_id) AS group_id FROM group_in_curated_list " . " JOIN event_in_group ON event_in_group.group_id = group_in_curated_list.group_id " . " WHERE event_in_group.event_id = :event_id AND group_in_curated_list.removed_at IS NULL AND event_in_group.removed_at IS NULL " . " GROUP BY group_in_curated_list.curated_list_id ) AS event_in_curated_list_via_group_table ON event_in_curated_list_via_group_table.curated_list_id = curated_list_information.id ";
             $this->select[] = " event_in_curated_list_via_group_table.group_id AS event_in_list_via_group_id ";
         }
     }
     if ($this->containsGroup) {
         $this->joins[] = " LEFT JOIN group_in_curated_list ON group_in_curated_list.curated_list_id = curated_list_information.id AND   " . " group_in_curated_list.group_id = :group_id AND group_in_curated_list.removed_at IS NULL ";
         $this->params['group_id'] = $this->containsGroup->getId();
         $this->where[] = " group_in_curated_list.added_at IS NOT NULL ";
     } else {
         if ($this->groupInfo) {
             $this->joins[] = " LEFT JOIN group_in_curated_list ON group_in_curated_list.curated_list_id = curated_list_information.id AND   " . " group_in_curated_list.group_id = :group_id AND group_in_curated_list.removed_at IS NULL ";
             $this->params['group_id'] = $this->groupInfo->getId();
             $this->select[] = " group_in_curated_list.added_at AS is_group_in_list ";
         }
     }
     if (!$this->include_deleted) {
         $this->where[] = " curated_list_information.is_deleted = '0' ";
     }
 }
 protected function build()
 {
     $this->select[] = 'user_account_information.*';
     $this->groupNeeded = false;
     if ($this->requestAccessSite) {
         $this->joins[] = " LEFT JOIN user_in_site_information ON user_in_site_information.user_account_id = user_account_information.id ";
         $this->joins[] = " JOIN site_access_request ON site_access_request.user_account_id = user_account_information.id AND " . " site_access_request.granted_by IS NULL AND site_access_request.rejected_by IS NULL AND site_access_request.site_id = :site_id";
         $this->where[] = " (  user_in_site_information.user_account_id IS NULL OR " . "(user_in_site_information.is_editor != '1' AND  user_in_site_information.is_administrator != '1' " . " AND  user_in_site_information.is_owner != '1' ))";
         $this->params['site_id'] = $this->requestAccessSite->getId();
         $this->groupNeeded = true;
     }
     if ($this->editNotOwnCuratedList) {
         $this->joins[] = " JOIN user_in_curated_list_information ON user_in_curated_list_information.user_account_id = user_account_information.id " . "AND user_in_curated_list_information.curated_list_id = :curated_list_id ";
         $this->params['curated_list_id'] = $this->editNotOwnCuratedList->getId();
         $this->where[] = " user_in_curated_list_information.is_owner = '0' AND user_in_curated_list_information.is_editor = '1' ";
     }
     if ($this->watchesSite) {
         $this->joins[] = " JOIN user_watches_site_information ON " . "user_watches_site_information.user_account_id = user_account_information.id  AND " . "user_watches_site_information.site_id = :site_id AND " . "user_watches_site_information.is_watching = '1'";
         $this->params['site_id'] = $this->watchesSite->getId();
     }
     if ($this->watchesGroup) {
         $this->joins[] = " JOIN user_watches_group_information ON " . "user_watches_group_information.user_account_id = user_account_information.id  AND " . "user_watches_group_information.group_id = :group_id AND " . "user_watches_group_information.is_watching = '1'";
         $this->params['group_id'] = $this->watchesGroup->getId();
     }
     if ($this->watchesArea) {
         $this->joins[] = " JOIN user_watches_area_information ON " . "user_watches_area_information.user_account_id = user_account_information.id  AND " . "user_watches_area_information.area_id = :area_id AND " . "user_watches_area_information.is_watching = '1'";
         $this->params['area_id'] = $this->watchesArea->getId();
     }
     if ($this->inUserGroup) {
         $this->joins[] = " JOIN user_in_user_group ON user_in_user_group.user_account_id = user_account_information.id " . "AND user_in_user_group.user_group_id = :user_group_id AND user_in_user_group.removed_at IS NULL  ";
         $this->params['user_group_id'] = $this->inUserGroup->getId();
     }
     if ($this->userHasNoEditorPermissionsInSite) {
         $this->joins[] = " JOIN user_has_no_editor_permissions_in_site ON user_has_no_editor_permissions_in_site.user_account_id = user_account_information.id " . "AND user_has_no_editor_permissions_in_site.site_id = :no_edit_permissions_in_site_id AND user_has_no_editor_permissions_in_site.removed_at IS NULL  ";
         $this->params['no_edit_permissions_in_site_id'] = $this->userHasNoEditorPermissionsInSite->getId();
     }
     if ($this->isOpenBySysAdminsOnly) {
         $this->where[] = " user_account_information.is_closed_by_sys_admin = '0' ";
     }
 }
 function testIsGroupRunningOutOfFutureEvents()
 {
     \TimeSource::mock(2014, 1, 1, 1, 1, 1);
     $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");
     $site->setPromptEmailsDaysInAdvance(28);
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, 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, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2014, 4, 1, 19, 0, 0, 'Europe/London'));
     $event->setEndAt(getUTCDateTime(2014, 4, 1, 21, 0, 0, 'Europe/London'));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user, $group);
     ### TEST
     \TimeSource::mock(2014, 2, 1, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 2, 15, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 1, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 2, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 3, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 4, 1, 1, 1);
     $this->assertEquals(0, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 5, 1, 1, 1);
     $this->assertEquals(1, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 6, 1, 1, 1);
     $this->assertEquals(1, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 7, 1, 1, 1);
     $this->assertEquals(1, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 3, 20, 1, 1, 1);
     $this->assertEquals(1, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 4, 1, 1, 1, 1);
     $this->assertEquals(1, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 4, 15, 1, 1, 1);
     $this->assertEquals(2, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
     ### TEST
     \TimeSource::mock(2014, 5, 1, 1, 1, 1);
     $this->assertEquals(2, $groupRepo->isGroupRunningOutOfFutureEvents($group, $site));
 }
 protected function build()
 {
     global $DB;
     $this->select[] = 'event_information.*';
     $this->select[] = " group_information.title AS group_title ";
     $this->select[] = " group_information.id AS group_id ";
     $this->joins[] = " LEFT JOIN event_in_group ON event_in_group.event_id = event_information.id AND event_in_group.removed_at IS NULL AND event_in_group.is_main_group = '1' ";
     $this->joins[] = " LEFT JOIN group_information ON group_information.id = event_in_group.group_id ";
     $joinsVenueInfoSQL = " LEFT JOIN venue_information ON venue_information.id = event_information.venue_id ";
     if ($this->site) {
         $this->where[] = " event_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->group) {
         // We use a seperate table here so if event is in 2 groups and we select events in 1 group that isn't the main group only,
         // the normal event_in_group table still shows the main group.
         $this->joins[] = " JOIN event_in_group AS event_in_group_select ON event_in_group_select.event_id = event_information.id " . "AND event_in_group_select.removed_at IS NULL AND event_in_group_select.group_id = :group_id ";
         $this->params['group_id'] = $this->group->getId();
     }
     if ($this->country) {
         $this->where[] = " event_information.country_id = :country_id ";
         $this->params['country_id'] = $this->country->getId();
     }
     if ($this->area) {
         // We were doing
         // $this->joins[] = " LEFT JOIN cached_area_has_parent ON cached_area_has_parent.area_id = venue_information.area_id";
         // $this->where[] =  " (venue_information.area_id = :area_id OR  cached_area_has_parent.has_parent_area_id = :area_id )";
         // but then we got duplicates
         $areaids = array($this->area->getId());
         $this->statAreas = $DB->prepare("SELECT area_id FROM cached_area_has_parent WHERE has_parent_area_id=:id");
         $this->statAreas->execute(array('id' => $this->area->getId()));
         while ($d = $this->statAreas->fetch()) {
             $areaids[] = $d['area_id'];
         }
         $this->joins[] = $joinsVenueInfoSQL;
         $this->where[] = " (venue_information.area_id IN (" . implode(",", $areaids) . ") " . "OR event_information.area_id IN (" . implode(",", $areaids) . ")) ";
     }
     if ($this->venue) {
         $this->where[] = " event_information.venue_id = :venue_id ";
         $this->params['venue_id'] = $this->venue->getId();
     }
     if ($this->importURL) {
         $this->joins[] = " LEFT JOIN imported_event_is_event ON imported_event_is_event.event_id = event_information.id ";
         $this->joins[] = " LEFT JOIN imported_event ON imported_event.id = imported_event_is_event.imported_event_id ";
         $this->where[] = " (imported_event.import_url_id = :import_url_id OR event_information.import_url_id = :import_url_id )";
         $this->params['import_url_id'] = $this->importURL->getId();
     }
     if ($this->importedEvent) {
         $this->joins[] = " JOIN imported_event_is_event ON imported_event_is_event.event_id = event_information.id AND imported_event_is_event.imported_event_id = :imported_event_id ";
         $this->params['imported_event_id'] = $this->importedEvent->getId();
     }
     if (!$this->site && !$this->group) {
         $this->joins[] = " JOIN site_information ON event_information.site_id = site_information.id ";
         $this->select[] = " site_information.slug AS site_slug ";
         if (!$this->includeEventsFromClosedSites) {
             $this->where[] = " site_information.is_closed_by_sys_admin = '0' ";
         }
     }
     if ($this->curatedList) {
         $this->joins[] = " LEFT JOIN event_in_curated_list ON event_in_curated_list.event_id = event_information.id " . " AND event_in_curated_list.removed_at IS NULL AND event_in_curated_list.curated_list_id = :curated_list";
         $this->joins[] = " LEFT JOIN  ( SELECT event_in_group.event_id, MAX(event_in_group.group_id) AS group_id" . " FROM event_in_group " . " JOIN group_in_curated_list ON group_in_curated_list.group_id = event_in_group.group_id " . " WHERE group_in_curated_list.curated_list_id = :curated_list AND group_in_curated_list.removed_at IS NULL AND event_in_group.removed_at IS NULL " . " GROUP BY event_in_group.event_id " . ") AS event_in_group_in_curated_list ON event_in_group_in_curated_list.event_id = event_information.id ";
         $this->where[] = " ( event_in_curated_list.curated_list_id IS NOT NULL OR event_in_group_in_curated_list.event_id IS NOT NULL )";
         $this->params['curated_list'] = $this->curatedList->getId();
         if ($this->curatedListInformation) {
             $this->joins[] = " LEFT JOIN group_information AS group_information_cl ON group_information_cl.id = event_in_group_in_curated_list.group_id ";
             $this->select[] = " (CASE WHEN event_in_curated_list.event_id IS NULL THEN 0 ELSE 1 END) AS is_event_in_curated_list ";
             $this->select[] = " group_information_cl.id AS in_curated_list_group_id ";
             $this->select[] = " group_information_cl.slug AS in_curated_list_group_slug ";
             $this->select[] = " group_information_cl.title AS in_curated_list_group_title ";
         }
     }
     if ($this->end) {
         $this->where[] = ' event_information.end_at = :end';
         $this->params['end'] = $this->end->format("Y-m-d H:i:s");
     } else {
         if ($this->after) {
             $this->where[] = ' event_information.end_at > :after';
             $this->params['after'] = $this->after->format("Y-m-d H:i:s");
         } else {
             if ($this->endBefore) {
                 $this->where[] = ' event_information.end_at < :before';
                 $this->params['before'] = $this->endBefore->format("Y-m-d H:i:s");
             }
         }
     }
     if ($this->start) {
         $this->where[] = ' event_information.start_at = :start';
         $this->params['start'] = $this->start->format("Y-m-d H:i:s");
     } else {
         if ($this->before) {
             $this->where[] = ' event_information.start_at < :before';
             $this->params['before'] = $this->before->format("Y-m-d H:i:s");
         } else {
             if ($this->startAfter) {
                 $this->where[] = ' event_information.start_at > :startAfter';
                 $this->params['startAfter'] = $this->startAfter->format("Y-m-d H:i:s");
             }
         }
     }
     if (!$this->include_deleted) {
         $this->where[] = " event_information.is_deleted = '0' ";
     }
     if (!$this->include_cancelled) {
         $this->where[] = " event_information.is_cancelled = '0' ";
     }
     if (!$this->include_imported) {
         $this->where[] = " event_information.import_url_id is null ";
     }
     if ($this->userAccount) {
         // user at event. we want info on this always for the extra selects, so outside if statement
         $this->joins[] = "  LEFT JOIN user_at_event_information ON user_at_event_information.event_id = event_information.id " . "AND user_at_event_information.user_account_id = :user_account_id ";
         $this->select[] = " user_at_event_information.is_plan_attending AS user_is_plan_attending ";
         $this->select[] = " user_at_event_information.is_plan_maybe_attending AS user_is_plan_maybe_attending ";
         if (!$this->userAccountIncludeAll) {
             $w = array();
             if ($this->userAccountIncludeWatching) {
                 $w[] = "  event_information.id IN (SELECT event_information.id FROM event_information " . " LEFT JOIN user_watches_site_information ON  user_watches_site_information.site_id = event_information.site_id " . "AND user_watches_site_information.user_account_id = :user_account_id AND user_watches_site_information.is_watching='1' " . "  LEFT JOIN event_in_group ON event_in_group.event_id = event_information.id AND event_in_group.removed_at IS NULL " . " LEFT JOIN user_watches_group_information ON user_watches_group_information.group_id = event_in_group.group_id " . "AND user_watches_group_information.user_account_id = :user_account_id AND user_watches_group_information.is_watching='1' " . " LEFT JOIN venue_information ON venue_information.id = event_information.venue_id " . " LEFT JOIN cached_area_has_parent ON ( venue_information.area_id = cached_area_has_parent.area_id OR event_information.area_id = cached_area_has_parent.area_id) " . " LEFT JOIN user_watches_area_information ON ( " . "user_watches_area_information.area_id = event_information.area_id OR user_watches_area_information.area_id = venue_information.area_id " . " OR user_watches_area_information.area_id = cached_area_has_parent.has_parent_area_id " . ") " . "AND user_watches_area_information.user_account_id = :user_account_id AND user_watches_area_information.is_watching='1' " . " WHERE user_watches_site_information.is_watching='1' OR user_watches_group_information.is_watching='1'  OR user_watches_area_information.is_watching='1'" . " )  ";
             }
             if ($this->userAccountIncludeAttending) {
                 if ($this->userAccountIncludePrivate) {
                     $w[] = " user_at_event_information.is_plan_attending = '1' ";
                     $w[] = " user_at_event_information.is_plan_maybe_attending = '1' ";
                 } else {
                     $w[] = " (user_at_event_information.is_plan_attending = '1' AND user_at_event_information.is_plan_public  = '1' )";
                     $w[] = " (user_at_event_information.is_plan_maybe_attending = '1' AND user_at_event_information.is_plan_public  = '1' )";
                 }
             }
             $this->where[] = "  (  " . implode(" OR ", $w) . ") ";
         }
         $this->params['user_account_id'] = $this->userAccount->getId();
     }
     if ($this->include_venue_information || $this->include_area_information || $this->must_have_lat_lng) {
         if (!in_array($joinsVenueInfoSQL, $this->joins)) {
             $this->joins[] = $joinsVenueInfoSQL;
         }
         if ($this->include_venue_information) {
             $this->select[] = "  venue_information.lng AS venue_lng";
             $this->select[] = "  venue_information.lat AS venue_lat";
             $this->select[] = "  venue_information.title AS venue_title";
             $this->select[] = "  venue_information.slug AS venue_slug";
             $this->select[] = "  venue_information.description AS venue_description";
             $this->select[] = "  venue_information.address AS venue_address";
             $this->select[] = "  venue_information.address_code AS venue_address_code";
         }
         if ($this->include_area_information) {
             $this->joins[] = " LEFT JOIN area_information ON area_information.id = event_information.area_id OR area_information.id = venue_information.area_id";
             $this->select[] = "  area_information.title AS area_title";
             $this->select[] = "  area_information.slug AS area_slug";
             $this->select[] = "  area_information.id AS area_information_id";
             // we already have a area_id so called something different
         }
         if ($this->must_have_lat_lng) {
             $this->where[] = " venue_information.lat IS NOT NULL ";
             $this->where[] = " venue_information.lng IS NOT NULL ";
         }
     }
     if ($this->include_country_information) {
         $this->joins[] = " LEFT JOIN country ON country.id = event_information.country_id  ";
         $this->select[] = "  country.two_char_code AS country_two_char_code";
         $this->select[] = "  country.title AS country_title";
     }
     if ($this->venueVirtualOnly) {
         $this->where[] = " event_information.is_virtual = '1' ";
     }
     if ($this->event_recur_set_id) {
         $this->where[] = " event_information.event_recur_set_id = :event_recur_set_id ";
         $this->params['event_recur_set_id'] = $this->event_recur_set_id;
     }
     if ($this->tag) {
         $this->joins[] = "  JOIN event_has_tag ON event_has_tag.event_id = event_information.id AND  event_has_tag.tag_id = :tag_id AND event_has_tag.removed_at IS NULL";
         $this->params['tag_id'] = $this->tag->getId();
     }
     if ($this->freeTextSearch) {
         $this->where[] = '(CASE WHEN event_information.summary IS NULL THEN \'\' ELSE event_information.summary END)   || ' . '\' \' || ' . '(CASE WHEN event_information.description IS NULL THEN \'\' ELSE event_information.description END) || ' . '\' \' || ' . '(CASE WHEN group_information.title IS NULL THEN \'\' ELSE group_information.title END)' . ' ILIKE :free_text_search ';
         $this->params['free_text_search'] = "%" . strtolower($this->freeTextSearch) . "%";
     }
     if ($this->includeMediasSlugs) {
         $this->select[] = "  (SELECT  array_to_string(array_agg(media_information.slug), ',') FROM media_information " . " JOIN media_in_event ON media_information.id = media_in_event.media_id " . " WHERE media_information.deleted_at IS NULL AND media_information.is_file_lost='0' " . " AND media_in_event.removal_approved_at IS NULL AND media_in_event.event_id = event_information.id " . " GROUP BY event_information.id ) AS media_event_slugs ";
         $this->select[] = "  (SELECT  array_to_string(array_agg(media_information.slug), ',') FROM media_information " . " JOIN media_in_group ON media_information.id = media_in_group.media_id " . " JOIN event_in_group ON event_in_group.group_id = media_in_group.group_id " . " WHERE media_information.deleted_at IS NULL AND media_information.is_file_lost='0' " . " AND media_in_group.removal_approved_at IS NULL " . " AND event_in_group.removal_approved_at IS NULL AND event_in_group.event_id = event_information.id " . " GROUP BY event_information.id ) AS media_group_slugs ";
         $this->select[] = "  (SELECT  array_to_string(array_agg(media_information.slug), ',') FROM media_information " . " JOIN media_in_venue ON media_information.id = media_in_venue.media_id " . " WHERE media_information.deleted_at IS NULL AND media_information.is_file_lost='0' " . " AND media_in_venue.removal_approved_at IS NULL AND media_in_venue.venue_id = event_information.venue_id " . " GROUP BY event_information.venue_id ) AS media_venue_slugs ";
     }
     if ($this->editedByUser) {
         $this->where[] = " event_information.id IN (SELECT event_id FROM event_history WHERE user_account_id = :editedByUser) ";
         $this->params['editedByUser'] = $this->editedByUser->getId();
     }
 }
 function testRRuleDeleteByExDate1()
 {
     global $CONFIG;
     \TimeSource::mock(2015, 1, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 77760000;
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // ============================================= Import CREATE
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part1.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     \TimeSource::mock(2015, 1, 2, 1, 1, 1);
     // ============================================= Import With no changes
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part1.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     \TimeSource::mock(2015, 1, 3, 1, 1, 1);
     // ============================================= Import WITH ONE DELETED!
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part2.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $erb->setIncludeDeleted(true);
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertTrue($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
 }
 function testAddSet()
 {
     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($this->mktime(2013, 8, 1, 19, 0, 0));
     $event->setEndAt($this->mktime(2013, 8, 1, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $group1 = new GroupModel();
     $group1->setTitle("test");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("test2");
     $group2->setDescription("test 2");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group1, $site, $user);
     $groupRepo->create($group2, $site, $user);
     ## Add event to group1, test
     $groupRepo->addEventToGroup($event, $group1, $user);
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($group1->getId(), $event->getGroupId());
     $this->assertEquals("test", $event->getGroupTitle());
     ## Add event to group2, test group1 is still main group
     $groupRepo->addEventToGroup($event, $group2, $user);
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($group1->getId(), $event->getGroupId());
     $this->assertEquals("test", $event->getGroupTitle());
     ## set main group to group2, test
     $groupRepo->setMainGroupForEvent($group2, $event, $user);
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($group2->getId(), $event->getGroupId());
     $this->assertEquals("test2", $event->getGroupTitle());
 }
 function testLimits()
 {
     global $CONFIG;
     \TimeSource::mock(2012, 9, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 77760000;
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ICALManyEvents.ical');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(2);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(2, count($events));
 }
 public function getNotificationURL()
 {
     global $CONFIG;
     $this->loadGroupIfNeeded();
     return $CONFIG->getWebSiteDomainSecure($this->site->getSlug()) . '/group/' . $this->group->getSlugForUrl();
 }
 /**
  * This function takes a set of proposed new events in. It looks for any duplicate events already saved and filters them out.
  * @return Array New proposed events where duplicates don't exist.
  */
 public function filterEventsForExisting(EventModel $sourceEvent, $events)
 {
     $group = new GroupModel();
     $group->setId($sourceEvent->getGroupId());
     $out = array();
     foreach ($events as $event) {
         $erb = new EventRepositoryBuilder();
         $erb->setGroup($group);
         $erb->setStart($event->getStartAt());
         $erb->setEnd($event->getEndAt());
         $existingEvents = $erb->fetchAll();
         if (count($existingEvents) > 0) {
         } else {
             $out[] = $event;
         }
     }
     return $out;
 }
 /**
  * @dataProvider dataForTestSet
  */
 function testSet($set, $result)
 {
     $group = new GroupModel();
     $group->setTwitterUsername($set);
     $this->assertEquals($result, $group->getTwitterUsername());
 }
 function testUserWatchingGroupsWithAnEventInTwoGroups()
 {
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $groupRepo = new GroupRepository();
     $eventRepository = new EventRepository();
     $userWatchesGroupRepo = new \repositories\UserWatchesGroupRepository();
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo->create($user);
     $userWatchesMain = new UserAccountModel();
     $userWatchesMain->setEmail("*****@*****.**");
     $userWatchesMain->setUsername("test1");
     $userWatchesMain->setPassword("password1");
     $userRepo->create($userWatchesMain);
     $userWatchesOther = new UserAccountModel();
     $userWatchesOther->setEmail("*****@*****.**");
     $userWatchesOther->setUsername("test2");
     $userWatchesOther->setPassword("password1");
     $userRepo->create($userWatchesOther);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo->create($site, $user, array($countryRepo->loadByTwoCharCode('GB')), $this->getSiteQuotaUsedForTesting());
     $groupMain = new GroupModel();
     $groupMain->setTitle("test");
     $groupRepo->create($groupMain, $site, $user);
     $groupOther = new GroupModel();
     $groupOther->setTitle("test");
     $groupRepo->create($groupOther, $site, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2014, 11, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 11, 10, 21, 0, 0));
     $eventRepository->create($event, $site, $user, $groupMain, array($groupOther));
     // test before
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesMain, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesMain, false, true, true, false);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesOther, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesOther, false, true, true, false);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     // test watching main group gets event
     $userWatchesGroupRepo->startUserWatchingGroup($userWatchesMain, $groupMain);
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesMain, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesMain, false, true, true, false);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     // test watching other group gets event
     $userWatchesGroupRepo->startUserWatchingGroup($userWatchesOther, $groupOther);
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesOther, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $erb = new EventRepositoryBuilder();
     $erb->setUserAccount($userWatchesOther, false, true, true, false);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
 }