Skip to content

Commit

Permalink
Merge pull request #7 from ramartins02/new-features
Browse files Browse the repository at this point in the history
Script to automatically delete old data
  • Loading branch information
ramartins02 authored Dec 12, 2023
2 parents 93260b0 + d7fccdb commit 10c0361
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 6 deletions.
249 changes: 249 additions & 0 deletions Jobs/deleteolddata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php
/**
* Cron Example:
* This script must be executed at a specific time to be enabled
* * * * * * /usr/bin/env php -f ${WWW_DIR}/librebooking/Jobs/sendmissedcheckin.php
* Each * correspods to Minute Hour Day_Of_Month Month Day_Of_Week, respectively
* /usr/bin/env php -f -> the path to php (run "which php" to know what it is)
* ${WWW_DIR}/librebooking/Jobs/sendmissedcheckin.php -> the path to this script
*/
define('ROOT_DIR', dirname(__FILE__) . '/../');
require_once(ROOT_DIR . 'Domain/Access/namespace.php');
require_once(ROOT_DIR . 'Jobs/JobCop.php');

Log::Debug('Running deleteolddata.php');

JobCop::EnsureCommandLine();

try {
//Delete announcements, blackouts and reservations older than $deleteBefore (years -> -2 = 2+ years older)
$deleteBefore = Date::Now()->AddYears(-(Configuration::Instance()->GetSectionKey(ConfigSection::DELETE_OLD_DATA, ConfigKeys::YEARS_OLD_DATA)));

//ANNOUNCEMENTS
if (Configuration::Instance()->GetSectionKey(ConfigSection::DELETE_OLD_DATA, ConfigKeys::DELETE_OLD_ANNOUNCEMENTS, new BooleanConverter())){
$getAnnouncements = GetAnnoucementsIdsToDeleteQuery($deleteBefore);
$reader = ServiceLocator::GetDatabase()->Query($getAnnouncements);
Log::Debug('Getting %s old announcements', $reader->NumRows());
while ($row = $reader->GetRow()) {
$annoucementId = $row[ColumnNames::ANNOUNCEMENT_ID];
DeleteAnnouncementsQuery($annoucementId);
}
$reader->Free();
}

//BLACKOUTS
if (Configuration::Instance()->GetSectionKey(ConfigSection::DELETE_OLD_DATA, ConfigKeys::DELETE_OLD_BLACKOUTS, new BooleanConverter())){
$getBlackouts = GetBlackoutsIdsToDeleteQuery($deleteBefore);
$reader = ServiceLocator::GetDatabase()->Query($getBlackouts);
Log::Debug('Getting %s old blackouts', $reader->NumRows());
while ($row = $reader->GetRow()) {
$blackoutId = $row[ColumnNames::BLACKOUT_SERIES_ID];
DeleteBlackoutsQuery($blackoutId);
}
$reader->Free();
}

//RESERVATIONS
if (Configuration::Instance()->GetSectionKey(ConfigSection::DELETE_OLD_DATA, ConfigKeys::DELETE_OLD_RESERVATIONS, new BooleanConverter())){
$getReservations = GetReservationsIdsToDeleteQuery($deleteBefore);
$reader = ServiceLocator::GetDatabase()->Query($getReservations);
Log::Debug('Getting %s old reservations', $reader->NumRows());
while ($row = $reader->GetRow()) {
$reservationId = $row[ColumnNames::RESERVATION_SERIES_ID];

//RESERVATION USERS
$getReservationUsers = GetReservationUsersToDelete($reservationId);
$auxReader = ServiceLocator::GetDatabase()->Query($getReservationUsers);
while ($auxRow = $auxReader->GetRow()){
$reservationUser = $auxRow[ColumnNames::RESERVATION_INSTANCE_ID];
DeleteReservationUsersQuery($reservationUser);
}

$auxReader->Free();
DeleteReservationsQuery($reservationId);
}
$reader->Free();
}

} catch (Exception $ex){
Log::Error('Error running deleteolddata.php: %s', $ex);
}

Log::Debug('Finished running deleteolddata.php');

//GET INSTANCES FUNCTIONS

// -> ANNOUNCEMENTS
function GetAnnoucementsIdsToDeleteQuery($deleteBefore){

$annoucementsIds = new AdHocCommand(
" SELECT announcementid
FROM announcements
WHERE end_date < '{$deleteBefore}'"
);

return $annoucementsIds;
}

