Spaces:
No application file
No application file
File size: 6,947 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
<?php
namespace Mautic\FormBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Mautic\CoreBundle\Doctrine\Common\DataFixtures\Event\PreExecuteEvent;
use Mautic\CoreBundle\Helper\CsvHelper;
use Mautic\CoreBundle\Helper\Serializer;
use Mautic\FormBundle\Entity\Action;
use Mautic\FormBundle\Entity\Field;
use Mautic\FormBundle\Entity\Form;
use Mautic\FormBundle\Model\ActionModel;
use Mautic\FormBundle\Model\FieldModel;
use Mautic\FormBundle\Model\FormModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class LoadFormData extends AbstractFixture implements OrderedFixtureInterface
{
public const FORM_PREFIX = 'form-';
/**
* @var array<int, Form>
*/
private array $formEntities = [];
/**
* @var array<int, Field>
*/
private array $fieldEntities = [];
/**
* @var array<int, Action>
*/
private array $actionEntities = [];
public function __construct(
private FormModel $formModel,
private FieldModel $formFieldModel,
private ActionModel $actionModel,
EventDispatcherInterface $eventDispatcher
) {
// this will load the data before fixtures are loaded
$eventDispatcher->addListener(PreExecuteEvent::class, function (PreExecuteEvent $event): void {
$formEntities = $this->getFormEntities();
$this->getFieldEntities();
$this->getActionEntities();
$firstId = 0;
// create the tables passed in LoadFormData fixture.
foreach ($formEntities as $form) {
$this->formModel->generateHtml($form);
if ($form->getId() < 1) {
// Method above saves the form entity. If this exception is thrown all you need to do is to save an entity.
throw new \RuntimeException('Form must have an ID set.');
}
if (0 === $firstId) {
$firstId = $form->getId();
}
$this->formModel->createTableSchema($form, true, true);
}
if ($event->isTruncate()) {
return;
}
// need to delete created form entities, because executor will wrap DELETE query into transaction and
// will insert form entries with new autoincrement.
foreach ($formEntities as $formEntity) {
$this->formModel->deleteEntity($formEntity);
}
// because form table data will be deleted we must have same autoincrement as before the insertion
// to have the form_results table to match the form id in table name e.g. form_results_69_kaleidosco
$formTableName = $this->formModel->getRepository()->getTableName();
$event->getEntityManager()->getConnection()->executeStatement(
'ALTER TABLE '.$formTableName.' AUTO_INCREMENT='.$firstId
);
});
}
public function load(ObjectManager $manager): void
{
$this->getFormEntities();
$this->getFieldEntities();
$this->getActionEntities();
foreach ($this->formEntities as $key => $formEntity) {
$this->formModel->getRepository()->saveEntity($formEntity);
$this->setReference(self::FORM_PREFIX.$key, $formEntity);
}
foreach ($this->fieldEntities as $field) {
$this->formFieldModel->getRepository()->saveEntity($field);
}
foreach ($this->actionEntities as $action) {
$this->actionModel->getRepository()->saveEntity($action);
}
}
public function getOrder(): int
{
return 8;
}
/**
* @return array<int, Form>
*/
private function getFormEntities(): array
{
$forms = CsvHelper::csv_to_array(__DIR__.'/fakeformdata.csv');
$this->formEntities = [];
foreach ($forms as $count => $rows) {
$form = new Form();
$key = $count + 1;
foreach ($rows as $col => $val) {
if ('NULL' !== $val) {
$setter = 'set'.ucfirst($col);
if ('dateAdded' === $col) {
$form->setDateAdded(new \DateTime($val));
} elseif ('cachedHtml' === $col) {
$val = stripslashes($val);
$form->setCachedHtml($val);
} else {
$form->$setter($val);
}
}
}
$this->formEntities[$key] = $form;
}
return $this->formEntities;
}
private function getFieldEntities(): void
{
if (0 === count($this->formEntities)) {
throw new \RuntimeException('This method must be called after getFormEntities.');
}
$this->fieldEntities = [];
$fields = CsvHelper::csv_to_array(__DIR__.'/fakefielddata.csv');
foreach ($fields as $count => $rows) {
$field = new Field();
foreach ($rows as $col => $val) {
if ('NULL' !== $val) {
$setter = 'set'.ucfirst($col);
if ('form' === $col) {
$form = $this->formEntities[$val];
$field->setForm($form);
$form->addField($count, $field);
} elseif (in_array($col, ['customParameters', 'properties'], true)) {
$val = Serializer::decode(stripslashes($val));
$field->$setter($val);
} else {
$field->$setter($val);
}
}
}
$this->fieldEntities[$count] = $field;
}
}
private function getActionEntities(): void
{
if (0 === count($this->formEntities)) {
throw new \RuntimeException('This method must be called after getFormEntities.');
}
$this->actionEntities = [];
$actions = CsvHelper::csv_to_array(__DIR__.'/fakeactiondata.csv');
foreach ($actions as $rows) {
$action = new Action();
foreach ($rows as $col => $val) {
if ('NULL' !== $val) {
$setter = 'set'.ucfirst($col);
if ('form' === $col) {
$action->setForm($this->formEntities[$val]);
} elseif ('properties' === $col) {
$val = Serializer::decode(stripslashes($val));
$action->setProperties($val);
} else {
$action->$setter($val);
}
}
}
$this->actionEntities[] = $action;
}
}
}
|