From f9252cb9baa60cde1634fa073051b80a11a61ca8 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Thu, 15 May 2014 12:15:24 -0300 Subject: [PATCH] partial #2969 --- db/migrate/000189_drivers_onboarding.sql | 13 +++- .../cockpit2/api/driver/documents/index.php | 14 +++- .../cockpit2/api/driver/list/index.php | 4 - .../cockpit2/api/driver/notify/index.php | 74 +++++++++++++++++++ .../cockpit2/api/driver/save/index.php | 18 +++-- include/library/Crunchbutton/Driver/Log.php | 16 ++++ .../frontend/drivers-onboarding-form.phtml | 24 +++--- www/assets/cockpit/js/controllers.drivers.js | 11 ++- .../cockpit/js/service.driveronboarding.js | 13 ++++ www/assets/cockpit/scss/cockpit.scss | 5 ++ 10 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 include/controllers/default/cockpit2/api/driver/notify/index.php create mode 100644 include/library/Crunchbutton/Driver/Log.php diff --git a/db/migrate/000189_drivers_onboarding.sql b/db/migrate/000189_drivers_onboarding.sql index 5f5876b5e..c732c7ff9 100644 --- a/db/migrate/000189_drivers_onboarding.sql +++ b/db/migrate/000189_drivers_onboarding.sql @@ -27,4 +27,15 @@ CREATE TABLE `driver_document_status` ( KEY `driver_document_status_ibfk_2` (`id_admin`), CONSTRAINT `driver_document_status_ibfk_1` FOREIGN KEY (`id_driver_document`) REFERENCES `driver_document` (`id_driver_document`) ON DELETE SET NULL ON UPDATE SET NULL, CONSTRAINT `driver_document_status_ibfk_2` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL -) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; \ No newline at end of file +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + +CREATE TABLE `driver_log` ( + `id_driver_log` int(11) unsigned NOT NULL AUTO_INCREMENT, + `id_admin` int(11) unsigned DEFAULT NULL, + `action` enum('created','notified','document') DEFAULT NULL, + `info` varchar(200) DEFAULT NULL, + `datetime` datetime DEFAULT NULL, + PRIMARY KEY (`id_driver_log`), + KEY `driver_log_ibfk_1` (`id_admin`), + CONSTRAINT `driver_log_ibfk_1` FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`) ON DELETE SET NULL ON UPDATE SET NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/include/controllers/default/cockpit2/api/driver/documents/index.php b/include/controllers/default/cockpit2/api/driver/documents/index.php index 502761fc3..b17caf815 100644 --- a/include/controllers/default/cockpit2/api/driver/documents/index.php +++ b/include/controllers/default/cockpit2/api/driver/documents/index.php @@ -7,6 +7,7 @@ class Controller_api_driver_documents extends Crunchbutton_Controller_RestAccoun switch ( c::getPagePiece( 3 ) ) { case 'upload': + if( $_FILES ){ $ext = pathinfo( $_FILES['file']['name'], PATHINFO_EXTENSION ); if( Util::allowedExtensionUpload( $ext ) ){ @@ -40,6 +41,7 @@ class Controller_api_driver_documents extends Crunchbutton_Controller_RestAccoun break; case 'save': + $id_admin = $this->request()[ 'id_admin' ]; $id_driver_document = $this->request()[ 'id_driver_document' ]; if( $id_admin && $id_driver_document ){ @@ -52,13 +54,23 @@ class Controller_api_driver_documents extends Crunchbutton_Controller_RestAccoun $docStatus->datetime = date('Y-m-d H:i:s'); $docStatus->file = $this->request()[ 'file' ]; $docStatus->save(); - echo '
';var_dump( $docStatus->exports() );exit();
+					
+					// save driver's log
+					$log = new Crunchbutton_Driver_Log();
+					$log->id_admin = $driver->id_admin;
+					$log->action = 'document';
+					$log->info = $docStatus->id_driver_document . ': ' . $docStatus->file;
+					$log->datetime = date('Y-m-d H:i:s');
+					$log->save();
+
 					echo json_encode( ['success' => 'success'] );	
 				} else {
 					$this->_error();
 				}
 				break;
+
 			default:
