No Description

UserTableSeeder.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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[5]));
  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. // 'program_id' => $program_id, //should change when I know how this info will be given
  68. 'has_access' => 1,
  69. 'updated_at' => date("Y-m-d H:i:s", time()),
  70. )
  71. );
  72. $this->command->info('Updated (via ssn): '.$email.': '.$surnames.', '.$first_name);
  73. $updated++;
  74. }
  75. // If user exists by name and last names, update information and add to updated
  76. elseif(User::where('first_name', $first_name)->where('surnames', $surnames)->count())
  77. {
  78. DB::table('users')
  79. ->where('first_name', ($first_name))
  80. ->where('surnames', ($surnames))
  81. ->update(array(
  82. 'ssn' => $ssn,
  83. 'email' => $email,
  84. // 'program_id' => $program_id, //should change when I know how this info will be given
  85. 'has_access' => 1,
  86. 'updated_at' => date("Y-m-d H:i:s", time()),
  87. )
  88. );
  89. $this->command->info('Updated (via name): '.$email.': '.$surnames.', '.$first_name);
  90. $updated++;
  91. }
  92. // If user exists by email, update information and add to updated
  93. elseif(User::where('email', $email)->count())
  94. {
  95. DB::table('users')
  96. ->where('email', ($email))
  97. ->update(array(
  98. 'ssn' => $ssn,
  99. 'first_name' => $first_name,
  100. 'surnames' => $surnames,
  101. // 'program_id' => $program_id, //should change when I know how this info will be given
  102. 'has_access' => 1,
  103. 'updated_at' => date("Y-m-d H:i:s", time()),
  104. )
  105. );
  106. $this->command->info('Updated (via email): '.$email.': '.$surnames.', '.$first_name);
  107. $updated++;
  108. }
  109. // Otherwise, create user and add to added
  110. else
  111. {
  112. $user = User::create(array(
  113. 'ssn' => $ssn,
  114. // 'number' => $number,
  115. 'first_name' => $first_name,
  116. 'surnames' => $surnames,
  117. 'email' => $email,
  118. 'has_access' => 1,
  119. 'role' => 4,
  120. // 'program_id' => $program_id, //should change when I know how this info will be given
  121. )
  122. );
  123. DB::table('program_user')->insert(
  124. array('program_id' => $program_id, 'user_id' => $user->id)
  125. );
  126. $this->command->info('Added '.$email.': '.$surnames.', '.$first_name);
  127. $added++;
  128. }
  129. }
  130. // If an exception is raised, show the message and add to error
  131. catch(Exception $e)
  132. {
  133. $this->command->info($e->getMessage());
  134. $error++;
  135. };
  136. }
  137. // Stop time
  138. $time_end = microtime(true);
  139. // Display results
  140. $this->command->info('------------------------------------------------------------');
  141. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  142. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  143. $this->command->info('- Read: '.$read);
  144. $this->command->info('- Updated (active): '.$updated);
  145. $this->command->info('- Added (new active): '.$added);
  146. $this->command->info('- Not updated (inactive): '.(User::count() - $updated - $added));
  147. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  148. // Close file
  149. fclose($file);
  150. }
  151. // File cannot be opened, display error and exit
  152. else
  153. {
  154. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  155. }
  156. }
  157. }