Esempio n. 1
0
 /**
  * Generates edit form and processes post data.
  *
  * @param int $objectId
  *
  * @return JsonResponse
  */
 public function editAction($objectId)
 {
     $session = $this->get('session');
     $method = $this->request->getMethod();
     $formId = $method == 'POST' ? $this->request->request->get('formaction[formId]', '', true) : $this->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 ($formAction !== null) {
         $actionType = $formAction['type'];
         $customComponents = $this->getModel('form.form')->getCustomComponents();
         $formAction['settings'] = $customComponents['actions'][$actionType];
         //ajax only for form fields
         if (!$actionType || !$this->request->isXmlHttpRequest() || !$this->get('mautic.security')->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')) {
             return $this->modalAccessDenied();
         }
         $form = $this->get('form.factory')->create('formaction', $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 ($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->get('session');
                     $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->get('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 ($actionType == 'button') {
                         $submits = $session->get('mautic.formactions.submits', []);
                         if ($formAction['properties']['type'] == 'submit' && !in_array($keyId, $submits)) {
                             //button type updated to submit
                             $submits[] = $keyId;
                             $session->set('mautic.formactions.submits', $submits);
                         } elseif ($formAction['properties']['type'] != 'submit' && 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'] = isset($formAction['settings']['formTheme']) ? $this->setFormTheme($form, 'MauticFormBundle:Builder:action.html.php', $formAction['settings']['formTheme']) : $form->createView();
             $viewParams['actionHeader'] = $this->get('translator')->trans($formAction['settings']['label']);
         }
         $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'] : 'MauticFormBundle:Action:generic.html.php';
             $passthroughVars['actionHtml'] = $this->renderView($template, ['inForm' => true, 'action' => $formAction, 'id' => $keyId, 'formId' => $formId]);
         }
         if ($closeModal) {
             //just close the modal
             $passthroughVars['closeModal'] = 1;
             $response = new JsonResponse($passthroughVars);
             return $response;
         }
         return $this->ajaxAction(['contentTemplate' => 'MauticFormBundle:Builder:' . $viewParams['tmpl'] . '.html.php', 'viewParameters' => $viewParams, 'passthroughVars' => $passthroughVars]);
     }
     $response = new JsonResponse(['success' => 0]);
     return $response;
 }
 /**
  * {@inheritDoc}
  */
 public function convertToArray()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'convertToArray', array());
     return parent::convertToArray();
 }
 /**
  * Undeletes the entity
  *
  * @param $objectId
  *
  * @return JsonResponse
  */
 public function undeleteAction($objectId)
 {
     $session = $this->factory->getSession();
     $formId = $this->request->query->get('formId');
     $actions = $session->get('mautic.form.' . $formId . '.actions.modified', array());
     $delete = $session->get('mautic.form.' . $formId . '.actions.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();
     }
     $formAction = array_key_exists($objectId, $actions) ? $actions[$objectId] : null;
     if ($this->request->getMethod() == 'POST' && $formAction !== null) {
         //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 . '.actions.deleted', $delete);
         }
         //take note if this is a submit button or not
         if ($formAction['type'] == 'button') {
             $properties = $formAction['properties'];
             if ($properties['type'] == 'submit') {
                 $submits = $session->get('mautic.formactions.submits', array());
                 $submits[] = $objectId;
                 $session->set('mautic.formactions.submits', $submits);
             }
         }
         $template = !empty($formAction['settings']['template']) ? $formAction['settings']['template'] : 'MauticFormBundle:Action:generic.html.php';
         //prevent undefined errors
         $entity = new Action();
         $blank = $entity->convertToArray();
         $formAction = array_merge($blank, $formAction);
         $dataArray = array('mauticContent' => 'formAction', 'success' => 1, 'target' => '#mauticform_' . $objectId, 'route' => false, 'actionId' => $objectId, 'actionHtml' => $this->renderView($template, array('inForm' => true, 'action' => $formAction, '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;
 }