From 2f48c594a21cbfd09080a8a597ee3805df7febdd Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Tue, 20 May 2014 10:50:52 -0300 Subject: [PATCH] partial #2969 - put the notification inside the timeout --- include/library/Cockpit/Driver/Notify.php | 62 ++++++++++++++--------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/include/library/Cockpit/Driver/Notify.php b/include/library/Cockpit/Driver/Notify.php index 2e68a7bca..822d05478 100644 --- a/include/library/Cockpit/Driver/Notify.php +++ b/include/library/Cockpit/Driver/Notify.php @@ -7,7 +7,6 @@ class Cockpit_Driver_Notify extends Cana_Table { public function send( $id_admin, $message ){ - $driver = Crunchbutton_Admin::o( $id_admin ); if( !$driver->id_admin ){ @@ -38,10 +37,40 @@ class Cockpit_Driver_Notify extends Cana_Table { if( trim( $message ) == '' ){ return [ 'error' => 'enter a message' ]; } - - $env = c::getEnv(); - //!todo: put this notifications at timeout + $notification = new Cockpit_Driver_Notify; + $notification->id_admin = $driver->id_admin; + $notification->phone = $phone; + $notification->email = $driver->email; + $notification->message = $message; + + + Cana::timeout( function() use( $notification ) { + $notification->notify(); + } ); + + // log + $log = new Cockpit_Driver_Log(); + $log->id_admin = $id_admin; + $log->action = Cockpit_Driver_Log::ACTION_NOTIFIED_SETUP; + $log->info = $phone . ' (' . $driver->email . ') ' . $message; + $log->datetime = date('Y-m-d H:i:s'); + $log->save(); + + return [ 'success' => 'notification sent' ]; + + } + + public function notify(){ + + $notification = $this; + + $message = $notification->message; + $id_admin = $notification->id_admin; + $phone = $notification->phone; + $email = $notification->email; + + $env = c::getEnv(); $twilio = new Twilio( c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token ); @@ -62,33 +91,18 @@ class Cockpit_Driver_Notify extends Cana_Table { } // Send email - if( $driver->email ){ - + if( $email ){ switch ( $message ) { case Cockpit_Driver_Notify::TYPE_WELCOME: - $mail = new Cockpit_Email_Driver_Welcome( [ 'id_admin' => $driver->id_admin ] ); - // $mail->send(); + $mail = new Cockpit_Email_Driver_Welcome( [ 'id_admin' => $id_admin ] ); + $mail->send(); break; case Cockpit_Driver_Notify::TYPE_SETUP: - $mail = new Cockpit_Email_Driver_Setup( [ 'id_admin' => $driver->id_admin ] ); - // $mail->send(); + $mail = new Cockpit_Email_Driver_Setup( [ 'id_admin' => $id_admin ] ); + $mail->send(); break; } } - - if( $isOk ){ - // log - $log = new Cockpit_Driver_Log(); - $log->id_admin = $driver->id_admin; - $log->action = Cockpit_Driver_Log::ACTION_NOTIFIED_SETUP; - $log->info = $phone . ': ' . join( $message ); - $log->datetime = date('Y-m-d H:i:s'); - $log->save(); - return [ 'success' => 'notification sent' ]; - } else { - return [ 'error' => 'notification not sent' ]; - } - } } \ No newline at end of file