*/ private Collection $outcomes; private ?string $name = null; private ?string $description = null; /** * The script's validity window: the "start" date and time. */ private ?\DateTime $publishUp = null; /** * The script's validity window: the "end" date and time. */ private ?\DateTime $publishDown = null; /** * Required in the form. The column is nullable only so that deleting the user doesn't delete the * script (ON DELETE SET NULL). */ private ?User $owner = null; private ?Category $category = null; /** * HTML with dynamic tokens like {contact.first_name}, kept as typed. */ private ?string $script = null; /** * The weekdays the calls may happen on, as ISO short names (mon..sun). Empty means every day. * * @var list */ private array $callingDays = []; /** * The calling window's hours, as times of day in the window's timezone. */ private ?\DateTime $callingFrom = null; private ?\DateTime $callingTo = null; private CallScriptTimezoneMode $callingTimezoneMode = CallScriptTimezoneMode::Contact; /** * The fixed timezone of the calling window, when its mode is "fixed". */ private ?string $callingTimezone = null; private int $retryMaxAttempts = self::RETRY_MAX_ATTEMPTS_DEFAULT; private int $retryDelayMinutes = self::RETRY_DELAY_DEFAULT; private CallScriptRetryBackoff $retryBackoff = CallScriptRetryBackoff::None; public function __construct() { $this->outcomes = new ArrayCollection(); } public static function loadMetadata(OrmClassMetadata $metadata): void { $builder = new ClassMetadataBuilder($metadata); $builder->setTable(self::TABLE_NAME) ->setCustomRepositoryClass(CallScriptRepository::class); $builder->addId(); $builder->createField('name', Types::STRING) ->length(self::NAME_MAX_LENGTH) ->build(); $builder->createField('description', Types::TEXT) ->nullable() ->build(); // The script's validity window: the "start" and "end" date and time in the form. $builder->addPublishDates(); $builder->createManyToOne('owner', User::class) ->addJoinColumn('owner_id', 'id', true, false, 'SET NULL') ->build(); $builder->addCategory(); // Types::TEXT is LONGTEXT on MySQL: the script keeps all its HTML. $builder->createField('script', Types::TEXT) ->nullable() ->build(); // Mapped on the raw metadata, like the Smart Links arrays: ClassMetadataBuilder forces // VARCHAR(191) on strings and doesn't accept JSON or enum types. $metadata->mapField([ 'fieldName' => 'callingDays', 'columnName' => 'calling_days', 'type' => Types::JSON, ]); $metadata->mapField([ 'fieldName' => 'callingFrom', 'columnName' => 'calling_from', 'type' => Types::TIME_MUTABLE, 'nullable' => true, ]); $metadata->mapField([ 'fieldName' => 'callingTo', 'columnName' => 'calling_to', 'type' => Types::TIME_MUTABLE, 'nullable' => true, ]); $metadata->mapField([ 'fieldName' => 'callingTimezoneMode', 'columnName' => 'calling_timezone_mode', 'type' => Types::STRING, 'length' => 10, 'enumType' => CallScriptTimezoneMode::class, 'options' => ['default' => CallScriptTimezoneMode::Contact->value], ]); $builder->createField('callingTimezone', Types::STRING) ->columnName('calling_timezone') ->length(self::TIMEZONE_MAX_LENGTH) ->nullable() ->build(); $builder->createField('retryMaxAttempts', Types::INTEGER) ->columnName('retry_max_attempts') ->option('default', self::RETRY_MAX_ATTEMPTS_DEFAULT) ->option('unsigned', true) ->build(); $builder->createField('retryDelayMinutes', Types::INTEGER) ->columnName('retry_delay_minutes') ->option('default', self::RETRY_DELAY_DEFAULT) ->option('unsigned', true) ->build(); $metadata->mapField([ 'fieldName' => 'retryBackoff', 'columnName' => 'retry_backoff', 'type' => Types::STRING, 'length' => 20, 'enumType' => CallScriptRetryBackoff::class, 'options' => ['default' => CallScriptRetryBackoff::None->value], ]); $builder->createOneToMany('outcomes', CallScriptOutcome::class) ->mappedBy('callScript') ->setOrderBy(['position' => 'ASC']) ->cascadePersist() ->cascadeRemove() ->orphanRemoval() ->build(); } public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('name', new NotBlank(message: 'mautic.core.name.required')); $metadata->addPropertyConstraint('name', new Length(max: self::NAME_MAX_LENGTH)); $metadata->addPropertyConstraint('owner', new NotBlank(message: 'mt3s.marketing_program.owner.required')); $metadata->addPropertyConstraint('outcomes', new Valid()); $metadata->addPropertyConstraint('retryMaxAttempts', new Range( notInRangeMessage: 'mt3s.call_script.retry_attempts.range', min: self::RETRY_ATTEMPTS_MIN, max: self::RETRY_ATTEMPTS_MAX, )); $metadata->addPropertyConstraint('retryDelayMinutes', new Range( notInRangeMessage: 'mt3s.call_script.retry_delay.range', min: 0, max: self::RETRY_DELAY_MAX, )); // The validity window: the end must be on or after the start (the form's "end" field is publishDown). $metadata->addConstraint(new EndDateAfterStartDate(atPath: 'publishDown')); $metadata->addConstraint(new ValidCallingWindow()); } public function getId(): ?int { return $this->id; } /** * The outcomes sorted by position, as a new collection (see Program::getObjectives()). * * @return Collection */ public function getOutcomes(): Collection { $rows = $this->outcomes->toArray(); usort($rows, fn (CallScriptOutcome $a, CallScriptOutcome $b): int => $a->getPosition() <=> $b->getPosition()); return new ArrayCollection($rows); } public function addOutcome(CallScriptOutcome $outcome): static { if (!$this->outcomes->contains($outcome)) { $outcome->setCallScript($this); $this->outcomes->add($outcome); } return $this; } public function removeOutcome(CallScriptOutcome $outcome): static { $this->outcomes->removeElement($outcome); return $this; } /** * Keeps the outcome positions contiguous (0..n-1) in their current order, before saving. */ public function normalizePositions(): void { $rows = $this->outcomes->toArray(); usort($rows, fn (CallScriptOutcome $a, CallScriptOutcome $b): int => $a->getPosition() <=> $b->getPosition()); foreach ($rows as $index => $row) { $row->setPosition($index); } } public function getName(): ?string { return $this->name; } public function setName(?string $name): static { $this->isChanged('name', $name); $this->name = $name; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(?string $description): static { $this->isChanged('description', $description); $this->description = $description; return $this; } public function getPublishUp(): ?\DateTime { return $this->publishUp; } public function setPublishUp(?\DateTime $publishUp): static { $this->isChanged('publishUp', $publishUp); $this->publishUp = $publishUp; return $this; } public function getPublishDown(): ?\DateTime { return $this->publishDown; } public function setPublishDown(?\DateTime $publishDown): static { $this->isChanged('publishDown', $publishDown); $this->publishDown = $publishDown; return $this; } /** * DateRangeInterface: the script's validity window, for EndDateAfterStartDate. */ public function getStartDate(): ?\DateTime { return $this->publishUp; } public function getEndDate(): ?\DateTime { return $this->publishDown; } public function getOwner(): ?User { return $this->owner; } public function setOwner(?User $owner): static { $this->isChanged('owner', $owner); $this->owner = $owner; return $this; } public function getCategory(): ?Category { return $this->category; } public function setCategory(?Category $category): static { $this->isChanged('category', $category); $this->category = $category; return $this; } public function getScript(): ?string { return $this->script; } public function setScript(?string $script): static { $this->isChanged('script', $script); $this->script = $script; return $this; } /** * @return list */ public function getCallingDays(): array { return $this->callingDays; } /** * @param list|null $callingDays */ public function setCallingDays(?array $callingDays): static { $days = []; foreach ($callingDays ?? [] as $day) { $day = (string) $day; if (in_array($day, self::WEEKDAYS, true) && !in_array($day, $days, true)) { $days[] = $day; } } $this->isChanged('callingDays', $days); $this->callingDays = $days; return $this; } public function getCallingFrom(): ?\DateTime { return $this->callingFrom; } public function setCallingFrom(?\DateTime $callingFrom): static { $this->isChanged('callingFrom', $callingFrom); $this->callingFrom = $callingFrom; return $this; } public function getCallingTo(): ?\DateTime { return $this->callingTo; } public function setCallingTo(?\DateTime $callingTo): static { $this->isChanged('callingTo', $callingTo); $this->callingTo = $callingTo; return $this; } public function getCallingTimezoneMode(): CallScriptTimezoneMode { return $this->callingTimezoneMode; } /** * Null (a mode field missing from the submitted form) keeps the current mode, like the task kind. */ public function setCallingTimezoneMode(?CallScriptTimezoneMode $callingTimezoneMode): static { if (null === $callingTimezoneMode || $callingTimezoneMode === $this->callingTimezoneMode) { return $this; } // isChanged() can't log enums (they aren't stringable), so log the backing values. $this->addChange('callingTimezoneMode', [$this->callingTimezoneMode->value, $callingTimezoneMode->value]); $this->callingTimezoneMode = $callingTimezoneMode; return $this; } public function getCallingTimezone(): ?string { return $this->callingTimezone; } public function setCallingTimezone(?string $callingTimezone): static { if (null !== $callingTimezone && '' === trim($callingTimezone)) { $callingTimezone = null; } $this->isChanged('callingTimezone', $callingTimezone); $this->callingTimezone = $callingTimezone; return $this; } public function getRetryMaxAttempts(): int { return $this->retryMaxAttempts; } /** * Null (an attempts field missing from the submitted form) keeps the current value. */ public function setRetryMaxAttempts(?int $retryMaxAttempts): static { if (null === $retryMaxAttempts || $retryMaxAttempts === $this->retryMaxAttempts) { return $this; } $this->isChanged('retryMaxAttempts', $retryMaxAttempts); $this->retryMaxAttempts = $retryMaxAttempts; return $this; } public function getRetryDelayMinutes(): int { return $this->retryDelayMinutes; } /** * Null (a delay field missing from the submitted form) keeps the current value. */ public function setRetryDelayMinutes(?int $retryDelayMinutes): static { if (null === $retryDelayMinutes || $retryDelayMinutes === $this->retryDelayMinutes) { return $this; } $this->isChanged('retryDelayMinutes', $retryDelayMinutes); $this->retryDelayMinutes = $retryDelayMinutes; return $this; } public function getRetryBackoff(): CallScriptRetryBackoff { return $this->retryBackoff; } /** * Null (a backoff field missing from the submitted form) keeps the current policy. */ public function setRetryBackoff(?CallScriptRetryBackoff $retryBackoff): static { if (null === $retryBackoff || $retryBackoff === $this->retryBackoff) { return $this; } // isChanged() can't log enums (they aren't stringable), so log the backing values. $this->addChange('retryBackoff', [$this->retryBackoff->value, $retryBackoff->value]); $this->retryBackoff = $retryBackoff; return $this; } }