/**
  * {@inheritdoc}
  */
 public function buildRow(Schedule $entity)
 {
     $row['label'] = $entity->label();
     $row['enabled'] = $entity->get('enabled') ? $this->t('Yes') : $this->t('No');
     $row['period'] = $entity->getPeriodFormatted();
     $row['last_run'] = $this->t('Never');
     if ($last_run = $entity->getLastRun()) {
         $row['last_run'] = \Drupal::service('date.formatter')->format($last_run, 'small');
         $row['last_run'] .= ' (' . $this->t('@time ago', array('@time' => \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $last_run))) . ')';
     }
     $row['next_run'] = $this->t('Not Scheduled');
     if (!$entity->get('enabled')) {
         $row['next_run'] = $this->t('Disabled');
     } else {
         if ($next_run = $entity->getNextRun()) {
             $interval = \Drupal::service('date.formatter')->formatInterval(abs($next_run - REQUEST_TIME));
             if ($next_run > REQUEST_TIME) {
                 $row['next_run'] = \Drupal::service('date.formatter')->format($next_run, 'small');
                 $row['next_run'] .= ' (' . $this->t('in @time', array('@time' => $interval)) . ')';
             } else {
                 $row['next_run'] = $this->t('Next cron run');
                 if ($last_run) {
                     $row['next_run'] .= ' (' . $this->t('was due @time ago', array('@time' => $interval)) . ')';
                 }
             }
         }
     }
     $row['keep'] = \Drupal::translation()->formatPlural($entity->get('keep'), 'Last 1 backup', 'Last @count backups');
     return $row + parent::buildRow($entity);
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function buildEntity(array $form, FormStateInterface $form_state)
 {
     // Save period.
     $type = Schedule::getPeriodType($form_state->getValue('period_type'));
     $seconds = Schedule::periodToSeconds(['number' => $form_state->getValue('period_number'), 'type' => $type]);
     $form_state->setValue('period', $seconds);
     return parent::buildEntity($form, $form_state);
 }
Beispiel #3
0
 /**
  * Get a backup period type given it's key.
  *
  * @param string $type
  * @return array
  */
 public static function getPeriodType($type)
 {
     return Schedule::getPeriodTypes()[$type];
 }
Beispiel #4
0
 /**
  * Convert a number of of seconds into a period array.
  *
  * @param int $seconds
  * @return array An array containing the period definition and the number of them.
  *  ['units' => 123, 'type' => [...]]
  * @throws \BackupMigrate\Core\Exception\BackupMigrateException
  */
 protected static function secondsToPeriod($seconds)
 {
     foreach (array_reverse(Schedule::getPeriodTypes()) as $type) {
         if ($seconds % $type['seconds'] === 0) {
             return ['units' => $seconds / $type['seconds'], 'type' => $type];
         }
     }
     throw new BackupMigrateException('Invalid period.');
 }