public function run()
 {
     DB::table('employees')->delete();
     $dirname = dirname(__FILE__);
     require 'employeeSeed.php';
     foreach ($employees as $employeeName => $employeeInfo) {
         if (Person::where('full_name', '=', $employeeName)->first()) {
             continue;
         }
         $person = new Person(['full_name' => $employeeName, 'honorific_prefix' => $employeeInfo['name'][0], 'given_name' => $employeeInfo['name'][1], 'additional_names' => $employeeInfo['name'][2], 'family_name' => $employeeInfo['name'][3], 'honorific_suffix' => $employeeInfo['name'][4], 'title' => $employeeInfo['title'], 'email' => $employeeInfo['email'], 'tel' => $employeeInfo['phone']]);
         $person->save();
         $employeeImage = null;
         if (!empty($employeeInfo['picture'])) {
             copy($dirname . "/default_images/reps/" . $employeeInfo['picture'], $dirname . "/" . $employeeInfo['picture']);
             $fileSize = filesize($dirname . "/" . $employeeInfo['picture']);
             $file = new UploadedFile($dirname . "/" . $employeeInfo['picture'], $employeeInfo['picture'], strpos($employeeInfo['picture'], '.jpg') === false ? "image/png" : "image/jpeg", $fileSize, null, true);
             $employeeImage = ImageList::upload($file);
         }
         $employee = new Employee();
         $employee->is($person);
         // Implies save
         $employee->service_area = $employeeInfo['service_area'];
         $employee->save();
         // Just in case
         if ($employeeImage != null) {
             $employee->picture()->save($employeeImage);
         }
         foreach ($employeeInfo['locations'] as $locationName) {
             $employee->locations()->attach(Location::where('name', '=', $locationName)->first());
         }
         $employee->save();
         // Last save shouldn't be necessary? Just in case, though...
     }
 }
