PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Manage/StaffController.php

https://gitlab.com/kalasi/vispunchcard.com
PHP | 329 lines | 231 code | 53 blank | 45 comment | 18 complexity | 4fdc71bb05d35479090c7b939d1dcca6 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers\Manage;
  3. use App\Http\Controllers\BaseController;
  4. use App\User;
  5. /**
  6. * Class StaffController
  7. * @package App\Http\Controllers\Manage
  8. */
  9. class StaffController extends BaseController
  10. {
  11. /**
  12. * @param $id
  13. * @param $type
  14. *
  15. * @return \Illuminate\View\View
  16. */
  17. public function index($id, $type)
  18. {
  19. $permissions = \Business::permissions($id);
  20. $role = \Business::matchingRole($type);
  21. if ($permissions->owner || $permissions->admin) {
  22. $staffQuery = \DB::table($role->name_trims)
  23. ->select('user')
  24. ->where('business', $id)
  25. ->get();
  26. $business = \Business::info($id);
  27. $staffs = [];
  28. foreach ($staffQuery as $user) {
  29. $userInfo = \DB::table('users')
  30. ->select('id', 'fname', 'area', 'exchange', 'sub')
  31. ->where('id', $user->user)
  32. ->first();
  33. array_push($staffs,$userInfo);
  34. }
  35. $page = \View::make('manage.staff.index');
  36. $page->with('bc', ['manage' => 'Management Center', 'manage/' . $business->id => $business->name]);
  37. $page->with('nav', 'Manage');
  38. $page->with('title', 'Manage ' . $role->names);
  39. $page->with('staffs', $staffs);
  40. $page->with('business', $business);
  41. $page->with('role', $role);
  42. } else {
  43. return \Business::invalidPermissions();
  44. }
  45. return $page;
  46. }
  47. /**
  48. * @param $id
  49. * @param $type
  50. *
  51. * @return \Illuminate\View\View
  52. */
  53. public function addForm($id, $type)
  54. {
  55. $permissions = \Business::permissions($id);
  56. $role = \Business::matchingRole($type);
  57. if ($permissions->owner || $permissions->admin) {
  58. $business = \Business::info($id);
  59. $page = \View::make('manage.staff.add.form');
  60. $bc = [
  61. 'manage' => 'Management Center',
  62. 'manage/' . $business->id => $business->name,
  63. 'manage/' . $business->id . '/' . $role->name_trim => 'Manage ' . $role->names,
  64. ];
  65. $page->with('bc', $bc);
  66. $page->with('title', 'Add '.$role->name);
  67. $page->with('css', 'rt-form');
  68. $page->with('js', 'rt-form');
  69. $page->with('role', $role);
  70. } else {
  71. return \Business::invalidPermissions();
  72. }
  73. return $page;
  74. }
  75. /**
  76. * @param $id
  77. * @param $type
  78. *
  79. * @return \Illuminate\View\View
  80. */
  81. public function addDone($id, $type) {
  82. $permissions = \Business::permissions($id);
  83. $role = \Business::matchingRole($type);
  84. if ($permissions->owner || $permissions->admin) {
  85. $business = \Business::info($id);
  86. $data = \Utilities::processPhone(\Input::get('phone'));
  87. $requirements = [
  88. 'area' => 'required|numeric',
  89. 'exchange' => 'required|numeric',
  90. 'sub' => 'required|numeric',
  91. ];
  92. $validator = \Validator::make($data,$requirements);
  93. if ($validator->passes()) {
  94. $userExists = \DB::table('users')
  95. ->select('id')
  96. ->where('area', $data['area'])
  97. ->where('exchange', $data['exchange'])
  98. ->where('sub', $data['sub'])
  99. ->first();
  100. if ($userExists) {
  101. $alreadyStaff = \DB::table($role->name_trims)
  102. ->select('id')
  103. ->where('user', $userExists->id)
  104. ->where('business', $business->id)
  105. ->first();
  106. if(!$alreadyStaff){
  107. \DB::table($role->name_trims)
  108. ->insertGetId([
  109. 'user' => $userExists->id,
  110. 'business' => $business->id,
  111. 'authorized_by' => User::$id,
  112. 'validated' => 0,
  113. ]);
  114. }
  115. $page = \View::make('manage.staff.add.done');
  116. $bc = [
  117. 'manage' => 'Management Center',
  118. 'manage/' . $business->id => $business->name,
  119. 'manage/' . $business->id . '/' . $role->name_trims => 'Manage ' . $role->names,
  120. ];
  121. $page->with('bc', $bc);
  122. $page->with('nav', 'Manage');
  123. $page->with('title', 'Added ' . $role->name);
  124. $page->with('area', $data['area']);
  125. $page->with('business', $business);
  126. $page->with('exchange', $data['exchange']);
  127. $page->with('sub', $data['sub']);
  128. $page->with('role', $role);
  129. } else {
  130. /**
  131. * Create new User to add as Administrator
  132. */
  133. $page = $this->createName($id, $type, $data);
  134. }
  135. } else {
  136. $page = \View::make('errors.manage.admins.add.input');
  137. $bc = [
  138. 'manage' => 'Management Center',
  139. 'manage/' . $business->id =>$business->name,
  140. 'manage/' . $business->id . '/admins' =>'Manage Admins',
  141. ];
  142. $page->with('bc', $bc);
  143. $page->with('title', 'Error Adding Employee');
  144. $page->with('titleSub', 'Input Validation');
  145. }
  146. } else{
  147. return \Business::invalidPermissions();
  148. }
  149. return $page;
  150. }
  151. /**
  152. * @param $id
  153. * @param $type
  154. * @param $userID
  155. *
  156. * @return \Illuminate\View\View
  157. */
  158. public function remove($id, $type, $userID)
  159. {
  160. $permissions = \Business::permissions($id);
  161. $role = \Business::matchingRole($type);
  162. if ($permissions->owner) {
  163. $business = \Business::info($id);
  164. $exists = \DB::table($role->name_trims)
  165. ->select('id')
  166. ->where('user', $userID)
  167. ->where('business', $id)
  168. ->first();
  169. if ($exists) {
  170. $userInfo = \DB::table('users')
  171. ->select('fname')
  172. ->where('id', $userID)
  173. ->first();
  174. \DB::table($role->name_trims)
  175. ->where('id', $exists->id)
  176. ->delete();
  177. $page = \View::make('manage.staff.remove');
  178. $page->with('bc', ['manage' => 'Management Center', 'manage/' . $business->id => $business->name]);
  179. $page->with('nav', 'Manage');
  180. $page->with('title', 'Management');
  181. $page->with('titleSub', $role->name . ' Removed');
  182. $page->with('fname', $userInfo->fname);
  183. } else {
  184. $page = \View::make('errors.manage.staff.remove.notFound');
  185. $page->with('title', 'Management');
  186. $page->with('titleSub', 'Error');
  187. $page->with('role', $role);
  188. $page->with('id', $userID);
  189. }
  190. $page->with('role', $role);
  191. $page->with('businessID', $id);
  192. } else {
  193. return \Business::invalidPermissions();
  194. }
  195. return $page;
  196. }
  197. /**
  198. * @param $id
  199. * @param $type
  200. * @param $data
  201. *
  202. * @return \Illuminate\View\View
  203. */
  204. public function createName($id, $type, $data)
  205. {
  206. $business = \Business::info($id);
  207. $role = \Business::matchingRole($type);
  208. $page = \View::make('manage.staff.create.name');
  209. $page->with('title', 'New User');
  210. $page->with('css', 'rt-form');
  211. $page->with('js', 'rt-form');
  212. $page->with('business', $business);
  213. $page->with('phone', \Crypt::encrypt(json_encode($data)));
  214. $page->with('role', $role);
  215. return $page;
  216. }
  217. /**
  218. * @param $id
  219. * @param $type
  220. *
  221. * @return \Illuminate\View\View
  222. */
  223. public function createUser($id, $type)
  224. {
  225. $data = json_decode(\Crypt::decrypt(\Input::get('phone')));
  226. $name = \Input::get('name');
  227. $data = [
  228. 'area' => $data->area,
  229. 'exchange' => $data->exchange,
  230. 'sub' => $data->sub,
  231. 'name' => $name,
  232. ];
  233. $requirements = [
  234. 'area' => 'required|size:3',
  235. 'exchange' => 'required|size:3',
  236. 'sub' => 'required|size:4|min:0',
  237. 'name' => 'required',
  238. ];
  239. $validator = \Validator::make($data,$requirements);
  240. if (!$validator->passes()) {
  241. abort(403);
  242. }
  243. $role = \Business::matchingRole($type);
  244. $business = \Business::info($id);
  245. $hashedPassword = \Hash::make('pass');
  246. $user = new User;
  247. $user->fname = $name;
  248. $user->area = $data['area'];
  249. $user->exchange = $data['exchange'];
  250. $user->sub = $data['sub'];
  251. $user->password = $hashedPassword;
  252. $user->save();
  253. \DB::table($role->name_trims)
  254. ->insertGetId([
  255. 'user' => $user->id,
  256. 'business' => $business->id,
  257. 'authorized_by' => User::$id,
  258. 'validated' => 0,
  259. ]);
  260. $page = \View::make('manage.staff.add.done');
  261. $bc = [
  262. 'manage' => 'Management Center',
  263. 'manage/' . $business->id => $business->name,
  264. 'manage/' . $business->id . '/' . $role->name_trim => 'Manage ' . $role->names,
  265. ];
  266. $page->with('bc', $bc);
  267. $page->with('nav', 'Manage');
  268. $page->with('title', 'Added ' . $role->name);
  269. $page->with('area', $data['area']);
  270. $page->with('business', $business);
  271. $page->with('exchange', $data['exchange']);
  272. $page->with('sub', $data['sub']);
  273. $page->with('role', $role);
  274. return $page;
  275. }
  276. }