/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // we are assuming that the sysop is always the first user
     $this->info('Creating administrator, default section and first topic...');
     $user = \Nexus\User::first();
     if (!$user) {
         $this->info("Please enter in values for the administrator account. Don't worry You can change this later.");
         $username = $this->ask('Username');
         $email = $this->ask('Email Address');
         $password = $this->ask('Password');
         $administrator = new \Nexus\User();
         $administrator->username = $username;
         $administrator->name = 'Administrator';
         $administrator->email = $email;
         $administrator->password = \Hash::make($password);
         $administrator->administrator = true;
         try {
             $administrator->save();
         } catch (\Exception $e) {
             $this->error('Failed to add administrator ' . $e);
         }
     } else {
         $this->error('There is already a user account');
     }
     $section = \Nexus\Section::first();
     if (!$section) {
         $this->info("Please enter in values for the main menu. Don't worry You can change this later.");
         $title = $this->ask('Title');
         $mainmenu = new \Nexus\Section();
         $mainmenu->title = $title;
         $mainmenu->user_id = $administrator->id;
         try {
             $mainmenu->save();
         } catch (\Exception $e) {
             $this->error('Failed to add main menu ' . $e);
         }
     } else {
         $this->error('There is already a main menu');
     }
     $topic = \Nexus\Topic::first();
     if (!$topic) {
         $this->info("Please enter in values for the first topic. Don't worry You can change this later.");
         $title = $this->ask('Title');
         $firstTopic = new \Nexus\Topic();
         $firstTopic->title = $title;
         $firstTopic->section_id = $mainmenu->id;
         try {
             $firstTopic->save();
         } catch (\Exception $e) {
             $this->error('Failed to add first topic ' . $e);
         }
     } else {
         $this->error('There is already a topic');
     }
 }
 private function migrateUsers()
 {
     $this->info('Importing Users');
     $errorCount = 0;
     if (!\Nexus\User::first()) {
         $count = \DB::select('select count(user_id) as count from usertable')[0]->count;
         $this->line("Found {$count} users ");
         $bar = $this->output->createProgressBar($count);
         $classicUsers = \DB::table('usertable')->get();
         foreach ($classicUsers as $classicUser) {
             $newUser = new \Nexus\User();
             $newUser->id = $classicUser->user_id;
             $newUser->username = $classicUser->user_name;
             $newUser->popname = $classicUser->user_popname;
             $newUser->about = $classicUser->user_comment;
             $newUser->location = $classicUser->user_town;
             if ($classicUser->user_sysop === 'y') {
                 $newUser->administrator = true;
             } else {
                 $newUser->administrator = false;
             }
             $newUser->totalVisits = $classicUser->user_totalvisits;
             $newUser->totalPosts = $classicUser->user_totaledits;
             $newUser->favouriteMovie = $classicUser->user_film;
             $newUser->favouriteMusic = $classicUser->user_band;
             if ($classicUser->user_hideemail === 'no') {
                 $newUser->private = false;
             } else {
                 $newUser->private = true;
             }
             $newUser->ipaddress = $classicUser->user_ipaddress;
             $lastLogin = \DB::table('whoison')->select('timeon')->where('user_id', $classicUser->user_id)->first();
             if ($lastLogin) {
                 $newUser->latestLogin = $lastLogin->timeon;
             }
             if ($classicUser->user_status === 'Invalid') {
                 $newUser->banned = true;
             }
             // avoid reusing email addresses
             $emailUses = \DB::table('usertable')->select('user_id')->where('user_email', $classicUser->user_email)->get();
             $count = count($emailUses);
             if ($count > 1) {
                 $newUser->email = $classicUser->user_name . '@fakeemail.com';
             } else {
                 $newUser->email = $classicUser->user_email;
             }
             $newUser->password = $classicUser->user_password;
             if ($classicUser->user_realname != "") {
                 $newUser->name = $classicUser->user_realname;
             } else {
                 $newUser->name = "Unknown";
             }
             try {
                 $newUser->save();
             } catch (\Exception $e) {
                 $errorCount++;
                 \Log::error('Nexus:upgrade - Failed to add user ' . $e);
             }
             $bar->advance();
         }
         $bar->finish();
         if ($errorCount) {
             $this->error("\nEncountered {$errorCount} errors. See log for details");
         }
         $this->info("\nUsers Complete\n");
         unset($classicUsers);
     } else {
         $this->error('Upgrade: found existing users - skipping Users');
     }
 }