public function getAreaForPostCode(PostcodeParser $postcodeParser)
 {
     global $CONFIG;
     if ($postcodeParser->isValid()) {
         $memcachedConnection = null;
         $areaRepo = new AreaRepository();
         if ($CONFIG->memcachedServer) {
             $memcachedConnection = new \Memcached();
             $memcachedConnection->addServer($CONFIG->memcachedServer, $CONFIG->memcachedPort);
             $url = $memcachedConnection->get($postcodeParser->getCanonical());
             if ($url) {
                 $urlBits = explode("/", $url);
                 $urlBitsBits = explode("-", $urlBits[2]);
                 $area = $areaRepo->loadBySlug($this->app['currentSite'], $urlBitsBits[0]);
                 if ($area) {
                     return $area;
                 }
             }
         }
         $url = "http://mapit.mysociety.org/postcode/" . urlencode($postcodeParser->getCanonical());
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_USERAGENT, 'Meet Your Next MP');
         $data = curl_exec($ch);
         $response = curl_getinfo($ch);
         curl_close($ch);
         if ($response['http_code'] != 200) {
             return;
         }
         $dataObj = json_decode($data);
         $mapItId = $dataObj->shortcuts->WMC;
         if (!$mapItId) {
             return;
         }
         $repo = new AreaMapItInfoRepository();
         $areaMapIdInfo = $repo->getByMapItID($mapItId);
         if (!$areaMapIdInfo) {
             return;
         }
         $area = $areaRepo->loadById($areaMapIdInfo->getAreaId());
         if (!$area) {
             return;
         }
         if ($memcachedConnection) {
             $memcachedConnection->set($postcodeParser->getCanonical(), '/area/' . $area->getSlugForUrl(), 60 * 60 * 24 * 30);
         }
         return $area;
     }
 }
 protected function build($slug, Request $request, Application $app)
 {
     $this->parameters = array();
     if (strpos($slug, "-") > 0) {
         $slugBits = explode("-", $slug, 2);
         $slug = $slugBits[0];
     }
     $ampir = new AreaMapItInfoRepository();
     $this->parameters['areaMapItInfo'] = $ampir->getByMapItID($slug);
     if (!$this->parameters['areaMapItInfo']) {
         return false;
     }
     $ar = new AreaRepository();
     $this->parameters['area'] = $ar->loadById($this->parameters['areaMapItInfo']->getAreaId());
     if (!$this->parameters['area']) {
         return false;
     }
     return true;
 }
 protected function findAreaForLinkToSeat(Request $request, Application $app)
 {
     $repo = new AreaMapItInfoRepository();
     $areaRepo = new AreaRepository();
     if ($request->query->has("mapitid")) {
         $areaInfo = $repo->getByMapItID($request->query->get("mapitid"));
         if ($areaInfo) {
             $area = $areaRepo->loadById($areaInfo->getAreaId());
             if ($area) {
                 return $area;
             }
         }
     }
     if ($request->query->has("gssid")) {
         $areaInfo = $repo->getByCodeGSS($request->query->get("gssid"));
         if ($areaInfo) {
             $area = $areaRepo->loadById($areaInfo->getAreaId());
             if ($area) {
                 return $area;
             }
         }
     }
     if ($request->query->has("title")) {
         $areaInfo = $repo->getByName($request->query->get("title"));
         if ($areaInfo) {
             $area = $areaRepo->loadById($areaInfo->getAreaId());
             if ($area) {
                 return $area;
             }
         }
     }
 }
 protected function run()
 {
     $this->localTimeZone = new \DateTimeZone("Europe/London");
     $siteRepo = new \repositories\SiteRepository();
     $site = $siteRepo->loadById($this->app['config']->singleSiteID);
     // TODO assumes single site!
     $areaRepository = new AreaRepository();
     $venueRepository = new VenueRepository();
     $countryRepository = new CountryRepository();
     $countries = array();
     $humanPopItRepository = new HumanPopItInfoRepository();
     $areaMapItRepo = new AreaMapItInfoRepository();
     $out = array('data' => array(), 'areasPastEvents' => array());
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setIncludeDeleted(true);
     $erb->setIncludeCancelled(true);
     $erb->setAfterNow();
     foreach ($erb->fetchAll() as $event) {
         $venue = null;
         $country = null;
         $area = null;
         $humans = array();
         if ($event->getCountryId()) {
             if (!isset($countries[$event->getCountryId()])) {
                 $countries[$event->getCountryId()] = $countryRepository->loadById($event->getCountryId());
             }
             $country = $countries[$event->getCountryId()];
         }
         if ($event->getVenueId()) {
             $venue = $venueRepository->loadById($event->getVenueId());
         }
         if ($event->getAreaId()) {
             $area = $areaRepository->loadById($event->getAreaId());
         } else {
             if ($venue && $venue->getAreaId()) {
                 $area = $areaRepository->loadById($venue->getAreaId());
             }
         }
         $thisOut = $this->addEvent($event, $venue, $area, $country);
         $thisOut['humans'] = array();
         $mapitids = array();
         if ($area) {
             $areamapit = $areaMapItRepo->getByAreaID($area->getId());
             if ($areamapit) {
                 $mapitids[] = $areamapit->getMapitId();
             }
         }
         $hrb = new HumanRepositoryBuilder();
         $hrb->setHumansForEvent($event);
         foreach ($hrb->fetchAll() as $human) {
             $popit = $humanPopItRepository->getByHumanID($human->getId());
             $thisOut['humans'][] = array('popit_id' => $popit->getPopitId());
             $arb = new AreaRepositoryBuilder();
             $arb->setIncludeDeleted(false);
             $arb->setHuman($human);
             foreach ($arb->fetchAll() as $areaForHuman) {
                 if (!$area || $area->getId() != $areaForHuman->getId()) {
                     $areamapit = $areaMapItRepo->getByAreaID($areaForHuman->getId());
                     if ($areamapit) {
                         $mapitids[] = $areamapit->getMapitId();
                     }
                 }
             }
         }
         $thisOut['mapitids'] = array_values(array_unique($mapitids));
         $out['data'][] = $thisOut;
     }
     $arb = new \com\meetyournextmp\repositories\builders\AreaRepositoryBuilder();
     $arb->setLimit(1000);
     $arb->setIncludeDeleted(false);
     $arb->setIsMapItAreaOnly(true);
     foreach ($arb->fetchAll() as $area) {
         $erb = new EventRepositoryBuilder();
         $erb->setIncludeDeleted(false);
         $erb->setIncludeCancelled(false);
         $erb->setArea($area);
         $erb->setBeforeNow();
         $areamapit = $areaMapItRepo->getByAreaID($area->getId());
         $out['areasPastEvents'][$areamapit->getMapitId()] = $erb->fetchCount();
     }
     file_put_contents(APP_ROOT_DIR . DIRECTORY_SEPARATOR . 'webSingleSite' . DIRECTORY_SEPARATOR . 'datadump' . DIRECTORY_SEPARATOR . 'ynmpread.json', json_encode($out));
     return array('result' => 'ok');
 }