67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Contacts;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Contact\Contact;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\ActivityStatisticService;
|
|
|
|
class ActivitiesController extends Controller
|
|
{
|
|
/**
|
|
* Statistics about an activity.
|
|
*
|
|
* @var ActivityStatisticService
|
|
*/
|
|
protected $activityStatisticService;
|
|
|
|
public function __construct(ActivityStatisticService $service)
|
|
{
|
|
$this->activityStatisticService = $service;
|
|
}
|
|
|
|
/**
|
|
* Get all the activities for this contact.
|
|
*/
|
|
public function index(Contact $contact)
|
|
{
|
|
// get the year of the most recent activity done with the contact
|
|
$year = $contact->activities->sortByDesc('date_it_happened')->first()->date_it_happened->year;
|
|
|
|
return redirect()->route('people.activities.year', [$contact, $year]);
|
|
}
|
|
|
|
/**
|
|
* Get all the activities for this contact for a specific year.
|
|
*/
|
|
public function year(Request $request, Contact $contact, int $year)
|
|
{
|
|
$startDate = Carbon::create($year, 1, 1);
|
|
$endDate = Carbon::create($year, 12, 31);
|
|
|
|
$activitiesLastTwelveMonths = $this->activityStatisticService
|
|
->activitiesWithContactInTimeRange($contact, Carbon::now()->subMonths(12), Carbon::now())
|
|
->count();
|
|
|
|
$uniqueActivityTypes = $this->activityStatisticService
|
|
->uniqueActivityTypesInTimeRange($contact, $startDate, $endDate);
|
|
|
|
$activitiesPerYear = $this->activityStatisticService->activitiesPerYearWithContact($contact);
|
|
|
|
$activitiesPerMonthForYear = $this->activityStatisticService
|
|
->activitiesPerMonthForYear($contact, $year)
|
|
->sortByDesc('month');
|
|
|
|
return view('people.activities.index')
|
|
->withTotalActivities($contact->activities->count())
|
|
->withActivitiesLastTwelveMonths($activitiesLastTwelveMonths)
|
|
->withUniqueActivityTypes($uniqueActivityTypes)
|
|
->withActivitiesPerYear($activitiesPerYear)
|
|
->withActivitiesPerMonthForYear($activitiesPerMonthForYear)
|
|
->withYear($year)
|
|
->withContact($contact);
|
|
}
|
|
}
|