dependencies[] = 'dateTime'; $this->dependencies[] = 'config'; } protected function getConfig() { return $this->getInjection('config'); } protected function getDateTime() { return $this->getInjection('dateTime'); } protected function convertDateTimeToDefaultTimezone($string) { $dateTime = \DateTime::createFromFormat($this->getDateTime()->getInternalDateTimeFormat(), $string); $timeZone = $this->getConfig()->get('timeZone'); if (empty($timeZone)) { $timeZone = 'UTC'; } $tz = $timezone = new \DateTimeZone($timeZone); if ($dateTime) { return $dateTime->setTimezone($tz)->format($this->getDateTime()->getInternalDateTimeFormat()); } return null; } protected function beforeSave(Entity $entity, array $options) { parent::beforeSave($entity, $options); if ($entity->isFieldChanged('status')) { if ($entity->get('status') == 'Completed') { $entity->set('dateCompleted', date('Y-m-d H:i:s')); } else { $entity->set('dateCompleted', null); } } if ($entity->has('dateStartDate')) { $dateStartDate = $entity->get('dateStartDate'); if (!empty($dateStartDate)) { $dateStart = $dateStartDate . ' 00:00:00'; $dateStart = $this->convertDateTimeToDefaultTimezone($dateStart); $entity->set('dateStart', $dateStart); } else { $entity->set('dateStartDate', null); } } if ($entity->has('dateEndDate')) { $dateEndDate = $entity->get('dateEndDate'); if (!empty($dateEndDate)) { $dateEnd = $dateEndDate . ' 00:00:00'; $dateEnd = $this->convertDateTimeToDefaultTimezone($dateEnd); $entity->set('dateEnd', $dateEnd); } else { $entity->set('dateEndDate', null); } } $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); if (!empty($parentId) || !empty($parentType)) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); if (!empty($parent)) { if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; } else if ($parent->has('accountId')) { $accountId = $parent->get('accountId'); } if (!empty($accountId)) { $entity->set('accountId', $accountId); } } } } }