fixed all recurring issues #315

This commit is contained in:
denisdulici
2018-05-02 00:59:55 +03:00
parent 7e21634d77
commit c049b8ce73
7 changed files with 110 additions and 41 deletions

View File

@ -2,6 +2,12 @@
namespace App\Traits;
use DateTime;
use DateTimeZone;
use Recurr\Rule;
use Recurr\Transformer\ArrayTransformer;
use Recurr\Transformer\ArrayTransformerConfig;
trait Recurring
{
@ -55,4 +61,92 @@ trait Recurring
'count' => (int) $request['recurring_count'],
]);
}
public function current()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->current()->getStart();
}
public function next()
{
if (!$schedule = $this->schedule()) {
return false;
}
if (!$next = $schedule->next()) {
return false;
}
return $next->getStart();
}
public function first()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->first()->getStart();
}
public function last()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->last()->getStart();
}
public function schedule()
{
$config = new ArrayTransformerConfig();
$config->enableLastDayOfMonthFix();
$transformer = new ArrayTransformer();
$transformer->setConfig($config);
return $transformer->transform($this->getRule());
}
public function getRule()
{
$rule = (new Rule())
->setStartDate($this->getRuleStartDate())
->setTimezone($this->getRuleTimeZone())
->setFreq($this->getRuleFrequency())
->setInterval($this->interval);
// 0 means infinite
if ($this->count != 0) {
$rule->setCount($this->getRuleCount());
}
return $rule;
}
public function getRuleStartDate()
{
return new DateTime($this->started_at, new DateTimeZone($this->getRuleTimeZone()));
}
public function getRuleTimeZone()
{
return setting('general.timezone');
}
public function getRuleCount()
{
// Fix for humans
return $this->count + 1;
}
public function getRuleFrequency()
{
return strtoupper($this->frequency);
}
}