Spaces:
No application file
No application file
File size: 3,068 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 |
<?php
namespace Mautic\CoreBundle\Controller;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\EmailBundle\Model\EmailModel;
use Mautic\PageBundle\Model\PageModel;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
trait VariantAjaxControllerTrait
{
/**
* @param string $modelName
* @param string $abSettingsFormName
* @param string $abSettingsFormBlockPrefix
* @param string $parentFormName
* @param string $abFormTemplate
* @param array $formThemes
*
* @return mixed
*/
private function getAbTestForm(Request $request, FormFactoryInterface $formFactory, $modelName, $abSettingsFormName, $abSettingsFormBlockPrefix, $parentFormName, $abFormTemplate, $formThemes = [])
{
$dataArray = [
'success' => 0,
'html' => '',
];
$type = InputHelper::clean($request->request->get('abKey'));
$id = (int) $request->request->get('id');
if (!empty($type)) {
// get the HTML for the form
$model = $this->getModel($modelName);
if (!$model instanceof EmailModel && !$model instanceof PageModel) {
throw new \InvalidArgumentException('Model should be either email or page model.');
}
$entity = $model->getEntity($id);
$abTestComponents = $model->getBuilderComponents($entity, 'abTestWinnerCriteria');
$abTestSettings = $abTestComponents['criteria'];
if (isset($abTestSettings[$type])) {
$html = '';
$formType = (!empty($abTestSettings[$type]['formType'])) ? $abTestSettings[$type]['formType'] : '';
if (!empty($formType)) {
$formOptions = (!empty($abTestSettings[$type]['formTypeOptions'])) ? $abTestSettings[$type]['formTypeOptions'] : [];
$form = $formFactory->create(
$abSettingsFormName,
[],
['formType' => $formType, 'formTypeOptions' => $formOptions]
);
$html = $this->renderView(
$abFormTemplate,
[
'form' => $this->setFormTheme($form, $formThemes),
]
);
}
$html = str_replace(
[
"{$abSettingsFormBlockPrefix}[",
"{$abSettingsFormBlockPrefix}_",
$abSettingsFormBlockPrefix,
],
[
"{$parentFormName}[variantSettings][",
"{$parentFormName}_variantSettings_",
$parentFormName,
],
$html
);
$dataArray['html'] = $html;
$dataArray['success'] = 1;
}
}
return $this->sendJsonResponse($dataArray);
}
}
|