Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Ainet\Controllers;
  4. use Ainet\Models\User;
  5.  
  6.  
  7. class UserController
  8. {
  9. public function listUsers(){
  10. $users = User::all();
  11. $title = 'List users';
  12. render_view('users.list', compact('title', 'users'));
  13. }
  14.  
  15. public function addUser(){
  16. $title = 'Add user';
  17. $user = new User;
  18. $errors = [];
  19. $formSubmitted = !empty($_POST);
  20. if (!$formSubmitted) {
  21. render_view('users.add', compact('title','user','errors'));
  22. }else{
  23. $user = $this->createUserFromData();
  24. $errors = $this->validate($user);
  25. require('src/views/users/partials/errors.view.php');
  26. render_view('users.add', compact('title','user','errors'));
  27. }
  28.  
  29. }
  30.  
  31. private function validate($user)
  32. {
  33.  
  34.  
  35. $errors = [];
  36. $passwordConfirmation = $_POST['password_confirmation'];
  37. if (!trim($user->fullname)) {
  38. $errors['Fullname'] = 'Fullname is required.';
  39. } elseif (!preg_match('/^[a-zA-Z ]+$/', $user->fullname)) {
  40. $errors['Fullname'] = 'Fullname must only contain letters and spaces.';
  41. }
  42. if (!$user->email) {
  43. $errors['Email'] = 'Email is required.';
  44. } elseif (!filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
  45. $errors['Email'] = 'Invalid email address.';
  46. }
  47.  
  48. if (!$user->password) {
  49. $errors['Password'] = 'Password is required.';
  50. } elseif (strlen($user->password) <= 8) {
  51. $errors['Password'] = 'Password must have at least 8 characters.';
  52. }
  53.  
  54. if ($user->password && $passwordConfirmation != $user->password) {
  55. $errors['PasswordConfirmation'] = 'Password confirmation must be equal to password.';
  56. }
  57.  
  58. if (!$user->user_type) {
  59. $errors['UserType'] = 'User type is required.';
  60. } else {
  61. switch ($user->user_type) {
  62. case 1:
  63. case 2:
  64. case 3:
  65. break;
  66. default:
  67. $errors['UserType'] = 'Type must be "administrator", "publisher" or "client".';
  68. }
  69. }
  70.  
  71.  
  72.  
  73. return $errors;
  74. }
  75.  
  76. private function createUserFromData()
  77. {
  78. $user = new User();
  79.  
  80. if (array_key_exists('fullname', $_POST)) {
  81. $user->fullname = $_POST['fullname'];
  82. }
  83. else{
  84. $user->fullname = null;
  85. }
  86.  
  87. if (array_key_exists('user_type', $_POST)) {
  88. $user->user_type = $_POST['user_type'];
  89. }
  90. else{
  91. $user->user_type = null;
  92. }
  93.  
  94. if (array_key_exists('email', $_POST))
  95. {
  96. $user->email = $_POST['email'];
  97. }
  98. else{
  99. $user->email = null;
  100. }
  101.  
  102. if(array_key_exists('password', $_POST))
  103. {
  104. $user->password = $_POST['password'];
  105. }
  106. else{
  107. $user->password = null;
  108. }
  109.  
  110. return $user;
  111. }
  112.  
  113.  
  114. }
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement