refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
207
app/Http/Controllers/Contacts/ActivitiesController.php
Normal file
207
app/Http/Controllers/Contacts/ActivitiesController.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helpers\AccountHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Account\ActivityType;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Account\Activity\Activity\CreateActivity;
|
||||
use App\Services\Account\Activity\Activity\UpdateActivity;
|
||||
use App\Services\Account\Activity\Activity\DestroyActivity;
|
||||
use App\Services\Account\Activity\ActivityStatisticService;
|
||||
use App\Http\Resources\Activity\Activity as ActivityResource;
|
||||
|
||||
class ActivitiesController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Get the list of activities.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$activities = $contact->activities()
|
||||
->orderBy('happened_at', 'desc')
|
||||
->limit(10)
|
||||
->get();
|
||||
|
||||
return ActivityResource::collection($activities)->additional(['meta' => [
|
||||
'statistics' => AccountHelper::getYearlyActivitiesStatistics($contact->account),
|
||||
]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of contacts available to associate the activity with
|
||||
* participants.
|
||||
* We could have chosen to query `/people` to get the full list of contacts
|
||||
* but some accounts have thousands of contacts. Thus for performance
|
||||
* purposes we have to create our own collection containing just the
|
||||
* necessary information.
|
||||
* Also we need to filter out the current contact from the list.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return Collection
|
||||
*/
|
||||
public function contacts(Request $request, Contact $contact)
|
||||
{
|
||||
return auth()->user()->account->contacts
|
||||
->filter(function ($c) use ($contact) {
|
||||
return $contact->id !== $c->id;
|
||||
})
|
||||
->map(function (Contact $c): array {
|
||||
return [
|
||||
'id' => $c->id,
|
||||
'name' => $c->name,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of activity categories.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Collection
|
||||
*/
|
||||
public function categories(Request $request)
|
||||
{
|
||||
$categories = auth()->user()->account->activityTypeCategories;
|
||||
|
||||
$array = collect([]);
|
||||
foreach ($categories as $category) {
|
||||
$types = ActivityType::where('activity_type_category_id', $category->id)->get();
|
||||
|
||||
$typeCollection = collect([]);
|
||||
foreach ($types as $type) {
|
||||
$typeCollection->push([
|
||||
'id' => $type->id,
|
||||
'name' => $type->name,
|
||||
]);
|
||||
}
|
||||
|
||||
$array->push([
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
'types' => $typeCollection,
|
||||
]);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the summary of activities for a given contact.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function summary(Request $request, Contact $contact)
|
||||
{
|
||||
// get the year of the most recent activity done with the contact
|
||||
$year = $contact->activities->sortByDesc('happened_at')
|
||||
->first()
|
||||
->happened_at
|
||||
->year;
|
||||
|
||||
return redirect()->route('people.activities.year', [$contact, $year]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the activities for this contact for a specific year.
|
||||
*/
|
||||
public function year(ActivityStatisticService $activityStatisticService, Contact $contact, int $year)
|
||||
{
|
||||
$startDate = Carbon::create($year, 1, 1);
|
||||
$endDate = Carbon::create($year, 12, 31);
|
||||
|
||||
$activitiesLastTwelveMonths = $activityStatisticService
|
||||
->activitiesWithContactInTimeRange($contact, now()->subMonths(12), now())
|
||||
->count();
|
||||
|
||||
$uniqueActivityTypes = $activityStatisticService
|
||||
->uniqueActivityTypesInTimeRange($contact, $startDate, $endDate);
|
||||
|
||||
$activitiesPerYear = $activityStatisticService->activitiesPerYearWithContact($contact);
|
||||
|
||||
$activitiesPerMonthForYear = $activityStatisticService
|
||||
->activitiesPerMonthForYear($contact, $year)
|
||||
->sortByDesc('month');
|
||||
|
||||
return view('people.activities.year')
|
||||
->withTotalActivities($contact->activities->count())
|
||||
->withActivitiesLastTwelveMonths($activitiesLastTwelveMonths)
|
||||
->withUniqueActivityTypes($uniqueActivityTypes)
|
||||
->withActivitiesPerYear($activitiesPerYear)
|
||||
->withActivitiesPerMonthForYear($activitiesPerMonthForYear)
|
||||
->withYear($year)
|
||||
->withContact($contact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the activity.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Contracts\Support\Responsable
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$activity = app(CreateActivity::class)->execute(
|
||||
$request->except(['account_id'])
|
||||
+
|
||||
[
|
||||
'account_id' => auth()->user()->account_id,
|
||||
]
|
||||
);
|
||||
|
||||
return new ActivityResource($activity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the activity.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Activity $activity
|
||||
* @return \Illuminate\Contracts\Support\Responsable
|
||||
*/
|
||||
public function update(Request $request, Activity $activity)
|
||||
{
|
||||
$activity = app(UpdateActivity::class)->execute(
|
||||
$request->except(['account_id', 'activity_id'])
|
||||
+
|
||||
[
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
]
|
||||
);
|
||||
|
||||
return new ActivityResource($activity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an activity.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Activity $activity
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Activity $activity)
|
||||
{
|
||||
app(DestroyActivity::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
]);
|
||||
|
||||
return $this->respondObjectDeleted($activity->id);
|
||||
}
|
||||
}
|
||||
144
app/Http/Controllers/Contacts/AddressesController.php
Normal file
144
app/Http/Controllers/Contacts/AddressesController.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Address;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Helpers\CountriesHelper;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Services\Contact\Address\CreateAddress;
|
||||
use App\Services\Contact\Address\UpdateAddress;
|
||||
use App\Services\Contact\Address\DestroyAddress;
|
||||
|
||||
class AddressesController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Get all the addresses for this contact.
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
$addresses = collect([]);
|
||||
|
||||
foreach ($contact->addresses as $address) {
|
||||
$addresses->push($this->addressObject($address));
|
||||
}
|
||||
|
||||
return $addresses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the countries.
|
||||
*/
|
||||
public function getCountries()
|
||||
{
|
||||
$key = 'countries.'.App::getLocale();
|
||||
|
||||
$countries = Cache::rememberForever($key, function () {
|
||||
return CountriesHelper::getAll();
|
||||
});
|
||||
|
||||
return response()->json($countries->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the address.
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
$datas = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
] + $request->only([
|
||||
'name',
|
||||
'country',
|
||||
'street',
|
||||
'city',
|
||||
'province',
|
||||
'postal_code',
|
||||
'latitude',
|
||||
'longitude',
|
||||
]);
|
||||
|
||||
$address = app(CreateAddress::class)->execute($datas);
|
||||
|
||||
return $this->setHTTPStatusCode(201)
|
||||
->respond($this->addressObject($address));
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the contact field.
|
||||
*/
|
||||
public function edit(Request $request, Contact $contact, Address $address)
|
||||
{
|
||||
$datas = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'address_id' => $address->id,
|
||||
] + $request->only([
|
||||
'name',
|
||||
'country',
|
||||
'street',
|
||||
'city',
|
||||
'province',
|
||||
'postal_code',
|
||||
'latitude',
|
||||
'longitude',
|
||||
]);
|
||||
|
||||
$address = app(UpdateAddress::class)->execute($datas);
|
||||
|
||||
return $this->respond($this->addressObject($address));
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the address.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Address $address
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Address $address)
|
||||
{
|
||||
$datas = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'address_id' => $address->id,
|
||||
];
|
||||
|
||||
if (app(DestroyAddress::class)->execute($datas)) {
|
||||
return $this->respondObjectDeleted($address->id);
|
||||
}
|
||||
|
||||
return $this->setHTTPStatusCode(400)
|
||||
->setErrorCode(32)
|
||||
->respondWithError();
|
||||
}
|
||||
|
||||
private function addressObject($address)
|
||||
{
|
||||
$place = $address->place;
|
||||
|
||||
return [
|
||||
'id' => $address->id,
|
||||
'name' => $address->name,
|
||||
'googleMapAddress' => $place->getGoogleMapAddress(),
|
||||
'googleMapAddressLatitude' => $place->getGoogleMapsAddressWithLatitude(),
|
||||
'address' => $place->getAddressAsString(),
|
||||
'country' => $place->country,
|
||||
'country_name' => $place->country_name,
|
||||
'street' => $place->street,
|
||||
'city' => $place->city,
|
||||
'province' => $place->province,
|
||||
'postal_code' => $place->postal_code,
|
||||
'latitude' => $place->latitude,
|
||||
'longitude' => $place->longitude,
|
||||
'edit' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
92
app/Http/Controllers/Contacts/AvatarController.php
Normal file
92
app/Http/Controllers/Contacts/AvatarController.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Services\Account\Photo\UploadPhoto;
|
||||
use App\Services\Contact\Avatar\UpdateAvatar;
|
||||
|
||||
class AvatarController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the Edit avatar screen.
|
||||
*/
|
||||
public function edit(Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
return view('people.avatar.edit')
|
||||
->withContact($contact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the avatar of the contact.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
*/
|
||||
public function update(Request $request, Contact $contact)
|
||||
{
|
||||
// update the avatar
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'source' => $request->input('avatar'),
|
||||
];
|
||||
|
||||
switch ($request->input('avatar')) {
|
||||
case 'upload':
|
||||
// if it's a new photo, we need to upload it
|
||||
$validator = Validator::make($request->all(), [
|
||||
'file' => 'image|max:'.config('monica.max_upload_size'),
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($validator);
|
||||
}
|
||||
|
||||
$photo = app(UploadPhoto::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'photo' => $request->photo,
|
||||
]);
|
||||
|
||||
$data['photo_id'] = $photo->id;
|
||||
$data['source'] = 'photo';
|
||||
break;
|
||||
case 'photo':
|
||||
$data['photo_id'] = $contact->avatar_photo_id;
|
||||
break;
|
||||
}
|
||||
|
||||
app(UpdateAvatar::class)->execute($data);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.information_edit_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given photo as avatar.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param int $photoId
|
||||
*/
|
||||
public function photo(Request $request, Contact $contact, $photoId)
|
||||
{
|
||||
// update the avatar
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'source' => 'photo',
|
||||
'photo_id' => $photoId,
|
||||
];
|
||||
|
||||
return app(UpdateAvatar::class)->execute($data);
|
||||
}
|
||||
}
|
||||
111
app/Http/Controllers/Contacts/CallsController.php
Normal file
111
app/Http/Controllers/Contacts/CallsController.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Models\Contact\Call;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Contact\Call\CreateCall;
|
||||
use App\Services\Contact\Call\UpdateCall;
|
||||
use App\Services\Contact\Call\DestroyCall;
|
||||
use App\Http\Resources\Call\Call as CallResource;
|
||||
|
||||
class CallsController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Display the list of calls.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$calls = $contact->calls()->get();
|
||||
|
||||
return CallResource::collection($calls);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the timestamp of the last phone contact.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function lastCalled(Contact $contact): JsonResponse
|
||||
{
|
||||
$lastTalkedTo = $contact->last_talked_to;
|
||||
|
||||
if ($lastTalkedTo !== null) {
|
||||
$lastTalkedTo = DateHelper::getShortDate($contact->last_talked_to);
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'last_talked_to' => $lastTalkedTo,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a call.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return Call
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
return app(CreateCall::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'content' => $request->input('content'),
|
||||
'called_at' => $request->input('called_at'),
|
||||
'contact_called' => $request->input('contact_called'),
|
||||
'emotions' => $request->input('emotions'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a call.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Call $call
|
||||
* @return Call
|
||||
*/
|
||||
public function update(Request $request, Contact $contact, Call $call)
|
||||
{
|
||||
return app(UpdateCall::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'call_id' => $call->id,
|
||||
'content' => $request->input('content'),
|
||||
'called_at' => $request->input('called_at'),
|
||||
'contact_called' => $request->input('contact_called'),
|
||||
'emotions' => $request->input('emotions'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the call.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Call $call
|
||||
* @return null|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Call $call): ?JsonResponse
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'call_id' => $call->id,
|
||||
];
|
||||
|
||||
if (app(DestroyCall::class)->execute($data)) {
|
||||
return $this->respondObjectDeleted($call->id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
26
app/Http/Controllers/Contacts/ContactAuditLogController.php
Normal file
26
app/Http/Controllers/Contacts/ContactAuditLogController.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\AuditLogHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class ContactAuditLogController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the page listing all the audit logs.
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
$logs = $contact->logs()
|
||||
->with('author')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(15);
|
||||
|
||||
return view('people.auditlogs.index')
|
||||
->withContact($contact)
|
||||
->withLogsCollection(AuditLogHelper::getCollectionOfAudits($logs))
|
||||
->withLogsPagination($logs);
|
||||
}
|
||||
}
|
||||
92
app/Http/Controllers/Contacts/ContactFieldsController.php
Normal file
92
app/Http/Controllers/Contacts/ContactFieldsController.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Contact\ContactField;
|
||||
use App\Jobs\Avatars\GetAvatarsFromInternet;
|
||||
use App\Http\Requests\People\ContactFieldsRequest;
|
||||
|
||||
class ContactFieldsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get all the contact information for this contact.
|
||||
*/
|
||||
public function getContactFields(Contact $contact)
|
||||
{
|
||||
$contactInformationData = collect([]);
|
||||
|
||||
foreach ($contact->contactFields as $contactField) {
|
||||
$data = [
|
||||
'id' => $contactField->id,
|
||||
'data' => $contactField->data,
|
||||
'name' => $contactField->contactFieldType->name,
|
||||
'fontawesome_icon' => (is_null($contactField->contactFieldType->fontawesome_icon) ? null : $contactField->contactFieldType->fontawesome_icon),
|
||||
'protocol' => (is_null($contactField->contactFieldType->protocol) ? null : $contactField->contactFieldType->protocol),
|
||||
'contact_field_type_id' => $contactField->contact_field_type_id,
|
||||
'edit' => false,
|
||||
];
|
||||
$contactInformationData->push($data);
|
||||
}
|
||||
|
||||
return $contactInformationData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the contact field types.
|
||||
*
|
||||
* @param Contact $contact
|
||||
*/
|
||||
public function getContactFieldTypes(Contact $contact)
|
||||
{
|
||||
return auth()->user()->account->contactFieldTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the contact field.
|
||||
*/
|
||||
public function storeContactField(ContactFieldsRequest $request, Contact $contact)
|
||||
{
|
||||
$contactField = $contact->contactFields()->create(
|
||||
$request->only([
|
||||
'contact_field_type_id',
|
||||
'data',
|
||||
])
|
||||
+ [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
]
|
||||
);
|
||||
|
||||
GetAvatarsFromInternet::dispatch($contact);
|
||||
|
||||
return $contactField;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the contact field.
|
||||
*/
|
||||
public function editContactField(ContactFieldsRequest $request, Contact $contact, ContactField $contactField)
|
||||
{
|
||||
$contactField->update(
|
||||
$request->only([
|
||||
'contact_field_type_id',
|
||||
'data',
|
||||
])
|
||||
+ [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
]
|
||||
);
|
||||
|
||||
GetAvatarsFromInternet::dispatch($contact);
|
||||
|
||||
return $contactField;
|
||||
}
|
||||
|
||||
public function destroyContactField(Contact $contact, ContactField $contactField)
|
||||
{
|
||||
$contactField->delete();
|
||||
|
||||
GetAvatarsFromInternet::dispatch($contact);
|
||||
}
|
||||
}
|
||||
289
app/Http/Controllers/Contacts/ConversationsController.php
Normal file
289
app/Http/Controllers/Contacts/ConversationsController.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Contact\Conversation;
|
||||
use App\Traits\JsonRespondController;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Contact\Conversation\DestroyMessage;
|
||||
use App\Services\Contact\Conversation\CreateConversation;
|
||||
use App\Services\Contact\Conversation\UpdateConversation;
|
||||
use App\Services\Contact\Conversation\DestroyConversation;
|
||||
use App\Services\Contact\Conversation\AddMessageToConversation;
|
||||
|
||||
class ConversationsController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Display the Create conversation page.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function create(Request $request, Contact $contact)
|
||||
{
|
||||
return view('people.conversations.new')
|
||||
->withContact($contact)
|
||||
->withContactFieldTypes(auth()->user()->account->contactFieldTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the list of conversations.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return Collection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$conversationsCollection = collect([]);
|
||||
$conversations = $contact->conversations()->get();
|
||||
|
||||
foreach ($conversations as $conversation) {
|
||||
$message = $conversation->messages->last();
|
||||
$data = [
|
||||
'id' => $conversation->id,
|
||||
'message_count' => $conversation->messages->count(),
|
||||
'contact_field_type' => $conversation->contactFieldType->name,
|
||||
'icon' => $conversation->contactFieldType->fontawesome_icon,
|
||||
'content' => ! is_null($message) ? mb_strimwidth($message->content, 0, 50, '…') : '',
|
||||
'happened_at' => DateHelper::getShortDate($conversation->happened_at),
|
||||
'route' => route('people.conversations.edit', [$contact, $conversation]),
|
||||
];
|
||||
$conversationsCollection->push($data);
|
||||
}
|
||||
|
||||
return $conversationsCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the conversation.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
$data = $this->validateAndGetDatas($request);
|
||||
|
||||
if ($data instanceof \Illuminate\Contracts\Validation\Validator) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($data);
|
||||
}
|
||||
|
||||
$date = $data['happened_at'];
|
||||
$data['contact_id'] = $contact->id;
|
||||
|
||||
// create the conversation
|
||||
try {
|
||||
$conversation = app(CreateConversation::class)->execute($data);
|
||||
} catch (ValidationException $e) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($e->validator);
|
||||
} catch (\Exception $e) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors(trans('app.error_save'));
|
||||
}
|
||||
|
||||
// add the messages to the conversation
|
||||
$result = $this->updateMessages($request, $conversation, $date);
|
||||
if ($result !== true) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($result);
|
||||
}
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.conversation_add_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a specific conversation.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function edit(Request $request, Contact $contact, Conversation $conversation)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
// preparing the messages for the Vue component
|
||||
$messages = collect([]);
|
||||
foreach ($conversation->messages as $message) {
|
||||
$messages->push([
|
||||
'uid' => $message->id,
|
||||
'content' => $message->content,
|
||||
'author' => ($message->written_by_me ? 'me' : 'other'),
|
||||
]);
|
||||
}
|
||||
|
||||
return view('people.conversations.edit')
|
||||
->withContact($contact)
|
||||
->withConversation($conversation)
|
||||
->withMessages($messages)
|
||||
->withContactFieldTypes(auth()->user()->account->contactFieldTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the conversation.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Conversation $conversation
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function update(Request $request, Contact $contact, Conversation $conversation)
|
||||
{
|
||||
$data = $this->validateAndGetDatas($request);
|
||||
|
||||
if ($data instanceof \Illuminate\Contracts\Validation\Validator) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($data);
|
||||
}
|
||||
|
||||
$date = $data['happened_at'];
|
||||
$data['conversation_id'] = $conversation->id;
|
||||
|
||||
// update the conversation
|
||||
try {
|
||||
$conversation = app(UpdateConversation::class)->execute($data);
|
||||
} catch (ValidationException $e) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($e->validator);
|
||||
} catch (\Exception $e) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors(trans('app.error_save'));
|
||||
}
|
||||
|
||||
// delete all current messages
|
||||
foreach ($conversation->messages as $message) {
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'conversation_id' => $conversation->id,
|
||||
'message_id' => $message->id,
|
||||
];
|
||||
app(DestroyMessage::class)->execute($data);
|
||||
}
|
||||
|
||||
// and create all new ones
|
||||
$result = $this->updateMessages($request, $conversation, $date);
|
||||
if ($result !== true) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($result);
|
||||
}
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.conversation_edit_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate datas and get an array for create or update a conversation.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array|\Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
private function validateAndGetDatas(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'conversationDateRadio' => 'required',
|
||||
'conversationDate' => 'required_unless:conversationDateRadio,today,yesterday',
|
||||
'messages' => 'required',
|
||||
'contactFieldTypeId' => 'required|integer|exists:contact_field_types,id',
|
||||
], [
|
||||
'messages.required' => trans('people.conversation_add_error'),
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $validator;
|
||||
}
|
||||
|
||||
// find out what the date is
|
||||
$chosenDate = $request->input('conversationDateRadio');
|
||||
if ($chosenDate == 'today') {
|
||||
$date = DateHelper::getDate(now($request->user()->timezone));
|
||||
} elseif ($chosenDate == 'yesterday') {
|
||||
$date = DateHelper::getDate(now($request->user()->timezone)->subDay());
|
||||
} else {
|
||||
$date = $request->input('conversationDate');
|
||||
}
|
||||
|
||||
return [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'happened_at' => $date,
|
||||
'contact_field_type_id' => $request->input('contactFieldTypeId'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update messages for conversation.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Conversation $conversation
|
||||
* @param string $date
|
||||
* @return bool|string|\Illuminate\Contracts\Validation\Validator
|
||||
* @psalm-return bool|array|string|\Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
private function updateMessages(Request $request, Conversation $conversation, string $date)
|
||||
{
|
||||
$messages = explode(',', $request->input('messages'));
|
||||
foreach ($messages as $messageId) {
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'conversation_id' => $conversation->id,
|
||||
'contact_id' => $conversation->contact_id,
|
||||
'written_at' => $date,
|
||||
'written_by_me' => ($request->input('who_wrote_'.$messageId) === 'me'),
|
||||
'content' => $request->input('content_'.$messageId),
|
||||
];
|
||||
|
||||
try {
|
||||
app(AddMessageToConversation::class)->execute($data);
|
||||
} catch (ValidationException $e) {
|
||||
return $e->validator;
|
||||
} catch (\Exception $e) {
|
||||
return trans('app.error_save');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the conversation.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Conversation $conversation
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Conversation $conversation)
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'conversation_id' => $conversation->id,
|
||||
];
|
||||
|
||||
try {
|
||||
app(DestroyConversation::class)->execute($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respondNotFound();
|
||||
}
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.conversation_delete_success'));
|
||||
}
|
||||
}
|
||||
139
app/Http/Controllers/Contacts/DebtController.php
Normal file
139
app/Http/Controllers/Contacts/DebtController.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Models\Contact\Debt;
|
||||
use App\Helpers\AccountHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\People\DebtRequest;
|
||||
|
||||
class DebtController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
return view('people.debt.index')
|
||||
->withContact($contact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function create(Contact $contact)
|
||||
{
|
||||
return view('people.debt.add')
|
||||
->withContact($contact)
|
||||
->withAccountHasLimitations(AccountHelper::hasLimitations(auth()->user()->account))
|
||||
->withDebt(new Debt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param DebtRequest $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(DebtRequest $request, Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$contact->debts()->create(
|
||||
$request->only([
|
||||
'in_debt',
|
||||
'amount',
|
||||
'reason',
|
||||
])
|
||||
+ [
|
||||
'account_id' => $contact->account_id,
|
||||
'status' => 'inprogress',
|
||||
]
|
||||
);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.debt_add_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Debt $debt
|
||||
* @return void
|
||||
*/
|
||||
public function show(Contact $contact, Debt $debt): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Debt $debt
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function edit(Contact $contact, Debt $debt)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
return view('people.debt.edit')
|
||||
->withContact($contact)
|
||||
->withAccountHasLimitations(AccountHelper::hasLimitations(auth()->user()->account))
|
||||
->withDebt($debt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param DebtRequest $request
|
||||
* @param Contact $contact
|
||||
* @param Debt $debt
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function update(DebtRequest $request, Contact $contact, Debt $debt)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$debt->update(
|
||||
$request->only([
|
||||
'in_debt',
|
||||
'amount',
|
||||
'reason',
|
||||
])
|
||||
+ [
|
||||
'account_id' => $contact->account_id,
|
||||
'status' => 'inprogress',
|
||||
]
|
||||
);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.debt_edit_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Debt $debt
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function destroy(Contact $contact, Debt $debt)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$debt->delete();
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.debt_delete_success'));
|
||||
}
|
||||
}
|
||||
85
app/Http/Controllers/Contacts/DocumentsController.php
Normal file
85
app/Http/Controllers/Contacts/DocumentsController.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Contact\Document;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Contact\Document\UploadDocument;
|
||||
use App\Services\Contact\Document\DestroyDocument;
|
||||
use App\Http\Resources\Document\Document as DocumentResource;
|
||||
|
||||
class DocumentsController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Instantiate a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('limitations')->only('store');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the list of documents.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$documents = $contact->documents()->get();
|
||||
|
||||
return DocumentResource::collection($documents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the document.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return Document
|
||||
*/
|
||||
public function store(Request $request, Contact $contact): Document
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
return app(UploadDocument::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'document' => $request->document,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the document.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Document $document
|
||||
* @return null|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Document $document): ?JsonResponse
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'document_id' => $document->id,
|
||||
];
|
||||
|
||||
try {
|
||||
if (app(DestroyDocument::class)->execute($data)) {
|
||||
return $this->respondObjectDeleted($document->id);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->respondNotFound();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
124
app/Http/Controllers/Contacts/GiftController.php
Normal file
124
app/Http/Controllers/Contacts/GiftController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Models\Contact\Gift;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Account\Photo;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Contact\Gift\CreateGift;
|
||||
use App\Services\Contact\Gift\UpdateGift;
|
||||
use App\Services\Contact\Gift\DestroyGift;
|
||||
use App\Http\Resources\Gift\Gift as GiftResource;
|
||||
use App\Services\Contact\Gift\AssociatePhotoToGift;
|
||||
|
||||
class GiftController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Get the list of gifts for the given contact.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$gifts = $contact->gifts()
|
||||
->orderBy('created_at', 'asc')
|
||||
->paginate();
|
||||
|
||||
return GiftResource::collection($gifts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the detail of a given gift.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Gift $gift
|
||||
* @return GiftResource|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function show(Request $request, Contact $contact, Gift $gift)
|
||||
{
|
||||
return new GiftResource($gift);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the gift.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return GiftResource|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
$gift = app(CreateGift::class)->execute(
|
||||
$request->except(['account_id', 'contact_id']) +
|
||||
[
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
]
|
||||
);
|
||||
|
||||
return new GiftResource($gift);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the gift.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Gift $gift
|
||||
* @return GiftResource|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function update(Request $request, Contact $contact, Gift $gift)
|
||||
{
|
||||
$gift = app(UpdateGift::class)->execute(
|
||||
$request->except(['account_id', 'contact_id', 'gift_id']) +
|
||||
[
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'gift_id' => $gift->id,
|
||||
]
|
||||
);
|
||||
|
||||
return new GiftResource($gift);
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a photo to the gift.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Gift $gift
|
||||
* @param Photo $photo
|
||||
* @return GiftResource|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function associate(Request $request, Contact $contact, Gift $gift, Photo $photo)
|
||||
{
|
||||
$gift = app(AssociatePhotoToGift::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'gift_id' => $gift->id,
|
||||
'photo_id' => $photo->id,
|
||||
]);
|
||||
|
||||
return new GiftResource($gift);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a gift.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Gift $gift
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Gift $gift)
|
||||
{
|
||||
app(DestroyGift::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'gift_id' => $gift->id,
|
||||
]);
|
||||
|
||||
return $this->respondObjectDeleted($gift->id);
|
||||
}
|
||||
}
|
||||
69
app/Http/Controllers/Contacts/IntroductionsController.php
Normal file
69
app/Http/Controllers/Contacts/IntroductionsController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Contact\Contact\UpdateContactIntroduction;
|
||||
use App\Http\Resources\Contact\ContactShort as ContactResource;
|
||||
|
||||
class IntroductionsController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function edit(Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$contacts = $contact->siblingContacts()
|
||||
->real()
|
||||
->active()
|
||||
->orderByUserPreference()
|
||||
->paginate(20);
|
||||
|
||||
$introducer = $contact->getIntroducer();
|
||||
if ($introducer !== null) {
|
||||
$introducer = new ContactResource($introducer);
|
||||
}
|
||||
|
||||
return view('people.introductions.edit')
|
||||
->withContact($contact)
|
||||
->withContacts(ContactResource::collection($contacts))
|
||||
->withIntroducer($introducer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function update(Request $request, Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$contact = app(UpdateContactIntroduction::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'met_through_contact_id' => $request->input('metThroughId'),
|
||||
'general_information' => $request->input('first_met_additional_info'),
|
||||
'is_date_known' => $request->input('is_first_met_date_known') == 'known',
|
||||
'day' => $request->input('first_met_day'),
|
||||
'month' => $request->input('first_met_month'),
|
||||
'year' => $request->input('first_met_year'),
|
||||
'add_reminder' => $request->addReminder == 'on',
|
||||
]);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.introductions_update_success'));
|
||||
}
|
||||
}
|
||||
138
app/Http/Controllers/Contacts/LifeEventsController.php
Normal file
138
app/Http/Controllers/Contacts/LifeEventsController.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Contact\LifeEvent;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Models\Contact\LifeEventCategory;
|
||||
use App\Services\Contact\LifeEvent\CreateLifeEvent;
|
||||
use App\Services\Contact\LifeEvent\DestroyLifeEvent;
|
||||
use App\Http\Resources\LifeEvent\LifeEventType as LifeEventTypeResource;
|
||||
use App\Http\Resources\LifeEvent\LifeEventCategory as LifeEventCategoryResource;
|
||||
|
||||
class LifeEventsController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Get the list of life event categories.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*/
|
||||
public function categories(Request $request)
|
||||
{
|
||||
$lifeEventCategories = auth()->user()->account->lifeEventCategories;
|
||||
|
||||
return LifeEventCategoryResource::collection($lifeEventCategories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of life event types for a given life event category.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $lifeEventCategoryId
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*/
|
||||
public function types(Request $request, int $lifeEventCategoryId)
|
||||
{
|
||||
$lifeEventCategory = LifeEventCategory::findOrFail($lifeEventCategoryId);
|
||||
$lifeEventTypes = $lifeEventCategory->lifeEventTypes;
|
||||
|
||||
return LifeEventTypeResource::collection($lifeEventTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the list of life events.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return Collection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$lifeEventsCollection = collect([]);
|
||||
$lifeEvents = $contact->lifeEvents()->get();
|
||||
|
||||
foreach ($lifeEvents as $lifeEvent) {
|
||||
$data = [
|
||||
'id' => $lifeEvent->id,
|
||||
'life_event_type' => $lifeEvent->lifeEventType->name,
|
||||
'default_life_event_type_key' => $lifeEvent->lifeEventType->default_life_event_type_key,
|
||||
'life_event_type_name' => $lifeEvent->lifeEventType->name,
|
||||
'name' => $lifeEvent->name,
|
||||
'note' => $lifeEvent->note,
|
||||
'happened_at' => DateHelper::getShortDate($lifeEvent->happened_at),
|
||||
];
|
||||
$lifeEventsCollection->push($data);
|
||||
}
|
||||
|
||||
return $lifeEventsCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the life event.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return LifeEvent|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'life_event_type_id' => $request->input('life_event_type_id'),
|
||||
'happened_at' => $request->input('happened_at'),
|
||||
'name' => $request->input('name'),
|
||||
'note' => $request->input('note'),
|
||||
'has_reminder' => $request->input('has_reminder'),
|
||||
'happened_at_month_unknown' => $request->input('happened_at_month_unknown'),
|
||||
'happened_at_day_unknown' => $request->input('happened_at_day_unknown'),
|
||||
];
|
||||
|
||||
// create the conversation
|
||||
try {
|
||||
$lifeEvent = app(CreateLifeEvent::class)->execute($data);
|
||||
} catch (\Exception $e) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors(trans('app.error_save'));
|
||||
}
|
||||
|
||||
return $lifeEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the life event.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param LifeEvent $lifeEvent
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function destroy(Request $request, LifeEvent $lifeEvent)
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'life_event_id' => $lifeEvent->id,
|
||||
];
|
||||
|
||||
try {
|
||||
app(DestroyLifeEvent::class)->execute($data);
|
||||
} catch (\Exception $e) {
|
||||
// We have to redirect with HTTP status 303 or the browser will issue a
|
||||
// DELETE request to the new location. This may result in deleting other
|
||||
// resources as well. Refer to Github issue #2415
|
||||
return back(303)
|
||||
->withInput()
|
||||
->withErrors(trans('app.error_save'));
|
||||
}
|
||||
|
||||
return $this->respondObjectDeleted($lifeEvent->id);
|
||||
}
|
||||
}
|
||||
101
app/Http/Controllers/Contacts/NotesController.php
Normal file
101
app/Http/Controllers/Contacts/NotesController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Models\Contact\Note;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\People\NotesRequest;
|
||||
use App\Http\Requests\People\NoteToggleRequest;
|
||||
|
||||
class NotesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get all the tasks of this contact.
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
$notesCollection = collect([]);
|
||||
$notes = $contact->notes()->latest()->get();
|
||||
|
||||
foreach ($notes as $note) {
|
||||
$data = [
|
||||
'id' => $note->id,
|
||||
'body' => $note->body,
|
||||
'is_favorited' => $note->is_favorited,
|
||||
'favorited_at' => $note->favorited_at,
|
||||
'favorited_at_short' => $note->favorited_at ? DateHelper::getShortDate($note->favorited_at) : null,
|
||||
'created_at' => $note->created_at,
|
||||
'created_at_short' => DateHelper::getShortDate($note->created_at),
|
||||
'edit' => false,
|
||||
];
|
||||
$notesCollection->push($data);
|
||||
}
|
||||
|
||||
return $notesCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the task.
|
||||
*/
|
||||
public function store(NotesRequest $request, Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
return $contact->notes()->create([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'body' => $request->input('body'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function toggle(NoteToggleRequest $request, Contact $contact, Note $note)
|
||||
{
|
||||
// check if the state of the note has changed
|
||||
if ($note->is_favorited) {
|
||||
$note->favorited_at = null;
|
||||
$note->is_favorited = false;
|
||||
} else {
|
||||
$note->is_favorited = true;
|
||||
$note->favorited_at = now();
|
||||
}
|
||||
|
||||
$note->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param NotesRequest $request
|
||||
* @param Contact $contact
|
||||
* @param Note $note
|
||||
* @return Note
|
||||
*/
|
||||
public function update(NotesRequest $request, Contact $contact, Note $note): Note
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$note->update(
|
||||
$request->only([
|
||||
'body',
|
||||
])
|
||||
+ ['account_id' => $contact->account_id]
|
||||
);
|
||||
|
||||
return $note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Note $note
|
||||
* @return void
|
||||
*/
|
||||
public function destroy(Contact $contact, Note $note): void
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$note->delete();
|
||||
}
|
||||
}
|
||||
114
app/Http/Controllers/Contacts/PetsController.php
Normal file
114
app/Http/Controllers/Contacts/PetsController.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Models\Contact\Pet;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Contact\PetCategory;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\People\PetsRequest;
|
||||
|
||||
class PetsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get all the pet categories.
|
||||
*/
|
||||
public function getPetCategories()
|
||||
{
|
||||
$petCategoriesData = collect([]);
|
||||
|
||||
$petCategories = PetCategory::all();
|
||||
|
||||
foreach ($petCategories as $petCategory) {
|
||||
$data = [
|
||||
'id' => $petCategory->id,
|
||||
'name' => $petCategory->name,
|
||||
'edit' => false,
|
||||
];
|
||||
$petCategoriesData->push($data);
|
||||
}
|
||||
|
||||
return $petCategoriesData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the pets for this contact.
|
||||
*
|
||||
* @param Contact $contact
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
$petsCollection = collect([]);
|
||||
$pets = $contact->pets;
|
||||
|
||||
foreach ($pets as $pet) {
|
||||
$data = [
|
||||
'id' => $pet->id,
|
||||
'name' => $pet->name,
|
||||
'pet_category_id' => $pet->pet_category_id,
|
||||
'category_name' => $pet->petCategory->name,
|
||||
'edit' => false,
|
||||
];
|
||||
$petsCollection->push($data);
|
||||
}
|
||||
|
||||
return $petsCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the pet.
|
||||
*/
|
||||
public function store(PetsRequest $request, Contact $contact)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$pet = $contact->pets()->create(
|
||||
$request->only([
|
||||
'pet_category_id',
|
||||
'name',
|
||||
])
|
||||
+ [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
]
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => $pet->id,
|
||||
'name' => $pet->name,
|
||||
'pet_category_id' => $pet->pet_category_id,
|
||||
'category_name' => $pet->petCategory->name,
|
||||
'edit' => false,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the pet.
|
||||
*/
|
||||
public function update(PetsRequest $request, Contact $contact, Pet $pet)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$pet->update(
|
||||
$request->only([
|
||||
'pet_category_id',
|
||||
'name',
|
||||
])
|
||||
+ [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
]
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => $pet->id,
|
||||
'name' => $pet->name,
|
||||
'pet_category_id' => $pet->pet_category_id,
|
||||
'category_name' => $pet->petCategory->name,
|
||||
'edit' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function destroy(Contact $contact, Pet $pet)
|
||||
{
|
||||
$pet->delete();
|
||||
}
|
||||
}
|
||||
84
app/Http/Controllers/Contacts/PhotosController.php
Normal file
84
app/Http/Controllers/Contacts/PhotosController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Account\Photo;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\JsonRespondController;
|
||||
use App\Services\Account\Photo\UploadPhoto;
|
||||
use App\Services\Account\Photo\DestroyPhoto;
|
||||
use App\Services\Contact\Avatar\UpdateAvatar;
|
||||
use App\Http\Resources\Photo\Photo as PhotoResource;
|
||||
|
||||
class PhotosController extends Controller
|
||||
{
|
||||
use JsonRespondController;
|
||||
|
||||
/**
|
||||
* Display the list of photos.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
*/
|
||||
public function index(Request $request, Contact $contact)
|
||||
{
|
||||
$photos = $contact->photos()->orderBy('created_at', 'desc')->get();
|
||||
|
||||
return PhotoResource::collection($photos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the Photo.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return PhotoResource
|
||||
*/
|
||||
public function store(Request $request, Contact $contact): PhotoResource
|
||||
{
|
||||
$photo = app(UploadPhoto::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'photo' => $request->photo,
|
||||
]);
|
||||
|
||||
return new PhotoResource($photo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the Photo.
|
||||
* Also, if this photo was the current avatar of the contact, change the
|
||||
* avatar to the default one.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Photo $photo
|
||||
* @return null|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Photo $photo)
|
||||
{
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'photo_id' => $photo->id,
|
||||
];
|
||||
|
||||
try {
|
||||
app(DestroyPhoto::class)->execute($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respondNotFound();
|
||||
}
|
||||
|
||||
if ($contact->avatar_source == 'photo'
|
||||
&& $contact->avatar_photo_id == $photo->id) {
|
||||
app(UpdateAvatar::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'source' => 'adorable',
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->respondObjectDeleted($photo->id);
|
||||
}
|
||||
}
|
||||
270
app/Http/Controllers/Contacts/RelationshipsController.php
Normal file
270
app/Http/Controllers/Contacts/RelationshipsController.php
Normal file
@@ -0,0 +1,270 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\View\View;
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Helpers\FormHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helpers\GenderHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use App\Models\Relationship\Relationship;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Services\Contact\Contact\CreateContact;
|
||||
use App\Services\Contact\Contact\UpdateContact;
|
||||
use App\Services\Contact\Relationship\CreateRelationship;
|
||||
use App\Services\Contact\Relationship\UpdateRelationship;
|
||||
use App\Services\Contact\Relationship\DestroyRelationship;
|
||||
use App\Http\Resources\Contact\ContactShort as ContactResource;
|
||||
|
||||
class RelationshipsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the Create relationship page.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return View
|
||||
*/
|
||||
public function create(Request $request, Contact $contact)
|
||||
{
|
||||
$existingContacts = Contact::search('', Auth::user()->account_id, 'updated_at')
|
||||
->whereNotIn('id', [$contact->id])
|
||||
->paginate(20);
|
||||
|
||||
return view('people.relationship.new')
|
||||
->withContact($contact)
|
||||
->withPartner(new Contact)
|
||||
->withGenders(GenderHelper::getGendersInput())
|
||||
->withRelationshipTypes($this->getRelationshipTypesList($contact))
|
||||
->withDefaultGender(auth()->user()->account->default_gender_id)
|
||||
->withDays(DateHelper::getListOfDays())
|
||||
->withMonths(DateHelper::getListOfMonths())
|
||||
->withBirthdate(now(DateHelper::getTimezone())->toDateString())
|
||||
->withExistingContacts(ContactResource::collection($existingContacts))
|
||||
->withType($request->input('type'))
|
||||
->withFormNameOrder(FormHelper::getNameOrderForForms(auth()->user()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
// case of linking to an existing contact
|
||||
if ($request->input('relationship_type') == 'existing') {
|
||||
$partnerId = $request->input('existing_contact_id');
|
||||
} else {
|
||||
|
||||
// case of creating a new contact
|
||||
$datas = $this->validateAndGetDatas($request);
|
||||
|
||||
if ($datas instanceof \Illuminate\Contracts\Validation\Validator) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($datas);
|
||||
}
|
||||
|
||||
$partner = app(CreateContact::class)->execute($datas);
|
||||
$partnerId = $partner->id;
|
||||
}
|
||||
|
||||
app(CreateRelationship::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_is' => $contact->id,
|
||||
'of_contact' => $partnerId,
|
||||
'relationship_type_id' => $request->input('relationship_type_id'),
|
||||
]);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.relationship_form_add_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Relationship $relationship
|
||||
* @return View
|
||||
*/
|
||||
public function edit(Contact $contact, Relationship $relationship)
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$otherContact = $relationship->ofContact;
|
||||
|
||||
$now = now();
|
||||
$age = (string) (! is_null($otherContact->birthdate) ? $otherContact->birthdate->getAge() : 0);
|
||||
$birthdate = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->toDateString() : $now->toDateString();
|
||||
$day = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->day : $now->day;
|
||||
$month = ! is_null($otherContact->birthdate) ? $otherContact->birthdate->date->month : $now->month;
|
||||
|
||||
$hasBirthdayReminder = is_null($otherContact->birthday_reminder_id) ? 0 : 1;
|
||||
|
||||
return view('people.relationship.edit')
|
||||
->withContact($contact)
|
||||
->withPartner($otherContact)
|
||||
->withGenders(auth()->user()->account->genders)
|
||||
->withRelationshipTypes($this->getRelationshipTypesList($contact))
|
||||
->withDays(DateHelper::getListOfDays())
|
||||
->withMonths(DateHelper::getListOfMonths())
|
||||
->withBirthdate($birthdate)
|
||||
->withRelationshipId($relationship->id)
|
||||
->withType($relationship->relationship_type_id)
|
||||
->withBirthdayState($otherContact->getBirthdayState())
|
||||
->withDay($day)
|
||||
->withMonth($month)
|
||||
->withAge($age)
|
||||
->withGenders(GenderHelper::getGendersInput())
|
||||
->withHasBirthdayReminder($hasBirthdayReminder)
|
||||
->withFormNameOrder(FormHelper::getNameOrderForForms(auth()->user()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Relationship $relationship
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function update(Request $request, Contact $contact, Relationship $relationship)
|
||||
{
|
||||
$otherContact = $relationship->ofContact;
|
||||
|
||||
if ($otherContact->is_partial) {
|
||||
$datas = $this->validateAndGetDatas($request);
|
||||
|
||||
if ($datas instanceof \Illuminate\Contracts\Validation\Validator) {
|
||||
return back()
|
||||
->withInput()
|
||||
->withErrors($datas);
|
||||
}
|
||||
|
||||
app(UpdateContact::class)->execute($datas + [
|
||||
'contact_id' => $otherContact->id,
|
||||
'author_id' => auth()->user()->id,
|
||||
]);
|
||||
}
|
||||
|
||||
// update the relationship
|
||||
app(UpdateRelationship::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'relationship_id' => $relationship->id,
|
||||
'relationship_type_id' => $request->input('relationship_type_id'),
|
||||
]);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.relationship_form_add_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate datas and get an array for create or update a contact.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array|\Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
private function validateAndGetDatas(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'first_name' => 'required|max:255',
|
||||
'last_name' => 'max:255',
|
||||
'gender_id' => 'nullable|integer',
|
||||
'birthdayDate' => 'date_format:Y-m-d',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $validator;
|
||||
}
|
||||
|
||||
// this is really ugly. it should be changed
|
||||
if ($request->input('birthdate') == 'exact') {
|
||||
$birthdate = $request->input('birthdayDate');
|
||||
$birthdate = DateHelper::parseDate($birthdate);
|
||||
$day = $birthdate->day;
|
||||
$month = $birthdate->month;
|
||||
$year = $birthdate->year;
|
||||
} else {
|
||||
$day = $request->input('day');
|
||||
$month = $request->input('month');
|
||||
$year = $request->input('year');
|
||||
}
|
||||
|
||||
return [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'author_id' => auth()->user()->id,
|
||||
'first_name' => $request->input('first_name'),
|
||||
'last_name' => $request->input('last_name'),
|
||||
'gender_id' => $request->input('gender_id'),
|
||||
'is_birthdate_known' => ! empty($request->input('birthdate')) && $request->input('birthdate') !== 'unknown',
|
||||
'birthdate_day' => $day,
|
||||
'birthdate_month' => $month,
|
||||
'birthdate_year' => $year,
|
||||
'birthdate_is_age_based' => $request->input('birthdate') === 'approximate',
|
||||
'birthdate_age' => $request->input('age'),
|
||||
'birthdate_add_reminder' => ! empty($request->input('addReminder')),
|
||||
'is_partial' => ! $request->input('realContact'),
|
||||
'is_deceased' => false,
|
||||
'is_deceased_date_known' => false,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Relationship $relationship
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function destroy(Contact $contact, Relationship $relationship)
|
||||
{
|
||||
if ($contact->account_id != auth()->user()->account_id) {
|
||||
return redirect()->route('people.index');
|
||||
}
|
||||
|
||||
if ($relationship->account_id != auth()->user()->account_id) {
|
||||
return redirect()->route('people.index');
|
||||
}
|
||||
|
||||
app(DestroyRelationship::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'relationship_id' => $relationship->id,
|
||||
]);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.relationship_form_deletion_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Building the list of relationship types specifically for the dropdown which asks
|
||||
* for an id and a name.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function getRelationshipTypesList(Contact $contact)
|
||||
{
|
||||
$relationshipTypes = collect();
|
||||
foreach (auth()->user()->account->relationshipTypes as $relationshipType) {
|
||||
$types = $relationshipTypes->get($relationshipType->relationshipTypeGroup->name, [
|
||||
'name' => trans('app.relationship_type_group_'.$relationshipType->relationshipTypeGroup->name),
|
||||
'options' => [],
|
||||
]);
|
||||
|
||||
$types['options'][] = [
|
||||
'id' => $relationshipType->id,
|
||||
'name' => $relationshipType->getLocalizedName($contact, true),
|
||||
];
|
||||
|
||||
$relationshipTypes->put($relationshipType->relationshipTypeGroup->name, $types);
|
||||
}
|
||||
|
||||
return $relationshipTypes;
|
||||
}
|
||||
}
|
||||
127
app/Http/Controllers/Contacts/RemindersController.php
Normal file
127
app/Http/Controllers/Contacts/RemindersController.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helpers\AccountHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Contact\Reminder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Contact\Reminder\CreateReminder;
|
||||
use App\Services\Contact\Reminder\UpdateReminder;
|
||||
use App\Services\Contact\Reminder\DestroyReminder;
|
||||
|
||||
class RemindersController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the form for creating a new reminder.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function create(Contact $contact)
|
||||
{
|
||||
return view('people.reminders.add')
|
||||
->withContact($contact)
|
||||
->withAccountHasLimitations(AccountHelper::hasLimitations(auth()->user()->account))
|
||||
->withReminder(new Reminder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a reminder.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, Contact $contact)
|
||||
{
|
||||
$frequency_type = $request->input('frequency_type');
|
||||
if ($frequency_type === 'recurrent') {
|
||||
$frequency_type = $request->input('frequency_number_select');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'initial_date' => $request->input('initial_date'),
|
||||
'frequency_type' => $frequency_type,
|
||||
'frequency_number' => is_null($request->input('frequency_number')) ? 1 : $request->input('frequency_number'),
|
||||
'title' => $request->input('title'),
|
||||
'description' => $request->input('description'),
|
||||
];
|
||||
|
||||
app(CreateReminder::class)->execute($data);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.reminders_create_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Contact $contact
|
||||
* @param Reminder $reminder
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function edit(Contact $contact, Reminder $reminder)
|
||||
{
|
||||
return view('people.reminders.edit')
|
||||
->withContact($contact)
|
||||
->withAccountHasLimitations(AccountHelper::hasLimitations(auth()->user()->account))
|
||||
->withReminder($reminder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the reminder.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Reminder $reminder
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function update(Request $request, Contact $contact, Reminder $reminder)
|
||||
{
|
||||
$frequency_type = $request->input('frequency_type');
|
||||
if ($frequency_type === 'recurrent') {
|
||||
$frequency_type = $request->input('frequency_number_select');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'reminder_id' => $reminder->id,
|
||||
'initial_date' => $request->input('initial_date'),
|
||||
'frequency_type' => $frequency_type,
|
||||
'frequency_number' => is_null($request->input('frequency_number')) ? 1 : $request->input('frequency_number'),
|
||||
'title' => $request->input('title'),
|
||||
'description' => $request->input('description'),
|
||||
];
|
||||
|
||||
app(UpdateReminder::class)->execute($data);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.reminders_update_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the reminder.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @param Reminder $reminder
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function destroy(Request $request, Contact $contact, Reminder $reminder)
|
||||
{
|
||||
$data = [
|
||||
'account_id' => $reminder->account_id,
|
||||
'reminder_id' => $reminder->id,
|
||||
];
|
||||
|
||||
app(DestroyReminder::class)->execute($data);
|
||||
|
||||
return redirect()->route('people.show', $contact)
|
||||
->with('success', trans('people.reminders_delete_success'));
|
||||
}
|
||||
}
|
||||
74
app/Http/Controllers/Contacts/TagsController.php
Normal file
74
app/Http/Controllers/Contacts/TagsController.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Contact\Tag\DetachTag;
|
||||
use App\Services\Contact\Tag\AssociateTag;
|
||||
use App\Http\Resources\Tag\Tag as TagResource;
|
||||
|
||||
class TagsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get the list of all the tags in the account.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$tags = auth()->user()->account->tags()->get();
|
||||
|
||||
return TagResource::collection($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of all the tags for this contact.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
*/
|
||||
public function get(Request $request, Contact $contact)
|
||||
{
|
||||
$tags = $contact->tags()->get();
|
||||
|
||||
return TagResource::collection($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Contact $contact
|
||||
* @return void
|
||||
*/
|
||||
public function update(Request $request, Contact $contact): void
|
||||
{
|
||||
$contact->throwInactive();
|
||||
|
||||
$tags = $request->all();
|
||||
|
||||
// detaching all the tags
|
||||
$contactTags = $contact->tags()->get();
|
||||
foreach ($contactTags as $tag) {
|
||||
app(DetachTag::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'tag_id' => $tag->id,
|
||||
]);
|
||||
}
|
||||
|
||||
// attach all the new/updated tags
|
||||
foreach ($tags as $tag) {
|
||||
if (! empty($tag['name'])) {
|
||||
app(AssociateTag::class)->execute([
|
||||
'account_id' => auth()->user()->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'name' => $tag['name'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
app/Http/Controllers/Contacts/TasksController.php
Normal file
32
app/Http/Controllers/Contacts/TasksController.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Contacts;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class TasksController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get all the tasks of this contact.
|
||||
*/
|
||||
public function index(Contact $contact)
|
||||
{
|
||||
$tasks = collect([]);
|
||||
|
||||
foreach ($contact->tasks as $task) {
|
||||
$data = [
|
||||
'id' => $task->id,
|
||||
'title' => $task->title,
|
||||
'description' => $task->description,
|
||||
'completed' => $task->completed,
|
||||
'completed_at' => ($task->completed_at) ? DateHelper::getShortDate($task->completed_at) : null,
|
||||
'edit' => false,
|
||||
];
|
||||
$tasks->push($data);
|
||||
}
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user