// -> BLACKOUTS
function GetBlackoutsIdsToDeleteQuery($deleteBefore){

$blackoutsIds = new AdHocCommand(
" SELECT blackout_series_id
FROM blackout_series
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(repeat_options, '|', -2),'=',-2),'|',1) < '{$deleteBefore}' AND repeat_options != ''
UNION
SELECT blackout_series.blackout_series_id
FROM blackout_series
LEFT JOIN blackout_instances
ON blackout_series.blackout_series_id = blackout_instances.blackout_series_id
WHERE blackout_series.repeat_type = 'none' AND blackout_instances.end_date < '{$deleteBefore}'"
);

return $blackoutsIds;
}

// -> RESERVATION USERS
function GetReservationUsersToDelete($reservationSeriesId){
$reservationInstance = new AdHocCommand(
" SELECT reservation_instance_id
FROM reservation_instances
WHERE series_id = '{$reservationSeriesId}'"
);
return $reservationInstance;
}

// -> RESERVATIONS
function GetReservationsIdsToDeleteQuery($deleteBefore) {

$reservationIds = new AdHocCommand(
" SELECT reservation_series.series_id
FROM reservation_series
LEFT JOIN reservation_instances
ON reservation_instances.series_id = reservation_series.series_id
WHERE reservation_series.repeat_type = 'none' AND end_date < '{$deleteBefore}'
UNION
SELECT series_id
FROM reservation_series
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(repeat_options, '|', -1), '=', -1) < '{$deleteBefore}' AND repeat_options != ''"
);

return $reservationIds;
}


//DELETE INSTANCES FUNCTIONS

