public function getSummary() { $txt = ''; if ($this->areaHistoryModel->getIsNew()) { $txt .= 'New! ' . "\n"; } if ($this->areaHistoryModel->isAnyChangeFlagsUnknown()) { $txt .= $this->areaHistoryModel->getDescription(); } else { if ($this->areaHistoryModel->getTitleChanged()) { $txt .= 'Title Changed. ' . "\n"; } if ($this->areaHistoryModel->getDescriptionChanged()) { $txt .= 'Description Changed. ' . "\n"; } if ($this->areaHistoryModel->getParentAreaIdChanged()) { $txt .= 'Parent Area Changed. ' . "\n"; } if ($this->areaHistoryModel->getCountryIdChanged()) { $txt .= 'Country Changed. ' . "\n"; } if ($this->areaHistoryModel->getIsDeletedChanged()) { $txt .= 'Deleted Changed: ' . ($this->areaHistoryModel->getIsDeleted() ? "Deleted" : "Restored") . "\n\n"; } } return $txt; }
function testIntegration1() { $this->addCountriesToTestDB(); \TimeSource::mock(2014, 1, 1, 12, 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 CountryRepository(); $gb = $countryRepo->loadByTwoCharCode('GB'); ## Create area \TimeSource::mock(2014, 1, 1, 13, 0, 0); $area = new AreaModel(); $area->setTitle("test"); $area->setDescription("test test"); $area->setCountryId($gb->getId()); $areaRepo = new AreaRepository(); $areaRepo->create($area, null, $site, $gb, $user); ## Edit area \TimeSource::mock(2014, 1, 1, 14, 0, 0); $area = $areaRepo->loadById($area->getId()); $area->setDescription("testy"); $areaRepo->edit($area, $user); ## Now save changed flags on these ..... $areaHistoryRepo = new AreaHistoryRepository(); $stat = $this->app['db']->prepare("SELECT * FROM area_history"); $stat->execute(); while ($data = $stat->fetch()) { $areaHistory = new AreaHistoryModel(); $areaHistory->setFromDataBaseRow($data); $areaHistoryRepo->ensureChangedFlagsAreSet($areaHistory); } ## Now load and check $historyRepo = new HistoryRepositoryBuilder(); $historyRepo->setIncludeEventHistory(false); $historyRepo->setIncludeVenueHistory(false); $historyRepo->setIncludeGroupHistory(false); $historyRepo->setIncludeAreaHistory(true); $histories = $historyRepo->fetchAll(); $this->assertEquals(2, count($histories)); #the edit $this->assertEquals(FALSE, $histories[0]->getTitleChanged()); $this->assertEquals(true, $histories[0]->getDescriptionChanged()); $this->assertEquals(false, $histories[0]->getCountryIdChanged()); $this->assertEquals(false, $histories[0]->getParentAreaIdChanged()); $this->assertEquals(false, $histories[0]->getIsDeletedChanged()); #the create $this->assertEquals(true, $histories[1]->getTitleChanged()); $this->assertEquals(true, $histories[1]->getDescriptionChanged()); $this->assertEquals(true, $histories[1]->getCountryIdChanged()); $this->assertEquals(false, $histories[1]->getParentAreaIdChanged()); $this->assertEquals(false, $histories[1]->getIsDeletedChanged()); }
protected function run() { $areaHistoryRepo = new AreaHistoryRepository(); $stat = $this->app['db']->prepare("SELECT * FROM area_history"); $stat->execute(); $count = 0; while ($data = $stat->fetch()) { $areaHistory = new AreaHistoryModel(); $areaHistory->setFromDataBaseRow($data); $areaHistoryRepo->ensureChangedFlagsAreSet($areaHistory); ++$count; } return array('result' => 'ok', 'count' => $count); }
function testSetChangedFlagsFromLast1() { $lastHistory = new AreaHistoryModel(); $lastHistory->setFromDataBaseRow(array('area_id' => 1, 'title' => 'Cat', 'description' => '', 'user_account_id' => 1, 'created_at' => '2014-02-01 10:00:00', 'parent_area_id' => '', 'country_id' => 77, 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'country_id_changed' => 0, 'parent_area_id_changed' => 0, 'is_deleted_changed' => 0)); $areaHistory = new AreaHistoryModel(); $areaHistory->setFromDataBaseRow(array('area_id' => 1, 'title' => 'Cat', 'description' => 'This area has no name', 'user_account_id' => 1, 'created_at' => '2014-02-01 12:00:00', 'parent_area_id' => 6, 'country_id' => 78, 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'country_id_changed' => 0, 'parent_area_id_changed' => 0, 'is_deleted_changed' => 0)); $areaHistory->setChangedFlagsFromLast($lastHistory); $this->assertEquals(false, $areaHistory->getTitleChanged()); $this->assertEquals(true, $areaHistory->getDescriptionChanged()); $this->assertEquals(true, $areaHistory->getCountryIdChanged()); $this->assertEquals(true, $areaHistory->getParentAreaIdChanged()); $this->assertEquals(false, $areaHistory->getIsDeletedChanged()); $this->assertEquals(false, $areaHistory->getIsNew()); }
public function ensureChangedFlagsAreSet(AreaHistoryModel $areaHistory) { global $DB; // do we already have them? if (!$areaHistory->isAnyChangeFlagsUnknown()) { return; } // load last. $stat = $DB->prepare("SELECT * FROM area_history WHERE area_id = :id AND created_at < :at " . "ORDER BY created_at DESC"); $stat->execute(array('id' => $areaHistory->getId(), 'at' => $areaHistory->getCreatedAt()->format("Y-m-d H:i:s"))); if ($stat->rowCount() == 0) { $areaHistory->setChangedFlagsFromNothing(); } else { while ($areaHistory->isAnyChangeFlagsUnknown() && ($lastHistoryData = $stat->fetch())) { $lastHistory = new AreaHistoryModel(); $lastHistory->setFromDataBaseRow($lastHistoryData); $areaHistory->setChangedFlagsFromLast($lastHistory); } } // Save back to DB $sqlFields = array(); $sqlParams = array('id' => $areaHistory->getId(), 'created_at' => $areaHistory->getCreatedAt()->format("Y-m-d H:i:s"), 'is_new' => $areaHistory->getIsNew() ? 1 : 0); if ($areaHistory->getTitleChangedKnown()) { $sqlFields[] = " title_changed = :title_changed "; $sqlParams['title_changed'] = $areaHistory->getTitleChanged() ? 1 : -1; } if ($areaHistory->getDescriptionChangedKnown()) { $sqlFields[] = " description_changed = :description_changed "; $sqlParams['description_changed'] = $areaHistory->getDescriptionChanged() ? 1 : -1; } if ($areaHistory->getCountryIdChangedKnown()) { $sqlFields[] = " country_id_changed = :country_id_changed "; $sqlParams['country_id_changed'] = $areaHistory->getCountryIdChanged() ? 1 : -1; } if ($areaHistory->getParentAreaIdChangedKnown()) { $sqlFields[] = " parent_area_id_changed = :parent_area_id_changed "; $sqlParams['parent_area_id_changed'] = $areaHistory->getParentAreaIdChanged() ? 1 : -1; } if ($areaHistory->getIsDuplicateOfIdChangedKnown()) { $sqlFields[] = " is_duplicate_of_id_changed = :is_duplicate_of_id_changed "; $sqlParams['is_duplicate_of_id_changed'] = $areaHistory->getIsDuplicateOfIdChangedKnown() ? 1 : -1; } if ($areaHistory->getIsDeletedChangedKnown()) { $sqlFields[] = " is_deleted_changed = :is_deleted_changed "; $sqlParams['is_deleted_changed'] = $areaHistory->getIsDeletedChanged() ? 1 : -1; } $statUpdate = $DB->prepare("UPDATE area_history SET " . " is_new = :is_new, " . implode(" , ", $sqlFields) . " WHERE area_id = :id AND created_at = :created_at"); $statUpdate->execute($sqlParams); }
public function fetchAll() { global $DB, $app; $results = array(); /////////////////////////// Events History if ($this->historyRepositoryBuilderConfig->getIncludeEventHistory()) { $where = array(); $joins = array(); $params = array(); if ($this->historyRepositoryBuilderConfig->getEvent()) { $where[] = 'event_information.id=:event'; $params['event'] = $this->historyRepositoryBuilderConfig->getEvent()->getId(); } if ($this->historyRepositoryBuilderConfig->getGroup()) { // We use a seperate table here so if event is in 2 groups and we select events in 1 group that isn't the main group only, // the normal event_in_group table still shows the main group. $joins[] = " JOIN event_in_group AS event_in_group_select ON event_in_group_select.event_id = event_information.id " . "AND event_in_group_select.removed_at IS NULL AND event_in_group_select.group_id = :group_id "; $params['group_id'] = $this->historyRepositoryBuilderConfig->getGroup()->getId(); } if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'event_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getVenue()) { $where[] = 'event_information.venue_id = :venue'; $params['venue'] = $this->historyRepositoryBuilderConfig->getVenue()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' event_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'event_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'event_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } if ($this->historyRepositoryBuilderConfig->getArea()) { $areaids = array($this->historyRepositoryBuilderConfig->getArea()->getId()); $this->statAreas = $DB->prepare("SELECT area_id FROM cached_area_has_parent WHERE has_parent_area_id=:id"); $this->statAreas->execute(array('id' => $this->historyRepositoryBuilderConfig->getArea()->getId())); while ($d = $this->statAreas->fetch()) { $areaids[] = $d['area_id']; } $joins[] = " LEFT JOIN venue_information ON event_information.venue_id = venue_information.id "; $where[] = ' (event_information.area_id IN (' . implode(",", $areaids) . ') OR venue_information.area_id IN (' . implode(",", $areaids) . ') ) '; } if ($this->historyRepositoryBuilderConfig->getVenueVirtualOnly()) { // we check both on an OR, that way we get both // a) events that were not virtual and became virtual, we get their full history // b) events that were virtual and now aren't, we get some of their history $where[] = " ( event_information.is_virtual = '1' OR event_history.is_virtual = '1' )"; } $sql = "SELECT event_history.*, group_information.title AS group_title, group_information.id AS group_id, event_information.slug AS event_slug, user_account_information.username AS user_account_username FROM event_history " . " LEFT JOIN user_account_information ON user_account_information.id = event_history.user_account_id " . " LEFT JOIN event_information ON event_information.id = event_history.event_id " . " LEFT JOIN event_in_group ON event_in_group.event_id = event_information.id AND event_in_group.removed_at IS NULL AND event_in_group.is_main_group = '1' " . " LEFT JOIN group_information ON group_information.id = event_in_group.group_id " . implode(" ", $joins) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY event_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $eventHistory = new EventHistoryModel(); $eventHistory->setFromDataBaseRow($data); $results[] = $eventHistory; } } /////////////////////////// Group History if ($this->historyRepositoryBuilderConfig->getIncludeGroupHistory()) { $where = array(); $params = array(); if ($this->historyRepositoryBuilderConfig->getEvent() && $this->historyRepositoryBuilderConfig->getEvent()->getGroupId()) { $where[] = 'group_information.id=:group'; $params['group'] = $this->historyRepositoryBuilderConfig->getEvent()->getGroupId(); } else { if ($this->historyRepositoryBuilderConfig->getGroup()) { $where[] = 'group_information.id =:group'; $params['group'] = $this->historyRepositoryBuilderConfig->getGroup()->getId(); } } if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'group_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' group_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'group_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'group_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } $sql = "SELECT group_history.*, group_information.slug AS group_slug, user_account_information.username AS user_account_username FROM group_history " . " LEFT JOIN user_account_information ON user_account_information.id = group_history.user_account_id " . " LEFT JOIN group_information ON group_information.id = group_history.group_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY group_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $groupHistory = new GroupHistoryModel(); $groupHistory->setFromDataBaseRow($data); $results[] = $groupHistory; } } /////////////////////////// Venue History if ($this->historyRepositoryBuilderConfig->getIncludeVenueHistory()) { $where = array(); $params = array(); if ($this->historyRepositoryBuilderConfig->getEvent() && $this->historyRepositoryBuilderConfig->getEvent()->getVenueId()) { $where[] = 'venue_information.id=:venue'; $params['venue'] = $this->historyRepositoryBuilderConfig->getEvent()->getVenueId(); } else { if ($this->historyRepositoryBuilderConfig->getVenue()) { $where[] = 'venue_information.id=:venue'; $params['venue'] = $this->historyRepositoryBuilderConfig->getVenue()->getId(); } } if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'venue_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' venue_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'venue_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'venue_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } $sql = "SELECT venue_history.*, venue_information.slug AS venue_slug, user_account_information.username AS user_account_username FROM venue_history " . " LEFT JOIN user_account_information ON user_account_information.id = venue_history.user_account_id " . " LEFT JOIN venue_information ON venue_information.id = venue_history.venue_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY venue_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $venueHistory = new VenueHistoryModel(); $venueHistory->setFromDataBaseRow($data); $results[] = $venueHistory; } } /////////////////////////// Area History if ($this->historyRepositoryBuilderConfig->getIncludeAreaHistory()) { $where = array(); $params = array(); $joins = array(); if ($this->historyRepositoryBuilderConfig->getArea()) { // Will this produce dupes? No evidence so far but there was a note in EventRepositoryBuilder that said so. $joins[] = " LEFT JOIN cached_area_has_parent ON cached_area_has_parent.area_id = area_information.id "; $where[] = ' (area_information.id =:area OR cached_area_has_parent.has_parent_area_id =:area )'; $params['area'] = $this->historyRepositoryBuilderConfig->getArea()->getId(); } if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'area_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' area_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'area_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'area_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } $sql = "SELECT area_history.*, area_information.slug AS area_slug, user_account_information.username AS user_account_username FROM area_history " . " LEFT JOIN user_account_information ON user_account_information.id = area_history.user_account_id " . " LEFT JOIN area_information ON area_information.id = area_history.area_id " . implode(" ", $joins) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY area_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $areaHistory = new AreaHistoryModel(); $areaHistory->setFromDataBaseRow($data); $results[] = $areaHistory; } } /////////////////////////// Tags History if ($this->historyRepositoryBuilderConfig->getIncludeTagHistory()) { $where = array(); $params = array(); if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'tag_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' tag_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'tag_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'tag_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } $sql = "SELECT tag_history.*, tag_information.slug AS tag_slug, user_account_information.username AS user_account_username FROM tag_history " . " LEFT JOIN user_account_information ON user_account_information.id = tag_history.user_account_id " . " LEFT JOIN tag_information ON tag_information.id = tag_history.tag_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY tag_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $tagHistory = new TagHistoryModel(); $tagHistory->setFromDataBaseRow($data); $results[] = $tagHistory; } } /////////////////////////// Import URL History if ($this->historyRepositoryBuilderConfig->getIncludeImportURLHistory()) { $where = array(); $params = array(); if ($this->historyRepositoryBuilderConfig->getSite()) { $where[] = 'import_url_information.site_id =:site'; $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId(); } if ($this->historyRepositoryBuilderConfig->getGroup()) { $where[] = 'import_url_information.group_id =:group'; $params['group'] = $this->historyRepositoryBuilderConfig->getGroup()->getId(); } if ($this->historyRepositoryBuilderConfig->getSince()) { $where[] = ' import_url_history.created_at >= :since '; $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s"); } if ($this->historyRepositoryBuilderConfig->getNotUser()) { $where[] = 'import_url_history.user_account_id != :userid '; $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId(); } if ($this->historyRepositoryBuilderConfig->getApi2app()) { $where[] = 'import_url_history.api2_application_id = :api2app'; $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId(); } $sql = "SELECT import_url_history.*, import_url_information.slug AS import_url_slug, " . "user_account_information.username AS user_account_username " . " FROM import_url_history " . " LEFT JOIN user_account_information ON user_account_information.id = import_url_history.user_account_id " . " LEFT JOIN import_url_information ON import_url_information.id = import_url_history.import_url_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY import_url_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit(); //var_dump($sql); var_dump($params); $stat = $DB->prepare($sql); $stat->execute($params); while ($data = $stat->fetch()) { $tagHistory = new ImportURLHistoryModel(); $tagHistory->setFromDataBaseRow($data); $results[] = $tagHistory; } } ////////////////////// Others! foreach ($app['extensions']->getExtensions() as $ext) { $results = array_merge($results, $ext->getHistoryRepositoryBuilderData($this->historyRepositoryBuilderConfig)); } ////////////////////// Finally sort & truncate $usort = function ($a, $b) { if ($a->getCreatedAtTimeStamp() == $b->getCreatedAtTimeStamp()) { return 0; } else { if ($a->getCreatedAtTimeStamp() > $b->getCreatedAtTimeStamp()) { return -1; } else { return 1; } } }; usort($results, $usort); array_slice($results, 0, $this->historyRepositoryBuilderConfig->getLimit()); return $results; }