12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Controllers\Web;
- use App\Utils\Api;
- use App\Utils\Url;
- use PH7\JustHttp\StatusCode;
- class ProfileController
- {
-
- private function getFormFields()
- {
- return ['email', 'username', 'password', 'pass_confirm'];
- }
-
- public function edit($req, $res)
- {
- $validations = [];
- $error = $req->session['error'] ?? null;
- $success = $req->session['success'] ?? null;
-
- foreach ($this->getFormFields() as $field) {
- $validations[$field] = $req->session['validations'][$field] ?? null;
- }
- foreach (['validations', 'error', 'success'] as $key) {
- unset($req->session[$key]);
- }
- $res->render('profile/edit', [
- 'app' => $req->app,
- 'userAuth' => $req->app->local('userAuth'),
- 'validations' => $validations,
- 'error' => $error,
- 'success' => $success
- ]);
- }
-
- public function update($req, $res)
- {
- $data = [];
-
- foreach ($this->getFormFields() as $field) {
- $data[$field] = $req->body[$field] ?? null;
- }
- $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
- $client = Api::client();
-
- $response = $client->put('v1/profile', $headers, $data);
- $body = json_decode($response->body ?? '', true);
-
- if (empty($response->success) || empty($body['data'])) {
-
- if (!empty($body['validations'])) {
- $req->session['validations'] = $body['validations'];
- }
-
- $req->session['error'] = $body['error'] ?? 'The profile could not be updated';
- $res->redirect(Url::build('profile/edit'), StatusCode::FOUND);
- }
- $req->session['success'] = 'The profile was modified correctly';
- $res->redirect(Url::build('profile/edit'), StatusCode::FOUND);
- }
- }
|