// -> ANNOUNCEMENTS
function DeleteAnnouncementsQuery($announcementid){
Log::Debug('Deleting announcement with id %s', $announcementid);

$deleteGroupAnnouncements = new AdHocCommand(
" DELETE FROM announcement_groups
WHERE announcementid = '{$announcementid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteGroupAnnouncements);

$deleteResourceAnnouncements = new AdHocCommand(
" DELETE FROM announcement_resources
WHERE announcementid = '{$announcementid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteResourceAnnouncements);

$deleteAnnouncements = new AdHocCommand(
" DELETE FROM announcements
WHERE announcementid = '{$announcementid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteAnnouncements);

Log::Debug('Announcement with id %s deleted', $announcementid);
}

// -> BLACKOUTS
function DeleteBlackoutsQuery($blackoutid){
Log::Debug('Deleting blackout with id %s', $blackoutid);

$deleteBlackoutSeriesResources = new AdHocCommand(
" DELETE FROM blackout_series_resources
WHERE blackout_series_id = '{$blackoutid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteBlackoutSeriesResources);

$deleteBlackoutInstances = new AdHocCommand(
" DELETE FROM blackout_instances
WHERE blackout_series_id = '{$blackoutid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteBlackoutInstances);

$deleteBlackoutSeries = new AdHocCommand(
" DELETE FROM blackout_series
WHERE blackout_series_id = '{$blackoutid}'"
);
ServiceLocator::GetDatabase()->Execute($deleteBlackoutSeries);

Log::Debug('Blackout with id %s deleted', $blackoutid);
}

// -> RESERVATION USERS
function DeleteReservationUsersQuery($reservationInstanceId){
$deleteReservationGuests = new AdHocCommand(
"DELETE FROM reservation_guests
WHERE reservation_instance_id = '{$reservationInstanceId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationGuests);

$deleteReservationUsers = new AdHocCommand(
"DELETE FROM reservation_users
WHERE reservation_instance_id = '{$reservationInstanceId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationUsers);
}

// -> RESERVATIONS
function DeleteReservationsQuery($reservationId){
Log::Debug('Deleting reservation with id %s', $reservationId);

$deleteReservationAcessories = new AdHocCommand(
"DELETE FROM reservation_accessories
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationAcessories);

$deleteReservationFiles = new AdHocCommand(
"DELETE FROM reservation_files
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationFiles);

$deleteReservationReminders = new AdHocCommand(
"DELETE FROM reservation_reminders
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationReminders);

$deleteReservationResources = new AdHocCommand(
"DELETE FROM reservation_resources
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationResources);

$deleteReservationInstances = new AdHocCommand(
"DELETE FROM reservation_instances
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationInstances);

$deleteReservationSeries = new AdHocCommand(
"DELETE FROM reservation_series
WHERE series_id = '{$reservationId}'"
);
ServiceLocator::GetDatabase()->Execute($deleteReservationSeries);

Log::Debug('Reservation with id %s deleted', $reservationId);
}
6 changes: 3 additions & 3 deletions Pages/LoginPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function SetAnnouncements($announcements)
* Sends the created google url in the presenter to the smarty page
*/
public function SetGoogleUrl($googleUrl){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_GOOGLE) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_GOOGLE, new BooleanConverter())){
$this->Set('GoogleUrl',$googleUrl);
}
}
Expand All @@ -310,7 +310,7 @@ public function SetGoogleUrl($googleUrl){
* Sends the created microsoft url in the presenter to the smarty page
*/
public function SetMicrosoftUrl($microsoftUrl){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_MICROSOFT) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_MICROSOFT, new BooleanConverter())){
$this->Set('MicrosoftUrl',$microsoftUrl);
}
}
Expand All @@ -319,7 +319,7 @@ public function SetMicrosoftUrl($microsoftUrl){
* Sends the created facebook url in the presenter to the smarty page
*/
public function SetFacebookUrl($FacebookUrl){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_FACEBOOK) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_FACEBOOK, new BooleanConverter())){
$this->Set('FacebookUrl',$FacebookUrl);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Presenters/LoginPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected function LoadValidators()
* Returns the created google url for the authentication
*/
public function GetGoogleUrl(){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_GOOGLE) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_GOOGLE, new BooleanConverter())){
$client = new Google\Client();
$client->setClientId(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::GOOGLE_CLIENT_ID));
$client->setClientSecret(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::GOOGLE_CLIENT_SECRET));
Expand All @@ -249,7 +249,7 @@ public function GetGoogleUrl(){
* Returns the created microsoft url for the authentication
*/
public function GetMicrosoftUrl(){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_MICROSOFT) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_MICROSOFT, new BooleanConverter())){
$MicrosoftUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?'
.'client_id=' . urlencode(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::MICROSOFT_CLIENT_ID))
.'&redirect_uri=' . urlencode(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::MICROSOFT_REDIRECT_URI))
Expand All @@ -266,7 +266,7 @@ public function GetMicrosoftUrl(){
* Returns the created facebook url for the authentication
*/
public function GetFacebookUrl(){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_FACEBOOK) == 'true'){
if(Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::AUTHENTICATION_ALLOW_FACEBOOK, new BooleanConverter())){
$facebook_Client = new Facebook\Facebook([
'app_id' => Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::FACEBOOK_CLIENT_ID),
'app_secret' => Configuration::Instance()->GetSectionKey(ConfigSection::AUTHENTICATION, ConfigKeys::FACEBOOK_CLIENT_SECRET),
Expand Down
8 changes: 8 additions & 0 deletions config/config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,11 @@
$conf['settings']['authentication']['facebook.client.id'] = '';
$conf['settings']['authentication']['facebook.client.secret'] = '';
$conf['settings']['authentication']['facebook.redirect.uri'] = '/Web/facebook-auth.php';
/**
* Delete old data job configuration
* Activate the deleteolddata.php as a background job to use this feature
*/
$conf['settings']['delete.old.data']['years.old.data'] = '3'; //Choose how long a blackout, announcement and reservation stay in the database (in years) counting from the end date
$conf['settings']['delete.old.data']['delete.old.announcements'] = 'false'; //Choose if this feature deletes old announcements from database
$conf['settings']['delete.old.data']['delete.old.blackouts'] = 'false'; //Choose if this feature deletes old blackouts from database
$conf['settings']['delete.old.data']['delete.old.reservations'] = 'false'; //Choose if this feature deletes old reservations from database
5 changes: 5 additions & 0 deletions lib/Config/ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ class ConfigKeys
public const FACEBOOK_CLIENT_SECRET = 'facebook.client.secret';
public const FACEBOOK_REDIRECT_URI = 'facebook.redirect.uri';

public const YEARS_OLD_DATA = 'years.old.data';
public const DELETE_OLD_ANNOUNCEMENTS = 'delete.old.announcements';
public const DELETE_OLD_BLACKOUTS = 'delete.old.blackouts';
public const DELETE_OLD_RESERVATIONS = 'delete.old.reservations';
}

class ConfigSection
Expand Down Expand Up @@ -201,4 +205,5 @@ class ConfigSection
public const TABLET_VIEW = 'tablet.view';
public const REGISTRATION = 'registration';
public const LOGGING = 'logging';
public const DELETE_OLD_DATA = 'delete.old.data';
}

0 comments on commit 10c0361

Please sign in to comment.