| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentForm\Framework\Validator\ValidationException; |
| 6 |
use FluentForm\App\Services\Manager\ManagerService; |
| 7 |
|
| 8 |
class ManagersController extends Controller |
| 9 |
{ |
| 10 |
public function index(ManagerService $managerService) |
| 11 |
{ |
| 12 |
$attributes = $this->request->all(); |
| 13 |
|
| 14 |
$sanitizeMap = [ |
| 15 |
'search' => 'sanitize_text_field', |
| 16 |
]; |
| 17 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 18 |
|
| 19 |
$result = $managerService->getManagers($attributes); |
| 20 |
return $this->sendSuccess($result); |
| 21 |
} |
| 22 |
|
| 23 |
public function addManager(ManagerService $managerService) |
| 24 |
{ |
| 25 |
try { |
| 26 |
$attributes = $this->request->all(); |
| 27 |
|
| 28 |
$sanitizeMap = [ |
| 29 |
'user_id' => 'intval', |
| 30 |
]; |
| 31 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 32 |
|
| 33 |
$result = $managerService->addManager($attributes); |
| 34 |
return $this->sendSuccess($result); |
| 35 |
} catch (ValidationException $exception) { |
| 36 |
return $this->sendError($exception->errors(), 422); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function removeManager(ManagerService $managerService) |
| 41 |
{ |
| 42 |
try { |
| 43 |
$attributes = $this->request->all(); |
| 44 |
|
| 45 |
$sanitizeMap = [ |
| 46 |
'user_id' => 'intval', |
| 47 |
]; |
| 48 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 49 |
|
| 50 |
$result = $managerService->removeManager($attributes); |
| 51 |
return $this->sendSuccess($result); |
| 52 |
} catch (ValidationException $exception) { |
| 53 |
return $this->sendError($exception->errors(), 422); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function getUsers(ManagerService $managerService) |
| 58 |
{ |
| 59 |
$search = sanitize_text_field($this->request->get('search', '')); |
| 60 |
|
| 61 |
$users = get_users([ |
| 62 |
'search' => "*{$search}*", |
| 63 |
'number' => 50, |
| 64 |
'fields' => ['ID', 'display_name', 'user_email'] |
| 65 |
]); |
| 66 |
|
| 67 |
$formattedUsers = []; |
| 68 |
foreach ($users as $user) { |
| 69 |
$formattedUsers[] = [ |
| 70 |
'ID' => $user->ID, |
| 71 |
'display_name' => $user->display_name, |
| 72 |
'user_email' => $user->user_email, |
| 73 |
'label' => $user->display_name . ' - ' . $user->user_email, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
return $this->sendSuccess([ |
| 78 |
'users' => $formattedUsers, |
| 79 |
]); |
| 80 |
} |
| 81 |
} |
| 82 |
|