/** * Undeletes the entity * * @param int $objectId * * @return JsonResponse */ public function undeleteAction($objectId) { $session = $this->factory->getSession(); $formId = $this->request->query->get('formId'); $fields = $session->get('mautic.form.' . $formId . '.fields.modified', array()); $delete = $session->get('mautic.form.' . $formId . '.fields.deleted', array()); //ajax only for form fields if (!$this->request->isXmlHttpRequest() || !$this->factory->getSecurity()->isGranted(array('form:forms:editown', 'form:forms:editother', 'form:forms:create'), 'MATCH_ONE')) { return $this->accessDenied(); } $formField = array_key_exists($objectId, $fields) ? $fields[$objectId] : null; if ($this->request->getMethod() == 'POST' && $formField !== null) { //set custom params from event if applicable $customParams = !empty($formField['isCustom']) ? $formField['customParameters'] : array(); //add the field to the delete list if (in_array($objectId, $delete)) { $key = array_search($objectId, $delete); unset($delete[$key]); $session->set('mautic.form.' . $formId . '.fields.deleted', $delete); } if (!empty($customParams)) { $template = $customParams['template']; } else { $template = 'MauticFormBundle:Field:' . $formField['type'] . '.html.php'; } //prevent undefined errors $entity = new Field(); $blank = $entity->convertToArray(); $formField = array_merge($blank, $formField); $dataArray = array('mauticContent' => 'formField', 'success' => 1, 'target' => '#mauticform_' . $objectId, 'route' => false, 'fieldId' => $objectId, 'fieldHtml' => $this->renderView($template, array('inForm' => true, 'field' => $formField, 'id' => $objectId, 'deleted' => false, 'formId' => $formId))); } else { $dataArray = array('success' => 0); } $response = new JsonResponse($dataArray); $response->headers->set('Content-Length', strlen($response->getContent())); return $response; }
/** * Generates edit form and processes post data * * @param int $objectId * * @return JsonResponse */ public function editAction($objectId) { $session = $this->factory->getSession(); $method = $this->request->getMethod(); $formId = $method == "POST" ? $this->request->request->get('formfield[formId]', '', true) : $this->request->query->get('formId'); $fields = $session->get('mautic.form.' . $formId . '.fields.modified', array()); $success = 0; $valid = $cancelled = false; $formField = array_key_exists($objectId, $fields) ? $fields[$objectId] : null; if ($formField !== null) { $fieldType = $formField['type']; //ajax only for form fields if (!$fieldType || !$this->request->isXmlHttpRequest() || !$this->factory->getSecurity()->isGranted(array('form:forms:editown', 'form:forms:editother', 'form:forms:create'), 'MATCH_ONE')) { return $this->modalAccessDenied(); } //set custom params from event if applicable $customParams = !empty($formField['isCustom']) ? $formField['customParameters'] : array(); // Only show the lead fields not already used $usedLeadFields = $session->get('mautic.form.' . $formId . '.fields.leadfields', array()); $testLeadFields = array_flip($usedLeadFields); $currentLeadField = isset($formField['leadField']) ? $formField['leadField'] : null; if (!empty($currentLeadField) && isset($testLeadFields[$currentLeadField])) { unset($testLeadFields[$currentLeadField]); } $leadFields = $this->getModel('lead.field')->getFieldList(); foreach ($leadFields as &$group) { $group = array_diff_key($group, $testLeadFields); } $form = $this->get('form.factory')->create('formfield', $formField, array('action' => $this->generateUrl('mautic_formfield_action', array('objectAction' => 'edit', 'objectId' => $objectId)), 'customParameters' => $customParams, 'leadFields' => $leadFields)); $form->get('formId')->setData($formId); //Check for a submitted form and process it if ($method == 'POST') { 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 = $this->factory->getSession(); $fields = $session->get('mautic.form.' . $formId . '.fields.modified'); $formData = $form->getData(); //overwrite with updated data $formField = array_merge($fields[$objectId], $formData); if (strpos($objectId, 'new') !== false) { // Get aliases in order to generate update for this one $aliases = array(); foreach ($fields as $k => $f) { if ($k != $objectId) { $aliases[] = $f['alias']; } } $formField['alias'] = $this->getModel('form.field')->generateAlias($formField['label'], $aliases); } $fields[$objectId] = $formField; $session->set('mautic.form.' . $formId . '.fields.modified', $fields); // Keep track of used lead fields if (!empty($formData['leadField'])) { $usedLeadFields[$objectId] = $formData['leadField']; } else { unset($usedLeadFields[$objectId]); } $session->set('mautic.form.' . $formId . '.fields.leadfields', $usedLeadFields); } } } $viewParams = array('type' => $fieldType); if ($cancelled || $valid) { $closeModal = true; } else { $closeModal = false; $viewParams['tmpl'] = 'field'; $viewParams['form'] = isset($customParams['formTheme']) ? $this->setFormTheme($form, 'MauticFormBundle:Builder:field.html.php', $customParams['formTheme']) : $form->createView(); $viewParams['fieldHeader'] = !empty($customParams) ? $this->get('translator')->trans($customParams['label']) : $this->get('translator')->transConditional('mautic.core.type.' . $fieldType, 'mautic.form.field.type.' . $fieldType); } $passthroughVars = array('mauticContent' => 'formField', 'success' => $success, 'route' => false); $passthroughVars['fieldId'] = $objectId; if (!empty($customParams)) { $template = $customParams['template']; } else { $template = 'MauticFormBundle:Field:' . $fieldType . '.html.php'; } //prevent undefined errors $entity = new Field(); $blank = $entity->convertToArray(); $formField = array_merge($blank, $formField); $passthroughVars['fieldHtml'] = $this->renderView('MauticFormBundle:Builder:fieldwrapper.html.php', ['template' => $template, 'inForm' => true, 'field' => $formField, 'id' => $objectId, 'formId' => $formId]); if ($closeModal) { //just close the modal $passthroughVars['closeModal'] = 1; $response = new JsonResponse($passthroughVars); return $response; } return $this->ajaxAction(array('contentTemplate' => 'MauticFormBundle:Builder:' . $viewParams['tmpl'] . '.html.php', 'viewParameters' => $viewParams, 'passthroughVars' => $passthroughVars)); } $response = new JsonResponse(array('success' => 0)); return $response; }
/** * Generates new form and processes post data. * * @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response * * @throws \Exception */ public function newAction() { /** @var \Mautic\FormBundle\Model\FormModel $model */ $model = $this->getModel('form'); $entity = $model->getEntity(); $session = $this->get('session'); if (!$this->get('mautic.security')->isGranted('form:forms:create')) { return $this->accessDenied(); } //set the page we came from $page = $this->get('session')->get('mautic.form.page', 1); $sessionId = $this->request->request->get('mauticform[sessionId]', 'mautic_' . sha1(uniqid(mt_rand(), true)), true); //set added/updated fields $modifiedFields = $session->get('mautic.form.' . $sessionId . '.fields.modified', []); $deletedFields = $session->get('mautic.form.' . $sessionId . '.fields.deleted', []); //set added/updated actions $modifiedActions = $session->get('mautic.form.' . $sessionId . '.actions.modified', []); $deletedActions = $session->get('mautic.form.' . $sessionId . '.actions.deleted', []); $action = $this->generateUrl('mautic_form_action', ['objectAction' => 'new']); $form = $model->createForm($entity, $this->get('form.factory'), $action); ///Check for a submitted form and process it if ($this->request->getMethod() == 'POST') { $valid = false; if (!($cancelled = $this->isFormCancelled($form))) { if ($valid = $this->isFormValid($form)) { //only save fields that are not to be deleted $fields = array_diff_key($modifiedFields, array_flip($deletedFields)); //make sure that at least one field is selected if (empty($fields)) { //set the error $form->addError(new FormError($this->get('translator')->trans('mautic.form.form.fields.notempty', [], 'validators'))); $valid = false; } else { $model->setFields($entity, $fields); try { // Set alias to prevent SQL errors $alias = $model->cleanAlias($entity->getName(), '', 10); $entity->setAlias($alias); // Set timestamps $model->setTimestamps($entity, true, false); // Save the form first and new actions so that new fields are available to actions. // Using the repository function to not trigger the listeners twice. $model->getRepository()->saveEntity($entity); // Only save actions that are not to be deleted $actions = array_diff_key($modifiedActions, array_flip($deletedActions)); // Set and persist actions $model->setActions($entity, $actions); // Save and trigger listeners $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); $this->addFlash('mautic.core.notice.created', ['%name%' => $entity->getName(), '%menu_link%' => 'mautic_form_index', '%url%' => $this->generateUrl('mautic_form_action', ['objectAction' => 'edit', 'objectId' => $entity->getId()])]); if ($form->get('buttons')->get('save')->isClicked()) { $viewParameters = ['objectAction' => 'view', 'objectId' => $entity->getId()]; $returnUrl = $this->generateUrl('mautic_form_action', $viewParameters); $template = 'MauticFormBundle:Form:view'; } else { //return edit view so that all the session stuff is loaded return $this->editAction($entity->getId(), true); } } catch (\Exception $e) { $form['name']->addError(new FormError($this->get('translator')->trans('mautic.form.schema.failed', [], 'validators'))); $valid = false; if ('dev' == $this->container->getParameter('kernel.environment')) { throw $e; } } } } } else { $viewParameters = ['page' => $page]; $returnUrl = $this->generateUrl('mautic_form_index', $viewParameters); $template = 'MauticFormBundle:Form:index'; } if ($cancelled || $valid && $form->get('buttons')->get('save')->isClicked()) { //clear temporary fields $this->clearSessionComponents($sessionId); return $this->postActionRedirect(['returnUrl' => $returnUrl, 'viewParameters' => $viewParameters, 'contentTemplate' => $template, 'passthroughVars' => ['activeLink' => '#mautic_form_index', 'mauticContent' => 'form']]); } } else { //clear out existing fields in case the form was refreshed, browser closed, etc $this->clearSessionComponents($sessionId); $modifiedFields = $modifiedActions = $deletedActions = $deletedFields = []; $form->get('sessionId')->setData($sessionId); //add a submit button $keyId = 'new' . hash('sha1', uniqid(mt_rand())); $field = new Field(); $modifiedFields[$keyId] = $field->convertToArray(); $modifiedFields[$keyId]['label'] = $this->translator->trans('mautic.core.form.submit'); $modifiedFields[$keyId]['alias'] = 'submit'; $modifiedFields[$keyId]['showLabel'] = 1; $modifiedFields[$keyId]['type'] = 'button'; $modifiedFields[$keyId]['id'] = $keyId; $modifiedFields[$keyId]['inputAttributes'] = 'class="btn btn-default"'; $modifiedFields[$keyId]['formId'] = $sessionId; unset($modifiedFields[$keyId]['form']); $session->set('mautic.form.' . $sessionId . '.fields.modified', $modifiedFields); } //fire the form builder event $customComponents = $model->getCustomComponents($sessionId); $fieldHelper = $this->get('mautic.helper.form.field_helper'); return $this->delegateView(['viewParameters' => ['fields' => $fieldHelper->getChoiceList($customComponents['fields']), 'actions' => $customComponents['choices'], 'actionSettings' => $customComponents['actions'], 'formFields' => $modifiedFields, 'formActions' => $modifiedActions, 'deletedFields' => $deletedFields, 'deletedActions' => $deletedActions, 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', 'activeForm' => $entity, 'form' => $form->createView(), 'contactFields' => $this->getModel('lead.field')->getFieldListWithProperties()], 'contentTemplate' => 'MauticFormBundle:Builder:index.html.php', 'passthroughVars' => ['activeLink' => '#mautic_form_index', 'mauticContent' => 'form', 'route' => $this->generateUrl('mautic_form_action', ['objectAction' => !empty($valid) ? 'edit' : 'new', 'objectId' => $entity->getId()])]]); }
/** * {@inheritDoc} */ public function convertToArray() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'convertToArray', array()); return parent::convertToArray(); }