+
 				$id_admin = false;
 				if( c::getPagePiece( 3 ) ){
 					$admin = Crunchbutton_Admin::o( c::getPagePiece( 3 ) );
diff --git a/include/controllers/default/cockpit2/api/driver/list/index.php b/include/controllers/default/cockpit2/api/driver/list/index.php
index b2e016274..d3042bfea 100644
--- a/include/controllers/default/cockpit2/api/driver/list/index.php
+++ b/include/controllers/default/cockpit2/api/driver/list/index.php
@@ -3,10 +3,6 @@
 class Controller_api_driver_list extends Crunchbutton_Controller_RestAccount {
 	
 	public function init() {	
-		$this->_list();
-	}
-
-	private function _list(){
 
 		$resultsPerPage = 20;
 
diff --git a/include/controllers/default/cockpit2/api/driver/notify/index.php b/include/controllers/default/cockpit2/api/driver/notify/index.php
new file mode 100644
index 000000000..5931326bc
--- /dev/null
+++ b/include/controllers/default/cockpit2/api/driver/notify/index.php
@@ -0,0 +1,74 @@
+method() != 'post' ){
+			// $this->_error();
+		}
+
+		$id_admin = c::getPagePiece( 3 );
+
+		$driver = Crunchbutton_Admin::o( $id_admin );
+
+		if( !$driver->id_admin ){
+			$this->_error();
+		}
+
+		$message = $this->request()[ 'message' ];
+
+		$phone = $driver->phone();
+
+		$phone = str_replace( '-' , '', $phone );
+
+		if( trim( $phone ) == '' ){
+			$this->_error( 'we need a phone number!' );
+		}
+
+		// Pre defined messages
+		switch ( $message ) {
+			case 'setup':
+				$message ="Access cbtn.io/setup/{$phone}";
+				break;
+			
+			default:
+				break;
+		}
+
+		if( trim( $message ) == '' ){
+			$this->_error();	
+		}
+		
+		$env = c::getEnv();
+
+		$twilio = new Twilio( c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token );
+
+		$message = str_split( $message, 160 );
+
+		$isOk = true;
+
+		foreach ( $message as $msg ) {
+			try {
+				// Log
+				Log::debug( [ 'action' => 'notify admin: ' . $id_admin, 'phone' => $phone, 'msg' => $msg, 'type' => 'admin-notification' ] );
+				$twilio->account->sms_messages->create( c::config()->twilio->{$env}->outgoingTextCustomer, '+1'. $phone, $msg );
+			} catch ( Exception $e ) {
+				$isOk = false;
+				// Log
+				Log::debug( [ 'action' => 'ERROR notify admin: ' . $id_admin, 'error' => $e->getInfo(), 'phone' => $phone, 'msg' => $msg, 'type' => 'admin-notification' ] );
+			}
+		}
+
+		if( $isOk ){
+			echo json_encode( [ 'success' => $driver->exports() ] );
+		} else {
+			$this->_error( 'notification not sent' );
+		}
+	}
+
+	private function _error( $error = 'invalid request' ){
+		echo json_encode( [ 'error' => $error ] );
+		exit();
+	}
+}
\ No newline at end of file
diff --git a/include/controllers/default/cockpit2/api/driver/save/index.php b/include/controllers/default/cockpit2/api/driver/save/index.php
index 9564c419e..02f05b1de 100644
--- a/include/controllers/default/cockpit2/api/driver/save/index.php
+++ b/include/controllers/default/cockpit2/api/driver/save/index.php
@@ -3,18 +3,18 @@
 class Controller_api_driver_save extends Crunchbutton_Controller_RestAccount {
 	
 	public function init() {
-		$this->_save();
-	}
-
-	private function _save(){
 
 		if( $this->method() != 'post' ){
 			$this->_error();
 		}
 
 		$id_admin = c::getPagePiece( 3 );
+
+		$newDriver = false;
+
 		// saves a new driver
 		if( !$id_admin ){
+			$newDriver = true;
 			$driver = new Crunchbutton_Admin();
 			// create the new driver as inactive
 			$driver->active = 0;
@@ -23,7 +23,7 @@ class Controller_api_driver_save extends Crunchbutton_Controller_RestAccount {
 		}
 			
 		$driver->name = $this->request()[ 'name' ];
-		$driver->phone = $this->request()[ 'phone' ];
+		$driver->phone = preg_replace( '/[^0-9]/i', '', $this->request()[ 'phone' ] );
 		$driver->email = $this->request()[ 'email' ];
 		
 		$driver->save();
@@ -51,6 +51,14 @@ class Controller_api_driver_save extends Crunchbutton_Controller_RestAccount {
 			}	
 		}
 
+		if( $newDriver ){
+			$log = new Crunchbutton_Driver_Log();
+			$log->id_admin = $driver->id_admin;
+			$log->action = 'created';
+			$log->datetime = date('Y-m-d H:i:s');
+			$log->save();
+		} 
+
 		echo json_encode( [ 'success' => $driver->exports() ] );
 		return;
 	}
diff --git a/include/library/Crunchbutton/Driver/Log.php b/include/library/Crunchbutton/Driver/Log.php
new file mode 100644
index 000000000..1b5418573
--- /dev/null
+++ b/include/library/Crunchbutton/Driver/Log.php
@@ -0,0 +1,16 @@
+table('driver_log')
+			->idVar('id_driver_log')
+			->load($id);
+	}
+
+	public function exports(){
+		return $this->properties();
+	}
+}
\ No newline at end of file
diff --git a/include/views/default/cockpit2/frontend/drivers-onboarding-form.phtml b/include/views/default/cockpit2/frontend/drivers-onboarding-form.phtml
index 7a4afe7be..b99052688 100644
--- a/include/views/default/cockpit2/frontend/drivers-onboarding-form.phtml
+++ b/include/views/default/cockpit2/frontend/drivers-onboarding-form.phtml
@@ -46,13 +46,15 @@
 						Enter a valid email.
   				
 				
