Spaces:
No application file
No application file
File size: 13,307 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
<?php
namespace Mautic\FormBundle\Controller;
use Mautic\CoreBundle\Controller\FormController as CommonFormController;
use Mautic\FormBundle\Entity\Action;
use Mautic\FormBundle\Form\Type\ActionType;
use Mautic\FormBundle\Model\FormModel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ActionController extends CommonFormController
{
/**
* Generates new form and processes post data.
*
* @return Response
*/
public function newAction(Request $request)
{
$success = 0;
$valid = $cancelled = false;
$method = $request->getMethod();
$session = $request->getSession();
if ('POST' == $method) {
$formAction = $request->request->all()['formaction'] ?? [];
$actionType = $formAction['type'];
$formId = $formAction['formId'];
} else {
$actionType = $request->query->get('type');
$formId = $request->query->get('formId');
$formAction = [
'type' => $actionType,
'formId' => $formId,
];
}
// ajax only for form fields
if (!$actionType
|| !$request->isXmlHttpRequest()
|| !$this->security->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
) {
return $this->modalAccessDenied();
}
// fire the form builder event
$formModel = $this->getModel('form.form');
\assert($formModel instanceof FormModel);
$customComponents = $formModel->getCustomComponents();
$form = $this->formFactory->create(ActionType::class, $formAction, [
'action' => $this->generateUrl('mautic_formaction_action', ['objectAction' => 'new']),
'settings' => $customComponents['actions'][$actionType],
'formId' => $formId,
]);
$form->get('formId')->setData($formId);
$formAction['settings'] = $customComponents['actions'][$actionType];
// Check for a submitted form and process it
if ('POST' == $method) {
if (!$cancelled = $this->isFormCancelled($form)) {
if ($valid = $this->isFormValid($form)) {
$success = 1;
// form is valid so process the data
$keyId = 'new'.hash('sha1', uniqid(mt_rand()));
// save the properties to session
$actions = $session->get('mautic.form.'.$formId.'.actions.modified', []);
$formData = $form->getData();
$formAction = array_merge($formAction, $formData);
$formAction['id'] = $keyId;
if (empty($formAction['name'])) {
// set it to the event default
$formAction['name'] = $this->translator->trans($formAction['settings']['label']);
}
$actions[$keyId] = $formAction;
$session->set('mautic.form.'.$formId.'.actions.modified', $actions);
} else {
$success = 0;
}
}
}
$viewParams = ['type' => $actionType];
if ($cancelled || $valid) {
$closeModal = true;
} else {
$closeModal = false;
$viewParams['tmpl'] = 'action';
$viewParams['form'] = $form->createView();
$header = $formAction['settings']['label'];
$viewParams['actionHeader'] = $this->translator->trans($header);
if (isset($formAction['settings']['formTheme'])) {
$viewParams['formTheme'] = $formAction['settings']['formTheme'];
}
}
$passthroughVars = [
'mauticContent' => 'formAction',
'success' => $success,
'route' => false,
];
if (!empty($keyId)) {
// prevent undefined errors
$entity = new Action();
$blank = $entity->convertToArray();
$formAction = array_merge($blank, $formAction);
$template = (!empty($formAction['settings']['template'])) ? $formAction['settings']['template'] :
'@MauticForm/Action/_generic.html.twig';
$passthroughVars['actionId'] = $keyId;
$passthroughVars['actionHtml'] = $this->renderView($template, [
'inForm' => true,
'action' => $formAction,
'id' => $keyId,
'formId' => $formId,
]);
}
if ($closeModal) {
// just close the modal
$passthroughVars['closeModal'] = 1;
return new JsonResponse($passthroughVars);
}
return $this->ajaxAction($request, [
'contentTemplate' => '@MauticForm/Builder/'.$viewParams['tmpl'].'.html.twig',
'viewParameters' => $viewParams,
'passthroughVars' => $passthroughVars,
]);
}
/**
* Generates edit form and processes post data.
*
* @param int $objectId
*
* @return Response
*/
public function editAction(Request $request, $objectId)
{
$session = $request->getSession();
$method = $request->getMethod();
$formaction = $request->request->get('formaction') ?? [];
$formId = 'POST' === $method ? ($formaction['formId'] ?? '') : $request->query->get('formId');
$actions = $session->get('mautic.form.'.$formId.'.actions.modified', []);
$success = 0;
$valid = $cancelled = false;
$formAction = array_key_exists($objectId, $actions) ? $actions[$objectId] : null;
if (null !== $formAction) {
$formModel = $this->getModel('form.form');
\assert($formModel instanceof FormModel);
$actionType = $formAction['type'];
$customComponents = $formModel->getCustomComponents();
$formAction['settings'] = $customComponents['actions'][$actionType];
// ajax only for form fields
if (!$actionType
|| !$request->isXmlHttpRequest()
|| !$this->security->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
) {
return $this->modalAccessDenied();
}
$form = $this->formFactory->create(ActionType::class, $formAction, [
'action' => $this->generateUrl('mautic_formaction_action', ['objectAction' => 'edit', 'objectId' => $objectId]),
'settings' => $formAction['settings'],
'formId' => $formId,
]);
$form->get('formId')->setData($formId);
// Check for a submitted form and process it
if ('POST' == $method) {
if (!$cancelled = $this->isFormCancelled($form)) {
if ($valid = $this->isFormValid($form)) {
$success = 1;
// form is valid so process the data
// save the properties to session
$session = $request->getSession();
$actions = $session->get('mautic.form.'.$formId.'.actions.modified');
$formData = $form->getData();
// overwrite with updated data
$formAction = array_merge($actions[$objectId], $formData);
if (empty($formAction['name'])) {
// set it to the event default
$formAction['name'] = $this->translator->trans($formAction['settings']['label']);
}
$actions[$objectId] = $formAction;
$session->set('mautic.form.'.$formId.'.actions.modified', $actions);
// generate HTML for the field
$keyId = $objectId;
// take note if this is a submit button or not
if ('button' == $actionType) {
$submits = $session->get('mautic.formactions.submits', []);
if ('submit' == $formAction['properties']['type'] && !in_array($keyId, $submits)) {
// button type updated to submit
$submits[] = $keyId;
$session->set('mautic.formactions.submits', $submits);
} elseif ('submit' != $formAction['properties']['type'] && in_array($keyId, $submits)) {
// button type updated to something other than submit
$key = array_search($keyId, $submits);
unset($submits[$key]);
$session->set('mautic.formactions.submits', $submits);
}
}
}
}
}
$viewParams = ['type' => $actionType];
if ($cancelled || $valid) {
$closeModal = true;
} else {
$closeModal = false;
$viewParams['tmpl'] = 'action';
$viewParams['form'] = $form->createView();
$viewParams['actionHeader'] = $this->translator->trans($formAction['settings']['label']);
if (isset($formAction['settings']['formTheme'])) {
$viewParams['formTheme'] = $formAction['settings']['formTheme'];
}
}
$passthroughVars = [
'mauticContent' => 'formAction',
'success' => $success,
'route' => false,
];
if (!empty($keyId)) {
$passthroughVars['actionId'] = $keyId;
// prevent undefined errors
$entity = new Action();
$blank = $entity->convertToArray();
$formAction = array_merge($blank, $formAction);
$template = (!empty($formAction['settings']['template'])) ? $formAction['settings']['template'] :
'@MauticForm/Action/_generic.html.twig';
$passthroughVars['actionHtml'] = $this->renderView($template, [
'inForm' => true,
'action' => $formAction,
'id' => $keyId,
'formId' => $formId,
]);
}
if ($closeModal) {
// just close the modal
$passthroughVars['closeModal'] = 1;
return new JsonResponse($passthroughVars);
}
return $this->ajaxAction($request, [
'contentTemplate' => '@MauticForm/Builder/'.$viewParams['tmpl'].'.html.twig',
'viewParameters' => $viewParams,
'passthroughVars' => $passthroughVars,
]);
}
return new JsonResponse(['success' => 0]);
}
/**
* Deletes the entity.
*
* @return JsonResponse
*/
public function deleteAction(Request $request, $objectId)
{
$session = $request->getSession();
$formId = $request->query->get('formId');
$actions = $session->get('mautic.form.'.$formId.'.actions.modified', []);
$delete = $session->get('mautic.form.'.$formId.'.actions.deleted', []);
// ajax only for form fields
if (!$request->isXmlHttpRequest()
|| !$this->security->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
) {
return $this->accessDenied();
}
$formAction = (array_key_exists($objectId, $actions)) ? $actions[$objectId] : null;
if ('POST' == $request->getMethod() && null !== $formAction) {
// add the field to the delete list
if (!in_array($objectId, $delete)) {
$delete[] = $objectId;
$session->set('mautic.form.'.$formId.'.actions.deleted', $delete);
}
// take note if this is a submit button or not
if ('button' == $formAction['type']) {
$submits = $session->get('mautic.formactions.submits', []);
$properties = $formAction['properties'];
if ('submit' == $properties['type'] && in_array($objectId, $submits)) {
$key = array_search($objectId, $submits);
unset($submits[$key]);
$session->set('mautic.formactions.submits', $submits);
}
}
$dataArray = [
'mauticContent' => 'formAction',
'success' => 1,
'route' => false,
];
} else {
$dataArray = ['success' => 0];
}
return new JsonResponse($dataArray);
}
}
|