function index($siteid, Request $request, Application $app)
 {
     $sr = new SiteRepository();
     $site = $sr->loadById($siteid);
     if (!$site) {
         die("404");
     }
     $arb = new AreaRepositoryBuilder();
     $arb->setSite($site);
     $areas = $arb->fetchAll();
     return $app['twig']->render('sysadmin/arealist/index.html.twig', array('site' => $site, 'areas' => $areas));
 }
 public function listJson(Request $request, Application $app)
 {
     $arb = new AreaRepositoryBuilder();
     $arb->setSite($app['currentSite']);
     $ourRequest = new \Request($request);
     $arb->setIncludeDeleted($ourRequest->getGetOrPostBoolean('include_deleted', false));
     $out = array('areas' => array());
     foreach ($arb->fetchAll() as $area) {
         $out['areas'][] = array('slug' => $area->getSlug(), 'slugForURL' => $area->getSlugForUrl(), 'title' => $area->getTitle());
     }
     return json_encode($out);
 }
 protected function build($slug, Request $request, Application $app)
 {
     global $CONFIG;
     $this->parameters = array('country' => null, 'area' => null, 'parentAreas' => array(), 'childAreas' => array(), 'venueIsDuplicateOf' => null);
     if (strpos($slug, "-")) {
         $slug = array_shift(explode("-", $slug, 2));
     }
     $vr = new VenueRepository();
     $this->parameters['venue'] = $vr->loadBySlug($app['currentSite'], $slug);
     if (!$this->parameters['venue']) {
         return false;
     }
     if ($this->parameters['venue']->getCountryID()) {
         $cr = new CountryRepository();
         $this->parameters['country'] = $cr->loadById($this->parameters['venue']->getCountryID());
     }
     if ($this->parameters['venue']->getAreaId()) {
         $ar = new AreaRepository();
         $this->parameters['area'] = $ar->loadById($this->parameters['venue']->getAreaId());
         if (!$this->parameters['area']) {
             return false;
         }
         $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;
         }
         $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();
     } else {
         $areaRepoBuilder = new AreaRepositoryBuilder();
         $areaRepoBuilder->setSite($app['currentSite']);
         $areaRepoBuilder->setCountry($this->parameters['country']);
         $areaRepoBuilder->setNoParentArea(true);
         $areaRepoBuilder->setIncludeDeleted(false);
         $this->parameters['childAreas'] = $areaRepoBuilder->fetchAll();
     }
     if ($this->parameters['venue']->getIsDuplicateOfId()) {
         $this->parameters['venueIsDuplicateOf'] = $vr->loadByID($this->parameters['venue']->getIsDuplicateOfId());
     }
     $app['currentUserActions']->set("org.openacalendar", "venueHistory", true);
     $app['currentUserActions']->set("org.openacalendar", "venueEditDetails", $app['currentUserPermissions']->hasPermission("org.openacalendar", "VENUES_CHANGE") && $app['currentSite']->getIsFeaturePhysicalEvents() && !$this->parameters['venue']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "venueDelete", $app['currentUserPermissions']->hasPermission("org.openacalendar", "VENUES_CHANGE") && $app['currentSite']->getIsFeaturePhysicalEvents() && !$this->parameters['venue']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "venueEditMedia", $app['currentUserPermissions']->hasPermission("org.openacalendar", "VENUES_CHANGE") && $app['currentSite']->getIsFeaturePhysicalEvents() && !$this->parameters['venue']->getIsDeleted() && $CONFIG->isFileStore());
     $app['currentUserActions']->set("org.openacalendar", "venueEditPushToChildAreas", $this->parameters['childAreas'] && $app['currentUserPermissions']->hasPermission("org.openacalendar", "VENUES_CHANGE") && $app['currentSite']->getIsFeaturePhysicalEvents() && !$this->parameters['venue']->getIsDeleted());
     return true;
 }
 protected function buildTree(SiteModel $site, AreaModel $parentArea = null)
 {
     $data = array('area' => $parentArea, 'children' => array());
     $areaRepoBuilder = new AreaRepositoryBuilder();
     $areaRepoBuilder->setSite($site);
     $areaRepoBuilder->setCountry($this->parameters['country']);
     if ($parentArea) {
         $areaRepoBuilder->setParentArea($parentArea);
     } else {
         $areaRepoBuilder->setNoParentArea(true);
     }
     foreach ($areaRepoBuilder->fetchAll() as $area) {
         $data['children'][] = $this->buildTree($site, $area);
     }
     return $data;
 }
 protected function build($slug, Request $request, Application $app)
 {
     $this->parameters = array();
     $gr = new CountryRepository();
     // we must accept both ID and Slug. Slug is proper one to use, but some JS needs to load by ID.
     $this->parameters['country'] = intval($slug) ? $gr->loadById($slug) : $gr->loadByTwoCharCode($slug);
     if (!$this->parameters['country']) {
         return false;
     }
     // check this country is or was valid for this site
     $countryInSiteRepo = new CountryInSiteRepository();
     if (!$countryInSiteRepo->isCountryInSite($this->parameters['country'], $app['currentSite'])) {
         return false;
     }
     $areaRepoBuilder = new AreaRepositoryBuilder();
     $areaRepoBuilder->setSite($app['currentSite']);
     $areaRepoBuilder->setCountry($this->parameters['country']);
     $areaRepoBuilder->setNoParentArea(true);
     $areaRepoBuilder->setIncludeDeleted(false);
     $this->parameters['childAreas'] = $areaRepoBuilder->fetchAll();
     return true;
 }
 protected function build($slug, Request $request, Application $app)
 {
     $this->parameters = array('country' => null, 'parentAreas' => array(), 'areaIsDuplicateOf' => null);
     if (strpos($slug, "-")) {
         $slug = array_shift(explode("-", $slug, 2));
     }
     $ar = new AreaRepository();
     $this->parameters['area'] = $ar->loadBySlug($app['currentSite'], $slug);
     if (!$this->parameters['area']) {
         return false;
     }
     $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;
     }
     if ($app['currentUser']) {
         $uwgr = new UserWatchesAreaRepository();
         $uwg = $uwgr->loadByUserAndArea($app['currentUser'], $this->parameters['area']);
         $this->parameters['currentUserWatchesArea'] = $uwg && $uwg->getIsWatching();
     }
     $cr = new CountryRepository();
     $this->parameters['country'] = $cr->loadById($this->parameters['area']->getCountryID());
     $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();
     if ($this->parameters['area']->getIsDuplicateOfId()) {
         $this->parameters['areaIsDuplicateOf'] = $ar->loadByID($this->parameters['area']->getIsDuplicateOfId());
     }
     $app['currentUserActions']->set("org.openacalendar", "areaHistory", true);
     $app['currentUserActions']->set("org.openacalendar", "actionAreaEditDetails", $app['currentUserPermissions']->hasPermission("org.openacalendar", "AREAS_CHANGE") && !$this->parameters['area']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "actionAreaNew", $app['currentUserPermissions']->hasPermission("org.openacalendar", "AREAS_CHANGE") && !$this->parameters['area']->getIsDeleted());
     return true;
 }
 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 edit($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Import does not exist.");
     }
     $form = $app['form.factory']->create(new ImportURLEditForm($app['currentSite']), $this->parameters['importurl']);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $area = null;
             $areaRepository = new AreaRepository();
             $postAreas = $request->request->get('areas');
             if (is_array($postAreas)) {
                 foreach ($postAreas as $areaCode) {
                     if (substr($areaCode, 0, 9) == 'EXISTING:') {
                         $area = $areaRepository->loadBySlug($app['currentSite'], substr($areaCode, 9));
                     }
                 }
             }
             $this->parameters['importurl']->setAreaId($area ? $area->getId() : null);
             $iRepository = new ImportURLRepository();
             $iRepository->edit($this->parameters['importurl'], $app['currentUser']);
             return $app->redirect("/importurl/" . $this->parameters['importurl']->getSlug());
         }
     }
     if ($this->parameters['country']) {
         $areaRepoBuilder = new AreaRepositoryBuilder();
         $areaRepoBuilder->setSite($app['currentSite']);
         $areaRepoBuilder->setCountry($this->parameters['country']);
         $areaRepoBuilder->setIncludeDeleted(false);
         if ($this->parameters['area']) {
             $areaRepoBuilder->setParentArea($this->parameters['area']);
         } else {
             $areaRepoBuilder->setNoParentArea(true);
         }
         $this->parameters['childAreas'] = $areaRepoBuilder->fetchAll();
     }
     $this->parameters['form'] = $form->createView();
     return $app['twig']->render('site/importurl/edit.html.twig', $this->parameters);
 }
 function editVenueNew($slug, Request $request, Application $app)
 {
     //var_dump($_POST); die();
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Event does not exist.");
     }
     if ($this->parameters['event']->getIsDeleted()) {
         die("No");
         // TODO
     }
     $areaRepository = new AreaRepository();
     $this->parameters['shouldWeAskForArea'] = $app['currentSite']->getIsFeaturePhysicalEvents() && $areaRepository->doesCountryHaveAnyNotDeletedAreas($app['currentSite'], $this->parameters['country']);
     $this->parameters['newVenueFieldsSubmitted'] = (bool) ('POST' == $request->getMethod() && $request->request->get('newVenueFieldsSubmitted'));
     //=====================================  Set Venue Object as Much as possible from what user passed!
     $this->parameters['venue'] = new VenueModel();
     $this->parameters['venue']->setTitle('POST' == $request->getMethod() ? $request->request->get('fieldTitle') : $request->query->get('fieldTitle'));
     $this->parameters['venue']->setDescription('POST' == $request->getMethod() ? $request->request->get('fieldDescription') : $request->query->get('fieldDescription'));
     $this->parameters['venue']->setAddress('POST' == $request->getMethod() ? $request->request->get('fieldAddress') : $request->query->get('fieldAddress'));
     $this->parameters['venue']->setAddressCode('POST' == $request->getMethod() ? $request->request->get('fieldAddressCode') : $request->query->get('fieldAddressCode'));
     $this->parameters['venue']->setCountryId($this->parameters['country']->getId());
     $this->parameters['venue']->setLat('POST' == $request->getMethod() ? $request->request->get('fieldLat') : $request->query->get('fieldLat'));
     $this->parameters['venue']->setLng('POST' == $request->getMethod() ? $request->request->get('fieldLng') : $request->query->get('fieldLng'));
     $this->parameters['fieldAreaObject'] = null;
     $this->parameters['noneOfAboveSelected'] = false;
     $this->parameters['areasToSelectSearch'] = false;
     $this->parameters['areasToSelectChildren'] = false;
     if ($this->parameters['shouldWeAskForArea']) {
         // has area already been passed?
         $this->parameters['fieldAreaSearchText'] = 'POST' == $request->getMethod() ? $request->request->get('fieldAreaSearchText') : $request->query->get('fieldAreaSearchText');
         $this->parameters['fieldAreaSlug'] = 'POST' == $request->getMethod() ? $request->request->get('fieldAreaSlug') : $request->query->get('fieldAreaSlug');
         $this->parameters['fieldAreaSlugSelected'] = 'POST' == $request->getMethod() ? $request->request->get('fieldAreaSlugSelected') : null;
         // Did the user select a area from a list?
         // -1 indicates skip, must check for that
         // we must check this before we check fieldAreaSlug so that this can override fieldAreaSlug
         if ($this->parameters['fieldAreaSlugSelected']) {
             $fass = intval($this->parameters['fieldAreaSlugSelected']);
             if ($fass == -1) {
                 $this->parameters['noneOfAboveSelected'] = true;
             } elseif ($fass > 0) {
                 $this->parameters['fieldAreaObject'] = $areaRepository->loadBySlug($app['currentSite'], $fass);
             }
         }
         // Slug passed?
         // -1 indicates skip, must check for that
         if (!$this->parameters['fieldAreaObject'] && $this->parameters['fieldAreaSlug']) {
             $fas = intval($this->parameters['fieldAreaSlug']);
             if ($fas == -1) {
                 $this->parameters['noneOfAboveSelected'] = true;
             } else {
                 if ($fas > 0) {
                     $this->parameters['fieldAreaObject'] = $areaRepository->loadBySlug($app['currentSite'], $fas);
                     if ($this->parameters['fieldAreaObject']) {
                         $this->parameters['fieldArea'] = $this->parameters['fieldAreaObject']->getTitle();
                     }
                 }
             }
         }
         // Free text search string passed that only has 1 result?
         if (!$this->parameters['fieldAreaObject'] && $this->parameters['fieldAreaSearchText']) {
             $arb = new AreaRepositoryBuilder();
             $arb->setSite($app['currentSite']);
             $arb->setCountry($this->parameters['country']);
             $arb->setFreeTextSearch($this->parameters['fieldAreaSearchText']);
             $arb->setIncludeParentLevels(1);
             $this->parameters['areasToSelectSearch'] = $arb->fetchAll();
             if (count($this->parameters['areasToSelectSearch']) == 1) {
                 $this->parameters['fieldAreaObject'] = $this->parameters['areasToSelectSearch'][0];
             }
         }
     }
     //=====================================  Call out to extensions to add details
     // Slightly ackward we have to set Area ID on venue object, then when extensions have done we need to reload the area object again.
     if ($this->parameters['fieldAreaObject']) {
         $this->parameters['venue']->setAreaId($this->parameters['fieldAreaObject']->getId());
     }
     foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
         $extension->addDetailsToVenue($this->parameters['venue']);
     }
     if ($this->parameters['venue']->getAreaId() && (!$this->parameters['fieldAreaObject'] || $this->parameters['fieldAreaObject']->getId() != $this->parameters['venue']->getAreaId())) {
         $this->parameters['fieldAreaObject'] = $areaRepository->loadById($this->parameters['venue']->getAreaId());
     }
     //===================================== User gets to add details?
     // Check newVenueFieldsSubmitted because we always show the fields to user once
     // also title is a required field.
     if (!$this->parameters['newVenueFieldsSubmitted'] || !trim($this->parameters['venue']->getTitle())) {
         return $app['twig']->render('site/event/edit.venue.new.html.twig', $this->parameters);
     }
     //===================================== Do we prompt the user for more?
     if ($this->parameters['shouldWeAskForArea']) {
         // Did user type in text we had multiple options for?
         if (!$this->parameters['fieldAreaObject'] && $this->parameters['fieldAreaSearchText'] && !$this->parameters['noneOfAboveSelected'] && count($this->parameters['areasToSelectSearch']) > 1) {
             return $app['twig']->render('site/event/edit.venue.new.area.html.twig', $this->parameters);
         }
         // Child areas?
         // -1 indicates "none of the above", so don't prompt the user again.
         if (!$this->parameters['noneOfAboveSelected']) {
             $areaRepoBuilder = new AreaRepositoryBuilder();
             $areaRepoBuilder->setSite($app['currentSite']);
             $areaRepoBuilder->setCountry($this->parameters['country']);
             $areaRepoBuilder->setIncludeDeleted(false);
             $areaRepoBuilder->setIncludeParentLevels(1);
             if ($this->parameters['fieldAreaObject']) {
                 $areaRepoBuilder->setParentArea($this->parameters['fieldAreaObject']);
             } else {
                 $areaRepoBuilder->setNoParentArea(true);
             }
             $childAreas = $areaRepoBuilder->fetchAll();
             if ($childAreas) {
                 $this->parameters['areasToSelectChildren'] = $childAreas;
                 return $app['twig']->render('site/event/edit.venue.new.area.html.twig', $this->parameters);
             }
         }
     }
     //===================================== No prompt? We can  save!
     $venueRepository = new VenueRepository();
     $venueRepository->create($this->parameters['venue'], $app['currentSite'], $app['currentUser']);
     $this->parameters['event']->setVenueId($this->parameters['venue']->getId());
     $this->parameters['event']->setAreaId(null);
     $eventRepository = new EventRepository();
     $eventRepository->edit($this->parameters['event'], $app['currentUser']);
     $repo = new EventRecurSetRepository();
     if ($repo->isEventInSetWithNotDeletedFutureEvents($this->parameters['event'])) {
         return $app->redirect("/event/" . $this->parameters['event']->getSlugforURL() . '/edit/future');
     } else {
         return $app->redirect("/event/" . $this->parameters['event']->getSlugforURL());
     }
 }
 /**
  * @return array
  */
 public function getUserNotifyContentForSiteAndUser(SiteModel $siteModel, UserAccountModel $userAccountModel)
 {
     global $CONFIG;
     $out = array();
     $grb = new AreaRepositoryBuilder();
     $grb->setSite($siteModel);
     $grb->setLimit(0);
     // all! No limit
     // TODO  don't we still want to do this? How will user A get a notification if user B deletes area? but then so far most area deletetions are by admins.
     $grb->setIncludeDeleted(false);
     foreach ($grb->fetchAll() as $area) {
         $uwg = $this->loadByUserAndArea($userAccountModel, $area);
         if ($uwg && $uwg->getIsWatching()) {
             $dateSince = $uwg->getSinceDateForNotifyChecking();
             $historyRepositoryBuilder = new HistoryRepositoryBuilder();
             $historyRepositoryBuilder->getHistoryRepositoryBuilderConfig()->setArea($area);
             $historyRepositoryBuilder->setSince($dateSince);
             $historyRepositoryBuilder->setNotUser($userAccountModel);
             // Only admins can change tags at the moment so don't include
             $historyRepositoryBuilder->setIncludeTagHistory(false);
             $histories = $historyRepositoryBuilder->fetchAll();
             if ($histories) {
                 $content = new UserWatchesAreaNotifyContent();
                 $content->setHistories($histories);
                 $userWatchesAreaStopRepository = new UserWatchesAreaStopRepository();
                 $userWatchesAreaStop = $userWatchesAreaStopRepository->getForUserAndArea($userAccountModel, $area);
                 $content->setUnwatchURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/area/' . $area->getSlugForURL() . '/stopWatchingFromEmail/' . $userAccountModel->getId() . '/' . $userWatchesAreaStop->getAccessKey());
                 $content->setUserAccount($userAccountModel);
                 $content->setSite($siteModel);
                 $content->setArea($area);
                 $content->setWatchedThingTitle($area->getTitle());
                 $content->setWatchedThingURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/area/' . $area->getSlugForURL() . '/history');
                 $out[] = $content;
             }
         }
     }
     return $out;
 }
 protected function getChildAreasForArea(AreaModel $areaModel = null, $limit = 500)
 {
     $arb = new AreaRepositoryBuilder();
     $arb->setSite($this->site);
     $arb->setIncludeDeleted(false);
     if ($areaModel) {
         $arb->setParentArea($areaModel);
     } else {
         $arb->setNoParentArea(true);
     }
     $arb->setLimit($limit);
     return $arb->fetchAll();
 }