Bez popisu

UsersController.php 11KB

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