-
-
-
-
-
+
New driver
+Edit driver
+
- - - step 2 - -
- - step 3 - -
diff --git a/include/controllers/default/cockpit2/api/driveronboarding/index.php b/include/controllers/default/cockpit2/api/driveronboarding/index.php new file mode 100644 index 000000000..a4dfdf180 --- /dev/null +++ b/include/controllers/default/cockpit2/api/driveronboarding/index.php @@ -0,0 +1,96 @@ +_save(); + break; + + case 'driver': + $this->_driver(); + break; + + default: + $this->_list(); + break; + } + } + + private function _driver(){ + $id_admin = c::getPagePiece( 3 ); + $driver = new Crunchbutton_Admin( $id_admin ); + if( $driver->id_admin ){ + echo json_encode( $driver->exports() ); + } else { + $this->_error( 'invalid object' ); + } + } + + private function _list(){ + $drivers = Crunchbutton_Admin::drivers(); + $list = []; + foreach( $drivers as $driver ){ + $list[] = $driver->exports(); + } + echo json_encode( $list ); + } + + + private function _save(){ + + if( $this->method() != 'post' ){ + $this->_error(); + } + + $id_admin = c::getPagePiece( 3 ); + // saves a new driver + if( !$id_admin ){ + $driver = new Crunchbutton_Admin(); + // create the new driver as inactive + $driver->active = 0; + } else { + $driver = Crunchbutton_Admin::o( $id_admin ); + } + + $driver->name = $this->request()[ 'name' ]; + $driver->phone = $this->request()[ 'phone' ]; + $driver->email = $this->request()[ 'email' ]; + + $driver->save(); + + // add the community + $id_community = $this->request()[ 'id_community' ]; + + // first remove the driver from the delivery groups + $_communities = Crunchbutton_Community::q( 'SELECT * FROM community ORDER BY name ASC' );; + foreach( $_communities as $community ){ + $group = $community->groupOfDrivers(); + if( $group->id_group ){ + $driver->removeGroup( $group->id_group ); + } + } + + if( $id_community ){ + $community = Crunchbutton_Community::o( $id_community ); + if( $community->id_community ){ + $group = $community->groupOfDrivers(); + $adminGroup = new Crunchbutton_Admin_Group(); + $adminGroup->id_admin = $driver->id_admin; + $adminGroup->id_group = $group->id_group; + $adminGroup->save(); + } + } + + echo json_encode( [ 'success' => $driver->exports() ] ); + return; + } + + private function _error( $error = 'invalid request' ){ + echo json_encode( [ 'error' => $error ] ); + exit(); + } + +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Admin.php b/include/library/Crunchbutton/Admin.php index 76a1fe9e6..a45bdd620 100644 --- a/include/library/Crunchbutton/Admin.php +++ b/include/library/Crunchbutton/Admin.php @@ -475,12 +475,20 @@ class Crunchbutton_Admin extends Cana_Table { public function exports() { $permissions = []; $groups = []; + $communities = []; + if( $this->groups() ){ foreach ($this->groups() as $group) { $groups[$group->id_group] = $group->name; } } - + + if( $this->communitiesHeDeliveriesFor() ){ + foreach( $this->communitiesHeDeliveriesFor() as $community ){ + $communities[ $community->id_community ] = $community->name; + } + } + if ($this->permission()->_permissions) { foreach ($this->permission()->_permissions as $group => $perms) { foreach ($perms as $key => $value) { @@ -511,7 +519,8 @@ class Crunchbutton_Admin extends Cana_Table { 'timezone' => $this->timezone, 'testphone' => $this->testphone, 'permissions' => $permissions, - 'groups' => $groups + 'groups' => $groups, + 'communities' => $communities ]; return $ex; } diff --git a/include/views/default/cockpit2/bundle/js.phtml b/include/views/default/cockpit2/bundle/js.phtml index eb872dfa7..1c788b8cf 100644 --- a/include/views/default/cockpit2/bundle/js.phtml +++ b/include/views/default/cockpit2/bundle/js.phtml @@ -34,7 +34,6 @@ - diff --git a/include/views/default/cockpit2/frontend/drivers-onboarding-detail.phtml b/include/views/default/cockpit2/frontend/drivers-onboarding-detail.phtml index de9cd9554..dded46f6c 100644 --- a/include/views/default/cockpit2/frontend/drivers-onboarding-detail.phtml +++ b/include/views/default/cockpit2/frontend/drivers-onboarding-detail.phtml @@ -1,56 +1,56 @@
| Name | ++ |
| {{driver.name}} | ++ + | +