/**
  *
  *
  */
 public function buildCacheAreaHasParent(AreaModel $area)
 {
     $statFirstArea = $this->siteContainer['databasehelper']->getPDO()->prepare("SELECT area_information.parent_area_id FROM area_information WHERE area_information.id=:id");
     // get first parent
     $areaParentID = null;
     $statFirstArea->execute(array('id' => $area->getId()));
     $d = $statFirstArea->fetch();
     if ($d) {
         $areaParentID = $d['parent_area_id'];
     }
     $statInsertCache = $this->siteContainer['databasehelper']->getPDO()->prepare("INSERT INTO cached_area_has_parent(area_id,has_parent_area_id) VALUES (:area_id,:has_parent_area_id)");
     $statNextArea = $this->siteContainer['databasehelper']->getPDO()->prepare("SELECT area_information.parent_area_id FROM area_information WHERE area_information.id=:id");
     while ($areaParentID) {
         // insert this parent into the cache
         $statInsertCache->execute(array('area_id' => $area->getId(), 'has_parent_area_id' => $areaParentID));
         // move up to next parent
         $statNextArea->execute(array('id' => $areaParentID));
         $d = $statNextArea->fetch();
         if ($d) {
             $areaParentID = $d['parent_area_id'];
         } else {
             $areaParentID = null;
         }
     }
 }
 protected function build()
 {
     $this->select[] = 'event_information.*';
     if ($this->country) {
         $this->where[] = " event_information.country_id = :country_id ";
         $this->params['country_id'] = $this->country->getId();
     }
     if ($this->area) {
         // We were doing
         // $this->joins[] = " LEFT JOIN cached_area_has_parent ON cached_area_has_parent.area_id = venue_information.area_id";
         // $this->where[] =  " (venue_information.area_id = :area_id OR  cached_area_has_parent.has_parent_area_id = :area_id )";
         // but then we got duplicates
         $areaids = array($this->area->getId());
         $this->statAreas = $this->siteContainer['databasehelper']->getPDO()->prepare("SELECT area_id FROM cached_area_has_parent WHERE has_parent_area_id=:id");
         $this->statAreas->execute(array('id' => $this->area->getId()));
         while ($d = $this->statAreas->fetch()) {
             $areaids[] = $d['area_id'];
         }
         $this->where[] = "  event_information.area_id IN (" . implode(",", $areaids) . ")";
     }
     if ($this->group) {
         $this->joins[] = " JOIN event_in_group AS event_in_group ON event_in_group.event_id = event_information.id " . " AND event_in_group.group_id = :group_id ";
         $this->params['group_id'] = $this->group->getId();
     }
     if ($this->after) {
         $this->where[] = ' event_information.end_at > :after';
         $this->params['after'] = $this->after;
     }
 }
 public function fetchAll()
 {
     $this->buildStart();
     $this->build();
     $this->buildStat();
     $results = array();
     while ($data = $this->stat->fetch()) {
         $area = new AreaModel();
         $area->setFromDataBaseRow($data);
         $results[] = $area;
     }
     return $results;
 }
 function loadData($filename, $folder = '', $defaults = array())
 {
     $isDefault = $filename == 'data.ini';
     $this->siteContainer['log']->debug('Starting DataLoader Ini', array('filename' => $filename, 'folder' => $folder, 'isDefault' => $isDefault));
     $data = parse_ini_file($this->siteContainer['site']->getDir() . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $filename, true);
     $out = new DataLoadResult();
     $group = null;
     if (isset($data['group'])) {
         $group = new GroupModel();
         if (isset($data['group']['slug']) && $data['group']['slug']) {
             $group->setSlug($data['group']['slug']);
         }
         $group->setTitle($data['group']['title']);
         if (isset($data['group']['description']) && $data['group']['description']) {
             $group->setDescription($data['group']['description']);
         }
         if (isset($data['group']['url']) && $data['group']['url']) {
             if (filter_var($data['group']['url'], FILTER_VALIDATE_URL)) {
                 $group->setUrl($data['group']['url']);
             } else {
                 // TODO warn!
             }
         }
         $this->siteContainer['site']->addGroup($group);
         if ($isDefault) {
             $out->addDefault($group);
         }
     }
     if (isset($data['event'])) {
         $event = new EventModel();
         $groupsForEvent = $group ? array($group) : array();
         $event->setCountry($this->siteContainer['site']->getDefaultCountry());
         $event->setTimeZone($this->siteContainer['site']->getDefaultTimeZone());
         if (isset($data['event']['slug']) && $data['event']['slug']) {
             $event->setSlug($data['event']['slug']);
         }
         foreach ($defaults as $default) {
             if (is_a($default, 'openacalendar\\staticweb\\models\\GroupModel')) {
                 $groupsForEvent[] = $default;
             } else {
                 if (is_a($default, 'openacalendar\\staticweb\\models\\CountryModel')) {
                     $event->setCountry($default);
                 } else {
                     if (is_a($default, 'openacalendar\\staticweb\\models\\AreaModel')) {
                         $event->setArea($default);
                     } else {
                         if (is_a($default, 'openacalendar\\staticweb\\models\\DefaultTimeZoneModel')) {
                             $event->setTimeZone($default->getCode());
                         }
                     }
                 }
             }
         }
         $event->setTitle($data['event']['title']);
         $event->setStart($data['event']['start']);
         $event->setEnd($data['event']['end']);
         if (isset($data['event']['description']) && $data['event']['description']) {
             $event->setDescription($data['event']['description']);
         }
         if (isset($data['event']['url']) && $data['event']['url']) {
             if (filter_var($data['event']['url'], FILTER_VALIDATE_URL)) {
                 $event->setUrl($data['event']['url']);
             } else {
                 // TODO warn!
             }
         }
         if (isset($data['event']['country']) && $data['event']['country']) {
             $country = $this->siteContainer['countryrepository']->loadByHumanInput($data['event']['country']);
             if (!$country) {
                 $out->addError(new DataErrorInvalidCountry());
                 return $out;
             }
             $event->setCountry($country);
         }
         if (isset($data['event']['timezone']) && $data['event']['timezone']) {
             $timezone = $data['event']['timezone'];
             if (!$this->siteContainer['countryrepository']->isTimeZoneValid($timezone)) {
                 $out->addError(new DataErrorInvalidTimeZone());
                 return $out;
             }
             $event->setTimeZone($timezone);
         }
         if (isset($data['event']['group_slug']) && $data['event']['group_slug']) {
             $groupSpeccedInEvent = $this->siteContainer['grouprepository']->loadBySlug($data['event']['group_slug']);
             if (!$groupSpeccedInEvent) {
                 // TODO error
             } else {
                 $groupsForEvent[] = $groupSpeccedInEvent;
             }
         }
         if (is_a($event->getCountry(), 'openacalendar\\staticweb\\models\\CountryModel') && $event->getTimeZone()) {
             if (!$event->getCountry()->hasTimeZone($event->getTimeZone())) {
                 $out->addError(new DataErrorInvalidTimeZoneForCountry());
                 return $out;
             }
         }
         $this->siteContainer['site']->addEvent($event);
         foreach ($groupsForEvent as $groupSpeccedInEvent) {
             $this->siteContainer['grouprepository']->addEventToGroup($event, $groupSpeccedInEvent);
         }
     }
     if (isset($data['area']) && isset($data['area']['slug'])) {
         $area = new AreaModel();
         $area->setSlug($data['area']['slug']);
         $area->setTitle(isset($data['area']['title']) ? $data['area']['title'] : $data['area']['slug']);
         $area->setCountry($this->siteContainer['site']->getDefaultCountry());
         foreach ($defaults as $default) {
             if (is_a($default, 'openacalendar\\staticweb\\models\\CountryModel')) {
                 $area->setCountry($default);
             } else {
                 if (is_a($default, 'openacalendar\\staticweb\\models\\AreaModel')) {
                     $area->setParentArea($default);
                 }
             }
         }
         // TODO also have to look for country in $out->defaults
         $this->siteContainer['site']->addArea($area);
         if ($isDefault) {
             $out->addDefault($area);
         }
     }
     if ($isDefault) {
         if (isset($data['country']) && isset($data['country']['code'])) {
             $country = $this->siteContainer['countryrepository']->loadByHumanInput($data['country']['code']);
             if (!$country) {
                 $out->addError(new DataErrorInvalidCountry());
                 return $out;
             }
             $out->addDefault($country);
         }
         if (isset($data['timezone']) && isset($data['timezone']['timezone'])) {
             $timezone = new DefaultTimeZoneModel($data['timezone']['timezone']);
             // TODO check exisits!
             if (!$timezone) {
                 $out->addError(new DataErrorInvalidTimeZone());
                 return $out;
             }
             $out->addDefault($timezone);
         }
     }
     $this->siteContainer['log']->debug('Finished DataLoader Ini');
     return $out;
 }
 /**
  * @param mixed $area
  */
 public function setArea(AreaModel $area)
 {
     $this->area_id = $area->getId();
     $this->area = $area;
 }
 public function addArea(AreaModel $area)
 {
     if (!$area->getSlug()) {
         $this->warnings[] = new DataWarningAreaHasNoSlug();
         $area->createSlug();
     }
     if ($this->siteContainer['arearepository']->loadBySlug($area->getSlug())) {
         $this->errors[] = new DataErrorTwoAreasHaveSameSlugs();
     }
     $this->siteContainer['log']->info("Adding area", array('area' => $area->getDataForLoggerInfo()));
     $this->siteContainer['arearepository']->create($area);
 }