123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
-
- class UserTableSeeder extends Seeder {
-
- public function run()
- {
-
- DB::disableQueryLog();
-
- $this->command->info('Users table seeding started...');
-
-
- $time_start = microtime(true);
-
-
- User::whereNull('last_login')
- ->orWhere('last_login', '<', DB::raw('DATE_SUB(NOW(), INTERVAL 1 YEAR)'))
- ->update(array(
- 'has_access' => 0
- ));
-
-
- if($file = fopen('app/database/csv/users.csv', 'r'))
- {
-
- $read = 0;
- $added = 0;
- $updated = 0;
- $error = 0;
-
-
- while($data = fgetcsv($file, 5000, "|"))
- {
-
- $read++;
-
- try
- {
-
- $ssn = trim($data[0]);
-
- $first_name = strtoupper(trim($data[1]));
- $surnames = strtoupper(trim($data[2]));
- $email = strtolower(trim($data[3]));
-
-
-
-
- $program_id = NULL;
-
-
-
-
-
-
-
-
-
-
- if(!$ssn || !$first_name || !$surnames || !$email)
- {
- throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
- }
-
-
- if(!$program_id)
- {
- $program_id = 999;
- }
-
-
- if(User::where('ssn', $ssn)->count())
- {
- DB::table('users')
- ->where('ssn', ($ssn))
- ->update(array(
- 'first_name' => $first_name,
- 'surnames' => $surnames,
- 'email' => $email,
-
- 'has_access' => 1,
- 'updated_at' => date("Y-m-d H:i:s", time()),
- )
- );
- $this->command->info('Updated (via ssn): '.$email.': '.$surnames.', '.$first_name);
- $updated++;
- }
-
-
- elseif(User::where('first_name', $first_name)->where('surnames', $surnames)->count())
- {
- DB::table('users')
- ->where('first_name', ($first_name))
- ->where('surnames', ($surnames))
- ->update(array(
- 'ssn' => $ssn,
- 'email' => $email,
-
- 'has_access' => 1,
- 'updated_at' => date("Y-m-d H:i:s", time()),
- )
- );
- $this->command->info('Updated (via name): '.$email.': '.$surnames.', '.$first_name);
- $updated++;
- }
-
- elseif(User::where('email', $email)->count())
- {
- DB::table('users')
- ->where('email', ($email))
- ->update(array(
- 'ssn' => $ssn,
- 'first_name' => $first_name,
- 'surnames' => $surnames,
-
- 'has_access' => 1,
- 'updated_at' => date("Y-m-d H:i:s", time()),
- )
- );
- $this->command->info('Updated (via email): '.$email.': '.$surnames.', '.$first_name);
- $updated++;
- }
-
-
- else
- {
- $user = User::create(array(
- 'ssn' => $ssn,
-
- 'first_name' => $first_name,
- 'surnames' => $surnames,
- 'email' => $email,
- 'has_access' => 1,
- 'role' => 4,
-
- )
- );
-
- DB::table('program_user')->insert(
- array('program_id' => $program_id, 'user_id' => $user->id)
- );
-
- $this->command->info('Added '.$email.': '.$surnames.', '.$first_name);
- $added++;
- }
- }
-
-
- catch(Exception $e)
- {
- $this->command->info($e->getMessage());
- $error++;
- };
- }
-
-
- $time_end = microtime(true);
-
-
- $this->command->info('------------------------------------------------------------');
- $this->command->info('Results on '.date('M d, Y, h:i:s a'));
- $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
- $this->command->info('- Read: '.$read);
- $this->command->info('- Updated (active): '.$updated);
- $this->command->info('- Added (new active): '.$added);
- $this->command->info('- Not updated (inactive): '.(User::count() - $updated - $added));
- $this->command->info('- Not added/updated (errors or missing information): '.($error));
-
-
- fclose($file);
- }
-
- else
- {
- $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
- }
- }
-
- }
|