/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, ['organization_name' => 'required', 'display_name' => 'required', 'sort_name' => 'required', 'email_primary' => 'email', 'url_main' => 'url', 'url_facebook' => 'url|regex:/facebook\\.com\\/.+/i', 'url_google' => 'url|regex:/plus\\.google\\.com\\/.+/i', 'url_twitter' => 'url|regex:/twitter\\.com\\/.+/i', 'url_instagram' => 'url|regex:/instagram\\.com\\/.+/i', 'url_linkedin' => 'url|regex:/linkedin\\.com\\/.+/i', 'phone_main_phone' => 'phone', 'phone_main_fax' => 'phone', 'avatar' => 'image|max:5000', 'attachment' => 'file|mimes:pdf,doc,docx|max:10000', 'attachment_description' => 'string|max:200']); $vendor = \montserrat\Contact::with('address_primary.state', 'address_primary.location', 'phone_primary.location', 'phone_main_fax', 'email_primary.location', 'website_main', 'notes')->findOrFail($request->input('id')); $vendor->organization_name = $request->input('organization_name'); $vendor->display_name = $request->input('display_name'); $vendor->sort_name = $request->input('sort_name'); $vendor->save(); if (empty($vendor->address_primary)) { $address_primary = new \montserrat\Address(); } else { $address_primary = \montserrat\Address::findOrNew($vendor->address_primary->id); } $address_primary->contact_id = $vendor->id; $address_primary->location_type_id = LOCATION_TYPE_MAIN; $address_primary->is_primary = 1; $address_primary->street_address = $request->input('street_address'); $address_primary->supplemental_address_1 = $request->input('supplemental_address_1'); $address_primary->city = $request->input('city'); $address_primary->state_province_id = $request->input('state_province_id'); $address_primary->postal_code = $request->input('postal_code'); $address_primary->country_id = $request->input('country_id'); $address_primary->save(); if (empty($vendor->phone_primary)) { $phone_primary = new \montserrat\Address(); } else { $phone_primary = \montserrat\Phone::findOrNew($vendor->phone_primary->id); } $phone_primary->contact_id = $vendor->id; $phone_primary->location_type_id = LOCATION_TYPE_MAIN; $phone_primary->is_primary = 1; $phone_primary->phone_type = 'Phone'; $phone_primary->phone = $request->input('phone_main_phone'); $phone_primary->save(); if (empty($vendor->phone_main_fax)) { $phone_main_fax = new \montserrat\Phone(); } else { $phone_main_fax = \montserrat\Phone::findOrNew($vendor->phone_main_fax->id); } $phone_main_fax->contact_id = $vendor->id; $phone_main_fax->location_type_id = LOCATION_TYPE_MAIN; $phone_main_fax->phone_type = 'Fax'; $phone_main_fax->phone = $request->input('phone_main_fax'); $phone_main_fax->save(); if (empty($vendor->email_primary)) { $email_primary = new \montserrat\Email(); } else { $email_primary = \montserrat\Email::findOrNew($vendor->email_primary->id); } $email_primary->contact_id = $vendor->id; $email_primary->is_primary = 1; $email_primary->location_type_id = LOCATION_TYPE_MAIN; $email_primary->email = $request->input('email_primary'); $email_primary->save(); if (null !== $request->file('avatar')) { $description = 'Avatar for ' . $vendor->organization_name; $attachment = new AttachmentsController(); $attachment->update_attachment($request->file('avatar'), 'contact', $vendor->id, 'avatar', $description); } if (null !== $request->file('attachment')) { $description = $request->input('attachment_description'); $attachment = new AttachmentsController(); $attachment->update_attachment($request->file('attachment'), 'contact', $vendor->id, 'attachment', $description); } $url_main = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Main']); $url_main->contact_id = $vendor->id; $url_main->url = $request->input('url_main'); $url_main->website_type = 'Main'; $url_main->save(); $url_work = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Work']); $url_work->contact_id = $vendor->id; $url_work->url = $request->input('url_work'); $url_work->website_type = 'Work'; $url_work->save(); $url_facebook = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Facebook']); $url_facebook->contact_id = $vendor->id; $url_facebook->url = $request->input('url_facebook'); $url_facebook->website_type = 'Facebook'; $url_facebook->save(); $url_google = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Google']); $url_google->contact_id = $vendor->id; $url_google->url = $request->input('url_google'); $url_google->website_type = 'Google'; $url_google->save(); $url_instagram = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Instagram']); $url_instagram->contact_id = $vendor->id; $url_instagram->url = $request->input('url_instagram'); $url_instagram->website_type = 'Instagram'; $url_instagram->save(); $url_linkedin = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'LinkedIn']); $url_linkedin->contact_id = $vendor->id; $url_linkedin->url = $request->input('url_linkedin'); $url_linkedin->website_type = 'LinkedIn'; $url_linkedin->save(); $url_twitter = \montserrat\Website::firstOrNew(['contact_id' => $vendor->id, 'website_type' => 'Twitter']); $url_twitter->contact_id = $vendor->id; $url_twitter->url = $request->input('url_twitter'); $url_twitter->website_type = 'Twitter'; $url_twitter->save(); return Redirect::action('VendorsController@index'); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // $this->validate($request, ['first_name' => 'required', 'last_name' => 'required', 'email' => 'email', 'dob' => 'date', 'url' => 'url', 'parish_id' => 'integer|min:0', 'gender' => 'in:Male,Female,Other,Unspecified']); $contact = \montserrat\Contact::with('addresses.location', 'emails.location', 'phones.location', 'websites', 'emergency_contact')->findOrFail($request->input('id')); $contact->title = $request->input('title'); $contact->firstname = $request->input('first_name'); $contact->middlename = $request->input('middle_name'); $contact->lastname = $request->input('last_name'); $contact->suffix = $request->input('suffix'); $contact->nickname = $request->input('nick_name'); $contact->display_name = $request->input('display_name'); $contact->sort_name = $request->input('sort_name'); //emergency contact info $contact->emergency_contact->name = $request->input('emergency_contact_name'); $contact->emergency_contact->relationship = $request->input('emergency_contact_relationship'); $contact->emergency_contact->phone = $request->input('emergency_contact_phone'); $contact->emergency_contact->phone_alternate = $request->input('emergency_contact_phone_alternate'); //demographic info $contact->gender = $request->input('gender'); $contact->dob = $request->input('dob'); $contact->ethnicity = $request->input('ethnicity'); $contact->parish_id = $request->input('parish_id'); $contact->languages = $request->input('languages'); //health info $contact->medical = $request->input('medical'); $contact->dietary = $request->input('dietary'); //misc info $contact->notes = $request->input('notes'); $contact->roompreference = $request->input('roompreference'); //group or roles info $contact->is_donor = $request->input('is_donor'); $contact->is_retreatant = $request->input('is_retreatant'); $contact->is_director = $request->input('is_director'); $contact->is_innkeeper = $request->input('is_innkeeper'); $contact->is_assistant = $request->input('is_assistant'); $contact->is_captain = $request->input('is_captain'); $contact->is_staff = $request->input('is_staff'); $contact->is_volunteer = $request->input('is_volunteer'); $contact->is_pastor = $request->input('is_pastor'); $contact->is_bishop = $request->input('is_bishop'); $contact->is_catholic = $request->input('is_catholic'); $contact->is_board = $request->input('is_board'); $contact->is_formerboard = $request->input('is_formerboard'); $contact->is_jesuit = $request->input('is_jesuit'); $contact->is_deceased = $request->input('is_deceased'); $contact->save(); $home_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME]); //dd($home_address); $home_address->street_address = $request->input('address_home_address1'); $home_address->supplemental_address_1 = $request->input('address_home_address2'); $home_address->city = $request->input('address_home_city'); $home_address->state_province_id = $request->input('address_home_state'); $home_address->postal_code = $request->input('address_home_zip'); $home_address->country_id = $request->input('address_home_country'); $home_address->save(); $work_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK]); $work_address->street_address = $request->input('address_work_address1'); $work_address->supplemental_address_1 = $request->input('address_work_address2'); $work_address->city = $request->input('address_work_city'); $work_address->state_province_id = $request->input('address_work_state'); $work_address->postal_code = $request->input('address_work_zip'); $work_address->country_id = $request->input('address_work_country'); $work_address->save(); $other_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER]); $other_address->street_address = $request->input('address_other_address1'); $other_address->supplemental_address_1 = $request->input('address_other_address2'); $other_address->city = $request->input('address_other_city'); $other_address->state_province_id = $request->input('address_other_state'); $other_address->postal_code = $request->input('address_other_zip'); $other_address->country_id = $request->input('address_other_country'); $other_address->save(); $phone_home_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Phone']); $phone_home_phone->phone = $request->input('phone_home_phone'); $phone_home_phone->save(); $phone_home_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Mobile']); $phone_home_mobile->phone = $request->input('phone_home_mobile'); $phone_home_mobile->save(); $phone_home_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Fax']); $phone_home_fax->phone = $request->input('phone_home_fax'); $phone_home_fax->save(); $phone_work_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Phone']); $phone_work_phone->phone = $request->input('phone_work_phone'); $phone_work_phone->save(); $phone_work_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Mobile']); $phone_work_mobile->phone = $request->input('phone_work_mobile'); $phone_work_mobile->save(); $phone_work_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Fax']); $phone_work_fax->phone = $request->input('phone_work_fax'); $phone_work_fax->save(); $phone_other_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Phone']); $phone_other_phone->phone = $request->input('phone_other_phone'); $phone_other_phone->save(); $phone_other_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Mobile']); $phone_other_mobile->phone = $request->input('phone_other_mobile'); $phone_other_mobile->save(); $phone_other_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Fax']); $phone_other_fax->phone = $request->input('phone_other_fax'); $phone_other_fax->save(); $email_home = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME]); $email_home->email = $request->input('email_home'); $email_home->save(); $email_work = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK]); $email_work->email = $request->input('email_work'); $email_work->save(); $email_other = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER]); $email_other->email = $request->input('email_other'); $email_other->save(); $url_main = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Main']); $url_main->url = $request->input('url_main'); $url_main->save(); $url_work = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Work']); $url_work->url = $request->input('url_work'); $url_work->save(); $url_facebook = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Facebook']); $url_facebook->url = $request->input('url_facebook'); $url_facebook->save(); $url_google = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Google']); $url_google->url = $request->input('url_google'); $url_google->save(); $url_instagram = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Instagram']); $url_instagram->url = $request->input('url_instagram'); $url_instagram->save(); $url_linkedin = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'LinkedIn']); $url_linkedin->url = $request->input('url_linkedin'); $url_linkedin->save(); $url_twitter = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Twitter']); $url_twitter->url = $request->input('url_twitter'); $url_twitter->save(); return Redirect::action('ContactsController@index'); // }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // $this->validate($request, ['organization_name' => 'required', 'display_name' => 'required', 'sort_name' => 'required', 'bishop_id' => 'integer|min:0', 'email_main' => 'email', 'url_main' => 'url', 'url_facebook' => 'url|regex:/facebook\\.com\\/.+/i', 'url_google' => 'url|regex:/plus\\.google\\.com\\/.+/i', 'url_twitter' => 'url|regex:/twitter\\.com\\/.+/i', 'url_instagram' => 'url|regex:/instagram\\.com\\/.+/i', 'url_linkedin' => 'url|regex:/linkedin\\.com\\/.+/i', 'phone_main_phone' => 'phone', 'phone_main_fax' => 'phone', 'avatar' => 'image|max:5000', 'attachment' => 'file|mimes:pdf,doc,docx|max:10000', 'attachment_description' => 'string|max:200']); $diocese = \montserrat\Contact::with('bishops.contact_b', 'parishes.contact_b', 'address_primary.state', 'address_primary.location', 'phone_primary.location', 'phone_main_fax.location', 'email_primary.location', 'website_main', 'notes')->findOrFail($id); $diocese->organization_name = $request->input('organization_name'); $diocese->display_name = $request->input('display_name'); $diocese->sort_name = $request->input('sort_name'); $diocese->contact_type = CONTACT_TYPE_ORGANIZATION; $diocese->subcontact_type = CONTACT_TYPE_DIOCESE; $diocese->save(); $address_primary = \montserrat\Address::findOrNew($diocese->address_primary->id); $address_primary->contact_id = $diocese->id; $address_primary->location_type_id = LOCATION_TYPE_MAIN; $address_primary->is_primary = 1; $address_primary->street_address = $request->input('street_address'); $address_primary->supplemental_address_1 = $request->input('supplemental_address_1'); $address_primary->city = $request->input('city'); $address_primary->state_province_id = $request->input('state_province_id'); $address_primary->postal_code = $request->input('postal_code'); $address_primary->country_id = COUNTRY_ID_USA; $address_primary->is_primary = 1; $address_primary->save(); $phone_primary = \montserrat\Phone::findOrNew($diocese->phone_primary->id); $phone_primary->contact_id = $diocese->id; $phone_primary->location_type_id = LOCATION_TYPE_MAIN; $phone_primary->is_primary = 1; $phone_primary->phone = $request->input('phone_main_phone'); $phone_primary->phone_type = 'Phone'; $phone_primary->save(); if (empty($diocese->phone_main_fax)) { $phone_main_fax = new \montserrat\Phone(); } else { $phone_main_fax = \montserrat\Phone::findOrNew($diocese->phone_main_fax->id); } $phone_main_fax->contact_id = $diocese->id; $phone_main_fax->location_type_id = LOCATION_TYPE_MAIN; $phone_main_fax->phone = $request->input('phone_main_fax'); $phone_main_fax->phone_type = 'Fax'; $phone_main_fax->save(); $email_primary = \montserrat\Email::findOrNew($diocese->email_primary->id); $email_primary->contact_id = $diocese->id; $email_primary->is_primary = 1; $email_primary->location_type_id = LOCATION_TYPE_MAIN; $email_primary->email = $request->input('email_primary'); $email_primary->save(); $url_main = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Main']); $url_main->contact_id = $diocese->id; $url_main->url = $request->input('url_main'); $url_main->website_type = 'Main'; $url_main->save(); $url_work = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Work']); $url_work->contact_id = $diocese->id; $url_work->url = $request->input('url_work'); $url_work->website_type = 'Work'; $url_work->save(); $url_facebook = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Facebook']); $url_facebook->contact_id = $diocese->id; $url_facebook->url = $request->input('url_facebook'); $url_facebook->website_type = 'Facebook'; $url_facebook->save(); $url_google = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Google']); $url_google->contact_id = $diocese->id; $url_google->url = $request->input('url_google'); $url_google->website_type = 'Google'; $url_google->save(); $url_instagram = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Instagram']); $url_instagram->contact_id = $diocese->id; $url_instagram->url = $request->input('url_instagram'); $url_instagram->website_type = 'Instagram'; $url_instagram->save(); $url_linkedin = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'LinkedIn']); $url_linkedin->contact_id = $diocese->id; $url_linkedin->url = $request->input('url_linkedin'); $url_linkedin->website_type = 'LinkedIn'; $url_linkedin->save(); $url_twitter = \montserrat\Website::firstOrNew(['contact_id' => $diocese->id, 'website_type' => 'Twitter']); $url_twitter->contact_id = $diocese->id; $url_twitter->url = $request->input('url_twitter'); $url_twitter->website_type = 'Twitter'; $url_twitter->save(); if ($request->input('bishop_id') > 0) { $bishop_id = $request->input('bishop_id'); $relationship_bishop = \montserrat\Relationship::firstOrNew(['contact_id_a' => $diocese->id, 'contact_id_b' => $bishop_id, 'relationship_type_id' => RELATIONSHIP_TYPE_BISHOP, 'is_active' => 1]); $relationship_bishop->contact_id_a = $diocese->id; $relationship_bishop->contact_id_b = $bishop_id; $relationship_bishop->relationship_type_id = RELATIONSHIP_TYPE_BISHOP; $relationship_bishop->is_active = 1; $relationship_bishop->save(); } if (null !== $request->file('avatar')) { $description = 'Avatar for ' . $diocese->organization_name; $attachment = new AttachmentsController(); $attachment->update_attachment($request->file('avatar'), 'contact', $diocese->id, 'avatar', $description); } if (null !== $request->file('attachment')) { $description = $request->input('attachment_description'); $attachment = new AttachmentsController(); $attachment->update_attachment($request->file('attachment'), 'contact', $diocese->id, 'attachment', $description); } return Redirect::action('DiocesesController@index'); }
public function add($id) { // $current_user = Auth::user(); $user_email = \montserrat\Email::whereEmail($current_user->email)->first(); $defaults['contact_id'] = $id; if (empty($user_email->contact_id)) { $defaults['user_id'] = 0; } else { $defaults['user_id'] = $user_email->contact_id; } //lookup the contact type of the touchpoint being added and show similar ones in drop down (persons, parishes, etc.) $contact = \montserrat\Contact::findOrFail($id); if (isset($contact->subcontact_type)) { $persons = \montserrat\Contact::whereSubcontactType($contact->subcontact_type)->orderBy('sort_name')->pluck('sort_name', 'id'); } else { $persons = \montserrat\Contact::whereContactType($contact->contact_type)->orderBy('sort_name')->pluck('sort_name', 'id'); } $staff = \montserrat\Contact::with('groups')->whereHas('groups', function ($query) { $query->where('group_id', '=', GROUP_ID_STAFF); })->orderBy('sort_name')->pluck('sort_name', 'id'); // TODO: replace this with an autocomplete text box for performance rather than a dropdown box return view('touchpoints.create', compact('staff', 'persons', 'defaults')); }
public function merge($contact_id, $merge_id = NULL) { $contact = \montserrat\Contact::findOrFail($contact_id); $similar = \montserrat\Contact::whereSortName($contact->sort_name)->get(); $duplicates = $similar->keyBy('id'); $duplicates->forget($contact->id); if (!empty($merge_id)) { $merge = \montserrat\Contact::findOrFail($merge_id); //dd($merge); if (empty($contact->prefix_id) && !empty($merge->prefix_id)) { $contact->prefix_id = $merge->prefix_id; } if (empty($contact->first_name) && !empty($merge->first_name)) { $contact->first_name = $merge->first_name; } if (empty($contact->nick_name) && !empty($merge->nick_name)) { $contact->nick_name = $merge->nick_name; } if (empty($contact->middle_name) && !empty($merge->middle_name)) { $contact->middle_name = $merge->middle_name; } if (empty($contact->last_name) && !empty($merge->last_name)) { $contact->last_name = $merge->last_name; } if (empty($contact->organization_name) && !empty($merge->organization_name)) { $contact->organization_name = $merge->organization_name; } if (empty($contact->suffix_id) && !empty($merge->suffix_id)) { $contact->suffix_id = $merge->suffix_id; } if (empty($contact->gender_id) && !empty($merge->gender_id)) { $contact->gender_id = $merge->gender_id; } if (empty($contact->birth_date) && !empty($merge->birth_date)) { $contact->birth_date = $merge->birth_date; } if (empty($contact->religion_id) && !empty($merge->religion_id)) { $contact->religion_id = $merge->religion_id; } if (empty($contact->occupation_id) && !empty($merge->occupation_id)) { $contact->occupation_id = $merge->occupation_id; } if (empty($contact->ethnicity_id) && !empty($merge->ethnicity_id)) { $contact->ethnicity_id = $merge->ethnicity_id; } $contact->save(); //addresses if (NULL === $contact->address_primary) { $contact->address_primary = new \montserrat\Address(); $contact->address_primary->contact_id = $contact->id; $contact->address_primary->is_primary = 1; } if (empty($contact->address_primary->street_address) && !empty($merge->address_primary->street_address)) { $contact->address_primary->street_address = $merge->address_primary->street_address; } if (empty($contact->address_primary->supplemental_address) && !empty($merge->address_primary->supplemental_address)) { $contact->address_primary->supplemental_address = $merge->address_primary->supplemental_address; } if (empty($contact->address_primary->city) && !empty($merge->address_primary->city)) { $contact->address_primary->city = $merge->address_primary->city; } if (empty($contact->address_primary->state_province_id) && !empty($merge->address_primary->state_province_id)) { $contact->address_primary->state_province_id = $merge->address_primary->state_province_id; } if (empty($contact->address_primary->postal_code) && !empty($merge->address_primary->postal_code)) { $contact->address_primary->postal_code = $merge->address_primary->postal_code; } if (empty($contact->address_primary->country_code) && !empty($merge->address_primary->country_code)) { $contact->address_primary->country_code = $merge->address_primary->country_code; } $contact->address_primary->save(); //emergency_contact_info if (NULL === $contact->emergency_contact) { $contact->emergency_contact = new \montserrat\EmergencyContact(); $contact->emergency_contact->contact_id = $contact->id; } if (empty($contact->emergency_contact->name) && !empty($merge->emergency_contact->name)) { $contact->emergency_contact->name = $merge->emergency_contact->name; } if (empty($contact->emergency_contact->relationship) && !empty($merge->emergency_contact->relationship)) { $contact->emergency_contact->relationship = $merge->emergency_contact->relationship; } if (empty($contact->emergency_contact->phone) && !empty($merge->emergency_contact->phone)) { $contact->emergency_contact->phone = $merge->emergency_contact->phone; } if (empty($contact->emergency_contact->phone_alternate) && !empty($merge->emergency_contact->phone_alternate)) { $contact->emergency_contact->phone_alternate = $merge->emergency_contact->phone_alternate; } $contact->emergency_contact->save(); //emails foreach ($merge->emails as $email) { $contact_email = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => $email->location_type_id]); $contact_email->contact_id = $contact->id; $contact_email->location_type_id = $email->location_type_id; $contact_email->is_primary = $email->is_primary; //only create or overwrite if the current email address for the location is empty if (empty($contact_email->email)) { $contact_email->email = $email->email; $contact_email->save(); } } //phones foreach ($merge->phones as $phone) { $contact_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, "location_type_id" => $phone->location_type_id, "phone_type" => $phone->phone_type]); $contact_phone->contact_id = $contact->id; $contact_phone->location_type_id = $phone->location_type_id; $contact_phone->phone_type = $phone->phone_type; $contact_phone->is_primary = $phone->is_primary; //only create or overwrite if the current email address for the location is empty if (empty($contact_phone->phone)) { $contact_phone->phone = $phone->phone; $contact_phone->save(); } } //notes - move all from merge to contact foreach ($merge->notes as $note) { $note->entity_id = $contact_id; $note->save(); } //groups - move all from merge to contact foreach ($merge->groups as $group) { $group_exist = \montserrat\GroupContact::whereContactId($contact_id)->whereGroupId($group->group_id)->first(); if (!isset($group_exist)) { $group->contact_id = $contact_id; $group->save(); } } //relationships foreach ($merge->a_relationships as $a_relationship) { $a_relationship_exist = \montserrat\Relationship::whereContactIdA($contact_id)->whereContactIdB($a_relationship->contact_id_b)->whereRelationshipTypeId($a_relationship->relationship_type_id)->first(); if (!isset($a_relationship_exist)) { $a_relationship->contact_id_a = $contact_id; $a_relationship->save(); } } foreach ($merge->b_relationships as $b_relationship) { $b_relationship_exist = \montserrat\Relationship::whereContactIdB($contact_id)->whereContactIdA($b_relationship->contact_id_a)->whereRelationshipTypeId($b_relationship->relationship_type_id)->first(); if (!isset($b_relationship_exist)) { $b_relationship->contact_id_b = $contact_id; $b_relationship->save(); } } //touchpoints foreach ($merge->touchpoints as $touchpoint) { $touchpoint->person_id = $contact_id; $touchpoint->save(); } //attachments foreach ($merge->attachments as $attachment) { $path = 'contact/' . $merge_id . '/attachments/' . $attachment->uri; $newpath = 'contact/' . $contact_id . '/attachments/' . $attachment->uri; Storage::move($path, $newpath); $attachment->entity_id = $contact->id; $attachment->save(); } } return view('persons.merge', compact('contact', 'duplicates')); }