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

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