PageRenderTime 24ms CodeModel.GetById 55ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Http/Controllers/BusinessController.php

https://gitlab.com/kalasi/vispunchcard.com
PHP | 191 lines | 137 code | 34 blank | 20 comment | 11 complexity | dd5111f47f5326140214506b9d857db6 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. /**
  4. * Class BusinessController
  5. * @package App\Http\Controllers
  6. */
  7. class BusinessController extends BaseController
  8. {
  9. /**
  10. * @return \Illuminate\View\View
  11. */
  12. public function index()
  13. {
  14. $cookie = \Business::cookie();
  15. $business = \Business::info($cookie->id);
  16. $staff = [];
  17. $owner = \DB::table('users')
  18. ->select('fname')
  19. ->where('id', $business->owner)
  20. ->first();
  21. $admins = \DB::table('admins')
  22. ->select('user')
  23. ->where('business', $business->id)
  24. ->get();
  25. $employees = \DB::table('employees')
  26. ->select('user')
  27. ->where('business', $business->id)
  28. ->get();
  29. $staff[$owner->fname] = [
  30. 'id' => $business->owner,
  31. 'name' => $owner->fname,
  32. ];
  33. foreach ($admins as $admin) {
  34. $adminInfo = \DB::table('users')
  35. ->select('fname')
  36. ->where('id', $admin->user)
  37. ->first();
  38. $staff[$adminInfo->fname] = [
  39. 'id' => $admin->user,
  40. 'name' => $adminInfo->fname,
  41. ];
  42. }
  43. foreach ($employees as $employee) {
  44. $employeeInfo = \DB::table('users')
  45. ->select('fname')
  46. ->where('id', $employee->user)
  47. ->first();
  48. $staff[$employeeInfo->fname] = [
  49. 'id' => $employee->user,
  50. 'name' => $employeeInfo->fname,
  51. ];
  52. }
  53. $signedIn = \Business::signedInInfo();
  54. ksort($staff);
  55. $staff = (object) json_decode(json_encode($staff));
  56. $page = \View::make('business.index');
  57. $page->with('bc', ['business' => 'Business']);
  58. $page->with('nav', 'Business');
  59. $page->with('title', $business->name);
  60. $page->with('business', $business);
  61. $page->with('signedIn', $signedIn);
  62. $page->with('staffList', $staff);
  63. return $page;
  64. }
  65. /**
  66. * @param $id
  67. *
  68. * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
  69. */
  70. public function signIn($id)
  71. {
  72. $business = \Business::info(\Business::cookie()->id);
  73. if (\Business::isStaff($id)) {
  74. $userInfo = \DB::table('users')
  75. ->select('id', 'fname')
  76. ->where('id', $id)
  77. ->first();
  78. $user=\Business::signedIn();
  79. if ($user) {
  80. if (\Business::signedInInfo()->id == $id) {
  81. return \Redirect::to(\Utilities::URLBusiness($business->id));
  82. }
  83. $page = \View::make('errors.business.signedIn');
  84. $page->with('bc', ['business' => 'Business']);
  85. $page->with('title', 'Already Signed In');
  86. $page->with('business', $business);
  87. $page->with('user', $userInfo);
  88. } else {
  89. $page = \View::make('business.signin.form');
  90. $page->with('title', 'Sign In to ' . $business->name);
  91. $page->with('css', 'rt-form');
  92. $page->with('business', $business);
  93. }
  94. } else {
  95. $page = \View::make('errors.business.signin.notStaff');
  96. $page->with('bc', ['business' => 'Business']);
  97. $page->with('title', 'Not a Staff Member');
  98. $page->with('business', $business);
  99. }
  100. return $page;
  101. }
  102. /**
  103. * @param $id
  104. *
  105. * @return $this|\Illuminate\View\View
  106. */
  107. public function signInDone($id)
  108. {
  109. $data = \Utilities::processPhone(\Input::get('phone'));
  110. $requirements = [
  111. 'area' => 'required|size:3',
  112. 'exchange' => 'required|size:3',
  113. 'sub' => 'required|size:4|min:0',
  114. ];
  115. $validator = \Validator::make($data,$requirements);
  116. if ($validator->passes()) {
  117. $user = \DB::table('users')
  118. ->select('fname')
  119. ->where('id',$id)
  120. ->first();
  121. $correct = \DB::table('users')
  122. ->select('id')
  123. ->where('area', $data['area'])
  124. ->where('exchange', $data['exchange'])
  125. ->where('sub', $data['sub'])
  126. ->first();
  127. if ($correct->id == $id) {
  128. $cookieData = \Crypt::encrypt(serialize(\Crypt::encrypt($data)));
  129. $cookie = \Cookie::forever('signed-in', $cookieData);
  130. return \Redirect::to(
  131. \Utilities::URLBusiness(
  132. \Business::cookie()->id))
  133. ->withCookie($cookie);
  134. } else {
  135. $page = \View::make('errors.business.signin.phone');
  136. $page->with('bc', ['business' => 'Business']);
  137. $page->with('nav', 'Business');
  138. $page->with('title', 'Logging Into ' . $user->fname . '\'s Account');
  139. }
  140. } else {
  141. $page = \View::make('errors.business.signin.input');
  142. $page->with('bc', ['business' => 'Business']);
  143. $page->with('nav', 'Business');
  144. $page->with('title', 'Input Validation Error');
  145. }
  146. return $page;
  147. }
  148. /**
  149. * @return $this
  150. */
  151. public function signOut()
  152. {
  153. $cookie = \Cookie::forget('signed-in');
  154. return \Redirect::to(\Utilities::URL('business'))
  155. ->withCookie($cookie);
  156. }
  157. }