FixedReferenceDateInterval.php in Recurring Time Period 8
File
src/Plugin/RecurringPeriod/FixedReferenceDateInterval.php
View source
<?php
namespace Drupal\recurring_period\Plugin\RecurringPeriod;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class FixedReferenceDateInterval extends RecurringPeriodBase {
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'reference_date' => '',
'interval' => [
'period' => '',
'interval' => '',
],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$form['reference_date'] = [
'#type' => 'date',
'#title' => $this
->t('Reference date'),
'#default_value' => $config['reference_date'],
'#required' => TRUE,
];
$form['interval'] = [
'#type' => 'interval',
'#title' => 'Interval',
'#default_value' => $config['interval'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['reference_date'] = $values['reference_date'];
$this->configuration['interval'] = $values['interval'];
}
public function calculateDate(\DateTimeImmutable $start) {
return $this
->calculateEnd($start);
}
public function calculateStart(\DateTimeImmutable $date) {
$config = $this
->getConfiguration();
$interval = $this
->getDateInterval();
$reference_date = new \DateTimeImmutable($config['reference_date'], $date
->getTimezone());
$start_date = $reference_date;
$is_reference_date_in_future = $start_date
->diff($date)->invert;
if ($is_reference_date_in_future) {
while ($start_date
->diff($date)->invert == TRUE) {
$start_date = $start_date
->sub($interval);
}
}
else {
while ($start_date
->diff($date)->invert == FALSE) {
$start_date = $start_date
->add($interval);
}
$start_date = $start_date
->sub($interval);
}
return $start_date;
}
public function calculateEnd(\DateTimeImmutable $start) {
$config = $this
->getConfiguration();
$date_interval = $this
->getDateInterval();
return $this
->findNextAppropriateDate($start, $config['reference_date'], $date_interval);
}
protected function getDateInterval() {
$config = $this
->getConfiguration();
$interval_configuration = $config['interval'];
$interval_plugin_id = $interval_configuration['period'];
$interval_plugin_definition = $this->pluginManagerIntervals
->getDefinition($interval_plugin_id);
$value = $interval_configuration['interval'] * $interval_plugin_definition['multiplier'];
$date_interval = \DateInterval::createFromDateString($value . ' ' . $interval_plugin_definition['php']);
return $date_interval;
}
protected function findNextAppropriateDate(\DateTimeImmutable $start_date, $reference_date_string, \DateInterval $interval) {
$reference_date = new \DateTimeImmutable($reference_date_string, $start_date
->getTimezone());
$is_reference_date_in_future = $reference_date
->diff($start_date)->invert;
if ($is_reference_date_in_future) {
while ($reference_date
->diff($start_date)->invert == TRUE) {
$reference_date = $reference_date
->sub($interval);
}
$reference_date = $reference_date
->add($interval);
}
else {
while ($reference_date
->diff($start_date)->invert == FALSE) {
$reference_date = $reference_date
->add($interval);
}
}
return $reference_date;
}
}