Няма описание

UsersController.php 9.2KB

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