/**
  * 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, ['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');
 }
 /**
  * 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');
     //
 }
 public function merge_destroy($id)
 {
     // TODO: consider creating a restore/{id} or undelete/{id}
     //delete existing groups and relationships when deleting user
     \montserrat\Relationship::whereContactIdA($id)->delete();
     \montserrat\Relationship::whereContactIdB($id)->delete();
     \montserrat\GroupContact::whereContactId($id)->delete();
     //delete address, email, phone, website, emergency contact, notes for deleted users
     \montserrat\Address::whereContactId($id)->delete();
     \montserrat\Email::whereContactId($id)->delete();
     \montserrat\Phone::whereContactId($id)->delete();
     \montserrat\Website::whereContactId($id)->delete();
     \montserrat\EmergencyContact::whereContactId($id)->delete();
     \montserrat\Note::whereContactId($id)->delete();
     \montserrat\Touchpoint::wherePersonId($id)->delete();
     //delete registrations
     \montserrat\Registration::whereContactId($id)->delete();
     \montserrat\Contact::destroy($id);
     return Redirect::back();
 }