function testFilterAreaAndIncludeAreaAndIncludeVenue()
 {
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $areaRepo = new AreaRepository();
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo->create($user);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo->create($site, $user, array($countryRepo->loadByTwoCharCode('GB')), $this->getSiteQuotaUsedForTesting());
     $area = new AreaModel();
     $area->setTitle("test");
     $area->setDescription("test test");
     $areaRepo->create($area, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaRepo->buildCacheAreaHasParent($area);
     ######################## For now just test it doesn't crash, I commited a bug that did crash here
     $erb = new EventRepositoryBuilder();
     $erb->setArea($area);
     $erb->setIncludeVenueInformation(true);
     $erb->setIncludeAreaInformation(true);
     $erb->fetchAll();
 }
 function testIntegration1()
 {
     $this->addCountriesToTestDB();
     \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());
     $countryRepo = new CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode('GB');
     ## Create area
     \TimeSource::mock(2014, 1, 1, 13, 0, 0);
     $area = new AreaModel();
     $area->setTitle("test");
     $area->setDescription("test test");
     $area->setCountryId($gb->getId());
     $areaRepo = new AreaRepository();
     $areaRepo->create($area, null, $site, $gb, $user);
     ## Edit area
     \TimeSource::mock(2014, 1, 1, 14, 0, 0);
     $area = $areaRepo->loadById($area->getId());
     $area->setDescription("testy");
     $areaRepo->edit($area, $user);
     ## Now save changed flags on these .....
     $areaHistoryRepo = new AreaHistoryRepository();
     $stat = $this->app['db']->prepare("SELECT * FROM area_history");
     $stat->execute();
     while ($data = $stat->fetch()) {
         $areaHistory = new AreaHistoryModel();
         $areaHistory->setFromDataBaseRow($data);
         $areaHistoryRepo->ensureChangedFlagsAreSet($areaHistory);
     }
     ## Now load and check
     $historyRepo = new HistoryRepositoryBuilder();
     $historyRepo->setIncludeEventHistory(false);
     $historyRepo->setIncludeVenueHistory(false);
     $historyRepo->setIncludeGroupHistory(false);
     $historyRepo->setIncludeAreaHistory(true);
     $histories = $historyRepo->fetchAll();
     $this->assertEquals(2, count($histories));
     #the edit
     $this->assertEquals(FALSE, $histories[0]->getTitleChanged());
     $this->assertEquals(true, $histories[0]->getDescriptionChanged());
     $this->assertEquals(false, $histories[0]->getCountryIdChanged());
     $this->assertEquals(false, $histories[0]->getParentAreaIdChanged());
     $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]->getCountryIdChanged());
     $this->assertEquals(false, $histories[1]->getParentAreaIdChanged());
     $this->assertEquals(false, $histories[1]->getIsDeletedChanged());
 }
 protected function build()
 {
     if ($this->site) {
         $this->where[] = " human_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->humansForEvent) {
         $this->joins[] = "  JOIN event_has_human ON event_has_human.human_id = human_information.id AND  event_has_human.event_id = :event_id AND event_has_human.removed_at IS NULL";
         $this->params['event_id'] = $this->humansForEvent->getId();
     } else {
         if ($this->humansNotForEvent) {
             $this->joins[] = " LEFT JOIN event_has_human ON event_has_human.human_id = human_information.id AND  event_has_human.event_id = :event_id AND event_has_human.removed_at IS NULL";
             $this->params['event_id'] = $this->humansNotForEvent->getId();
             $this->where[] = ' event_has_human.event_id IS NULL ';
         }
     }
     if ($this->area) {
         // TODO direct areas only, should do child areas to. But not now.
         $this->joins[] = "  JOIN human_in_area ON human_in_area.human_id = human_information.id AND  human_in_area.area_id = :area_id AND human_in_area.removed_at IS NULL";
         $this->params['area_id'] = $this->area->getId();
     }
     if (!$this->include_deleted) {
         $this->where[] = " human_information.is_deleted = '0' ";
     }
     if ($this->freeTextSearch) {
         $this->where[] = '(CASE WHEN human_information.title IS NULL THEN \'\' ELSE human_information.title END )  || \' \' || ' . '(CASE WHEN human_information.description IS NULL THEN \'\' ELSE human_information.description END )' . ' ILIKE :free_text_search ';
         $this->params['free_text_search'] = "%" . strtolower($this->freeTextSearch) . "%";
     }
 }
 /**
  * @dataProvider dataForTestSet
  */
 function testSet1($slug, $text, $result)
 {
     $area = new AreaModel();
     $area->setSlug($slug);
     $area->setTitle($text);
     $this->assertEquals($result, $area->getSlugForUrl());
 }
 function test1()
 {
     $this->addCountriesToTestDB();
     \TimeSource::mock(2014, 10, 1, 1, 1, 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());
     $countryRepo = new CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode('GB');
     $area = new AreaModel();
     $area->setTitle("test");
     $area->setDescription("test test");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($area, null, $site, $gb, $user);
     $venue = new VenueModel();
     $venue->setTitle("test");
     $venue->setDescription("test test");
     $venue->setCountryId($gb->getId());
     $venue->setAreaId($area->getId());
     $venueRepo = new VenueRepository();
     $venueRepo->create($venue, $site, $user);
     $venueDuplicate = new VenueModel();
     $venueDuplicate->setTitle("test Duplicate");
     $venueRepo->create($venueDuplicate, $site, $user);
     \TimeSource::mock(2014, 10, 1, 1, 2, 0);
     $venueRepo->markDuplicate($venueDuplicate, $venue, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0, 'Europe/London'));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0, 'Europe/London'));
     $event->setVenueId($venue->getId());
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     ## Test
     $this->assertNotNull($venueRepo->loadBySlug($site, $venue->getSlug()));
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($venue->getId(), $event->getVenueId());
     ## Now Purge!
     $venueRepo->purge($venue);
     ## Test
     $this->assertNull($venueRepo->loadBySlug($site, $venue->getSlug()));
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertNull($event->getVenueId());
 }
 public function addEvent(EventModel $event, $groups = array(), VenueModel $venue = null, AreaModel $area = null, CountryModel $country = null, $eventMedias = array())
 {
     global $CONFIG;
     $out = array('slug' => $event->getSlug(), 'slugforurl' => $event->getSlugForUrl(), 'summary' => $event->getSummary(), 'summaryDisplay' => $event->getSummaryDisplay(), 'description' => $event->getDescription() ? $event->getDescription() : '', 'deleted' => (bool) $event->getIsDeleted(), 'cancelled' => (bool) $event->getIsCancelled(), 'is_physical' => (bool) $event->getIsPhysical(), 'is_virtual' => (bool) $event->getIsVirtual(), 'custom_fields' => array());
     $out['siteurl'] = $CONFIG->isSingleSiteMode ? 'http://' . $CONFIG->webSiteDomain . '/event/' . $event->getSlugForUrl() : 'http://' . ($this->site ? $this->site->getSlug() : $event->getSiteSlug()) . "." . $CONFIG->webSiteDomain . '/event/' . $event->getSlugForUrl();
     $out['url'] = $event->getUrl() && filter_var($event->getUrl(), FILTER_VALIDATE_URL) ? $event->getUrl() : $out['siteurl'];
     $out['ticket_url'] = $event->getTicketUrl() && filter_var($event->getTicketUrl(), FILTER_VALIDATE_URL) ? $event->getTicketUrl() : null;
     $out['timezone'] = $event->getTimezone();
     $startLocal = clone $event->getStartAt();
     $startLocal->setTimeZone($this->localTimeZone);
     $startTimeZone = clone $event->getStartAt();
     $startTimeZone->setTimeZone(new \DateTimeZone($event->getTimezone()));
     $out['start'] = array('timestamp' => $event->getStartAt()->getTimestamp(), 'rfc2882utc' => $event->getStartAt()->format('r'), 'rfc2882local' => $startLocal->format('r'), 'displaylocal' => $startLocal->format('D j M Y h:ia'), 'yearlocal' => $startLocal->format('Y'), 'monthlocal' => $startLocal->format('n'), 'daylocal' => $startLocal->format('j'), 'hourlocal' => $startLocal->format('G'), 'minutelocal' => $startLocal->format('i'), 'rfc2882timezone' => $startTimeZone->format('r'), 'displaytimezone' => $startTimeZone->format('D j M Y h:ia'), 'yeartimezone' => $startTimeZone->format('Y'), 'monthtimezone' => $startTimeZone->format('n'), 'daytimezone' => $startTimeZone->format('j'), 'hourtimezone' => $startTimeZone->format('G'), 'minutetimezone' => $startTimeZone->format('i'));
     $endLocal = clone $event->getEndAt();
     $endLocal->setTimeZone($this->localTimeZone);
     $endTimeZone = clone $event->getEndAt();
     $endTimeZone->setTimeZone(new \DateTimeZone($event->getTimezone()));
     $out['end'] = array('timestamp' => $event->getEndAt()->getTimestamp(), 'rfc2882utc' => $event->getEndAt()->format('r'), 'rfc2882local' => $endLocal->format('r'), 'displaylocal' => $endLocal->format('D j M Y h:ia'), 'yearlocal' => $endLocal->format('Y'), 'monthlocal' => $endLocal->format('n'), 'daylocal' => $endLocal->format('j'), 'hourlocal' => $endLocal->format('G'), 'minutelocal' => $endLocal->format('i'), 'rfc2882timezone' => $endTimeZone->format('r'), 'displaytimezone' => $endTimeZone->format('D j M Y h:ia'), 'yeartimezone' => $endTimeZone->format('Y'), 'monthtimezone' => $endTimeZone->format('n'), 'daytimezone' => $endTimeZone->format('j'), 'hourtimezone' => $endTimeZone->format('G'), 'minutetimezone' => $endTimeZone->format('i'));
     if (is_array($groups)) {
         $out['groups'] = array();
         foreach ($groups as $group) {
             $out['groups'][] = array('slug' => $group->getSlug(), 'title' => $group->getTitle(), 'description' => $group->getDescription());
         }
     }
     if ($venue) {
         $out['venue'] = array('slug' => $venue->getSlug(), 'title' => $venue->getTitle(), 'description' => $venue->getDescription(), 'address' => $venue->getAddress(), 'addresscode' => $venue->getAddressCode(), 'lat' => $venue->getLat(), 'lng' => $venue->getLng());
     }
     if ($area) {
         $out['areas'] = array(array('slug' => $area->getSlug(), 'title' => $area->getTitle()));
     }
     if ($country) {
         $out['country'] = array('title' => $country->getTitle());
     }
     if (is_array($eventMedias)) {
         $out['medias'] = array();
         $siteurl = $CONFIG->getWebSiteDomainSecure($this->site->getSlug());
         foreach ($eventMedias as $eventMedia) {
             $out['medias'][] = array('slug' => $eventMedia->getSlug(), 'title' => $eventMedia->getTitle(), 'sourceUrl' => $eventMedia->getSourceUrl(), 'sourcetext' => $eventMedia->getSourceText(), 'picture' => array('fullURL' => $siteurl . '/media/' . $eventMedia->getSlug() . '/full', 'normalURL' => $siteurl . '/media/' . $eventMedia->getSlug() . '/normal', 'thumbnailURL' => $siteurl . '/media/' . $eventMedia->getSlug() . '/thumbnail'));
         }
     }
     if ($this->site) {
         foreach ($this->site->getCachedEventCustomFieldDefinitionsAsModels() as $customField) {
             $out['custom_fields'][$customField->getKey()] = $event->getCustomField($customField);
         }
     }
     $this->events[] = $out;
 }
 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->area->setTitleIfDifferent($ourRequest->getGetOrPostString('title', ''))) {
         $edits = true;
     }
     if ($edits) {
         $repo = new AreaRepository();
         $repo->edit($this->area, $app['apiUser']);
         $out = array('edited' => true);
     } else {
         $out = array('edited' => false);
     }
     return json_encode($out);
 }
 /**
  * This will always return something. If one doesn't exist, one will be created.
  * @return UserWatchesSiteStopModel
  */
 public function getForUserAndArea(UserAccountModel $user, AreaModel $area)
 {
     global $DB;
     $stat = $DB->prepare("SELECT * FROM user_watches_area_stop WHERE user_account_id=:uid AND area_id=:gid");
     $stat->execute(array('uid' => $user->getId(), 'gid' => $area->getId()));
     if ($stat->rowCount() > 0) {
         $uwgs = new UserWatchesAreaStopModel();
         $uwgs->setFromDataBaseRow($stat->fetch());
         return $uwgs;
     }
     $uwgs = new UserWatchesAreaStopModel();
     $uwgs->setUserAccountId($user->getId());
     $uwgs->setAreaId($area->getId());
     $uwgs->setAccessKey(createKey(2, 150));
     // TODO check not already used
     $stat = $DB->prepare("INSERT INTO user_watches_area_stop (user_account_id, area_id, access_key, created_at) " . "VALUES (:user_account_id, :area_id, :access_key, :created_at)");
     $stat->execute(array('user_account_id' => $uwgs->getUserAccountId(), 'area_id' => $uwgs->getAreaId(), 'access_key' => $uwgs->getAccessKey(), 'created_at' => \TimeSource::getFormattedForDataBase()));
     return $uwgs;
 }
 public function create(AreaCommentModel $areaComment, AreaModel $area, UserAccountModel $creator)
 {
     global $DB;
     try {
         $DB->beginTransaction();
         $stat = $DB->prepare("SELECT max(slug) AS c FROM area_comment_information WHERE area_id=:area_id");
         $stat->execute(array('area_id' => $area->getId()));
         $data = $stat->fetch();
         $areaComment->setSlug($data['c'] + 1);
         $stat = $DB->prepare("INSERT INTO area_comment_information (area_id, slug, title, comment, user_account_id, is_deleted, created_at, approved_at) " . "VALUES (:area_id, :slug, :title, :comment, :user_account_id, '0', :created_at, :approved_at) RETURNING id");
         $stat->execute(array('area_id' => $area->getId(), 'slug' => $areaComment->getSlug(), 'title' => substr($areaComment->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'comment' => $areaComment->getComment(), 'user_account_id' => $creator->getId(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $data = $stat->fetch();
         $areaComment->setId($data['id']);
         $stat = $DB->prepare("INSERT INTO area_comment_history (area_comment_id, title, comment, user_account_id, is_deleted, created_at, approved_at, is_new) VALUES " . "(:area_comment_id, :title, :comment, :user_account_id, '0', :created_at, :approved_at, '1')");
         $stat->execute(array('area_comment_id' => $areaComment->getId(), 'title' => substr($areaComment->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'comment' => $areaComment->getComment(), 'user_account_id' => $creator->getId(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 public function setFromDataBaseRow($data)
 {
     $this->id = $data['id'];
     $this->area_id = $data['area_id'];
     $this->slug = $data['slug'];
     $this->title = $data['title'];
     $this->comment = $data['comment'];
     $this->user_account_id = $data['user_account_id'];
     if (isset($data['user_account_username'])) {
         $this->user_account_username = $data['user_account_username'];
     }
     $this->is_deleted = $data['is_deleted'];
     $this->is_closed_by_admin = $data['is_closed_by_admin'];
     $utc = new \DateTimeZone("UTC");
     $this->created_at = new \DateTime($data['created_at'], $utc);
     if (isset($data['area_information_slug']) && $data['area_information_slug']) {
         $this->area = new AreaModel();
         $this->area->setTitle($data['area_information_title']);
         $this->area->setSlug($data['area_information_slug']);
     }
 }
 protected function build()
 {
     global $DB;
     $this->select[] = "  venue_information.* ";
     if ($this->site) {
         $this->where[] = " venue_information.site_id = :site_id ";
         $this->params['site_id'] = $this->site->getId();
     }
     if ($this->country) {
         $this->where[] = " venue_information.country_id = :country_id ";
         $this->params['country_id'] = $this->country->getId();
     }
     if ($this->area) {
         $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->where[] = " venue_information.area_id IN (" . implode(",", $areaids) . ")";
     }
     if ($this->freeTextSearch) {
         $this->where[] = '(CASE WHEN venue_information.title IS NULL THEN \'\' ELSE venue_information.title END )  || \' \' || ' . '(CASE WHEN venue_information.description IS NULL THEN \'\' ELSE venue_information.description END ) || \' \' || ' . '(CASE WHEN venue_information.address IS NULL THEN \'\' ELSE venue_information.address END ) || \' \' || ' . '(CASE WHEN venue_information.address_code IS NULL THEN \'\' ELSE venue_information.address_code END ) ' . ' ILIKE :free_text_search ';
         $this->params['free_text_search'] = "%" . strtolower($this->freeTextSearch) . "%";
     }
     if ($this->freeTextSearchAddressCode) {
         $this->where[] = ' venue_information.address_code ILIKE :free_text_search_address_code ';
         $this->params['free_text_search_address_code'] = "%" . strtolower($this->freeTextSearchAddressCode) . "%";
     }
     if ($this->freeTextSearchAddress) {
         $this->where[] = 'venue_information.address ILIKE :free_text_search_address ';
         $this->params['free_text_search_address'] = "%" . strtolower($this->freeTextSearchAddress) . "%";
     }
     if ($this->freeTextSearchTitle) {
         $this->where[] = 'venue_information.title ILIKE :free_text_search_title ';
         $this->params['free_text_search_title'] = "%" . strtolower($this->freeTextSearchTitle) . "%";
     }
     if (!$this->include_deleted) {
         $this->where[] = " venue_information.is_deleted = '0' ";
     }
     if ($this->includeMediasSlugs) {
         $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 = venue_information.id " . " GROUP BY venue_information.id ) AS media_venue_slugs ";
     }
     if ($this->must_have_lat_lng) {
         $this->where[] = " venue_information.lat IS NOT NULL ";
         $this->where[] = " venue_information.lng IS NOT NULL ";
     }
     if ($this->editedByUser) {
         $this->where[] = " venue_information.id IN (SELECT venue_id FROM venue_history WHERE user_account_id = :editedByUser) ";
         $this->params['editedByUser'] = $this->editedByUser->getId();
     }
 }
function stripCompassEndOfArea(\models\AreaModel $areaModel, $modeCurrent, $magicWords)
{
    $title = str_replace(",", " ", $areaModel->getTitle());
    do {
        $flag = false;
        foreach ($magicWords as $check) {
            if ($modeCurrent == MODE_AFTER) {
                if (strtolower(substr($title, 0 - strlen(" " . $check))) == strtolower(" " . $check)) {
                    $title = trim(substr($title, 0, 0 - strlen($check)));
                    $flag = true;
                }
            }
            if ($modeCurrent == MODE_PRE) {
                if (strtolower(substr($title, 0, strlen($check . " "))) == strtolower($check . " ")) {
                    $title = trim(substr($title, strlen($check)));
                    $flag = true;
                }
            }
        }
    } while ($flag);
    return trim($title);
}
 protected function newEventFromImportedEventModel(ImportedEventModel $importedEvent)
 {
     $event = new EventModel();
     $event->setFromImportedEventModel($importedEvent);
     if ($this->site->getIsFeaturePhysicalEvents() && !$this->site->getIsFeatureVirtualEvents()) {
         $event->setIsPhysical(true);
         $event->setIsVirtual(false);
     } else {
         if (!$this->site->getIsFeaturePhysicalEvents() && $this->site->getIsFeatureVirtualEvents()) {
             $event->setIsPhysical(false);
             $event->setIsVirtual(true);
         }
     }
     if ($this->country) {
         // country is set on importer.
         $event->setCountryId($this->country->getId());
         $timezones = $this->country->getTimezonesAsList();
         if ($importedEvent->getTimezone() && in_array($importedEvent->getTimezone(), $timezones)) {
             $event->setTimezone($importedEvent->getTimezone());
         } else {
             if ($timezones) {
                 // take first timezone in that country at random :-/
                 $event->setTimezone($timezones[0]);
             }
         }
         if ($this->area) {
             $event->setAreaId($this->area->getId());
         }
     } else {
         // if no country set on importer, we just pick first one at random :-/
         $crb = new \repositories\builders\CountryRepositoryBuilder();
         $crb->setSiteIn($this->site);
         $crb->setLimit(1);
         $countries = $crb->fetchAll();
         if (count($countries) > 0) {
             $country = $countries[0];
             $event->setCountryId($country->getId());
             $timezones = $country->getTimezonesAsList();
             if ($importedEvent->getTimezone() && in_array($importedEvent->getTimezone(), $timezones)) {
                 $event->setTimezone($importedEvent->getTimezone());
             } else {
                 if ($timezones) {
                     // take first timezone in that country at random :-/
                     $event->setTimezone($timezones[0]);
                 }
             }
         }
     }
     return $event;
 }
 public function addEvent(EventModel $event, $groups = array(), VenueModel $venue = null, AreaModel $area = null, CountryModel $country = null, $eventMedias = array())
 {
     global $CONFIG;
     $siteurlbase = $CONFIG->getWebSiteDomainSecure($this->site ? $this->site->getSlug() : $event->getSiteSlug());
     $siteurl = $siteurlbase . '/event/' . $event->getSlugForUrl();
     $url = $event->getUrl() && filter_var($event->getUrl(), FILTER_VALIDATE_URL) ? $event->getUrl() : $siteurl;
     $ticket_url = $event->getTicketUrl() && filter_var($event->getTicketUrl(), FILTER_VALIDATE_URL) ? $event->getTicketUrl() : null;
     $this->events[] = $this->getCell($event->getSlug()) . $this->delimiter . $this->getCell($event->getSlugForUrl()) . $this->delimiter . $this->getCell($event->getSummary()) . $this->delimiter . $this->getCell($event->getSummaryDisplay()) . $this->delimiter . $this->getCell($event->getDescription()) . $this->delimiter . $this->getCellBoolean($event->getIsDeleted()) . $this->delimiter . $this->getCellBoolean($event->getIsCancelled()) . $this->delimiter . $this->getCellBoolean($event->getIsPhysical()) . $this->delimiter . $this->getCellBoolean($event->getIsVirtual()) . $this->delimiter . $this->getCell($siteurl) . $this->delimiter . $this->getCell($url) . $this->delimiter . $this->getCell($ticket_url) . $this->delimiter . $this->getCell($event->getTimezone()) . $this->delimiter . $this->getCell($event->getStartAtInUTC()->format("r")) . $this->delimiter . $this->getCell($event->getEndAtInUTC()->format("r")) . $this->delimiter . $this->getCell($event->getStartAtInTimezone()->format("r")) . $this->delimiter . $this->getCell($event->getEndAtInTimezone()->format("r")) . $this->delimiter . ($venue ? $this->getCell($venue->getSlug()) . $this->delimiter . $this->getCell($venue->getSlugForUrl()) . $this->delimiter . $this->getCell($venue->getTitle()) . $this->delimiter . $this->getCell($venue->getDescription()) . $this->delimiter . $this->getCell($venue->getAddress()) . $this->delimiter . $this->getCell($venue->getAddressCode()) . $this->delimiter . $this->getCell($venue->getLat()) . $this->delimiter . $this->getCell($venue->getLng()) . $this->delimiter . $this->getCell($siteurlbase . '/venue/' . $venue->getSlugForUrl()) . $this->delimiter : $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter) . ($area ? $this->getCell($area->getSlug()) . $this->delimiter . $this->getCell($area->getSlugForUrl()) . $this->delimiter . $this->getCell($area->getTitle()) . $this->delimiter . $this->getCell($area->getDescription()) . $this->delimiter . $this->getCell($siteurlbase . '/area/' . $area->getSlugForUrl()) . $this->delimiter : $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter . $this->delimiter) . ($country ? $this->getCell($country->getTwoCharCode()) . $this->delimiter . $this->getCell($country->getTitle()) . $this->delimiter . $this->getCell($siteurlbase . '/country/' . $country->getTwoCharCode()) . $this->delimiter : $this->delimiter . $this->delimiter . $this->delimiter);
 }
 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 newVenue(Request $request, Application $app)
 {
     $areaRepository = new AreaRepository();
     $countryRepository = new CountryRepository();
     $venue = new VenueModel();
     $this->parameters = array('country' => null, 'parentAreas' => array(), 'area' => null, 'childAreas' => array(), 'startAreaBrowserFromScratch' => true);
     if (isset($_GET['area_id'])) {
         $ar = new AreaRepository();
         $this->parameters['area'] = $ar->loadBySlug($app['currentSite'], $_GET['area_id']);
         if ($this->parameters['area']) {
             $checkArea = $this->parameters['area']->getParentAreaId() ? $ar->loadById($this->parameters['area']->getParentAreaId()) : null;
             while ($checkArea) {
                 array_unshift($this->parameters['parentAreas'], $checkArea);
                 $checkArea = $checkArea->getParentAreaId() ? $ar->loadById($checkArea->getParentAreaId()) : null;
             }
             $cr = new CountryRepository();
             $this->parameters['country'] = $cr->loadById($this->parameters['area']->getCountryID());
             $venue->setCountryId($this->parameters['country']->getId());
             $areaRepoBuilder = new AreaRepositoryBuilder();
             $areaRepoBuilder->setSite($app['currentSite']);
             $areaRepoBuilder->setCountry($this->parameters['country']);
             $areaRepoBuilder->setParentArea($this->parameters['area']);
             $areaRepoBuilder->setIncludeDeleted(false);
             $this->parameters['childAreas'] = $areaRepoBuilder->fetchAll();
             $this->parameters['startAreaBrowserFromScratch'] = false;
         }
     }
     $form = $app['form.factory']->create(new VenueNewForm($app['currentTimeZone'], $app), $venue);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $postAreas = $request->request->get('areas');
             if (is_array($postAreas)) {
                 $area = null;
                 foreach ($postAreas as $areaCode) {
                     if (substr($areaCode, 0, 9) == 'EXISTING:') {
                         $area = $areaRepository->loadBySlug($app['currentSite'], substr($areaCode, 9));
                     } else {
                         if (substr($areaCode, 0, 4) == 'NEW:' && $app['currentUserPermissions']->hasPermission('org.openacalendar', 'AREAS_CHANGE')) {
                             $newArea = new AreaModel();
                             $newArea->setTitle(substr($areaCode, 4));
                             $areaRepository->create($newArea, $area, $app['currentSite'], $countryRepository->loadById($venue->getCountryId()), $app['currentUser']);
                             $areaRepository->buildCacheAreaHasParent($newArea);
                             $area = $newArea;
                         }
                     }
                 }
                 if ($area) {
                     $venue->setAreaId($area->getId());
                 }
             }
             foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
                 $extension->addDetailsToVenue($venue);
             }
             $venueEditMetaData = new VenueEditMetaDataModel();
             $venueEditMetaData->setUserAccount($app['currentUser']);
             if ($form->has('edit_comment')) {
                 $venueEditMetaData->setEditComment($form->get('edit_comment')->getData());
             }
             $venueRepository = new VenueRepository();
             $venueRepository->createWithMetaData($venue, $app['currentSite'], $venueEditMetaData);
             return $app->redirect("/venue/" . $venue->getSlug());
         }
     }
     $this->parameters['form'] = $form->createView();
     return $app['twig']->render('site/venuenew/new.html.twig', $this->parameters);
 }
 function testUserWatchingParentAreaWithVenue()
 {
     TimeSource::mock(2014, 01, 01, 9, 0, 0);
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $areaRepo = new AreaRepository();
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $venueRepo = new \repositories\VenueRepository();
     $eventRepository = new EventRepository();
     $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository();
     $GB = $countryRepo->loadByTwoCharCode("GB");
     $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);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo->create($site, $user, array($countryRepo->loadByTwoCharCode('GB')), $this->getSiteQuotaUsedForTesting());
     $area = new AreaModel();
     $area->setTitle("Scotland");
     $areaRepo->create($area, null, $site, $GB);
     $areaChild = new AreaModel();
     $areaChild->setTitle("Edinburgh");
     $areaRepo->create($areaChild, $area, $site, $GB);
     $venue = new \models\VenueModel();
     $venue->setTitle("Castle");
     $venue->setAreaId($areaChild->getId());
     $venueRepo->create($venue, $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);
     $event->setVenueId($venue->getId());
     TimeSource::mock(2014, 01, 01, 9, 1, 0);
     $eventRepository->edit($event);
     // have to update child cache
     $areaRepo->buildCacheAreaHasParent($area);
     $areaRepo->buildCacheAreaHasParent($areaChild);
     // 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));
     // test watching main group gets event
     $userWatchesAreaRepo->startUserWatchingArea($userWatchesMain, $area);
     $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));
 }
 public function update(AreaModel $area, $fields, AreaEditMetaDataModel $areaEditMetaDataModel)
 {
     $alreadyInTransaction = $this->db->inTransaction();
     // Make Information Data
     $fieldsSQL1 = array();
     $fieldsParams1 = array('id' => $area->getId());
     foreach ($fields as $field) {
         $fieldsSQL1[] = " " . $field . "=:" . $field . " ";
         if ($field == 'title') {
             $fieldsParams1['title'] = substr($area->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
         } else {
             if ($field == 'description') {
                 $fieldsParams1['description'] = $area->getDescription();
             } else {
                 if ($field == 'parent_area_id') {
                     $fieldsParams1['parent_area_id'] = $area->getParentAreaId();
                 } else {
                     if ($field == 'country_id') {
                         $fieldsParams1['country_id'] = $area->getCountryId();
                     } else {
                         if ($field == 'is_duplicate_of_id') {
                             $fieldsParams1['is_duplicate_of_id'] = $area->getIsDuplicateOfId();
                         } else {
                             if ($field == 'is_deleted') {
                                 $fieldsParams1['is_deleted'] = $area->getIsDeleted() ? 1 : 0;
                             }
                         }
                     }
                 }
             }
         }
     }
     // Make History Data
     $fieldsSQL2 = array('area_id', 'user_account_id', 'created_at', 'approved_at');
     $fieldsSQLParams2 = array(':area_id', ':user_account_id', ':created_at', ':approved_at');
     $fieldsParams2 = array('area_id' => $area->getId(), 'user_account_id' => $areaEditMetaDataModel->getUserAccount() ? $areaEditMetaDataModel->getUserAccount()->getId() : null, 'created_at' => $this->timesource->getFormattedForDataBase(), 'approved_at' => $this->timesource->getFormattedForDataBase());
     if ($areaEditMetaDataModel->getEditComment()) {
         $fieldsSQL2[] = ' edit_comment ';
         $fieldsSQLParams2[] = ' :edit_comment ';
         $fieldsParams2['edit_comment'] = $areaEditMetaDataModel->getEditComment();
     }
     foreach ($this->possibleFields as $field) {
         if (in_array($field, $fields) || $field == 'title') {
             $fieldsSQL2[] = " " . $field . " ";
             $fieldsSQLParams2[] = " :" . $field . " ";
             if ($field == 'title') {
                 $fieldsParams2['title'] = substr($area->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
             } else {
                 if ($field == 'description') {
                     $fieldsParams2['description'] = $area->getDescription();
                 } else {
                     if ($field == 'country_id') {
                         $fieldsParams2['country_id'] = $area->getCountryId();
                     } else {
                         if ($field == 'parent_area_id') {
                             $fieldsParams2['parent_area_id'] = $area->getParentAreaId();
                         } else {
                             if ($field == 'is_duplicate_of_id') {
                                 $fieldsParams2['is_duplicate_of_id'] = $area->getIsDuplicateOfId();
                             } else {
                                 if ($field == 'is_deleted') {
                                     $fieldsParams2['is_deleted'] = $area->getIsDeleted() ? 1 : 0;
                                 }
                             }
                         }
                     }
                 }
             }
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " 0 ";
         } else {
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " -2 ";
         }
     }
     try {
         if (!$alreadyInTransaction) {
             $this->db->beginTransaction();
         }
         // Information SQL
         $stat = $this->db->prepare("UPDATE area_information  SET " . implode(",", $fieldsSQL1) . " WHERE id=:id");
         $stat->execute($fieldsParams1);
         // History SQL
         $stat = $this->db->prepare("INSERT INTO area_history (" . implode(",", $fieldsSQL2) . ") VALUES (" . implode(",", $fieldsSQLParams2) . ")");
         $stat->execute($fieldsParams2);
         if (!$alreadyInTransaction) {
             $this->db->commit();
         }
     } catch (Exception $e) {
         if (!$alreadyInTransaction) {
             $this->db->rollBack();
         }
         throw $e;
     }
 }
 function testBasicThenDeletedByVanishing()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $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($countryRepo->loadByTwoCharCode('GB')), $this->getSiteQuotaUsedForTesting());
     $areaRepo = new AreaRepository();
     $area = new AreaModel();
     $area->setTitle("test");
     $area->setDescription("test test");
     $areaRepo->create($area, null, $site, $countryRepo->loadByTwoCharCode('GB'), $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);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setCountryId($countryRepo->loadByTwoCharCode('GB')->getId());
     $importURL->setAreaId($area->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/BasicICAL.ical');
     $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("Test 3 SpecFic Writing Group", $event->getSummary());
     $this->assertEquals('2013-11-12 18:00:00', $event->getStartAtInUTC()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-11-12 20:30:00', $event->getEndAtInUTC()->format('Y-m-d H:i:s'));
     $this->assertEquals('http://opentechcalendar.co.uk/index.php/event/166', $event->getDescription());
     $this->assertEquals('http://opentechcalendar.co.uk/index.php/event/166', $event->getURL());
     $this->assertFalse($event->getIsDeleted());
     $this->assertEquals($countryRepo->loadByTwoCharCode('GB')->getId(), $event->getCountryId());
     $this->assertEquals($area->getId(), $event->getAreaId());
     $this->assertEquals("Europe/London", $event->getTimezone());
     // Import again
     \TimeSource::mock(2013, 10, 1, 1, 1, 2);
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/BasicICALNoEvents.ical');
     $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->assertTrue($event->getIsDeleted());
 }
 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();
     }
 }
 protected function checkChildAreaInTest1(AreaModel $area)
 {
     $this->assertEquals("test test child", $area->getDescription());
     $this->assertEquals("test child", $area->getTitle());
 }
 function testInChildArea()
 {
     $this->addCountriesToTestDB();
     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());
     $area1 = new AreaModel();
     $area1->setTitle("scotland");
     $area1child = new AreaModel();
     $area1child->setTitle("edinburgh");
     $area2 = new AreaModel();
     $area2->setTitle("england");
     $areaRepo = new AreaRepository();
     $countryRepo = new CountryRepository();
     $areaRepo->create($area1, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaRepo->buildCacheAreaHasParent($area1);
     $areaRepo->create($area1child, $area1, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaRepo->buildCacheAreaHasParent($area1child);
     $areaRepo->create($area2, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaRepo->buildCacheAreaHasParent($area2);
     $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));
     $event->setAreaId($area1child->getId());
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     #test - find in erb
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setArea($area1);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $this->assertEquals($event->getId(), $events[0]->getId());
     #test - find in erb
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setArea($area1child);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $this->assertEquals($event->getId(), $events[0]->getId());
     #test - don't find in erb
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setArea($area2);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
 }
 public function removeHumanFromAllButOneArea(HumanModel $human, AreaModel $area, UserAccountModel $user = null)
 {
     global $DB;
     $stat = $DB->prepare("UPDATE human_in_area SET removed_by_user_account_id=:removed_by_user_account_id," . " removed_at=:removed_at, removal_approved_at=:removal_approved_at WHERE " . " area_id != :area_id AND human_id=:human_id AND removed_at IS NULL ");
     $stat->execute(array('area_id' => $area->getId(), 'human_id' => $human->getId(), 'removed_at' => \TimeSource::getFormattedForDataBase(), 'removal_approved_at' => \TimeSource::getFormattedForDataBase(), 'removed_by_user_account_id' => $user ? $user->getId() : null));
 }
 public function fetchAll()
 {
     $this->buildStart();
     $this->build();
     $this->buildStat();
     $results = array();
     while ($data = $this->stat->fetch()) {
         $area = new AreaModel();
         $area->setFromDataBaseRow($data);
         $results[] = $area;
     }
     return $results;
 }
 function test1()
 {
     \TimeSource::mock(2014, 1, 1, 0, 0, 0);
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $areaRepo = new AreaRepository();
     $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($countryRepo->loadByTwoCharCode('GB')), $this->getSiteQuotaUsedForTesting());
     $area1 = new AreaModel();
     $area1->setTitle("test");
     $area1->setDescription("test test");
     $area2 = new AreaModel();
     $area2->setTitle("test this looks similar");
     $area2->setDescription("test test");
     $areaRepo->create($area1, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaRepo->create($area2, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $areaChild = new AreaModel();
     $areaChild->setTitle("child");
     $areaChild->setDescription("child");
     $areaRepo->create($areaChild, $area2, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $area1 = $areaRepo->loadById($area1->getId());
     $area2 = $areaRepo->loadById($area2->getId());
     $countryRepo = new CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode('GB');
     $venue = new VenueModel();
     $venue->setTitle("test");
     $venue->setDescription("test test");
     $venue->setCountryId($gb->getId());
     $venue->setAreaId($area2->getId());
     $venueRepo = new VenueRepository();
     $venueRepo->create($venue, $site, $user);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $event->setAreaId($area2->getId());
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     // Test before
     $venue = $venueRepo->loadById($venue->getId());
     $this->assertEquals($area2->getId(), $venue->getAreaId());
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($area2->getId(), $event->getAreaId());
     $areaChild = $areaRepo->loadById($areaChild->getId());
     $this->assertEquals($area2->getId(), $areaChild->getParentAreaId());
     $area2 = $areaRepo->loadById($area2->getId());
     $this->assertFalse($area2->getIsDeleted());
     $this->assertNull($area2->getIsDuplicateOfId());
     // Mark
     \TimeSource::mock(2014, 1, 1, 2, 0, 0);
     $areaRepo->markDuplicate($area2, $area1, $user);
     // Test Duplicate
     $venue = $venueRepo->loadById($venue->getId());
     $this->assertEquals($area1->getId(), $venue->getAreaId());
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals($area1->getId(), $event->getAreaId());
     $areaChild = $areaRepo->loadById($areaChild->getId());
     $this->assertEquals($area1->getId(), $areaChild->getParentAreaId());
     $area2 = $areaRepo->loadById($area2->getId());
     $this->assertTrue($area2->getIsDeleted());
     $this->assertEquals($area1->getId(), $area2->getIsDuplicateOfId());
 }
 function testMoveAllFutureEventsAtVenueToNoSetVenue()
 {
     $this->addCountriesToTestDB();
     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());
     $area = new AreaModel();
     $area->setTitle("scotland");
     $areaRepo = new AreaRepository();
     $countryRepo = new CountryRepository();
     $areaRepo->create($area, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     $venue = new VenueModel();
     $venue->setCountryId($countryRepo->loadByTwoCharCode('GB')->getId());
     $venue->setTitle("edinburgh hall");
     $venue->setAreaId($area->getId());
     $venueRepo = new VenueRepository();
     $venueRepo->create($venue, $site, $user);
     #### Event To Change
     $event = new EventModel();
     $event->setCountryId($countryRepo->loadByTwoCharCode('GB')->getId());
     $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));
     $event->setVenueId($venue->getId());
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     #### Load Event, Check in Venue
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals(false, $event->getIsDeleted());
     $this->assertNull($event->getAreaId());
     $this->assertEquals($venue->getId(), $event->getVenueId());
     #### In preperation for deleting event, call moveAllFutureEventsAtVenueToNoSetVenue()
     TimeSource::mock(2013, 7, 1, 8, 0, 0);
     $eventRepository->moveAllFutureEventsAtVenueToNoSetVenue($venue, $user);
     #### Load event, check in area
     $event = $eventRepository->loadBySlug($site, $event->getSlug());
     $this->assertEquals(false, $event->getIsDeleted());
     $this->assertNull($event->getVenueId());
     $this->assertEquals($area->getId(), $event->getAreaId());
 }
 public function addEvent(EventModel $event, VenueModel $venue = null, AreaModel $area = null, CountryModel $country = null)
 {
     global $CONFIG;
     $out = array('slug' => $event->getSlug(), 'slugforurl' => $event->getSlugForUrl(), 'summary' => $event->getSummary(), 'summaryDisplay' => $event->getSummaryDisplay(), 'description' => $event->getDescription() ? $event->getDescription() : '', 'deleted' => (bool) $event->getIsDeleted(), 'cancelled' => (bool) $event->getIsCancelled(), 'is_physical' => (bool) $event->getIsPhysical(), 'is_virtual' => (bool) $event->getIsVirtual());
     $out['siteurl'] = $CONFIG->isSingleSiteMode ? 'http://' . $CONFIG->webSiteDomain . '/event/' . $event->getSlugForUrl() : 'http://' . ($this->site ? $this->site->getSlug() : $event->getSiteSlug()) . "." . $CONFIG->webSiteDomain . '/event/' . $event->getSlugForUrl();
     $out['url'] = $event->getUrl() && filter_var($event->getUrl(), FILTER_VALIDATE_URL) ? $event->getUrl() : $out['siteurl'];
     $out['ticket_url'] = $event->getTicketUrl() && filter_var($event->getTicketUrl(), FILTER_VALIDATE_URL) ? $event->getTicketUrl() : null;
     $out['timezone'] = $event->getTimezone();
     $startLocal = clone $event->getStartAt();
     $startLocal->setTimeZone($this->localTimeZone);
     $startTimeZone = clone $event->getStartAt();
     $startTimeZone->setTimeZone(new \DateTimeZone($event->getTimezone()));
     $out['start'] = array('timestamp' => $event->getStartAt()->getTimestamp(), 'rfc2882utc' => $event->getStartAt()->format('r'), 'rfc2882local' => $startLocal->format('r'), 'displaylocal' => $startLocal->format('D j M Y h:ia'), 'yearlocal' => $startLocal->format('Y'), 'monthlocal' => $startLocal->format('n'), 'daylocal' => $startLocal->format('j'), 'hourlocal' => $startLocal->format('G'), 'minutelocal' => $startLocal->format('i'), 'rfc2882timezone' => $startTimeZone->format('r'), 'displaytimezone' => $startTimeZone->format('D j M Y h:ia'), 'yeartimezone' => $startTimeZone->format('Y'), 'monthtimezone' => $startTimeZone->format('n'), 'daytimezone' => $startTimeZone->format('j'), 'hourtimezone' => $startTimeZone->format('G'), 'minutetimezone' => $startTimeZone->format('i'));
     $endLocal = clone $event->getEndAt();
     $endLocal->setTimeZone($this->localTimeZone);
     $endTimeZone = clone $event->getEndAt();
     $endTimeZone->setTimeZone(new \DateTimeZone($event->getTimezone()));
     $out['end'] = array('timestamp' => $event->getEndAt()->getTimestamp(), 'rfc2882utc' => $event->getEndAt()->format('r'), 'rfc2882local' => $endLocal->format('r'), 'displaylocal' => $endLocal->format('D j M Y h:ia'), 'yearlocal' => $endLocal->format('Y'), 'monthlocal' => $endLocal->format('n'), 'daylocal' => $endLocal->format('j'), 'hourlocal' => $endLocal->format('G'), 'minutelocal' => $endLocal->format('i'), 'rfc2882timezone' => $endTimeZone->format('r'), 'displaytimezone' => $endTimeZone->format('D j M Y h:ia'), 'yeartimezone' => $endTimeZone->format('Y'), 'monthtimezone' => $endTimeZone->format('n'), 'daytimezone' => $endTimeZone->format('j'), 'hourtimezone' => $endTimeZone->format('G'), 'minutetimezone' => $endTimeZone->format('i'));
     if ($venue) {
         $out['venue'] = array('slug' => $venue->getSlug(), 'title' => $venue->getTitle(), 'description' => $venue->getDescription(), 'address' => $venue->getAddress(), 'addresscode' => $venue->getAddressCode(), 'lat' => $venue->getLat(), 'lng' => $venue->getLng());
     }
     if ($area) {
         $out['areas'] = array(array('slug' => $area->getSlug(), 'title' => $area->getTitle()));
     }
     if ($country) {
         $out['country'] = array('title' => $country->getTitle());
     }
     return $out;
 }
 function editDetails($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Venue does not exist.");
     }
     if ($this->parameters['venue']->getIsDeleted()) {
         die("No");
         // TODO
     }
     $form = $app['form.factory']->create(new VenueEditForm($app), $this->parameters['venue']);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $area = null;
             if (is_array($request->request->get('areas'))) {
                 $areaRepository = new AreaRepository();
                 $countryRepository = new CountryRepository();
                 foreach ($request->request->get('areas') as $areaCode) {
                     if (substr($areaCode, 0, 9) == 'EXISTING:') {
                         $area = $areaRepository->loadBySlug($app['currentSite'], substr($areaCode, 9));
                     } else {
                         if (substr($areaCode, 0, 4) == 'NEW:' && $app['currentUserPermissions']->hasPermission('org.openacalendar', 'AREAS_CHANGE')) {
                             $newArea = new AreaModel();
                             $newArea->setTitle(substr($areaCode, 4));
                             $areaRepository->create($newArea, $area, $app['currentSite'], $this->parameters['country'], $app['currentUser']);
                             $areaRepository->buildCacheAreaHasParent($newArea);
                             $area = $newArea;
                         }
                     }
                 }
             }
             if ($area) {
                 $this->parameters['venue']->setAreaId($area->getId());
             } else {
                 $this->parameters['venue']->setAreaId(null);
             }
             foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
                 $extension->addDetailsToVenue($this->parameters['venue']);
             }
             $venueEditMetaData = new VenueEditMetaDataModel();
             $venueEditMetaData->setUserAccount($app['currentUser']);
             if ($form->has('edit_comment')) {
                 $venueEditMetaData->setEditComment($form->get('edit_comment')->getData());
             }
             $venueEditMetaData->setFromRequest($request);
             $venueRepository = new VenueRepository();
             $venueRepository->editWithMetaData($this->parameters['venue'], $venueEditMetaData);
             return $app->redirect("/venue/" . $this->parameters['venue']->getSlugForURL());
         }
     }
     $this->parameters['form'] = $form->createView();
     return $app['twig']->render('site/venue/edit.html.twig', $this->parameters);
 }
 public function markDuplicateWithMetaData(AreaModel $duplicateArea, AreaModel $originalArea, AreaEditMetaDataModel $areaEditMetaDataModel)
 {
     global $DB;
     if ($duplicateArea->getId() == $originalArea->getId()) {
         return;
     }
     try {
         $DB->beginTransaction();
         $duplicateArea->setIsDuplicateOfId($originalArea->getId());
         $duplicateArea->setIsDeleted(true);
         $this->areaDBAccess->update($duplicateArea, array('is_duplicate_of_id', 'is_deleted'), $areaEditMetaDataModel);
         // Move Venues
         $venueDBAccess = new VenueDBAccess($DB, new \TimeSource());
         $vrb = new VenueRepositoryBuilder();
         $vrb->setArea($duplicateArea);
         $venueEditMetaData = new VenueEditMetaDataModel();
         $venueEditMetaData->setUserAccount($areaEditMetaDataModel->getUserAccount());
         foreach ($vrb->fetchAll() as $venue) {
             $venue->setAreaId($originalArea->getId());
             $venueDBAccess->update($venue, array('area_id'), $venueEditMetaData);
         }
         // Move Events
         $eventRepoBuilder = new EventRepositoryBuilder();
         $eventRepoBuilder->setArea($duplicateArea);
         $eventDBAccess = new EventDBAccess($DB, new \TimeSource());
         $eventEditMetaData = new EventEditMetaDataModel();
         $eventEditMetaData->setUserAccount($areaEditMetaDataModel->getUserAccount());
         foreach ($eventRepoBuilder->fetchAll() as $event) {
             // Check Area actually matches here because we may get events at a venue.
             // Based on the order we do things in (ie Move Venue, Move Event) we shouldn't but let's be safe.
             if ($event->getAreaId() == $duplicateArea->getId() && $event->getVenueId() == null) {
                 $event->setAreaId($originalArea->getId());
                 $eventDBAccess->update($event, array('area_id'), $eventEditMetaData);
             }
         }
         // Move Child Areas
         $areaRepoBuilder = new AreaRepositoryBuilder();
         $areaRepoBuilder->setParentArea($duplicateArea);
         $areaRepoBuilder->setIncludeParentLevels(0);
         $flag = false;
         foreach ($areaRepoBuilder->fetchAll() as $area) {
             // lets just double check we haven't got any child areas.
             if ($area->getParentAreaId() == $duplicateArea->getId()) {
                 $area->setParentAreaId($originalArea->getId());
                 $this->areaDBAccess->update($area, array('parent_area_id'), $areaEditMetaDataModel);
                 $flag = true;
             }
         }
         if ($flag) {
             // now must clear caches
             $this->deleteParentCacheForArea($originalArea);
             $this->deleteParentCacheForArea($duplicateArea);
         }
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 public function startUserWatchingAreaIfNotWatchedBefore(UserAccountModel $user, AreaModel $area)
 {
     $this->startUserWatchingAreaIdIfNotWatchedBefore($user, $area->getId());
 }