Files
monica/app/Reminder.php
T
Yamamoto KadirandRégis Freyd 50005544ee Fix some relationship inconsistencies and reduce dashboard queries (#209)
This normalizes all relationships and adds relations to models to take advantage of laravel's eagerloading and reduce queries to the database.

This might go against the 'add any kind of complexities whatsoever' rule in the contribution guide, but I think adding relations would eventually simplify the code base. The amount of queries just on the dashboard fell from ~70 to 26. There's also a lot of boilerplate code in the Contact model to retrieve relationships and to maintain accurate count of them (fields like number_of_*). They can be greatly simplified with the addition of relations.

One other thing in the Contact model was the definition of the Contact-User relation. This definition uses the account_id field in the contacts table and and relates it to id in users. I'm not sure if this is the intention, since the id from accounts table is used when assigning account_id for a contact. This seems to have worked so far, because both a user and its associated account are created at the same time during registration and their ids match, but if there was any difference, this relationship would relate wrong contacts and users. I changed the relationship name to account. This makes the path to a user from a contact is $contact->account->user right now.
2017-06-11 23:01:23 -04:00

104 lines
2.7 KiB
PHP

<?php
namespace App;
use Auth;
use Carbon\Carbon;
use App\Helpers\DateHelper;
use Illuminate\Database\Eloquent\Model;
use MartinJoiner\OrdinalNumber\OrdinalNumber;
class Reminder extends Model
{
protected $dates = ['last_triggered', 'next_expected_date'];
/**
* Get the account record associated with the reminder.
*/
public function account()
{
return $this->belongsTo('App\Account');
}
/**
* Get the contact record associated with the reminder.
*/
public function contact()
{
return $this->belongsTo('App\Contact');
}
/**
* Get the title of a reminder.
* @return string
*/
public function getTitle()
{
if (is_null($this->title)) {
return null;
}
return $this->title;
}
/**
* Get the description of a reminder.
* @return string
*/
public function getDescription()
{
if (is_null($this->description)) {
return null;
}
return $this->description;
}
/**
* Return the next expected date.
*
* @return string
*/
public function getNextExpectedDate()
{
return $this->next_expected_date->format('Y-m-d');
}
/**
* Calculate the next expected date for this reminder based on the current
* start date.
* @param Carbon $startDate
* @param string $frequencyTYpe
* @param int $frequencyNumber
* @return void
*/
public function calculateNextExpectedDate($startDate, $frequencyType, $frequencyNumber)
{
if ($startDate->isToday()) {
$nextDate = DateHelper::calculateNextOccuringDate($startDate, $frequencyType, $frequencyNumber);
$this->next_expected_date = $nextDate;
}
if ($startDate >= $startDate->tomorrow()) {
$this->next_expected_date = $startDate;
} else {
// Date is in the past, we need to extract the month and day, and
// setup the next occurence at those dates.
$nextDate = DateHelper::calculateNextOccuringDate($startDate, $frequencyType, $frequencyNumber);
while ($nextDate->isPast()) {
$nextDate = DateHelper::calculateNextOccuringDate($nextDate, $frequencyType, $frequencyNumber);
}
// This is the case where we set the date in the past, but the next
// occuring date is still today, so we make sure it skips to the
// next occuring date.
if ($startDate->isToday()) {
$nextDate = DateHelper::calculateNextOccuringDate($startDate, $frequencyType, $frequencyNumber);
}
$this->next_expected_date = $nextDate;
}
}
}