public function run()
 {
     $forms = ['App\\Models\\Forms\\BuildingQuote:Rigid Frame', 'App\\Models\\Forms\\BuildingQuote:Galv-Econ', 'App\\Models\\Forms\\BuildingQuote:Pole Barn', 'App\\Models\\Forms\\BuildingQuote:Roof', 'App\\Models\\Forms\\ContactForm', 'App\\Models\\Forms\\EmploymentApp', 'App\\Models\\Forms\\CreditApp'];
     foreach (Location::all() as $location) {
         foreach ($forms as $form) {
             $formRotation = FormRotation::create(['location_id' => $location->id, 'form' => $form, 'current' => 0]);
         }
     }
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $nonFormPostURLS = ['api/', 'admi', 'auth'];
     if ($request->method() == 'POST' && !in_array(substr($request->path(), 0, 4), $nonFormPostURLS)) {
         // Simple anti-spam check
         if (Input::get('antispam')) {
             return redirect('/');
         }
         // And generic form filter
         if ($this->filter_form(Input::all())) {
             return redirect('/');
         }
         // POST was made to non-API URI
         // Looks like a form submission... Attempt to treat it as such
         $model = Input::get('model');
         if ($model == null) {
             abort(400);
         }
         // Extract form name to simplify this next part
         $formName = lcfirst(substr($model, strrpos($model, '\\') + 1));
         // Determine who should receive the next form submission
         $curLocation = Location::current();
         $formType = $model;
         if ($model == 'App\\Models\\Forms\\BuildingQuote') {
             // We also have a TYPE
             $formType .= ':' . Input::get('building_type');
         }
         $settings = $curLocation->formSettings()->where('model', $formType)->first();
         // Get a list of all employees for the desired department
         $possibleEmployees = $settings->rotation->users;
         foreach ($possibleEmployees as $key => $employee) {
             if (!$employee->locations->contains($curLocation)) {
                 unset($possibleEmployees[$key]);
             }
         }
         $possibleEmployees = $possibleEmployees->flatten();
         if (count($possibleEmployees) > 0) {
             // Figure out where we are in the rotation
             $formRotation = FormRotation::where('location_id', '=', $curLocation->id)->where('form', '=', $formType)->first();
             $formRotation->current = ($formRotation->current + 1) % count($possibleEmployees);
             $formRotation->save();
             // And grab the recipient
             $sendTo = Person::where('email', $possibleEmployees[$formRotation->current]->email)->first();
             $sendTo = Employee::whereIs($sendTo);
         } else {
             // Error: No one in this department. Send to the general manager and owner
         }
         // Create and save an instance of the submitted form
         $modelInstance = new $model();
         $modelInstance->fill(Input::all());
         $modelInstance->employee()->associate($sendTo);
         $modelInstance->location()->associate(Location::find(Input::get('location_id')));
         $modelInstance->save();
         // Handle uploaded files
         $files = $request->files->all();
         $filesToBeAttached = [];
         if (count($files) > 0) {
             if (!file_exists(storage_path($formName))) {
                 mkdir(storage_path($formName));
             }
             $storageFolder = storage_path($formName . '/' . $modelInstance->id . '-' . date('Y-m-d'));
             if (!file_exists(storage_path($formName . '/' . $modelInstance->id . '-' . date('Y-m-d')))) {
                 mkdir($storageFolder);
             }
             foreach ($files as $fileName => $uploadedFileInfo) {
                 $newFileName = $fileName . '-' . $modelInstance->id . '-' . date('Y-m-d') . '.' . $uploadedFileInfo->getClientOriginalExtension();
                 $uploadedFileInfo->move($storageFolder, $newFileName);
                 $filesToBeAttached[] = $storageFolder . '/' . $newFileName;
             }
         }
         $copyTo = [];
         foreach ($settings->copyTo as $copyRecipient) {
             if ($copyRecipient->employee != null) {
                 array_push($copyTo, $copyRecipient->employee);
             } else {
                 array_push($copyTo, $copyRecipient->email);
             }
         }
         // Avoid sending duplicates
         if (($key = array_search($sendTo, $copyTo)) !== false) {
             unset($copyTo[$key]);
         }
         $copyTo = array_unique($copyTo);
         // Send email to employees - informing them of new form submission
         Mail::send('emails.employee.' . $formName, $modelInstance->getAttributes(), function ($m) use($sendTo, $copyTo, $model, $modelInstance, $filesToBeAttached) {
             // To
             /*
             if( is_string($sendTo) ) {
             	$m->to($sendTo, $sendTo);
             } else {
             	$m->to($sendTo->email, $sendTo->full_name);
             }
             foreach( $copyTo as $copy ) {
             	if( is_string($copy) ) {
             		$m->cc($copy, $copy);
             	} else {
             		$m->cc($copy->email, $copy->full_name);
             	}
             }
             */
             $m->to('*****@*****.**', 'Steven Barnett');
             // From
             $m->from('*****@*****.**', 'Reed\'s Metals');
             // Subject
             if ($model == 'App\\Models\\Forms\\BuildingQuote') {
                 $m->subject("{$modelInstance->building_type} Quote ID: {$modelInstance->id} ({$modelInstance->customer_name})");
             } else {
                 if ($model == 'App\\Models\\Forms\\ContactForm') {
                     $m->subject("Contact Form Submission ID: {$modelInstance->id} ({$modelInstance->name})");
                 } else {
                     if ($model == 'App\\Models\\Forms\\EmploymentApp') {
                         $m->subject("Employment Application ID: {$modelInstance->id} ({$modelInstance->name})");
                     } else {
                         if ($model == 'App\\Models\\Forms\\CreditApp') {
                             $m->subject("Business Credit Application ID: {$modelInstance->id} ({$modelInstance->name})");
                         } else {
                             $m->subject("Unknown Form Submission Through ReedsMetals.com");
                         }
                     }
                 }
             }
             // ReplyTo
             if ($modelInstance->customer_email != null) {
                 $m->replyTo($modelInstance->customer_email, $modelInstance->customer_name);
             } else {
                 if ($modelInstance->email != null) {
                     $m->replyTo($modelInstance->email, $modelInstance->name);
                 }
             }
             // Attachments
             foreach ($filesToBeAttached as $file) {
                 $m->attach($file);
             }
         });
         // Send email to customer - informing them we received their submission
         Mail::send('emails.customer.' . $formName, $modelInstance->getAttributes(), function ($m) use($sendTo, $model, $modelInstance) {
             // To
             /*
             if( $modelInstance->customer_email != null ) {
             	$m->to($modelInstance->customer_email, $modelInstance->customer_name);
             } else if( $modelInstance->email != null ) {
             	$m->to($modelInstance->email, $modelInstance->name);
             }
             */
             $m->to('*****@*****.**', 'Steven Barnett');
             // From
             $m->from('*****@*****.**', 'Reed\'s Metals');
             // Subject
             if ($model == 'App\\Models\\Forms\\BuildingQuote') {
                 $m->subject("Your Reed's Metals " . $modelInstance->building_type . " Quote has been received");
             } else {
                 if ($model == 'App\\Models\\Forms\\ContactForm') {
                     $m->subject("Your Reed's Metals Contact Form submission has been received");
                 } else {
                     if ($model == 'App\\Models\\Forms\\EmploymentApp') {
                         $m->subject("Your Reed's Metals Employment App has been received");
                     } else {
                         if ($model == 'App\\Models\\Forms\\CreditApp') {
                             $m->subject("Your Reed's Metals Business Credit App has been received");
                         } else {
                             $m->subject("Unknown Form Submission Through ReedsMetals.com");
                         }
                     }
                 }
             }
             // ReplyTo
             if (is_string($sendTo)) {
                 $m->replyTo($sendTo, $sendTo);
             } else {
                 $m->replyTo($sendTo->email, $sendTo->full_name);
             }
         });
         // Also pass the variables to the "thank you" view... for reasons
         View::share($modelInstance->getAttributes());
         View::share(['employee' => $modelInstance->employee]);
     }
     return $next($request);
 }