Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\FormBundle\Model;
|
||||
|
||||
use Mautic\CoreBundle\Model\FormModel as CommonFormModel;
|
||||
use Mautic\FormBundle\Entity\Action;
|
||||
use Mautic\FormBundle\Form\Type\ActionType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
|
||||
/**
|
||||
* @extends CommonFormModel<Action>
|
||||
*/
|
||||
class ActionModel extends CommonFormModel
|
||||
{
|
||||
/**
|
||||
* @return \Mautic\FormBundle\Entity\ActionRepository
|
||||
*/
|
||||
public function getRepository()
|
||||
{
|
||||
return $this->em->getRepository(Action::class);
|
||||
}
|
||||
|
||||
public function getPermissionBase(): string
|
||||
{
|
||||
return 'form:forms';
|
||||
}
|
||||
|
||||
public function getEntity($id = null): ?Action
|
||||
{
|
||||
if (null === $id) {
|
||||
return new Action();
|
||||
}
|
||||
|
||||
return parent::getEntity($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $entity
|
||||
* @param array $options
|
||||
*/
|
||||
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): \Symfony\Component\Form\FormInterface
|
||||
{
|
||||
if (!$entity instanceof Action) {
|
||||
throw new \InvalidArgumentException('Entity must be of class Action');
|
||||
}
|
||||
|
||||
if ($action) {
|
||||
$options['action'] = $action;
|
||||
}
|
||||
|
||||
if (empty($options['formId']) && null !== $entity->getForm()) {
|
||||
$options['formId'] = $entity->getForm()->getId();
|
||||
}
|
||||
|
||||
return $formFactory->create(ActionType::class, $entity->convertToArray(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get segments which are dependent on given segment.
|
||||
*
|
||||
* @param int $segmentId
|
||||
*/
|
||||
public function getFormsIdsWithDependenciesOnSegment($segmentId): array
|
||||
{
|
||||
$filter = [
|
||||
'force' => [
|
||||
['column' => 'e.type', 'expr' => 'LIKE', 'value'=>'lead.changelist'],
|
||||
],
|
||||
];
|
||||
$entities = $this->getEntities(
|
||||
[
|
||||
'filter' => $filter,
|
||||
]
|
||||
);
|
||||
$dependents = [];
|
||||
foreach ($entities as $entity) {
|
||||
$properties = $entity->getProperties();
|
||||
foreach ($properties as $property) {
|
||||
if (in_array($segmentId, $property)) {
|
||||
$dependents[] = $entity->getForm()->getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $dependents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function getFormsIdsWithDependenciesOnEmail(int $emailId): array
|
||||
{
|
||||
$filter = [
|
||||
'force' => [
|
||||
['column' => 'e.type', 'expr' => 'LIKE', 'value' => 'email.send%'],
|
||||
],
|
||||
];
|
||||
$entities = $this->getEntities(
|
||||
[
|
||||
'filter' => $filter,
|
||||
]
|
||||
);
|
||||
$formIds = [];
|
||||
foreach ($entities as $entity) {
|
||||
$properties = $entity->getProperties();
|
||||
if (isset($properties['email']) && (int) $properties['email'] === $emailId) {
|
||||
$formIds[] = $entity->getForm()->getid();
|
||||
}
|
||||
if (isset($properties['useremail']['email']) && (int) $properties['useremail']['email'] === $emailId) {
|
||||
$formIds[] = $entity->getForm()->getid();
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($formIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function getFormsIdsWithDependenciesOnTag(string $tagName): array
|
||||
{
|
||||
$filter = [
|
||||
'force' => [
|
||||
['column' => 'e.type', 'expr' => 'EQ', 'value' => 'lead.changetags'],
|
||||
],
|
||||
];
|
||||
$entities = $this->getEntities(
|
||||
[
|
||||
'filter' => $filter,
|
||||
]
|
||||
);
|
||||
$dependents = [];
|
||||
|
||||
foreach ($entities as $entity) {
|
||||
$properties = $entity->getProperties();
|
||||
foreach ($properties as $property) {
|
||||
if (in_array($tagName, $property)) {
|
||||
$dependents[] = $entity->getForm()->getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $dependents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\FormBundle\Model;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Mautic\CoreBundle\Doctrine\Helper\ColumnSchemaHelper;
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use Mautic\CoreBundle\Helper\UserHelper;
|
||||
use Mautic\CoreBundle\Model\FormModel as CommonFormModel;
|
||||
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
|
||||
use Mautic\CoreBundle\Translation\Translator;
|
||||
use Mautic\FormBundle\Entity\Field;
|
||||
use Mautic\FormBundle\Event\FormFieldEvent;
|
||||
use Mautic\FormBundle\Form\Type\FieldType;
|
||||
use Mautic\FormBundle\FormEvents;
|
||||
use Mautic\LeadBundle\Model\FieldModel as LeadFieldModel;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* @extends CommonFormModel<Field>
|
||||
*/
|
||||
class FieldModel extends CommonFormModel
|
||||
{
|
||||
public function __construct(
|
||||
protected LeadFieldModel $leadFieldModel,
|
||||
EntityManager $em,
|
||||
CorePermissions $security,
|
||||
EventDispatcherInterface $dispatcher,
|
||||
UrlGeneratorInterface $router,
|
||||
Translator $translator,
|
||||
UserHelper $userHelper,
|
||||
LoggerInterface $mauticLogger,
|
||||
CoreParametersHelper $coreParametersHelper,
|
||||
private RequestStack $requestStack,
|
||||
private ColumnSchemaHelper $columnSchemaHelper,
|
||||
) {
|
||||
parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $mauticLogger, $coreParametersHelper);
|
||||
}
|
||||
|
||||
private function getSession(): SessionInterface
|
||||
{
|
||||
return $this->requestStack->getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object|array<mixed> $entity
|
||||
* @param string|null $action
|
||||
* @param array $options
|
||||
*
|
||||
* @return \Symfony\Component\Form\FormInterface<mixed>
|
||||
*/
|
||||
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): \Symfony\Component\Form\FormInterface
|
||||
{
|
||||
if ($action) {
|
||||
$options['action'] = $action;
|
||||
}
|
||||
|
||||
return $formFactory->create(FieldType::class, $entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Mautic\FormBundle\Entity\FieldRepository
|
||||
*/
|
||||
public function getRepository()
|
||||
{
|
||||
return $this->em->getRepository(Field::class);
|
||||
}
|
||||
|
||||
public function getPermissionBase(): string
|
||||
{
|
||||
return 'form:forms';
|
||||
}
|
||||
|
||||
public function getEntity($id = null): ?Field
|
||||
{
|
||||
if (null === $id) {
|
||||
return new Field();
|
||||
}
|
||||
|
||||
return parent::getEntity($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fields saved in session.
|
||||
*/
|
||||
public function getSessionFields($formId): array
|
||||
{
|
||||
$fields = $this->getSession()->get('mautic.form.'.$formId.'.fields.modified', []);
|
||||
$remove = $this->getSession()->get('mautic.form.'.$formId.'.fields.deleted', []);
|
||||
|
||||
return array_diff_key($fields, array_flip($remove));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $aliases
|
||||
*/
|
||||
public function generateAlias(string $label, array &$aliases): string
|
||||
{
|
||||
$alias = $this->cleanAlias($label, 'f_', 25);
|
||||
|
||||
// make sure alias is not already taken
|
||||
$testAlias = $alias;
|
||||
|
||||
$count = (int) in_array($alias, $aliases);
|
||||
$aliasTag = $count;
|
||||
|
||||
while ($count) {
|
||||
$testAlias = $alias.$aliasTag;
|
||||
$count = (int) in_array($testAlias, $aliases);
|
||||
++$aliasTag;
|
||||
}
|
||||
|
||||
// Prevent internally used identifiers in the form HTML from colliding with the generated field's ID
|
||||
$internalUse = ['message', 'error', 'id', 'return', 'name', 'messenger'];
|
||||
if (in_array($testAlias, $internalUse)) {
|
||||
$testAlias = 'f_'.$testAlias;
|
||||
}
|
||||
|
||||
$aliases[] = $testAlias;
|
||||
|
||||
return $testAlias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws MethodNotAllowedHttpException
|
||||
*/
|
||||
protected function dispatchEvent($action, &$entity, $isNew = false, ?Event $event = null): ?Event
|
||||
{
|
||||
if (!$entity instanceof Field) {
|
||||
throw new MethodNotAllowedHttpException(['Form']);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'pre_save':
|
||||
$name = FormEvents::FIELD_PRE_SAVE;
|
||||
break;
|
||||
case 'post_save':
|
||||
$name = FormEvents::FIELD_POST_SAVE;
|
||||
break;
|
||||
case 'pre_delete':
|
||||
$name = FormEvents::FIELD_PRE_DELETE;
|
||||
break;
|
||||
case 'post_delete':
|
||||
$name = FormEvents::FIELD_POST_DELETE;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->dispatcher->hasListeners($name)) {
|
||||
if (empty($event)) {
|
||||
$event = new FormFieldEvent($entity, $isNew);
|
||||
}
|
||||
|
||||
$this->dispatcher->dispatch($event, $name);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the table structure for form results.
|
||||
*/
|
||||
public function removeFieldColumn(Field $field): void
|
||||
{
|
||||
$form = $field->getForm();
|
||||
|
||||
$name = 'form_results_'.$form->getId().'_'.$form->getAlias();
|
||||
|
||||
$schemaHelper = $this->columnSchemaHelper->setName($name);
|
||||
$schemaHelper->dropColumn($field->getAlias());
|
||||
$schemaHelper->executeChanges();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\FormBundle\Model;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Mautic\CoreBundle\Model\MauticModelInterface;
|
||||
use Mautic\FormBundle\Entity\Submission;
|
||||
use Mautic\FormBundle\Entity\SubmissionRepository;
|
||||
|
||||
class SubmissionResultLoader implements MauticModelInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function getSubmissionWithResult($id): ?Submission
|
||||
{
|
||||
$repository = $this->getRepository();
|
||||
|
||||
return $repository->getEntity($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SubmissionRepository
|
||||
*/
|
||||
private function getRepository()
|
||||
{
|
||||
return $this->entityManager->getRepository(Submission::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user