Default to first active form if widget form id not present

[MAILPOET-3847]
This commit is contained in:
Brezo Cordero
2021-10-13 17:18:55 -05:00
committed by Veljko V
parent 98eb024027
commit 51e03911b8
2 changed files with 26 additions and 2 deletions

View File

@@ -27,6 +27,22 @@ class FormsRepository extends Repository {
->getResult();
}
/**
* @return FormEntity[]
*/
public function findAllActive(): array {
return $this->entityManager
->createQueryBuilder()
->select('f')
->from(FormEntity::class, 'f')
->where('f.status = (:enabled)')
->andWhere('f.deletedAt IS NULL')
->setParameter('enabled', FormEntity::STATUS_ENABLED)
->orderBy('f.name', 'asc')
->getQuery()
->getResult();
}
public function getNamesOfFormsForSegments(): array {
$allNonDeletedForms = $this->findAllNotDeleted();

View File

@@ -197,8 +197,16 @@ class Widget extends \WP_Widget {
);
// get form
if (empty($instance['form'])) return '';
if (!empty($instance['form'])) {
$form = $this->formsRepository->findOneById($instance['form']);
} else {
// Backwards compatibility for MAILPOET-3847
// Get first active form
$forms = $this->formsRepository->findAllActive();
if (empty($forms)) return '';
$form = $forms[0];
}
if (!$form) return '';
if ($form->getDeletedAt()) return '';
if ($form->getStatus() !== FormEntity::STATUS_ENABLED) return '';