No Description

UsersController.php 12KB

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