/**
  * Ajax implementation of the function "validateField". Will display the
  * result encoded in JSON.
  *
  * @param  array $arguments Parameters.
  * @return string    JSON encoded result.
  */
 public function ajaxValidateField($arguments)
 {
     // @todo : Exception ?
     if (!isset($arguments['fieldName']) || !isset($arguments['value']) || !isset($arguments['pageUid'])) {
         return '';
     }
     // @todo : check pageUid
     $field = Fields\Field::getField($arguments['fieldName'], $arguments['pageUid']);
     $field->setValue($arguments['value']);
     $validation = $this->validateField($field);
     $validationResult = Core::convertValidationResultToArray($validation['validationResult']);
     $validation['validationResult'] = $validationResult;
     return $validation;
 }
 /**
  * Will initialize all the fields, based on the uid of the model page. Will
  * fill the values of these fields with the duplication data.
  *
  * @param	array	$fieldsValues	The fields values in an array: name => value.
  * @return	$this
  * @throws	\Exception
  */
 private function initializeFields(array $fieldsValues)
 {
     $id = $this->getModelPageUid();
     if ($id) {
         $this->fields = Field::getFields($id);
         if (is_array($fieldsValues) && !empty($fieldsValues)) {
             foreach ($fieldsValues as $fieldName => $fieldValue) {
                 if (array_key_exists($fieldName, $this->fields)) {
                     $this->fields[$fieldName]->setValue($fieldValue);
                 }
             }
         }
     }
     return $this;
 }
 /**
  * Contains the main form for the creation of a new site.
  *
  * If the form has been submitted, fields' values will be checked so they
  * must match their rules declared in the main configuration. If no error
  * occurs, the site duplication process will start.
  *
  * If the argument "modifySite" is sent, the site has already been
  * duplicated and saved, and the form will be filled with the already saved
  * configuration of this site.
  *
  * @throws \Exception
  */
 public function newAction()
 {
     // Models sites which can be duplicated.
     $modelSites = FieldsConfigurationPresets::getModelSitesList();
     if (empty($modelSites)) {
         $this->view->assign('noModelSite', true);
     } else {
         // Getting the selected model site. If none has been submitted, the default one (first one in the list) is used.
         $selectedModelSite = $this->request->hasArgument('fields') && isset($this->request->getArgument('fields')['modelSite']) ? $this->request->getArgument('fields')['modelSite'] : key($modelSites);
         if (!$selectedModelSite) {
             throw new \Exception('Fatal error: the module should have a model site value.', 1423830741);
         }
         if (!Core::checkUidIsModelSite($selectedModelSite)) {
             throw new \Exception('Fatal error: the model site "' . $selectedModelSite . '" is not a correct value.', 1423830834);
         }
         $checkHideInSiteModification = false;
         // Checking if a site is being edited, and if the uid of this site is correct.
         $modifySite = null;
         if ($this->request->hasArgument('modifySite')) {
             $modifySite = intval($this->request->getArgument('modifySite'));
             Core::checkUidIsSavedSite($modifySite);
             $checkHideInSiteModification = true;
         }
         if ($modifySite !== null) {
             $pageUid = $this->request->getArgument('modifySite');
             $this->view->assign('modifySite', $pageUid);
             /** @var Save $savedSite */
             $savedSite = $this->saveRepository->findOneByRootPageUid($pageUid);
             $fields = $this->fillFieldsValuesFromSavedSite($savedSite, false);
         } else {
             $fields = Field::getFields($selectedModelSite, $checkHideInSiteModification);
         }
         // The form has been submitted, the site might be duplicated.
         if ($this->request->hasArgument('submitted')) {
             $this->processFormSubmit($fields);
         } else {
             $this->view->assign('refreshForm', true);
             if ($modifySite === null) {
                 $constantsValues = ConstantManagerUtility::getTemplateConstantsValues($selectedModelSite, array_keys($fields));
                 foreach ($constantsValues as $fieldName => $fieldValue) {
                     $fields[$fieldName]->setValue($fieldValue);
                 }
             }
         }
         // Creating a unique id for the generated form.
         $this->view->assign('formId', 'SiteFactoryForm_' . md5(serialize($this)));
         $this->view->assign('fieldsConfiguration', $fields);
     }
 }