Ejemplo n.º 2
0
 public function search()
 {
     $query = Request::get('query');
     // Find employees and emails that match -- return both
     $people = Person::where('full_name', 'like', "%{$query}%")->limit(6)->get();
     $employees = [];
     foreach ($people as $person) {
         array_push($employees, Employee::whereIs($person));
     }
     $formRecipients = FormRecipient::where('email', 'like', "%{$query}%")->limit(6)->get();
     $return = [];
     foreach ($employees as $employee) {
         array_push($return, ['value' => $employee->full_name]);
     }
     foreach ($formRecipients as $formRecipient) {
         array_push($return, ['value' => $formRecipient->email]);
     }
     return array_unique($return, SORT_REGULAR);
 }
 public function run()
 {
     DB::table('form_settings')->delete();
     $alwaysCopy = ['*****@*****.**', '*****@*****.**'];
     $perLocationCopy = ['Brookhaven' => ['BuildingQuote:Rigid Frame' => ['*****@*****.**'], 'BuildingQuote:Galv-Econ' => ['*****@*****.**'], 'BuildingQuote:Pole Barn' => ['*****@*****.**'], 'BuildingQuote:Roof' => [], 'ContactForm' => [], 'EmploymentApp' => [], 'CreditApp' => []], 'Tupelo' => ['BuildingQuote:Rigid Frame' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Galv-Econ' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Pole Barn' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Roof' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'ContactForm' => [], 'EmploymentApp' => [], 'CreditApp' => []], 'Lake Charles' => ['BuildingQuote:Rigid Frame' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Galv-Econ' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Pole Barn' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Roof' => [], 'ContactForm' => [], 'EmploymentApp' => [], 'CreditApp' => []], 'Benton' => ['BuildingQuote:Rigid Frame' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Galv-Econ' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Pole Barn' => ['*****@*****.**', '*****@*****.**'], 'BuildingQuote:Roof' => [], 'ContactForm' => [], 'EmploymentApp' => [], 'CreditApp' => []], 'Jackson' => ['BuildingQuote:Rigid Frame' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Galv-Econ' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Pole Barn' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'BuildingQuote:Roof' => ['*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**'], 'ContactForm' => [], 'EmploymentApp' => [], 'CreditApp' => []]];
     $locations = Location::all();
     $rigidFrameRole = Role::where('name', 'RigidFrameSales')->first();
     $galvEconRole = Role::where('name', 'GalvEconSales')->first();
     $poleBarnRole = Role::where('name', 'PoleBarnSales')->first();
     $roofingRole = Role::where('name', 'RoofingSales')->first();
     $contactRole = Role::where('name', 'GeneralManager')->first();
     $employeeRole = Role::where('name', 'HR')->first();
     foreach ($locations as $location) {
         $rigidFrameBuildingQuoteSettings = new FormSettings();
         $rigidFrameBuildingQuoteSettings->model = 'App\\Models\\Forms\\BuildingQuote:Rigid Frame';
         $rigidFrameBuildingQuoteSettings->rotation()->associate($rigidFrameRole);
         $location->formSettings()->save($rigidFrameBuildingQuoteSettings);
         $galvEconBuildingQuoteSettings = new FormSettings();
         $galvEconBuildingQuoteSettings->model = 'App\\Models\\Forms\\BuildingQuote:Galv-Econ';
         $galvEconBuildingQuoteSettings->rotation()->associate($galvEconRole);
         $location->formSettings()->save($galvEconBuildingQuoteSettings);
         $poleBarnBuildingQuoteSettings = new FormSettings();
         $poleBarnBuildingQuoteSettings->model = 'App\\Models\\Forms\\BuildingQuote:Pole Barn';
         $poleBarnBuildingQuoteSettings->rotation()->associate($poleBarnRole);
         $location->formSettings()->save($poleBarnBuildingQuoteSettings);
         $roofBuildingQuoteSettings = new FormSettings();
         $roofBuildingQuoteSettings->model = 'App\\Models\\Forms\\BuildingQuote:Roof';
         $roofBuildingQuoteSettings->rotation()->associate($roofingRole);
         $location->formSettings()->save($roofBuildingQuoteSettings);
         $contactFormSettings = new FormSettings();
         $contactFormSettings->model = 'App\\Models\\Forms\\ContactForm';
         $contactFormSettings->rotation()->associate($contactRole);
         $location->formSettings()->save($contactFormSettings);
         $employmentAppSettings = new FormSettings();
         $employmentAppSettings->model = 'App\\Models\\Forms\\EmploymentApp';
         $employmentAppSettings->rotation()->associate($employeeRole);
         $location->formSettings()->save($employmentAppSettings);
         $creditAppSettings = new FormSettings();
         $creditAppSettings->model = 'App\\Models\\Forms\\CreditApp';
         $creditAppSettings->rotation()->associate($contactRole);
         $location->formSettings()->save($creditAppSettings);
         $rigidFrameBuildingQuoteSettings->save();
         $galvEconBuildingQuoteSettings->save();
         $poleBarnBuildingQuoteSettings->save();
         $roofBuildingQuoteSettings->save();
         $contactFormSettings->save();
         $employmentAppSettings->save();
         $creditAppSettings->save();
         $settingsArr = [$rigidFrameBuildingQuoteSettings, $galvEconBuildingQuoteSettings, $poleBarnBuildingQuoteSettings, $roofBuildingQuoteSettings, $contactFormSettings, $employmentAppSettings, $creditAppSettings];
         if ($location->parent) {
             $location = $location->parent;
         }
         foreach ($perLocationCopy[$location->name] as $key => $value) {
             foreach ($value as $recipient) {
                 $formRecipient = new FormRecipient();
                 if ($person = Person::where('email', $recipient)->first()) {
                     $employee = Employee::whereIs($person);
                     if ($employee != null) {
                         $formRecipient->employee()->associate(Employee::whereIs($person));
                     }
                 } else {
                     $formRecipient->email = $recipient;
                 }
                 $formRecipient->save();
                 foreach ($settingsArr as $settings) {
                     if (strpos($settings->model, $key) !== false) {
                         $settings->copyTo()->save($formRecipient);
                         break;
                     }
                 }
             }
         }
         foreach ($alwaysCopy as $recipient) {
             $formRecipient = new FormRecipient();
             if ($person = Person::where('email', $recipient)->first()) {
                 $employee = Employee::whereIs($person);
                 if ($employee != null) {
                     $formRecipient->employee()->associate(Employee::whereIs($person));
                 }
             } else {
                 $formRecipient->email = $recipient;
             }
             $formRecipient->save();
             foreach ($settingsArr as $settings) {
                 $settings->copyTo()->save($formRecipient);
             }
         }
     }
 }
Ejemplo n.º 4
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);
 }