PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/webmin/apis/doctors/save.php

https://bitbucket.org/subhro/timelycarev2
PHP | 123 lines | 106 code | 17 blank | 0 comment | 6 complexity | 770e7ec8ab73d32dacbd2a3f92116c7e MD5 | raw file
  1. <?php
  2. $data = false;
  3. $events = false;
  4. $shift_ids = [];
  5. $uid = Input::get('id', 0);
  6. $rules = [
  7. 'first_name' => 'required|max:20',
  8. 'last_name' => 'required|max:20',
  9. 'description_id' => 'required',
  10. 'phone' => 'required|min:10|max:12|numeric',
  11. 'email' => 'required|email',
  12. 'priority' => 'required',
  13. 'shift_ids' => 'required'
  14. ];
  15. $messages = [
  16. 'first_name:required' => 'Please provide first name',
  17. 'first_name:max' => 'Please provide valid name',
  18. 'last_name:required' => 'Please provide last name',
  19. 'last_name:max' => 'Please provide valid name',
  20. 'phone:min' => 'Please provide a valid number',
  21. 'phone:max' => 'Please provide a valid number'
  22. ];
  23. $errors = Input::validate($rules, $messages);
  24. if($errors){
  25. $events = [
  26. 'message.show' => [
  27. 'type' => 'error',
  28. 'text' => $errors[0]
  29. ]
  30. ];
  31. goto RESPONSE;
  32. }
  33. $shift = Input::get('shift_ids');
  34. foreach($shift as $val){
  35. $shift_ids[] = isset($val['value']) ? $val['value'] : $val;
  36. }
  37. $doctor = User::where('id', $uid)->first();
  38. if($doctor) {
  39. $checkUser = User::where('email', Input::get('email'))->where('id', '!=', $uid)->first();
  40. if($checkUser) {
  41. $events = [
  42. 'message.show' => [
  43. 'type' => 'error',
  44. 'text' => 'Email address already exist.'
  45. ]
  46. ];
  47. goto RESPONSE;
  48. }
  49. $flag = 'edit';
  50. $pass = Input::get('password') ? Input::get('password') : false;
  51. $password = Input::get('password') ? Crypto::hash($pass) : $doctor->password;
  52. $message = 'Doctor details saved successfully.';
  53. } else {
  54. $checkUser = User::where('email', Input::get('email'))->first();
  55. if($checkUser) {
  56. $events = [
  57. 'message.show' => [
  58. 'type' => 'error',
  59. 'text' => 'Email address already exist.'
  60. ]
  61. ];
  62. goto RESPONSE;
  63. }
  64. $doctor = new User;
  65. $flag = 'new';
  66. $pass = Input::get('password') ? Input::get('password') : uniqid();
  67. $password = Crypto::hash($pass);
  68. $message = 'Doctor details saved successfully.';
  69. }
  70. $doctor->type = 0;
  71. $doctor->name = Input::get('first_name').' '.Input::get('last_name');
  72. $doctor->email = Input::get('email');
  73. $doctor->password = $password;
  74. $doctor->phone = Input::get('phone');
  75. $doctor->description_id = Input::get('description_id');
  76. $doctor->status = 0;
  77. $doctor->priority = Input::get('priority');
  78. $doctor->shift_ids = $shift_ids;
  79. $doctor->timestamp = date('Y-m-d H:i:s');
  80. $doctor->trash = 0;
  81. $doctor->save();
  82. $template = ($flag=='new') ? 'welcome' : 'update';
  83. if($pass){
  84. $userData = [
  85. 'name' => Input::get('first_name'),
  86. 'email' => Input::get('email'),
  87. 'password' => $pass,
  88. 'site_url' => SITE_URL
  89. ];
  90. $to = Input::get('email');
  91. $subject = 'Timely Care';
  92. $body = Email::message($template, $userData);
  93. Email::send($to, $subject, $body);
  94. }
  95. $events = [
  96. 'modal.close' => false,
  97. 'message.show' => [
  98. 'type' => 'success',
  99. 'text' => $message
  100. ]
  101. ];
  102. RESPONSE:
  103. return [
  104. 'data' => $data,
  105. 'events' => $events
  106. ];