-				

Documents

- +
  • +

    Documents

    + +
  • @@ -60,11 +62,11 @@
    -
    +

    Upload zone

    • -
    +
    +
    + +
    diff --git a/www/assets/cockpit/js/controllers.drivers.js b/www/assets/cockpit/js/controllers.drivers.js index 32cef9429..668a933c0 100644 --- a/www/assets/cockpit/js/controllers.drivers.js +++ b/www/assets/cockpit/js/controllers.drivers.js @@ -201,6 +201,7 @@ NGApp.controller( 'DriversOnboardingFormCtrl', function ( $scope, $fileUploader, var docs = function(){ // Load the docs DriverOnboardingService.docs.list( function( data ){ + console.log('data',data); $scope.documents = data; } ); } @@ -221,9 +222,13 @@ NGApp.controller( 'DriversOnboardingFormCtrl', function ( $scope, $fileUploader, $scope.submitted = true; return; } - DriverOnboardingService.save( $scope.driver, function(){ - $scope.navigation.link( '/drivers/onboarding/' ); - $scope.flash.setMessage( 'Driver saved!' ); + DriverOnboardingService.save( $scope.driver, function( json ){ + if( json.success ){ + $scope.navigation.link( '/drivers/onboarding/' + json.success.id_admin ); + $scope.flash.setMessage( 'Driver saved!' ); + } else { + $scope.flash.setMessage( 'Driver not saved: ' + json.error , 'error' ); + } } ); } diff --git a/www/assets/cockpit/js/service.driveronboarding.js b/www/assets/cockpit/js/service.driveronboarding.js index 17367623e..d251b37d4 100644 --- a/www/assets/cockpit/js/service.driveronboarding.js +++ b/www/assets/cockpit/js/service.driveronboarding.js @@ -5,6 +5,7 @@ NGApp.factory( 'DriverOnboardingService', function( $rootScope, $resource, $rout // Create a private resource 'drivers' var drivers = $resource( App.service + 'driver/:action/:id_admin/:page/:search', { id_admin: '@id_admin', action: '@action' }, { 'get' : { 'method': 'GET', params : { action : null } }, + 'notify' : { 'method': 'POST', params : { action: 'notify' } }, 'list' : { 'method': 'GET', params : { action: 'list', id_admin: null } }, 'save' : { 'method': 'POST', params : { action: 'save' } } } @@ -17,6 +18,18 @@ NGApp.factory( 'DriverOnboardingService', function( $rootScope, $resource, $rout } ); + service.notifySetup = function( id_admin, callback ){ + var message = 'setup'; + service.notify( id_admin, message, callback ); + } + + service.notify = function( id_admin, message, callback ){ + var params = { id_admin : id_admin, message : message }; + drivers.notify( params, function( data ){ + callback( data ); + } ); + } + service.save = function( driver, callback ){ drivers.save( driver, function( driver ){ callback( driver ); diff --git a/www/assets/cockpit/scss/cockpit.scss b/www/assets/cockpit/scss/cockpit.scss index 2b2bfc0f8..e16f24bfd 100644 --- a/www/assets/cockpit/scss/cockpit.scss +++ b/www/assets/cockpit/scss/cockpit.scss @@ -1262,6 +1262,11 @@ b, strong{ font-weight: bold; } margin-left: -100px; border-radius: 5px; } + .flash-message.error{ + background: #f2dede; + color: #a94442; + border: 1px solid #ebccd1; + } .flash-message.success{ background:#dff0d8; color: #64763d;