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');
 }
<?php

use Abraham\TwitterOAuth\TwitterOAuth;
define('APP_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
require_once (defined('COMPOSER_ROOT_DIR') ? COMPOSER_ROOT_DIR : APP_ROOT_DIR) . '/vendor/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoloadCLI.php';
/**
 *
 * @package com.meetyournextmp
 * @license Closed Source
 * @copyright (c) 2013-2015, JMB Technology Limited, http://jmbtechnology.co.uk/
 * @author James Baster <*****@*****.**>
 */
// setup
$siteRepo = new \repositories\SiteRepository();
$site = $siteRepo->loadById($CONFIG->singleSiteID);
$customFields = $site->getCachedEventCustomFieldDefinitionsAsModels();
$customFieldOrganiserEmail = $customFields[0];
$userRepo = new \repositories\UserAccountRepository();
$organiserEmailRepo = new \com\meetyournextmp\repositories\OrganiserEmailRepository();
configureAppForSite($site);
$nowPlusSomeEmailReadingTime = TimeSource::getDateTime();
$nowPlusSomeEmailReadingTime->add(new \DateInterval("P1D"));
// Events
$erb = new \repositories\builders\EventRepositoryBuilder();
$erb->setSite($site);
$erb->setAfterNow();
$erb->setIncludeCancelled(false);
$erb->setIncludeDeleted(false);
foreach ($erb->fetchAll() as $event) {
<?php

define('APP_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
require_once (defined('COMPOSER_ROOT_DIR') ? COMPOSER_ROOT_DIR : APP_ROOT_DIR) . '/vendor/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoloadCLI.php';
/**
 *
 * @package com.meetyournextmp
 * @license Closed Source
 * @copyright (c) 2013-2015, JMB Technology Limited, http://jmbtechnology.co.uk/
 * @author James Baster <*****@*****.**>
 */
$slug = intval($argv[1]);
if ($slug < 1) {
    die("PASS SLUG");
}
$siteRepo = new \repositories\SiteRepository();
$app['currentSite'] = $siteRepo->loadById($CONFIG->singleSiteID);
$humanRepo = new \com\meetyournextmp\repositories\HumanRepository();
$human = $humanRepo->loadBySlug($app['currentSite'], $slug);
if ($human) {
    $humanRepo->delete($human);
}
 /** @return Array Roady to be made into JSON */
 protected function run()
 {
     // This would be nice, but then we wouldn't delete candidates mistakenly marked as standing
     // $url = 'http://yournextmp.popit.mysociety.org/api/v0.1/search/persons?q=_exists_:standing_in.2015.post_id&page=';
     $url = 'https://yournextmp.popit.mysociety.org/api/v0.1/persons?page=';
     $page = 0;
     $morePages = true;
     $countryRepo = new \repositories\CountryRepository();
     $areaRepo = new \repositories\AreaRepository();
     $gb = $countryRepo->loadByTwoCharCode("GB");
     $siteRepo = new \repositories\SiteRepository();
     $site = $siteRepo->loadById($this->app['config']->singleSiteID);
     // TODO assumes single site!
     $areaMapItInfoRepo = new \com\meetyournextmp\repositories\AreaMapItInfoRepository();
     $humanPopItInfoRepo = new \com\meetyournextmp\repositories\HumanPopItInfoRepository();
     $humanRepo = new HumanRepository();
     $areaRepo = new \repositories\AreaRepository();
     while ($morePages) {
         ++$page;
         $this->logVerbose("Getting page " . $page);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url . $page);
         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 array('result' => 'error', 'errorHTTPCode' => 200);
         }
         $dataObj = json_decode($data);
         foreach ($dataObj->result as $humanObj) {
             $this->logVerbose("Human " . $humanObj->name . "  POPITID " . $humanObj->id);
             /** @var $humanPopItInfo HumanPopItInfoModel */
             $humanPopItInfo = $humanPopItInfoRepo->getByPopItID($humanObj->id);
             $human = null;
             $standing = false;
             if (isset($humanObj->standing_in)) {
                 $standingIn = get_object_vars($humanObj->standing_in);
                 $standingIn2015 = null;
                 if (isset($standingIn['2015'])) {
                     $standingIn2015 = get_object_vars($standingIn['2015']);
                     $standing = true;
                 }
                 $partyMemberships = isset($humanObj->party_memberships) ? get_object_vars($humanObj->party_memberships) : array();
                 $partyMemberships2015 = null;
                 if (isset($partyMemberships['2015'])) {
                     $partyMemberships2015 = get_object_vars($partyMemberships['2015']);
                 }
             }
             if ($standing) {
                 $facebook = null;
                 $twitter = null;
                 $imageURL = null;
                 $imageProxyURL = null;
                 $imageHumanURL = null;
                 if (isset($humanObj->links)) {
                     foreach ($humanObj->links as $linkData) {
                         $linkDataArray = get_object_vars($linkData);
                         if ($linkDataArray['note'] == 'facebook page') {
                             $facebook = $linkDataArray['url'];
                         }
                     }
                 }
                 if (isset($humanObj->contact_details)) {
                     foreach ($humanObj->contact_details as $linkData) {
                         $linkDataArray = get_object_vars($linkData);
                         if ($linkDataArray['type'] == 'twitter') {
                             $twitter = $linkDataArray['value'];
                         }
                     }
                 }
                 if (isset($humanObj->images)) {
                     foreach ($humanObj->images as $imageData) {
                         $imageDataArray = get_object_vars($imageData);
                         $imageProxyURL = $imageDataArray['proxy_url'] . '/300/0';
                         $imageURL = $imageDataArray['url'];
                         $imageHumanURL = $imageProxyURL ? $imageProxyURL : $imageURL;
                     }
                 }
                 $this->logVerbose(" - Standing in 2015!");
                 $description = "Standing in " . $standingIn2015['name'] . " for " . $partyMemberships2015['name'] . ".";
                 $humanUrl = "https://yournextmp.com/person/" . $humanObj->id;
                 $bits = explode('/', $standingIn2015['mapit_url']);
                 $mapItID = $bits[4];
                 if ($humanPopItInfo) {
                     $this->logVerbose(" - Found existing record");
                     $human = $humanRepo->loadById($humanPopItInfo->getHumanId());
                     if ($human->getIsDeleted()) {
                         $this->logVerbose(" - Undeleting existing Human record");
                         $humanRepo->undelete($human);
                     }
                     if ($human->getTitle() != $humanObj->name || $description != $human->getDescription() || $human->getEmail() != $humanObj->email || $human->getUrl() != $humanUrl || $human->getTwitter() != $twitter || $human->getParty() != $partyMemberships2015['name'] || $human->getImageUrl() != $imageHumanURL) {
                         $this->logVerbose(" - Updating existing Human record");
                         $human->setTitle($humanObj->name);
                         $human->setDescription($description);
                         $human->setEmail($humanObj->email);
                         $human->setTwitter($twitter);
                         $human->setParty($partyMemberships2015['name']);
                         $human->setImageUrl($imageHumanURL);
                         $human->setUrl($humanUrl);
                         $humanRepo->edit($human);
                     }
                     $humanPopItInfo->setMapitId($mapItID);
                     $humanPopItInfo->setName($humanObj->name);
                     if (isset($humanObj->gender)) {
                         $humanPopItInfo->setGenderFromString($humanObj->gender);
                     }
                     if (isset($humanObj->email)) {
                         $humanPopItInfo->setEmail($humanObj->email);
                     }
                     $humanPopItInfo->setParty($partyMemberships2015['name']);
                     // TODO if we reenable this we need to make sure it works for "YYYY" format to.
                     //if (isset($humanObj->birth_date)) {
                     //	$humanPopItInfo->setBirthDate($humanObj->birth_date);
                     //}
                     $humanPopItInfo->setFacebook($facebook);
                     $humanPopItInfo->setTwitter($twitter);
                     $humanPopItInfo->setImageUrl($imageURL);
                     $humanPopItInfo->setImageProxyUrl($imageProxyURL);
                     $humanPopItInfoRepo->edit($humanPopItInfo);
                 } else {
                     $this->logVerbose(" - Adding new record");
                     $human = new \com\meetyournextmp\models\HumanModel();
                     $human->setTitle($humanObj->name);
                     $human->setDescription($description);
                     $human->setEmail($humanObj->email);
                     $human->setTwitter($twitter);
                     $human->setImageUrl($imageHumanURL);
                     $human->setUrl($humanUrl);
                     $human->setParty($partyMemberships2015['name']);
                     $humanRepo->create($human, $site);
                     $humanPopItInfo = new \com\meetyournextmp\models\HumanPopItInfoModel();
                     $humanPopItInfo->setHumanId($human->getId());
                     $humanPopItInfo->setPopitId($humanObj->id);
                     $humanPopItInfo->setMapitId($mapItID);
                     $humanPopItInfo->setName($humanObj->name);
                     if (isset($humanObj->gender)) {
                         $humanPopItInfo->setGenderFromString($humanObj->gender);
                     }
                     if (isset($humanObj->email)) {
                         $humanPopItInfo->setEmail($humanObj->email);
                     }
                     $humanPopItInfo->setParty($partyMemberships2015['name']);
                     // TODO if we reenable this we need to make sure it works for "YYYY" format to.
                     //if (isset($humanObj->birth_date)) {
                     //	$humanPopItInfo->setBirthDate($humanObj->birth_date);
                     //}
                     $humanPopItInfo->setFacebook($facebook);
                     $humanPopItInfo->setTwitter($twitter);
                     $humanPopItInfo->setImageUrl($imageURL);
                     $humanPopItInfo->setImageProxyUrl($imageProxyURL);
                     $humanPopItInfoRepo->create($humanPopItInfo);
                 }
                 $areaMapItInfo = $areaMapItInfoRepo->getByMapItID($humanPopItInfo->getMapitId());
                 if ($areaMapItInfo) {
                     $area = $areaRepo->loadById($areaMapItInfo->getAreaId());
                     if ($area) {
                         $humanRepo->addHumanToArea($human, $area);
                         $humanRepo->removeHumanFromAllButOneArea($human, $area);
                     }
                 }
             } else {
                 $this->logVerbose(" - NOT Standing in 2015!");
                 if ($humanPopItInfo) {
                     $human = $humanRepo->loadById($humanPopItInfo->getHumanId());
                     if ($human && !$human->getIsDeleted()) {
                         $this->logVerbose(" - Deleteing ");
                         $humanRepo->delete($human);
                     }
                 }
             }
             // Next Task - have any humans been merged into this one?
             foreach ($humanObj->versions as $humanVersion) {
                 $idOfOldRecord = $humanVersion->data->id;
                 if ($idOfOldRecord != $humanObj->id) {
                     $this->logVerbose(" - found a record that was merged into this one: " . $idOfOldRecord);
                     $humanPopItInfoOfOldRecord = $humanPopItInfoRepo->getByPopItID($idOfOldRecord);
                     if ($humanPopItInfoOfOldRecord) {
                         $this->logVerbose(" - found HumanPopIt for old record ");
                         $humanOfOldRecord = $humanRepo->loadById($humanPopItInfoOfOldRecord->getHumanId());
                         if ($humanOfOldRecord) {
                             $this->logVerbose(" - found Human for old record");
                             if (!$humanOfOldRecord->getIsDeleted()) {
                                 if ($human) {
                                     $this->logVerbose(" - marking duplicate human of old record ");
                                     $humanRepo->markDuplicate($humanOfOldRecord, $human);
                                 } else {
                                     $this->logVerbose(" - deleting human of old record");
                                     $humanRepo->delete($humanOfOldRecord);
                                 }
                             } else {
                                 $this->logVerbose(" - human for old record already deleted");
                             }
                         }
                     }
                 }
             }
         }
         $morePages = $dataObj->has_more;
     }
     return array('result' => 'ok');
 }
 function testSiteOwnerSpecificEdit()
 {
     global $CONFIG;
     $CONFIG->newUsersAreEditors = true;
     $this->addCountriesToTestDB();
     $userOwner = new UserAccountModel();
     $userOwner->setEmail("*****@*****.**");
     $userOwner->setUsername("test");
     $userOwner->setPassword("password");
     $userVerified = new UserAccountModel();
     $userVerified->setEmail("*****@*****.**");
     $userVerified->setUsername("verified");
     $userVerified->setPassword("password");
     $userUnverified = new UserAccountModel();
     $userUnverified->setEmail("*****@*****.**");
     $userUnverified->setUsername("unverified");
     $userUnverified->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($userOwner);
     $userRepo->verifyEmail($userOwner);
     $userRepo->create($userVerified);
     $userRepo->verifyEmail($userVerified);
     $userRepo->create($userUnverified);
     // reload user object so all flags set correctly
     $userOwner = $userRepo->loadByUserName($userOwner->getUsername());
     $userVerified = $userRepo->loadByUserName($userVerified->getUsername());
     $userUnverified = $userRepo->loadByUserName($userUnverified->getUsername());
     $extensionsManager = new ExtensionManager($this->app);
     $userPerRepo = new \repositories\UserPermissionsRepository($extensionsManager);
     $siteModel = new \models\SiteModel();
     $siteModel->setTitle("Test");
     $siteModel->setSlug("test");
     $siteRepository = new \repositories\SiteRepository();
     $countryRepository = new \repositories\CountryRepository();
     $siteRepository->create($siteModel, $userOwner, array($countryRepository->loadByTwoCharCode("GB")), $this->getSiteQuotaUsedForTesting(), false);
     ## Check!
     $extensionsManager = new ExtensionManager($this->app);
     $userPerRepo = new \repositories\UserPermissionsRepository($extensionsManager);
     $permissions = $userPerRepo->getPermissionsForUserInSite($userOwner, $siteModel, false);
     $this->assertEquals(2, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForUserInSite($userOwner, $siteModel, true);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForUserInSite($userVerified, $siteModel, false);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForUserInSite($userVerified, $siteModel, true);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForUserInSite($userUnverified, $siteModel, false);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForUserInSite($userUnverified, $siteModel, true);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForAnonymousInSite($siteModel, false, false);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForAnyUserInSite($siteModel, false, false);
     $this->assertEquals(0, count($permissions->getPermissions()));
     $permissions = $userPerRepo->getPermissionsForAnyVerifiedUserInSite($siteModel, false, false);
     $this->assertEquals(0, count($permissions->getPermissions()));
 }
 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');
 }