function testUserWatchesArea()
 {
     $this->addCountriesToTestDB();
     $countryRepo = new CountryRepository();
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userTest = new UserAccountModel();
     $userTest->setEmail("*****@*****.**");
     $userTest->setUsername("testtest");
     $userTest->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userTest);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $area = new \models\AreaModel();
     $area->setTitle("Test");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($area, null, $site, $countryRepo->loadByTwoCharCode('GB'), $user);
     // Test user doesn't have it
     $srb = new \repositories\builders\SiteRepositoryBuilder();
     $srb->setUserInterestedIn($userTest);
     $sites = $srb->fetchAll();
     $this->assertEquals(0, count($sites));
     // watch area
     $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository();
     $userWatchesAreaRepo->startUserWatchingArea($userTest, $area);
     // has it!
     $srb = new \repositories\builders\SiteRepositoryBuilder();
     $srb->setUserInterestedIn($userTest);
     $sites = $srb->fetchAll();
     $this->assertEquals(1, count($sites));
 }
    $areaMapItInfo = $areaMapItInfoRepo->getByCodeGSS($json_blob->codes->gss);
    if ($areaMapItInfo) {
        print " ... found area ID " . $areaMapItInfo->getAreaId() . "\n";
        $areaMapItInfo->setName($json_blob->name);
        $areaMapItInfo->setCodeGss($json_blob->codes->gss);
        if (isset($json_blob->codes->unit_id)) {
            $areaMapItInfo->setCodeUnitId($json_blob->codes->unit_id);
        }
        $areaMapItInfo->setMapitId($json_blob->id);
        $areaMapItInfoRepo->edit($areaMapItInfo);
    } else {
        print " ... creating!\n";
        if (!array_key_exists($json_blob->country_name, $parentAreas)) {
            die("No Parent Area: " . $json_blob->country_name . "\n");
        }
        $area = new \models\AreaModel();
        $area->setTitle($json_blob->name);
        $area->setCountryId($gb->getId());
        $area->setSiteId($CONFIG->singleSiteID);
        $areaRepo->create($area, $parentAreas[$json_blob->country_name], $site, $gb, null);
        $areaMapItInfo = new \com\meetyournextmp\models\AreaMapItInfoModel();
        $areaMapItInfo->setName($json_blob->name);
        $areaMapItInfo->setCodeGss($json_blob->codes->gss);
        if (isset($json_blob->codes->unit_id)) {
            $areaMapItInfo->setCodeUnitId($json_blob->codes->unit_id);
        }
        $areaMapItInfo->setMapitId($json_blob->id);
        $areaMapItInfo->setAreaId($area->getId());
        $areaMapItInfoRepo->create($areaMapItInfo);
    }
}
$site = $siteRepo->loadById($CONFIG->singleSiteID);
configureAppForSite($site);
for ($i = 1; $i <= 10; $i++) {
    // Get Human
    $stat = $DB->prepare("SELECT human_information.* FROM human_information " . " LEFT JOIN human_email ON human_email.human_id = human_information.id AND human_email.created_at > '2015-04-10 00:00:00'" . " JOIN human_in_area ON human_in_area.human_id = human_information.id " . " JOIN area_information ON area_information.id = human_in_area.area_id " . " WHERE  human_information.is_deleted = '0' AND " . " human_information.email IS NOT NULL AND human_information.email != '' AND human_email.id IS NULL" . " LIMIT 1");
    $stat->execute();
    if ($stat->rowCount() == 0) {
        die("No Human Data");
    }
    $human = new \com\meetyournextmp\models\HumanModel();
    $human->setFromDataBaseRow($stat->fetch());
    // Get Area
    $stat = $DB->prepare("SELECT area_information.* FROM area_information " . " JOIN human_in_area ON human_in_area.area_id = area_information.id AND human_in_area.human_id = :human_id");
    $stat->execute(array('human_id' => $human->getId()));
    $stat->execute();
    if ($stat->rowCount() == 0) {
        die("No Area Data");
    }
    $area = new \models\AreaModel();
    $area->setFromDataBaseRow($stat->fetch());
    // make email
    $email = new \com\meetyournextmp\models\HumanEmailModel();
    $email->setFromAppAndHumanAndArea($app, $human, $area);
    // save email
    $repo = new \com\meetyournextmp\repositories\HumanEmailRepository();
    $repo->create($email);
    // send email
    $email->send($app);
    // record sent
    $repo->markSent($email);
}
                print "\n";
            }
        }
    }
}
sleep(10);
die;
$countryRepo = new \repositories\CountryRepository();
$areaRepo = new \repositories\AreaRepository();
$gb = $countryRepo->loadByTwoCharCode("GB");
$siteRepo = new \repositories\SiteRepository();
$site = $siteRepo->loadById($CONFIG->singleSiteID);
foreach ($parentAreas as $parentArea) {
    print "In: " . $parentArea->getTitle() . "\n\n";
    if (isset($workToDo[$parentArea->getTitle()])) {
        foreach ($workToDo[$parentArea->getTitle()] as $newParentAreaTitle => $newParentAreaAreas) {
            if (count($newParentAreaAreas) > 1) {
                print "Make: " . $newParentAreaTitle . "\n";
                $newParentAreaModel = new \models\AreaModel();
                $newParentAreaModel->setTitle($newParentAreaTitle);
                $areaRepo->create($newParentAreaModel, $parentArea, $site, $gb);
                foreach ($newParentAreaAreas as $area) {
                    print "  -  Area: " . $area->getTitle() . "\n";
                    $area->setParentAreaId($newParentAreaModel->getId());
                    $areaRepo->editParentArea($area);
                }
                print "\n";
            }
        }
    }
}
<?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 <*****@*****.**>
 */
