/** * Store a newly created resource in storage. * * @return Response */ public function store() { // TODO: smarter $req = Request::get('recipient'); $type = Request::get('subsection'); if (strpos($req, '@') !== false) { $recipient = new FormRecipient(); $recipient->email = $req; } else { $person = Person::where('full_name', 'like', "%{$req}%")->first(); $employee = Employee::whereIs($person); $recipient = new FormRecipient(); $recipient->employee()->associate($employee); } $recipient->type = $type; // Duplicates are prevented by an integrity constraint Location::current()->{Request::get('section')}->{$type}()->save($recipient); $recipient->save(); return ['id' => $recipient->id, 'email' => $recipient->email]; }
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); } } } }