diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index eb51755ed..ae5926523 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -205,7 +205,7 @@ class AppController extends Controller $stateUser->email != $json['user']['email']) { $stateUser->first_name = $json['user']['first_name']; $stateUser->last_name = $json['user']['last_name']; - $stateUser->username = $json['user']['username']; + //$stateUser->username = $json['user']['username']; $stateUser->email = $json['user']['email']; if (!$stateUserTable->save($stateUser)) { $this->Flash->error(__('error updating state user ' . json_encode($stateUser->errors()))); @@ -222,7 +222,7 @@ class AppController extends Controller $newStateUser->public_key = $public_key_bin; $newStateUser->first_name = $json['user']['first_name']; $newStateUser->last_name = $json['user']['last_name']; - $newStateUser->username = $json['user']['username']; + //$newStateUser->username = $json['user']['username']; $newStateUser->email = $json['user']['email']; if (!$stateUserTable->save($newStateUser)) { $this->Flash->error(__('error saving state user ' . json_encode($newStateUser->errors()))); diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index facd9d973..46a99f9bf 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -2,7 +2,6 @@ namespace App\Controller; use App\Controller\AppController; -use App\Controller\CommunityProfilesController; use Model\Navigation\NaviHierarchy; use Model\Navigation\NaviHierarchyEntry; @@ -29,6 +28,93 @@ class ProfileController extends AppController add(new NaviHierarchyEntry(__('Mein Profil'), 'Profile', 'index', true)) ); } + /** + * Get binary file data from request data + * + * @return binary data + */ + protected function getFileData($requestData) + { + $binaryFileData = null; + // Get a list of UploadedFile objects + $file = $requestData['profile_img']; + // Read the file data. + $type = $file['type']; + $error = $file['error']; + if ($error === 0 && strpos($type, 'image/') === 0) { + $path = new File($file['tmp_name']); + $binaryFileData = $path->read(true, 'r'); + $this->log("binaryFileData: ".$binaryFileData, 'debug'); + } + return $binaryFileData; + } + /** + * Update Profile Data + * + * ...which is spread over two tables. + * + * @throws Exception + */ + protected function updateProfileData($requestData, $userId, $communityProfile) + { + // Update Profile with Form Data! + $usersTable = TableRegistry::getTableLocator()->get('StateUsers'); + $stateUserQuery = $usersTable + ->find('all') + ->select(['id', 'first_name', 'last_name']) + ->where(['id' => $userId]); + + if ($stateUserQuery->count() == 1) { + $stateUser = $stateUserQuery->first(); + $stateUser = $usersTable->patchEntity($stateUser, $requestData); + + $profilesTable = TableRegistry::getTableLocator()->get('CommunityProfiles'); + // Save old binary data, because the file input is always empty, in HTML! + $oldBinaryData = $communityProfile['profile_img']; + + $communityProfile = $profilesTable->patchEntity($communityProfile, $requestData); + $communityProfile['state_user_id'] = $userId; + + $binaryFileData = $this->getFileData($requestData); + if ($binaryFileData !== null) { + $this->log("CommunityProfile: Writing binary img data.", 'debug'); + $communityProfile['profile_img'] = $binaryFileData; + } else { + $this->log("CommunityProfile: Nothing uploaded!", 'debug'); + $communityProfile['profile_img'] = $oldBinaryData; + } + if ($profilesTable->save($communityProfile) && + $usersTable->save($stateUser) + ) { + $this->Flash->success(__('Dein Profil wurde aktualisiert!')); + } + } else { + $this->Flash->error(__("Non-recoverable database problem - state_user doesn't exist or not unique!")); + } + return [$stateUser, $communityProfile]; + } + /** + * Get or create CommunityProfile + * + * @return \Cake\ORM\CommunityProfile + */ + protected function getCommunityProfile($userId) + { + $profilesTable = TableRegistry::getTableLocator()->get('CommunityProfiles'); + $communityProfileQuery = $profilesTable + ->find('all') + ->select(['id', 'profile_img', 'profile_desc']) + ->where(['state_user_id' => $userId]); + if ($communityProfileQuery->count() != 1) { + $communityProfile = $profilesTable->newEntity(); + if ($profilesTable->save($communityProfile)) { + $this->log("CommunityProfile created.", 'debug'); + } + } else { + $communityProfile = $communityProfileQuery->first(); + } + return $communityProfile; + } /** * Index method * @@ -45,23 +131,10 @@ class ProfileController extends AppController } $user = $session->read('StateUser'); $communityProfile = $session->read('CommunityProfile'); - if (!isset($communityProfile)) { - $profilesTable = TableRegistry::getTableLocator()->get('CommunityProfiles'); - $communityProfileQuery = $profilesTable - ->find('all') - ->select(['id', 'profile_img', 'profile_desc']) - ->where(['state_user_id' => $user['id']]); - if ($communityProfileQuery->count() != 1) { - $communityProfile = $profilesTable->newEntity(); - if ($profilesTable->save($communityProfile)) { - $this->Flash->success(__('Neues Profil erstellt.')); - } - } else { - $communityProfile = $communityProfileQuery->first(); - } - $session->write('CommunityProfile', $communityProfile); + if (!$communityProfile) { + $this->log("CommunityProfile not found in session! Loading or creating new one.", 'debug'); + $session->write('CommunityProfile', $this->getCommunityProfile($user['id'])); } - $this->set('user', $user); $this->set('communityProfile', $communityProfile); $this->set('timeUsed', microtime(true) - $startTime); @@ -76,6 +149,7 @@ class ProfileController extends AppController $startTime = microtime(true); $this->viewBuilder()->setLayout('frontend'); $session = $this->getRequest()->getSession(); + $user = $session->read('StateUser'); $communityProfile = $session->read('CommunityProfile'); if (!$user) { @@ -85,50 +159,21 @@ class ProfileController extends AppController } $user = $session->read('StateUser'); } + if (!$communityProfile) { + $this->log("CommunityProfile not found in session! Loading or creating new one.", 'debug'); + $session->write('CommunityProfile', $this->getCommunityProfile($user['id'])); + } + $profileForm = new ProfileForm(); if ($this->request->is('post')) { $requestData = $this->request->getData(); if ($profileForm->validate($requestData)) { - // Get a list of UploadedFile objects - $file = $requestData['profile_img']; - var_dump($file); - $binaryFileData = null; - // Read the file data. - $type = $file['type']; - $error = $file['error']; - if ($error === 0 && strpos($type, 'image/') === 0) { - $path = new File($file['tmp_name']); - $binaryFileData = $path->read(true, 'r'); - } - // Update Profile with Form Data! - $usersTable = TableRegistry::getTableLocator()->get('StateUsers'); - $stateUserQuery = $usersTable - ->find('all') - ->select(['id', 'first_name', 'last_name']) - ->where(['id' => $user['id']]); - if ($stateUserQuery->count() == 1) { - $stateUser = $stateUserQuery->first(); - $stateUser = $usersTable->patchEntity($stateUser, $requestData); - $profilesTable = TableRegistry::getTableLocator()->get('CommunityProfiles'); - $communityProfile = $profilesTable->patchEntity($communityProfile, $requestData); - $communityProfile['state_user_id'] = $user['id']; - if ($binaryFileData !== null) { - echo "schreibe neue daten"; - $communityProfile['profile_img'] = $binaryFileData; - } - if ($profilesTable->save($communityProfile) && - $usersTable->save($stateUser) - ) { - $user['first_name'] = $stateUser['first_name']; - $user['last_name'] = $stateUser['last_name']; - $session->write('StateUser.first_name', $stateUser['first_name']); - $session->write('StateUser.last_name', $stateUser['last_name']); - $session->write('CommunityProfile', $communityProfile); - $this->Flash->success(__('Dein Profil wurde aktualisiert!')); - } - } else { - $this->Flash->error(__("Non-recoverable database problem - state_user doesn't exist or not unique!")); - } + [$stateUser, $communityProfile] = $this->updateProfileData($requestData, $user['id'], $communityProfile); + $user['first_name'] = $stateUser['first_name']; + $user['last_name'] = $stateUser['last_name']; + $session->write('StateUser.first_name', $stateUser['first_name']); + $session->write('StateUser.last_name', $stateUser['last_name']); + $session->write('CommunityProfile', $communityProfile); } else { $this->Flash->error(__('Something was invalid, please try again!')); } diff --git a/src/Controller/StateUsersController.php b/src/Controller/StateUsersController.php index bf09b12b3..01b306afa 100644 --- a/src/Controller/StateUsersController.php +++ b/src/Controller/StateUsersController.php @@ -145,7 +145,7 @@ class StateUsersController extends AppController $communityUsers->where(['OR' => [ 'first_name LIKE' => $globalSearch, 'last_name LIKE' => $globalSearch, - 'username LIKE' => $globalSearch, + //'username LIKE' => $globalSearch, 'email LIKE' => $globalSearch ]]); @@ -183,7 +183,7 @@ class StateUsersController extends AppController $finalUser['name'] = $c_user->first_name . ' ' . $c_user->last_name; $finalUser['first_name'] = $c_user->first_name; $finalUser['last_name'] = $c_user->last_name; - $finalUser['username'] = $c_user->username; + //$finalUser['username'] = $c_user->username; $finalUser['email'] = $c_user->email; } } elseif (count($user['login']) == 1) { @@ -205,7 +205,7 @@ class StateUsersController extends AppController $finalUser['name'] = $l_user['first_name'] . ' ' . $l_user['last_name']; $finalUser['first_name'] = $l_user['first_name']; $finalUser['last_name'] = $l_user['last_name']; - $finalUser['username'] = $l_user['username']; + //$finalUser['username'] = $l_user['username']; $finalUser['email'] = $l_user['email']; $finalUser['created'] = new FrozenTime($l_user['created']); } else { @@ -232,7 +232,7 @@ class StateUsersController extends AppController $finalUser['name'] = $user['first_name'] . ' ' . $user['last_name']; $finalUser['first_name'] = $user['first_name']; $finalUser['last_name'] = $user['last_name']; - $finalUser['username'] = $user['username']; + //$finalUser['username'] = $user['username']; $finalUser['email'] = $user['email']; $finalUser['created'] = new FrozenTime($user['created']); $finalUser['indicator'] = ['name' => $state, 'color' => $color]; diff --git a/src/Template/Dashboard/index.ctp b/src/Template/Dashboard/index.ctp index d5370b1d4..bb8b7f9dc 100644 --- a/src/Template/Dashboard/index.ctp +++ b/src/Template/Dashboard/index.ctp @@ -63,7 +63,7 @@ $this->assign(