$stat = $DB->prepare("SELECT area_information.*, " . "area_mapit_information.mapit_id, area_mapit_information.code_unit_id, area_mapit_information.code_gss, area_mapit_information.name " . " FROM area_information JOIN area_mapit_information ON area_mapit_information.area_id = area_information.id");
$stat->execute(array());
$fp = fopen(__DIR__ . "/../../../webSingleSite/datadump/seats.csv", 'w');
if ($fp) {
    fputcsv($fp, array('name', 'mapit_id', 'code_unit_id', 'code_gss', "URL"));
    while ($data = $stat->fetch()) {
        $area = new \models\AreaModel();
        $area->setFromDataBaseRow($data);
        fputcsv($fp, array($data['name'], $data['mapit_id'], $data['code_unit_id'], $data['code_gss'], "http://" . $CONFIG->webSiteDomain . "/area/" . $area->getSlugForUrl()));
    }
    fclose($fp);
} else {
    die("OH NO");
}
print "Actually SEND: " . ($actuallySEND ? "YES" : "nah") . "\n";
if (!$actuallySEND) {
    die("DIE\n");
}
// setup
$siteRepo = new \repositories\SiteRepository();
$site = $siteRepo->loadById($CONFIG->singleSiteID);
configureAppForSite($site);
$innerSQL = "SELECT human_in_area.area_id, COUNT(human_tweet.id) As tweets FROM human_in_area " . " LEFT JOIN human_tweet ON human_tweet.human_id = human_in_area.human_id AND human_in_area.removed_at IS NULL  " . "GROUP BY human_in_area.area_id";
// Get area
$stat = $DB->prepare("SELECT area_information.* FROM area_information " . " JOIN area_mapit_information ON area_mapit_information.area_id = area_information.id " . " JOIN (" . $innerSQL . ") AS area_tweets ON area_tweets.area_id = area_information.id " . " WHERE area_tweets.tweets = 0 AND  area_information.is_deleted = '0' AND area_information.cached_future_events >  " . EVENTS_NEEDED_IN_AREA . " ORDER BY area_information.cached_future_events DESC LIMIT 1 ");
$stat->execute();
if ($stat->rowCount() == 0) {
    die("No AREA Data");
}
$area = new \models\AreaModel();
$area->setFromDataBaseRow($stat->fetch());
$areaURL = "http://" . $CONFIG->webSiteDomain . "/area/" . $area->getSlugForUrl();
print "Found Area " . $area->getTitle() . "\n";
$erb = new \com\meetyournextmp\repositories\builders\EventRepositoryBuilder();
$erb->setAfterNow();
$erb->setArea($area);
$erb->setIncludeCancelled(false);
$erb->setIncludeDeleted(false);
$events = $erb->fetchCount();
if ($events < EVENTS_NEEDED_IN_AREA) {
    die("We checked and found less than " . EVENTS_NEEDED_IN_AREA . " events; is cache broken!");
}
$hrb = new \com\meetyournextmp\repositories\builders\HumanRepositoryBuilder();
$hrb->setIncludeDeleted(false);
$hrb->setArea($area);
 function testParentChildSearches()
 {
     $this->addCountriesToTestDB();
     TimeSource::mock(2014, 5, 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());
     $countryRepo = new \repositories\CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode("GB");
     $areaParent = new \models\AreaModel();
     $areaParent->setTitle("Parent");
     $areaCentre = new \models\AreaModel();
     $areaCentre->setTitle("Centre");
     $areaChild = new \models\AreaModel();
     $areaChild->setTitle("Child");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($areaParent, null, $site, $gb);
     $areaRepo->create($areaCentre, $areaParent, $site, $gb);
     $areaRepo->create($areaChild, $areaCentre, $site, $gb);
     $areaRepo->buildCacheAreaHasParent($areaParent);
     $areaRepo->buildCacheAreaHasParent($areaCentre);
     $areaRepo->buildCacheAreaHasParent($areaChild);
     $commentParent = new \org\openacalendar\comments\models\AreaCommentModel();
     $commentParent->setTitle("Parent");
     $commentCentre = new \org\openacalendar\comments\models\AreaCommentModel();
     $commentCentre->setTitle("Centre");
     $commentChild = new \org\openacalendar\comments\models\AreaCommentModel();
     $commentChild->setTitle("Child");
     $areaCommentRepo = new \org\openacalendar\comments\repositories\AreaCommentRepository();
     TimeSource::mock(2014, 5, 1, 7, 0, 0);
     $areaCommentRepo->create($commentParent, $areaParent, $user);
     TimeSource::mock(2014, 5, 1, 7, 0, 1);
     $areaCommentRepo->create($commentCentre, $areaCentre, $user);
     TimeSource::mock(2014, 5, 1, 7, 0, 2);
     $areaCommentRepo->create($commentChild, $areaChild, $user);
     ##################################### Test Centre
     $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder();
     $areaCommentRepoBuilder->setArea($areaCentre, false, false);
     $areaComments = $areaCommentRepoBuilder->fetchAll();
     $this->assertEquals(1, count($areaComments));
     $this->assertEquals($commentCentre->getId(), $areaComments[0]->getId());
     ##################################### Test Centre + Parents
     $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder();
     $areaCommentRepoBuilder->setArea($areaCentre, true, false);
     $areaComments = $areaCommentRepoBuilder->fetchAll();
     $this->assertEquals(2, count($areaComments));
     $this->assertEquals($commentParent->getId(), $areaComments[0]->getId());
     $this->assertEquals($commentCentre->getId(), $areaComments[1]->getId());
     ##################################### Test Centre + Children
     $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder();
     $areaCommentRepoBuilder->setArea($areaCentre, false, true);
     $areaComments = $areaCommentRepoBuilder->fetchAll();
     $this->assertEquals(2, count($areaComments));
     $this->assertEquals($commentCentre->getId(), $areaComments[0]->getId());
     $this->assertEquals($commentChild->getId(), $areaComments[1]->getId());
     ##################################### Test Centre + Parents + Children
     $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder();
     $areaCommentRepoBuilder->setArea($areaCentre, true, true);
     $areaComments = $areaCommentRepoBuilder->fetchAll();
     $this->assertEquals(3, count($areaComments));
     $this->assertEquals($commentParent->getId(), $areaComments[0]->getId());
     $this->assertEquals($commentCentre->getId(), $areaComments[1]->getId());
     $this->assertEquals($commentChild->getId(), $areaComments[2]->getId());
     ##################################### Test Child + Children
     $areaCommentRepoBuilder = new \org\openacalendar\comments\repositories\builders\AreaCommentRepositoryBuilder();
     $areaCommentRepoBuilder->setArea($areaChild, false, true);
     $areaComments = $areaCommentRepoBuilder->fetchAll();
     $this->assertEquals(1, count($areaComments));
     $this->assertEquals($commentChild->getId(), $areaComments[0]->getId());
 }
 function testEventInNoAreaHumanInCentre()
 {
     $this->addCountriesToTestDB();
     TimeSource::mock(2014, 5, 1, 7, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userWatching = new UserAccountModel();
     $userWatching->setEmail("*****@*****.**");
     $userWatching->setUsername("test1");
     $userWatching->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userWatching);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $countryRepo = new \repositories\CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode("GB");
     $areaParent = new \models\AreaModel();
     $areaParent->setTitle("Parent");
     $areaChild = new \models\AreaModel();
     $areaChild->setTitle("Child");
     $areaOtherChild = new \models\AreaModel();
     $areaOtherChild->setTitle("Other Child");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($areaParent, null, $site, $gb);
     $areaRepo->create($areaChild, $areaParent, $site, $gb);
     $areaRepo->create($areaOtherChild, $areaParent, $site, $gb);
     $areaRepo->buildCacheAreaHasParent($areaParent);
     $areaRepo->buildCacheAreaHasParent($areaChild);
     $areaRepo->buildCacheAreaHasParent($areaOtherChild);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt($this->mktime(2014, 5, 10, 19, 0, 0, 'Europe/London'));
     $event->setEndAt($this->mktime(2014, 5, 10, 21, 0, 0, 'Europe/London'));
     $event->setUrl("http://www.info.com");
     $event->setTicketUrl("http://www.tickets.com");
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $human = new \com\meetyournextmp\models\HumanModel();
     $human->setTitle("Bob");
     $humanRepo = new \com\meetyournextmp\repositories\HumanRepository();
     $humanRepo->create($human, $site, null);
     $humanRepo->addHumanToArea($human, $areaChild, null);
     $humanRepo->addHumanToEvent($human, $event, null);
     $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository();
     //////////////////////////////// TEST ALL EVENTS
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST OTHER CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaOtherChild);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaOtherChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(0, count($histories));
     //////////////////////////////// TEST CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST PARENT
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// USER WATCHES NOTHING
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     //////////////////////////////// USER WATCHES PARENT
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaParent);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     //////////////////////////////// USER WATCHES CHILD
     $userWatchesAreaRepo->stopUserWatchingArea($userWatching, $areaParent);
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaChild);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
 }