This commit is contained in:
Daniel Camargo 2014-05-15 12:15:24 -03:00
parent c811753946
commit f9252cb9ba
10 changed files with 169 additions and 23 deletions

View File

@ -28,3 +28,14 @@ CREATE TABLE `driver_document_status` (
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;
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;

View File

@ -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 '<pre>';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 ) );

View File

@ -3,10 +3,6 @@
class Controller_api_driver_list extends Crunchbutton_Controller_RestAccount {
public function init() {
$this->_list();
}
private function _list(){
$resultsPerPage = 20;

View File

@ -0,0 +1,74 @@
<?php
class Controller_api_driver_notify extends Crunchbutton_Controller_RestAccount {
public function init() {
if( $this->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();
}
}

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
<?php
class Crunchbutton_Driver_Log extends Cana_Table {
public function __construct($id = null) {
parent::__construct();
$this
->table('driver_log')
->idVar('id_driver_log')
->load($id);
}
public function exports(){
return $this->properties();
}
}

View File

@ -46,6 +46,7 @@
<small class="error" ng-show="form.driverEmail.$invalid">Enter a valid email.</small>
</div>
</li>
<li ng-if="driver.id_admin">
<h2>Documents</h2>
<ul>
<li ng-repeat="doc in documents">
@ -53,6 +54,7 @@
</div>
</li>
</ul>
</li>
<li class="buttons">
<button ng-click="save();">Save</button>
<button ng-click="cancel()">Cancel</button>
@ -60,11 +62,11 @@
</ul>
</form>
<hr/>
<form name="documents">
<form name="docs" ng-if="driver.id_admin">
<h2>Upload zone</h2>
<ul>
<li>
<label for="doc_uploaded">
<label for="driverDocument">
<div class="label">Select the file type:</div>
<select name="driverDocument" ng-model="doc_uploaded" ng-options="opt.id_driver_document as opt.name for opt in documents"></select>
</label>
@ -80,5 +82,9 @@
</li>
</ul>
</form>
<hr/>
<div class="notifications" ng-if="driver.id_admin">
</div>
</div>
</div>

View File

@ -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/' );
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' );
}
} );
}

View File

@ -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 );

View File

@ -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;