public function setFromAppAndHumanAndArea(Application $app, HumanModel $humanModel, AreaModel $areaModel)
 {
     $this->human_id = $humanModel->getId();
     $this->email = $humanModel->getEmail();
     $this->subject = "Can you tell voters which hustings events you are attending?";
     $eventRepoBuilder = new EventRepositoryBuilder();
     $eventRepoBuilder->setIncludeDeleted(false);
     $eventRepoBuilder->setIncludeCancelled(false);
     $eventRepoBuilder->setAfterNow();
     $eventRepoBuilder->setArea($areaModel);
     $events = $eventRepoBuilder->fetchAll();
     $this->body_html = $app['twig']->render('email/humanEmail.html.twig', array('human' => $humanModel, 'area' => $areaModel, 'events' => $events, 'email' => $this->email));
     if ($app['config']->isDebug) {
         file_put_contents('/tmp/humanEmail.html', $this->body_html);
     }
     $this->body_text = $app['twig']->render('email/humanEmail.txt.twig', array('human' => $humanModel, 'area' => $areaModel, 'events' => $events, 'email' => $this->email));
     if ($app['config']->isDebug) {
         file_put_contents('/tmp/humanEmail.txt', $this->body_text);
     }
     $this->created_at = $app['timesource']->getDateTime();
 }
 protected function run()
 {
     $siteRepo = new \repositories\SiteRepository();
     $site = $siteRepo->loadById($this->app['config']->singleSiteID);
     // TODO assumes single site!
     $out = array();
     $erb = new EventRepositoryBuilder();
     $erb->setIncludeDeleted(false);
     $erb->setIncludeCancelled(false);
     $erb->setSite($site);
     $out['countEventsTotal'] = count($erb->fetchAll());
     $erb = new EventRepositoryBuilder();
     $erb->setIncludeDeleted(false);
     $erb->setIncludeCancelled(false);
     $erb->setSite($site);
     $erb->setBefore($this->app['timesource']->getDateTime());
     $out['countEventsBeforeNow'] = count($erb->fetchAll());
     $erb = new EventRepositoryBuilder();
     $erb->setIncludeDeleted(false);
     $erb->setIncludeCancelled(false);
     $erb->setSite($site);
     $erb->setAfterNow();
     $out['countEventsAfterNow'] = count($erb->fetchAll());
     $arb = new \com\meetyournextmp\repositories\builders\AreaRepositoryBuilder();
     $arb->setIsMapItAreaOnly(true);
     $arb->setIncludeDeleted(false);
     $arb->setIncludeAreasWithNoEventsOnly(true);
     $arb->setLimit(800);
     $out['countSeatsWithNoEvents'] = count($arb->fetchAll());
     $areaRepo = new AreaRepository();
     foreach (array(3 => 'countEventsInScotland', 1 => 'countEventsInEngland', 2 => 'countEventsInWales', 4 => 'countEventsInNIreland', 712 => 'countEventsInGreaterLondon') as $areaSlug => $key) {
         $erb = new EventRepositoryBuilder();
         $erb->setIncludeDeleted(false);
         $erb->setIncludeCancelled(false);
         $erb->setSite($site);
         $erb->setArea($areaRepo->loadBySlug($site, $areaSlug));
         $out[$key] = count($erb->fetchAll());
     }
     // =================================== Events by day
     $report = $this->getValueReport('com.meetyournextmp', 'NonDeletedNonCancelledEventsStartAtReport', $this->app);
     $startAt = \TimeSource::getDateTime();
     $startAt->setTime(0, 0, 0);
     $endAt = new \DateTime('2015-05-07 10:00:00');
     $period = "P1D";
     $report->setFilterSiteId($this->app['config']->singleSiteID);
     $reportByTime = new SeriesOfValueByTimeReport($report, $startAt, $endAt, $period);
     $reportByTime->run();
     $out['countEventsByDay'] = array();
     foreach ($reportByTime->getData() as $data) {
         $out['countEventsByDay'][] = array('count' => $data->getData(), 'date' => $data->getLabelStart()->format('D d F Y'));
     }
     // =================================== Users with edits
     $report = $this->getSeriesReport("org.openacalendar", "UsersWithEventsEdited", $this->app);
     $report->run();
     $out['userEventsEdited'] = array();
     foreach ($report->getData() as $data) {
         $out['userEventsEdited'][] = array('count' => $data->getData(), 'userID' => $data->getLabelID(), 'userUserName' => $data->getLabelText());
     }
     //var_dump($out);
     file_put_contents(APP_ROOT_DIR . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'numbers.json', json_encode($out));
     return array('result' => 'ok');
 }
 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');
 }