/** * Generates edit form and processes post data * * @param int $objectId * @param bool $ignorePost * @param bool $forceTypeSelection * * @return \Symfony\Component\HttpFoundation\JsonResponse|Response */ public function editAction($objectId, $ignorePost = false, $forceTypeSelection = false) { /** @var \Mautic\FormBundle\Model\FormModel $model */ $model = $this->factory->getModel('form'); $formData = $this->request->request->get('mauticform'); $sessionId = isset($formData['sessionId']) ? $formData['sessionId'] : null; if ($objectId instanceof Form) { $entity = $objectId; $objectId = sha1(uniqid(mt_rand(), true)); } else { $entity = $model->getEntity($objectId); // Process submit of cloned form if ($entity == null && $objectId == $sessionId) { $entity = $model->getEntity(); } } $session = $this->factory->getSession(); $cleanSlate = true; //set the page we came from $page = $this->factory->getSession()->get('mautic.form.page', 1); //set the return URL $returnUrl = $this->generateUrl('mautic_form_index', array('page' => $page)); $postActionVars = array('returnUrl' => $returnUrl, 'viewParameters' => array('page' => $page), 'contentTemplate' => 'MauticFormBundle:Form:index', 'passthroughVars' => array('activeLink' => '#mautic_form_index', 'mauticContent' => 'form')); //form not found if ($entity === null) { return $this->postActionRedirect(array_merge($postActionVars, array('flashes' => array(array('type' => 'error', 'msg' => 'mautic.form.error.notfound', 'msgVars' => array('%id%' => $objectId)))))); } elseif (!$this->factory->getSecurity()->hasEntityAccess('form:forms:editown', 'form:forms:editother', $entity->getCreatedBy())) { return $this->accessDenied(); } elseif ($model->isLocked($entity)) { //deny access if the entity is locked return $this->isLocked($postActionVars, $entity, 'form.form'); } $action = $this->generateUrl('mautic_form_action', array('objectAction' => 'edit', 'objectId' => $objectId)); $form = $model->createForm($entity, $this->get('form.factory'), $action); ///Check for a submitted form and process it if (!$ignorePost && $this->request->getMethod() == 'POST') { $valid = false; if (!($cancelled = $this->isFormCancelled($form))) { //set added/updated fields $modifiedFields = $session->get('mautic.form.' . $objectId . '.fields.modified', array()); $deletedFields = $session->get('mautic.form.' . $objectId . '.fields.deleted', array()); $fields = array_diff_key($modifiedFields, array_flip($deletedFields)); //set added/updated actions $modifiedActions = $session->get('mautic.form.' . $objectId . '.actions.modified', array()); $deletedActions = $session->get('mautic.form.' . $objectId . '.actions.deleted', array()); $actions = array_diff_key($modifiedActions, array_flip($deletedActions)); if ($valid = $this->isFormValid($form)) { //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', array(), 'validators'))); $valid = false; } else { $model->setFields($entity, $fields); $model->deleteFields($entity, $deletedFields); if ($entity->isStandalone()) { if (!($alias = $entity->getAlias())) { $alias = $model->cleanAlias($entity->getName(), '', 10); $entity->setAlias($alias); } if (!$entity->getId()) { // Set timestamps because this is a new clone $model->setTimestamps($entity, true, false); } // save the form first so that new fields are available to actions // use the repository method to not trigger listeners twice $model->getRepository()->saveEntity($entity); if (count($actions)) { // Now set and persist the actions $model->setActions($entity, $actions); } // Delete deleted actions if (count($deletedActions)) { $this->factory->getModel('form.action')->deleteEntities($deletedActions); } } else { // Clear the actions $entity->clearActions(); // Delete all actions if (count($modifiedActions)) { $this->factory->getModel('form.action')->deleteEntities(array_keys($modifiedActions)); } } // Persist and execute listeners $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); // Reset objectId to entity ID (can be session ID in case of cloned entity) $objectId = $entity->getId(); $this->addFlash('mautic.core.notice.updated', array('%name%' => $entity->getName(), '%menu_link%' => 'mautic_form_index', '%url%' => $this->generateUrl('mautic_form_action', array('objectAction' => 'edit', 'objectId' => $entity->getId())))); if ($form->get('buttons')->get('save')->isClicked()) { $viewParameters = array('objectAction' => 'view', 'objectId' => $entity->getId()); $returnUrl = $this->generateUrl('mautic_form_action', $viewParameters); $template = 'MauticFormBundle:Form:view'; } } } } else { //unlock the entity $model->unlockEntity($entity); $viewParameters = array('page' => $page); $returnUrl = $this->generateUrl('mautic_form_index', $viewParameters); $template = 'MauticFormBundle:Form:index'; } if ($cancelled || $valid && $form->get('buttons')->get('save')->isClicked()) { //remove fields from session $this->clearSessionComponents($objectId); // Clear session items in case columns changed $session->remove('mautic.formresult.' . $entity->getId() . '.orderby'); $session->remove('mautic.formresult.' . $entity->getId() . '.orderbydir'); $session->remove('mautic.formresult.' . $entity->getId() . '.filters'); return $this->postActionRedirect(array_merge($postActionVars, array('returnUrl' => $returnUrl, 'viewParameters' => $viewParameters, 'contentTemplate' => $template))); } elseif ($form->get('buttons')->get('apply')->isClicked()) { //rebuild everything to include new ids $cleanSlate = true; $reorder = true; } } else { $cleanSlate = true; //lock the entity $model->lockEntity($entity); $form->get('sessionId')->setData($objectId); } // Get field and action settings $customComponents = $model->getCustomComponents(); $fieldHelper = new FormFieldHelper($this->get('translator')); $availableFields = $fieldHelper->getList($customComponents['fields']); if ($cleanSlate) { //clean slate $this->clearSessionComponents($objectId); //load existing fields into session $modifiedFields = array(); $usedLeadFields = array(); $existingFields = $entity->getFields()->toArray(); foreach ($existingFields as $formField) { // Check to see if the field still exists if ($formField->getType() !== 'button' && !isset($availableFields[$formField->getType()])) { continue; } $id = $formField->getId(); $field = $formField->convertToArray(); if (!$id) { // Cloned entity $id = $field['id'] = $field['sessionId'] = 'new' . hash('sha1', uniqid(mt_rand())); } unset($field['form']); if (isset($customComponents['fields'][$field['type']])) { // Set the custom parameters $field['customParameters'] = $customComponents['fields'][$field['type']]; } $modifiedFields[$id] = $field; if (!empty($field['leadField'])) { $usedLeadFields[$id] = $field['leadField']; } } $session->set('mautic.form.' . $objectId . '.fields.leadfields', $usedLeadFields); if (!empty($reorder)) { uasort($modifiedFields, function ($a, $b) { return $a['order'] > $b['order']; }); } $session->set('mautic.form.' . $objectId . '.fields.modified', $modifiedFields); $deletedFields = array(); // Load existing actions into session $modifiedActions = array(); $existingActions = $entity->getActions()->toArray(); foreach ($existingActions as $formAction) { // Check to see if the action still exists if (!isset($customComponents['actions'][$formAction->getType()])) { continue; } $id = $formAction->getId(); $action = $formAction->convertToArray(); if (!$id) { // Cloned entity so use a random Id instead $action['id'] = $id = 'new' . hash('sha1', uniqid(mt_rand())); } unset($action['form']); $modifiedActions[$id] = $action; } if (!empty($reorder)) { uasort($modifiedActions, function ($a, $b) { return $a['order'] > $b['order']; }); } $session->set('mautic.form.' . $objectId . '.actions.modified', $modifiedActions); $deletedActions = array(); } return $this->delegateView(array('viewParameters' => array('fields' => $availableFields, 'actions' => $customComponents['choices'], 'formFields' => $modifiedFields, 'formActions' => $modifiedActions, 'deletedFields' => $deletedFields, 'deletedActions' => $deletedActions, 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', 'activeForm' => $entity, 'form' => $form->createView(), 'forceTypeSelection' => $forceTypeSelection), 'contentTemplate' => 'MauticFormBundle:Builder:index.html.php', 'passthroughVars' => array('activeLink' => '#mautic_form_index', 'mauticContent' => 'form', 'route' => $this->generateUrl('mautic_form_action', array('objectAction' => 'edit', 'objectId' => $entity->getId()))))); }