Files
monica/app/Http/Controllers/Contacts/RelationshipsController.php
T
Tom RochetteandRégis Freyd d2c9c75644 Fix tests to prepare for foreign keys (#1289)
* Fix AccountTest tests.
Update the model factories not to expect a constant value for account_id.
Add various missing relationships in the model factories.

* Fix AddressTest after the addition of foreign keys.

* Fix ContactTest after the addition of foreign keys.
Set contact/account before saving a special date using Contact::setSpecialDate or Contact::setSpecialDateFromAge.

* Fix DayTest after the addition of foreign keys.

* Fix EntryTest after the addition of foreign keys.

* Fix GiftTest after the addition of foreign keys.

* Fix JournalEntryTest after the addition of foreign keys.

* Fix NoteTest after the addition of foreign keys.

* Fix NotificationTest after the addition of foreign keys.

* Fix PetTest after the addition of foreign keys.

* Return the account id and not the account object in many model factories definition.

* Fix ReminderTest after the addition of foreign keys.

* Fix SpecialDateTest after the addition of foreign keys.

* Fix UserTest after the addition of foreign keys.

* Make instead of creating in Contact::logEvent() as we cannot create an event without giving it an account_id.

* Fix ActivityTest after the addition of foreign keys.

* Fix SendNotificationsTest after the addition of foreign keys.

* Fix SendNotificationsTest after the addition of foreign keys.
- Remove the test_it_deletes_the_notification_if_contact_does_not_exist test because on deletion of a contact, all his associated notifications will be deleted due to cascade constraints.

* Fix SendRemindersTest after the addition of foreign keys.
- Remove the test_it_deletes_the_reminder_if_contact_doesnt_exist test because on deletion of a contact, all his associated reminders will be deleted due to cascade constraints.

* Fix TagTest after the addition of foreign keys.

* Update the logic in Contact::removeSpecialDate so that a foreign key constraint is not triggered when removing the associated relation.
Set a the partner gender before saving the partner as the gender is mandatory.
Add the ConvertEmptyStringsToNull middleware.
Set the metThroughId default option as empty so it is converted to null by the ConvertEmptyStringsToNull middleware.
Set the activity_type_id as empty so it is converted to null by the ConvertEmptyStringsToNull middleware.
Mark the Reminder title has required.
2018-05-08 21:33:12 -04:00

322 lines
12 KiB
PHP

<?php
namespace App\Http\Controllers\Contacts;
use App\Contact;
use App\Relationship;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
class RelationshipsController extends Controller
{
/**
* Display the Create relationship page.
*
* @param Contact $contact
* @return \Illuminate\Http\Response
*/
public function new(Request $request, Contact $contact)
{
// getting the list of existing contacts
$existingContacts = auth()->user()->account->contacts()
->real()
->select(['id', 'first_name', 'last_name'])
->sortedBy('name')
->get();
// Building the list of contacts specifically for the dropdown which asks
// for an id and a name. Also filter out the current contact.
$arrayContacts = collect();
foreach ($existingContacts as $existingContact) {
if ($existingContact->id == $contact->id) {
continue;
}
$arrayContacts->push([
'id' => $existingContact->id,
'name' => $existingContact->getCompleteName(),
]);
}
// Building the list of relationship types specifically for the dropdown which asks
// for an id and a name.
$arrayRelationshipTypes = collect();
foreach (auth()->user()->account->relationshipTypes as $relationshipType) {
$arrayRelationshipTypes->push([
'id' => $relationshipType->id,
'name' => $relationshipType->getLocalizedName($contact, true),
]);
}
return view('people.relationship.new')
->withContact($contact)
->withPartner(new Contact)
->withGenders(auth()->user()->account->genders)
->withRelationshipTypes($arrayRelationshipTypes)
->withDays(\App\Helpers\DateHelper::getListOfDays())
->withMonths(\App\Helpers\DateHelper::getListOfMonths())
->withBirthdate(now()->format('Y-m-d'))
->withExistingContacts($arrayContacts)
->withType($request->get('type'));
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @param Contact $contact
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Contact $contact)
{
// case of linking to an existing contact
if ($request->get('relationship_type') == 'existing') {
$partner = Contact::findOrFail($request->get('existing_contact_id'));
$contact->setRelationship($partner, $request->get('relationship_type_id'));
return redirect('/people/'.$contact->id)
->with('success', trans('people.relationship_form_add_success'));
}
// case of creating a new contact
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:50',
'last_name' => 'max:100',
'gender_id' => 'required',
]);
if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}
// set the name of the contact
$partner = new Contact;
$partner->account_id = $contact->account->id;
// set gender
$partner->gender_id = $request->input('gender_id');
$partner->is_partial = true;
if (! $partner->setName($request->input('first_name'), $request->input('last_name'))) {
return back()
->withInput()
->withErrors('There has been a problem with saving the name.');
}
$partner->save();
// Handling the case of the birthday
$partner->removeSpecialDate('birthdate');
switch ($request->input('birthdate')) {
case 'unknown':
break;
case 'approximate':
$specialDate = $partner->setSpecialDateFromAge('birthdate', $request->input('age'));
break;
case 'almost':
$specialDate = $partner->setSpecialDate(
'birthdate',
0,
$request->input('month'),
$request->input('day')
);
if ($request->input('addReminder') != '') {
$newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $partner->first_name]));
}
break;
case 'exact':
$birthdate = $request->input('birthdayDate');
$birthdate = new \Carbon\Carbon($birthdate);
$specialDate = $partner->setSpecialDate(
'birthdate',
$birthdate->year,
$birthdate->month,
$birthdate->day
);
if ($request->input('addReminder') != '') {
$newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $partner->first_name]));
}
break;
}
// set avatar color
$partner->setAvatarColor();
// create the relationship
$contact->setRelationship($partner, $request->get('relationship_type_id'));
// check if the contact is partial
if ($request->get('realContact')) {
$partner->is_partial = false;
$partner->save();
}
return redirect('/people/'.$contact->hashID())
->with('success', trans('people.relationship_form_add_success'));
}
/**
* Show the form for editing the specified resource.
*
* @param Contact $contact
* @param SignificantOther $significantOther
* @return \Illuminate\Http\Response
*/
public function edit(Contact $contact, Contact $otherContact)
{
$age = (string) (! is_null($otherContact->birthdate) ? $otherContact->birthdate->getAge() : 0);
$birthdate = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->format('Y-m-d') : \Carbon\Carbon::now()->format('Y-m-d');
$day = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->day : \Carbon\Carbon::now()->day;
$month = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->month : \Carbon\Carbon::now()->month;
$hasBirthdayReminder = ! is_null($otherContact->birthdate) ? (is_null($otherContact->birthdate->reminder) ? 0 : 1) : 0;
// Building the list of relationship types specifically for the dropdown which asks
// for an id and a name.
$arrayRelationshipTypes = collect();
foreach (auth()->user()->account->relationshipTypes as $relationshipType) {
$arrayRelationshipTypes->push([
'id' => $relationshipType->id,
'name' => $relationshipType->getLocalizedName($contact, true),
]);
}
// Get the nature of the current relationship
$type = $contact->getRelationshipNatureWith($otherContact);
return view('people.relationship.edit')
->withContact($contact)
->withPartner($otherContact)
->withDays(\App\Helpers\DateHelper::getListOfDays())
->withMonths(\App\Helpers\DateHelper::getListOfMonths())
->withBirthdayState($otherContact->getBirthdayState())
->withBirthdate($birthdate)
->withDay($day)
->withMonth($month)
->withAge($age)
->withGenders(auth()->user()->account->genders)
->withHasBirthdayReminder($hasBirthdayReminder)
->withRelationshipTypes($arrayRelationshipTypes)
->withType($type->relationship_type_id);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Contact $contact
* @param SignificantOther $significantOther
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Contact $contact, Contact $otherContact)
{
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:50',
'last_name' => 'max:100',
'gender_id' => 'required',
]);
if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}
// set the name of the contact
if (! $otherContact->setName($request->input('first_name'), $request->input('last_name'))) {
return back()
->withInput()
->withErrors('There has been a problem with saving the name.');
}
// set gender
$otherContact->gender_id = $request->input('gender_id');
$otherContact->save();
// Handling the case of the birthday
$otherContact->removeSpecialDate('birthdate');
switch ($request->input('birthdate')) {
case 'unknown':
break;
case 'approximate':
$specialDate = $otherContact->setSpecialDateFromAge('birthdate', $request->input('age'));
break;
case 'almost':
$specialDate = $otherContact->setSpecialDate(
'birthdate',
0,
$request->input('month'),
$request->input('day')
);
if ($request->input('addReminder') != '') {
$newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $otherContact->first_name]));
}
break;
case 'exact':
$birthdate = $request->input('birthdayDate');
$birthdate = new \Carbon\Carbon($birthdate);
$specialDate = $otherContact->setSpecialDate(
'birthdate',
$birthdate->year,
$birthdate->month,
$birthdate->day
);
if ($request->input('addReminder') != '') {
$newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $otherContact->first_name]));
}
break;
}
// update the relationship
$contact->updateRelationship($otherContact, $request->get('type'), $request->get('relationship_type_id'));
// check if the contact is partial
if ($request->get('realContact')) {
$otherContact->is_partial = false;
$otherContact->save();
}
return redirect('/people/'.$contact->hashID())
->with('success', trans('people.relationship_form_add_success'));
}
/**
* Remove the specified resource from storage.
*
* @param Contact $contact
* @param Contact $partner
* @return \Illuminate\Http\Response
*/
public function destroy(Contact $contact, Contact $otherContact)
{
if ($contact->account_id != auth()->user()->account_id) {
return redirect('/people/');
}
if ($otherContact->account_id != auth()->user()->account_id) {
return redirect('/people/');
}
$type = $contact->getRelationshipNatureWith($otherContact);
$contact->deleteRelationship($otherContact, $type->relationship_type_id);
// the contact is partial - if the relationship is deleted, the partial
// contact has no reason to exist anymore
if ($otherContact->is_partial) {
$otherContact->deleteEverything();
}
return redirect('/people/'.$contact->hashID())
->with('success', trans('people.relationship_form_deletion_success'));
}
}