Ingen beskrivning

UserTableSeeder.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. class UserTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Disable query logging to avoid memory exhaustion
  6. DB::disableQueryLog();
  7. $this->command->info('Users table seeding started...');
  8. // Initiates time
  9. $time_start = microtime(true);
  10. // Remove access privileges to users that haven't logged in in 1 year
  11. User::whereNull('last_login')
  12. ->orWhere('last_login', '<', DB::raw('DATE_SUB(NOW(), INTERVAL 1 YEAR)'))
  13. ->update(array(
  14. 'has_access' => 0
  15. ));
  16. // Open file
  17. if($file = fopen('app/database/csv/empleados.txt', 'r'))
  18. {
  19. // Initialize count variables
  20. $read = 0;
  21. $added = 0;
  22. $updated = 0;
  23. $error = 0;
  24. // Read each row
  25. while($data = fgetcsv($file, 0,";"))
  26. {
  27. // Add read count
  28. $read++;
  29. try
  30. {
  31. // Get row info
  32. // $ssn = trim($data[14]);
  33. // // $number = (NOT YET ADDED)
  34. // $first_name = strtoupper(trim($data[3]))." ".strtoupper(trim($data[4]));
  35. // $surnames = strtoupper(trim($data[5]));
  36. // $email = strtolower(trim($data[15]));
  37. // $emp_number = strtolower(trim($data[0]));
  38. $ssn = trim($data[0]);
  39. // $number = (NOT YET ADDED)
  40. $first_name = strtoupper(trim($data[1]))." ".strtoupper(trim($data[2]));
  41. $surnames = strtoupper(trim($data[3]));
  42. $email = strtolower(trim($data[4]));
  43. $emp_number = strtolower(trim($data[5]));
  44. // var_dump($ssn);
  45. // // $number = (NOT YET ADDED)
  46. // var_dump($first_name);
  47. // var_dump($surnames);
  48. // var_dump($email);
  49. // var_dump($emp_number);
  50. //
  51. // exit();
  52. if($email=="null" or $email=="")
  53. {
  54. $email="email".rand()."@upr.edu";
  55. // print($email);
  56. // var_dump($data);exit();
  57. }
  58. if($emp_number=="null" or $emp_number=="")
  59. {
  60. $emp_number="NO".rand(10000000,99999999);
  61. // print($emp_number);
  62. // var_dump($data);exit();
  63. }
  64. // Program unavailable.For the moment, I'm setting it to null, but it should eventually be the commented line below
  65. // $program_id = trim($data[4]);
  66. $program_id = NULL;
  67. // To output info for debugging purposes
  68. // $this->command->info(substr($ssn, -4));
  69. // $this->command->info($first_name);
  70. // $this->command->info($surnames);
  71. // $this->command->info($email);
  72. // $this->command->info($program_id);
  73. // $this->command->info('---');
  74. // If any required field is empty, raise an exception
  75. if(!$ssn || !$first_name || !$surnames || !$email)
  76. {
  77. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  78. }
  79. // If program is empty, assign default
  80. if(!$program_id)
  81. {
  82. $program_id = 999; // Default program id, see DB.
  83. }
  84. // If user exists by ssn
  85. if(User::where('ssn', $ssn)->count())
  86. {
  87. DB::table('users')
  88. ->where('ssn', ($ssn))
  89. ->update(array(
  90. 'first_name' => $first_name,
  91. 'surnames' => $surnames,
  92. 'email' => $email,
  93. 'number' => $emp_number,
  94. // 'program_id' => $program_id, //should change when I know how this info will be given
  95. 'has_access' => 1,
  96. 'updated_at' => date("Y-m-d H:i:s", time()),
  97. )
  98. );
  99. $this->command->info('Updated (via ssn): '.$email.': '.$surnames.', '.$first_name);
  100. $updated++;
  101. }
  102. // If user exists by employee number, update information and add to updated
  103. elseif(User::where('number', $emp_number)->count())
  104. {
  105. DB::table('users')
  106. ->where('number', ($emp_number))
  107. ->update(array(
  108. 'ssn' => $ssn,
  109. 'email' => $email,
  110. 'first_name' => $first_name,
  111. 'surnames' => $surnames,
  112. // 'program_id' => $program_id, //should change when I know how this info will be given
  113. 'has_access' => 1,
  114. 'updated_at' => date("Y-m-d H:i:s", time()),
  115. )
  116. );
  117. $this->command->info('Updated (via name): '.$email.': '.$surnames.', '.$first_name);
  118. $updated++;
  119. }
  120. // If user exists by email, update information and add to updated
  121. elseif(User::where('email', $email)->count())
  122. {
  123. DB::table('users')
  124. ->where('email', ($email))
  125. ->update(array(
  126. 'ssn' => $ssn,
  127. 'first_name' => $first_name,
  128. 'surnames' => $surnames,
  129. 'number' => $emp_number,
  130. // 'program_id' => $program_id, //should change when I know how this info will be given
  131. 'has_access' => 1,
  132. 'updated_at' => date("Y-m-d H:i:s", time()),
  133. )
  134. );
  135. $this->command->info('Updated (via email): '.$email.': '.$surnames.', '.$first_name);
  136. $updated++;
  137. }
  138. // Otherwise, create user and add to added
  139. else
  140. {
  141. $user = User::create(array(
  142. 'ssn' => $ssn,
  143. // 'number' => $number,
  144. 'first_name' => $first_name,
  145. 'surnames' => $surnames,
  146. 'email' => $email,
  147. 'number' => $emp_number,
  148. 'has_access' => 1,
  149. 'role' => 4,
  150. // 'program_id' => $program_id, //should change when I know how this info will be given
  151. )
  152. );
  153. DB::table('program_user')->insert(
  154. array('program_id' => $program_id, 'user_id' => $user->id)
  155. );
  156. $this->command->info('Added '.$email.': '.$surnames.', '.$first_name);
  157. $added++;
  158. }
  159. }
  160. // If an exception is raised, show the message and add to error
  161. catch(Exception $e)
  162. {
  163. $this->command->info($e->getMessage());
  164. $error++;
  165. };
  166. }
  167. // Stop time
  168. $time_end = microtime(true);
  169. // Display results
  170. $this->command->info('------------------------------------------------------------');
  171. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  172. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  173. $this->command->info('- Read: '.$read);
  174. $this->command->info('- Updated (active): '.$updated);
  175. $this->command->info('- Added (new active): '.$added);
  176. $this->command->info('- Not updated (inactive): '.(User::count() - $updated - $added));
  177. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  178. // Close file
  179. fclose($file);
  180. }
  181. // File cannot be opened, display error and exit
  182. else
  183. {
  184. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  185. }
  186. }
  187. }