Нема описа

UserTableSeeder.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/users.csv', '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, 5000, "|"))
  26. {
  27. // Add read count
  28. $read++;
  29. try
  30. {
  31. // Get row info
  32. $ssn = trim($data[0]);
  33. // $number = (NOT YET ADDED)
  34. $first_name = strtoupper(trim($data[1]));
  35. $surnames = strtoupper(trim($data[2]));
  36. $email = strtolower(trim($data[3]));
  37. $emp_number = strtolower(trim($data[4]));
  38. // Program unavailable.For the moment, I'm setting it to null, but it should eventually be the commented line below
  39. // $program_id = trim($data[4]);
  40. $program_id = NULL;
  41. // To output info for debugging purposes
  42. // $this->command->info(substr($ssn, -4));
  43. // $this->command->info($first_name);
  44. // $this->command->info($surnames);
  45. // $this->command->info($email);
  46. // $this->command->info($program_id);
  47. // $this->command->info('---');
  48. // If any required field is empty, raise an exception
  49. if(!$ssn || !$first_name || !$surnames || !$email)
  50. {
  51. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  52. }
  53. // If program is empty, assign default
  54. if(!$program_id)
  55. {
  56. $program_id = 999; // Default program id, see DB.
  57. }
  58. // If user exists by ssn
  59. if(User::where('ssn', $ssn)->count())
  60. {
  61. DB::table('users')
  62. ->where('ssn', ($ssn))
  63. ->update(array(
  64. 'first_name' => $first_name,
  65. 'surnames' => $surnames,
  66. 'email' => $email,
  67. 'number' => $emp_number,
  68. // 'program_id' => $program_id, //should change when I know how this info will be given
  69. 'has_access' => 1,
  70. 'updated_at' => date("Y-m-d H:i:s", time()),
  71. )
  72. );
  73. $this->command->info('Updated (via ssn): '.$email.': '.$surnames.', '.$first_name);
  74. $updated++;
  75. }
  76. // If user exists by employee number, update information and add to updated
  77. elseif(User::where('number', $emp_number)->count())
  78. {
  79. DB::table('users')
  80. ->where('number', ($emp_number))
  81. ->update(array(
  82. 'ssn' => $ssn,
  83. 'email' => $email,
  84. 'first_name' => $first_name,
  85. 'surnames' => $surnames,
  86. // 'program_id' => $program_id, //should change when I know how this info will be given
  87. 'has_access' => 1,
  88. 'updated_at' => date("Y-m-d H:i:s", time()),
  89. )
  90. );
  91. $this->command->info('Updated (via name): '.$email.': '.$surnames.', '.$first_name);
  92. $updated++;
  93. }
  94. // If user exists by email, update information and add to updated
  95. elseif(User::where('email', $email)->count())
  96. {
  97. DB::table('users')
  98. ->where('email', ($email))
  99. ->update(array(
  100. 'ssn' => $ssn,
  101. 'first_name' => $first_name,
  102. 'surnames' => $surnames,
  103. 'number' => $emp_number,
  104. // 'program_id' => $program_id, //should change when I know how this info will be given
  105. 'has_access' => 1,
  106. 'updated_at' => date("Y-m-d H:i:s", time()),
  107. )
  108. );
  109. $this->command->info('Updated (via email): '.$email.': '.$surnames.', '.$first_name);
  110. $updated++;
  111. }
  112. // Otherwise, create user and add to added
  113. else
  114. {
  115. $user = User::create(array(
  116. 'ssn' => $ssn,
  117. // 'number' => $number,
  118. 'first_name' => $first_name,
  119. 'surnames' => $surnames,
  120. 'email' => $email,
  121. 'number' => $emp_number,
  122. 'has_access' => 1,
  123. 'role' => 4,
  124. // 'program_id' => $program_id, //should change when I know how this info will be given
  125. )
  126. );
  127. DB::table('program_user')->insert(
  128. array('program_id' => $program_id, 'user_id' => $user->id)
  129. );
  130. $this->command->info('Added '.$email.': '.$surnames.', '.$first_name);
  131. $added++;
  132. }
  133. }
  134. // If an exception is raised, show the message and add to error
  135. catch(Exception $e)
  136. {
  137. $this->command->info($e->getMessage());
  138. $error++;
  139. };
  140. }
  141. // Stop time
  142. $time_end = microtime(true);
  143. // Display results
  144. $this->command->info('------------------------------------------------------------');
  145. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  146. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  147. $this->command->info('- Read: '.$read);
  148. $this->command->info('- Updated (active): '.$updated);
  149. $this->command->info('- Added (new active): '.$added);
  150. $this->command->info('- Not updated (inactive): '.(User::count() - $updated - $added));
  151. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  152. // Close file
  153. fclose($file);
  154. }
  155. // File cannot be opened, display error and exit
  156. else
  157. {
  158. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  159. }
  160. }
  161. }