From 9d854e964253c15084fbfa8a270147e7276ae608 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 30 Oct 2014 13:07:54 +0000 Subject: [PATCH] Use regular "Thread::save" to close old invitations --- src/mibew/libs/invitation.php | 58 ++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/mibew/libs/invitation.php b/src/mibew/libs/invitation.php index e45c657c..88f3eba2 100644 --- a/src/mibew/libs/invitation.php +++ b/src/mibew/libs/invitation.php @@ -252,9 +252,27 @@ function invitation_close_old() // Run only one instance of cleaning process. $lock = new ProcessLock('invitations_close_old'); if ($lock->get()) { + // Freeze the time for the whole cleaning process. + $now = time(); + $db = Database::getInstance(); - // Get all threads to close + // Remove links beteen visitors and invitations that will be closed. + $db->query( + ("UPDATE {sitevisitor} v, {thread} t SET " + . "v.threadid = NULL " + . "WHERE t.istate = :state_invited " + . "AND t.invitationstate = :invitation_wait " + . "AND (:now - t.dtmcreated) > :lifetime"), + array( + ':invitation_wait' => Thread::INVITATION_WAIT, + ':state_invited' => Thread::STATE_INVITED, + ':lifetime' => Settings::get('invitation_lifetime'), + ':now' => $now, + ) + ); + + // Get all invitations to close $threads = $db->query( ("SELECT * FROM {thread} " . "WHERE istate = :state_invited " @@ -264,38 +282,28 @@ function invitation_close_old() ':invitation_wait' => Thread::INVITATION_WAIT, ':state_invited' => Thread::STATE_INVITED, ':lifetime' => Settings::get('invitation_lifetime'), - ':now' => time(), + ':now' => $now, ), array('return_rows' => Database::RETURN_ALL_ROWS) ); - // Remove old invitations - $db->query( - ("UPDATE {sitevisitor} v, {thread} t SET " - . "t.invitationstate = :invitation_ignored, " - . "t.istate = :state_closed, " - . "t.dtmclosed = :now, " - . "v.threadid = NULL " - . "WHERE t.istate = :state_invited " - . "AND t.invitationstate = :invitation_wait " - . "AND (:now - t.dtmcreated) > :lifetime"), - array( - ':invitation_ignored' => Thread::INVITATION_IGNORED, - ':invitation_wait' => Thread::INVITATION_WAIT, - ':state_closed' => Thread::STATE_CLOSED, - ':state_invited' => Thread::STATE_INVITED, - ':lifetime' => Settings::get('invitation_lifetime'), - ':now' => time(), - ) - ); - - // Iterate over all threads and send messages to operator about close by - // timeout + // Close the invitations foreach ($threads as $thread_info) { $thread = Thread::createFromDbInfo($thread_info); + $thread->invitationState = Thread::INVITATION_IGNORED; + $thread->state = Thread::STATE_CLOSED; + $thread->closed = $now; + $thread->save(); + + // Notify the operator about autoclosing $thread->postMessage( Thread::KIND_FOR_AGENT, - getlocal('Visitor ignored invitation and it was closed automatically', null, $thread->locale, true) + getlocal( + 'Visitor ignored invitation and it was closed automatically', + null, + $thread->locale, + true + ) ); $args = array('invitation' => $thread);