설명 없음

UsersController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. class UsersController extends \BaseController {
  3. /**
  4. * Display a listing of the users.
  5. *
  6. * @return Response
  7. */
  8. public function index()
  9. {
  10. $title="Users";
  11. $users = User::
  12. with('programs', 'school')
  13. ->orderBy('surnames')
  14. ->orderBy('first_name')
  15. ->get();
  16. $schools = School::orderBy('name', 'asc')->get();
  17. $access_level = count(User::select('role')->where('has_access', 1)->groupBy('role')->get());
  18. return View::make('local.managers.admins.users', compact('title', 'users', 'schools', 'access_level'));
  19. }
  20. /**
  21. * Show the form for editing the user.
  22. *
  23. * @param int $id
  24. * @return Response
  25. */
  26. public function edit()
  27. {
  28. $user = Auth::user();
  29. $title = "Profile";
  30. $schools = School::orderBy('name', 'asc')->get();
  31. $programs = $user->programs;
  32. return View::make('global.profile', compact('user', 'title', 'schools', 'programs'));
  33. }
  34. /**
  35. * Create the user in storage.
  36. *
  37. * @param int $id
  38. * @return Response
  39. */
  40. public function store()
  41. {
  42. $user = Auth::user();
  43. if(Input::get('submit_new_user') && Auth::user()->role==1)
  44. {
  45. $first_name = strtoupper(Input::get('new_first_name'));
  46. $surnames = strtoupper(Input::get('new_surnames'));
  47. $email = strtolower(Input::get('new_email'));
  48. $school_id = Input::get('new_school');
  49. // Validation rules
  50. $validator = Validator::make(
  51. array(
  52. 'first_name' => $first_name,
  53. 'surnames' => $surnames,
  54. 'email' => $email,
  55. 'school_id' => $school_id,
  56. ),
  57. array(
  58. 'first_name' => 'required',
  59. 'surnames' => 'required',
  60. 'email' => 'required|email',
  61. 'school_id' => 'integer',
  62. )
  63. );
  64. /** If validation fails */
  65. if ($validator->fails())
  66. {
  67. /** Prepare error message */
  68. $message = 'Error(s) creating a user:<ul>';
  69. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  70. {
  71. $message.=$validationError;
  72. }
  73. $message.='</ul>';
  74. /** Send error message and old data */
  75. Session::flash('status', 'danger');
  76. Session::flash('message', $message);
  77. return Redirect::back()->withInput();
  78. }
  79. DB::beginTransaction();
  80. try
  81. {
  82. switch (Input::get('new_role'))
  83. {
  84. case '1':
  85. User::create(array(
  86. 'first_name' => $first_name,
  87. 'surnames' => $surnames,
  88. 'email'=> $email,
  89. 'role'=> 1,
  90. 'school_id'=> NULL,
  91. 'has_access' => 1
  92. ));
  93. break;
  94. case '2':
  95. User::create(array(
  96. 'first_name' => $first_name,
  97. 'surnames' => $surnames,
  98. 'email'=> $email,
  99. 'role'=> 2,
  100. 'school_id'=> (int)Input::get('new_school'), // como que aqui
  101. 'has_access' => 1
  102. ));
  103. break;
  104. case '3':
  105. $user = User::create(array(
  106. 'first_name' => $first_name,
  107. 'surnames' => $surnames,
  108. 'email'=> $email,
  109. 'role'=> 3,
  110. 'school_id'=> NULL,
  111. 'has_access' => 1
  112. ));
  113. // Attach new programs
  114. foreach (Input::get('programs') as $key => $program_id)
  115. {
  116. $user->programs()->attach($program_id);
  117. }
  118. $user->save();
  119. break;
  120. case '4':
  121. $user = User::create(array(
  122. 'first_name' => $first_name,
  123. 'surnames' => $surnames,
  124. 'email'=> $email,
  125. 'role'=> 4,
  126. 'school_id'=> NULL,
  127. 'has_access' => 1
  128. ));
  129. // Attach new programs
  130. foreach (Input::get('new_programs') as $key => $program_id)
  131. {
  132. $user->programs()->attach($program_id);
  133. }
  134. $user->save();
  135. break;
  136. }
  137. DB::commit();
  138. Session::flash('status', 'success');
  139. Session::flash('message', 'User created ('.date('m/d/y h:i:s A').')');
  140. return Redirect::back();
  141. }
  142. catch(Exception $e)
  143. {
  144. DB::rollBack();
  145. Session::flash('status', 'danger');
  146. Session::flash('message', 'Error creating. Try again later or contact the system administrator.');
  147. return Redirect::back();
  148. }
  149. }
  150. else
  151. App::abort('404');
  152. }
  153. /**
  154. * Update the user in storage.
  155. *
  156. * @param int $id
  157. * @return Response
  158. */
  159. public function update()
  160. {
  161. $user = Auth::user();
  162. if(Input::get('submit_contact_info'))
  163. {
  164. // Validation rules
  165. $validator = Validator::make(
  166. array(
  167. 'office_phone' => Input::get('office_phone'),
  168. 'office_extension' => Input::get('office_extension'),
  169. 'cell_phone' => Input::get('cell_phone'),
  170. ),
  171. array(
  172. 'office_phone' => 'string|max:20|required_with:office_extension',
  173. 'office_extension' => 'digits_between:1,5|required_with:office_phone|unique:users,office_extension,'.$user->id,
  174. 'cell_phone' => 'string|max:20'
  175. )
  176. );
  177. /** If validation fails */
  178. if ($validator->fails())
  179. {
  180. /** Prepare error message */
  181. $message = 'Error(s) updating your Contact nformation<ul>';
  182. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  183. {
  184. $message.=$validationError;
  185. }
  186. $message.='</ul>';
  187. /** Send error message and old data */
  188. Session::flash('status', 'danger');
  189. Session::flash('message', $message);
  190. return Redirect::back()->withInput();
  191. }
  192. else
  193. {
  194. /** Set new contact info */
  195. if(Input::get('office_phone'))
  196. {
  197. $user->office_phone = Input::get('office_phone');
  198. $user->office_extension = Input::get('office_extension');
  199. }
  200. else
  201. {
  202. $user->office_phone = NULL;
  203. $user->office_extension = NULL;
  204. }
  205. if(Input::get('cell_phone'))
  206. {
  207. $user->cell_phone = Input::get('cell_phone');
  208. }
  209. else
  210. {
  211. $user->cell_phone = NULL;
  212. }
  213. /** If alt email is updated, send success message */
  214. if($user->save())
  215. {
  216. Session::flash('status', 'success');
  217. Session::flash('message', 'Contact Information updated.');
  218. return Redirect::back();
  219. }
  220. /** If saving fails, send error message and old data */
  221. else
  222. {
  223. Session::flash('status', 'warning');
  224. Session::flash('message', 'Error updating your Contact Information. Please try again later.');
  225. return Redirect::back()->withInput();
  226. }
  227. }
  228. }
  229. else if(Input::get('submit_roles') && Auth::user()->role==1)
  230. {
  231. try
  232. {
  233. $exception = DB::transaction(function()
  234. {
  235. $user = User::find(Input::get('id'));
  236. switch (Input::get('role')) {
  237. case '1':
  238. $user->role = 1;
  239. $user->school_id = NULL;
  240. // Delete all programs associated to the user
  241. $user->programs()->detach();
  242. $user->has_access = 1;
  243. break;
  244. case '2':
  245. $user->role = 2;
  246. $user->school_id = Input::get('school');
  247. // Delete all programs associated to the user
  248. $user->programs()->detach();
  249. break;
  250. case '3':
  251. $user->role = 3;
  252. $user->school_id = NULL;
  253. // Delete all programs associated to the user
  254. $user->programs()->detach();
  255. // Attach new programs
  256. foreach (Input::get('programs') as $key => $program_id)
  257. {
  258. $user->programs()->attach($program_id);
  259. }
  260. // $user->program_id = Input::get('program');
  261. break;
  262. case '4':
  263. $user->role = 4;
  264. $user->school_id = NULL;
  265. // Delete all programs associated to the user
  266. $user->programs()->detach();
  267. // Attach new programs
  268. foreach (Input::get('programs') as $key => $program_id)
  269. {
  270. $user->programs()->attach($program_id);
  271. }
  272. // $user->program_id = Input::get('program');
  273. break;
  274. }
  275. $user->has_access = Input::get('has_access');
  276. $user->save();
  277. });
  278. if(is_null($exception))
  279. {
  280. Session::flash('status', 'success');
  281. Session::flash('message', 'User <b>'.User::find(Input::get('id'))->email.'</b> updated ('.date('m/d/y h:i:s A').'). To ensure proper access, click \'Update\' in the \'Access Level\' section at the bottom of the page.');
  282. return Redirect::back();
  283. }
  284. }
  285. catch(Exception $e)
  286. {
  287. Session::flash('status', 'danger');
  288. Session::flash('message', 'Error updating users. Try again later.');
  289. return Redirect::back();
  290. }
  291. }
  292. else
  293. App::abort('403');
  294. }
  295. public function updateAccess()
  296. {
  297. try
  298. {
  299. $exception = DB::transaction(function()
  300. {
  301. switch (Input::get('access_level')) {
  302. case '1':
  303. DB::table('users')
  304. ->whereIn('role', array(1))
  305. ->update(array('has_access' => 1));
  306. DB::table('users')
  307. ->whereIn('role', array(2, 3, 4))
  308. ->update(array('has_access' => 0));
  309. break;
  310. case '2':
  311. DB::table('users')
  312. ->whereIn('role', array(1, 2))
  313. ->update(array('has_access' => 1));
  314. DB::table('users')
  315. ->whereIn('role', array(3, 4))
  316. ->update(array('has_access' => 0));
  317. break;
  318. case '3':
  319. DB::table('users')
  320. ->whereIn('role', array(1, 2, 3))
  321. ->update(array('has_access' => 1));
  322. DB::table('users')
  323. ->whereIn('role', array(4))
  324. ->update(array('has_access' => 0));
  325. break;
  326. case '4':
  327. DB::table('users')
  328. ->whereIn('role', array(1, 2, 3, 4))
  329. ->update(array('has_access' => 1));
  330. break;
  331. }
  332. });
  333. if(is_null($exception))
  334. {
  335. Session::flash('status', 'success');
  336. Session::flash('message', 'Access level updated ('.date('m/d/y, h:i:s a').').');
  337. }
  338. }
  339. catch(Exception $e)
  340. {
  341. Session::flash('status', 'danger');
  342. Session::flash('message', 'Error updating access level. Try again later ('.date('m/d/y, h:i:s a').').');
  343. }
  344. return Redirect::back();
  345. }
  346. }