/** * @param $request * @return SS_HTTPResponse|void */ public function AddEntity($request) { try { if (!Member::currentUser()) { return $this->httpError(403); } $step = $request->param('STEP_SLUG'); $sub_step = $request->param('SUB_STEP_SLUG'); $current_survey = $this->getCurrentSurveyInstance(); $current_step = $current_survey->currentStep(); if (!$current_step instanceof ISurveyDynamicEntityStep) { if ($this->current_survey->isAllowedStep($step)) { $current_step = $this->current_survey->getStep($step); $this->survey_manager->registerCurrentStep($this->current_survey, $step); } else { throw new LogicException(sprintf('template %s', $current_step->template()->title())); } } //create the current survey entity $this->current_entity_survey = $this->survey_manager->buildEntitySurvey($current_step, Member::currentUserID()); $url = sprintf('%s%s/edit/%s', $this->Link(), $current_step->template()->title(), $this->current_entity_survey->getIdentifier()); $this->redirect($url); } catch (Exception $ex) { SS_Log::log($ex->getMessage(), SS_Log::ERR); return $this->httpError(404); } }
/** * @param ISurvey $survey * @param ISurveyBuilder $survey_builder * @param ISurveyManager $manager */ public function autoPopulate(ISurvey $survey, ISurveyBuilder $survey_builder, ISurveyManager $manager) { $template = $survey->template(); $owner = $survey->createdBy(); $mappings = $template->getAutopopulationMappings(); // get former survey ... $old_survey = DeploymentSurvey::get()->filter('MemberID', $owner->getIdentifier())->sort('Created', 'DESC')->first(); if (is_null($old_survey)) { return; } foreach ($mappings as $mapping) { if (!$mapping instanceof IOldSurveyMigrationMapping) { continue; } $origin_table_name = $mapping->getOriginTableName(); $origin_field_name = $mapping->getOriginFieldName(); $question = $mapping->getTargetQuestion(); if (is_null($question)) { continue; } $step_template = $question->step(); if (is_null($step_template)) { continue; } $step = $survey->getStep($step_template->title()); if (is_null($step)) { continue; } if (!$step instanceof ISurveyRegularStep) { continue; } if ($origin_table_name === 'DeploymentSurvey') { $old_data = $old_survey->{$origin_field_name}; // old data is only a label, we need to find out the value $data = $old_data; if ($question instanceof IMultiValueQuestionTemplate) { $data = ''; if ($question instanceof IDropDownQuestionTemplate && $question->isCountrySelector()) { $data = $old_data; } else { $old_data = explode(',', $old_data); foreach ($old_data as $od) { $v = $question->getValueByValue($od); if (is_null($v)) { continue; } if (!empty($data)) { $data .= ','; } $data .= $v->getIdentifier(); } } } if (empty($data)) { continue; } $step->addAnswer($survey_builder->buildAnswer($question, $data)); } if ($origin_table_name === 'AppDevSurvey') { $app_dev_survey = $old_survey->AppDevSurveys()->first(); if (!$app_dev_survey) { continue; } $old_data = $app_dev_survey->{$origin_field_name}; $data = $old_data; if ($question instanceof IMultiValueQuestionTemplate) { $data = ''; $old_data = explode(',', $old_data); foreach ($old_data as $od) { $v = $question->getValueByValue($od); if (is_null($v)) { continue; } if (!empty($data)) { $data .= ','; } $data .= $v->getIdentifier(); } } if (empty($data)) { continue; } $step->addAnswer($survey_builder->buildAnswer($question, $data)); } } //check if we need to auto-populate entities foreach ($template->getEntities() as $entity_survey) { if ($entity_survey->belongsToDynamicStep() && $entity_survey->shouldPrepopulateWithFormerData()) { $mappings = $entity_survey->getAutopopulationMappings(); $dyn_step = $survey->getStep($entity_survey->getDynamicStepTemplate()->title()); if (is_null($dyn_step)) { continue; } foreach ($old_survey->Deployments() as $old_deployment) { $entity_survey = $manager->buildEntitySurvey($dyn_step, $owner->getIdentifier()); foreach ($mappings as $mapping) { if (!$mapping instanceof IOldSurveyMigrationMapping) { continue; } $origin_table_name = $mapping->getOriginTableName(); $origin_field_name = $mapping->getOriginFieldName(); $question = $mapping->getTargetQuestion(); $step_template = $question->step(); $step = $entity_survey->getStep($step_template->title()); if (!$step instanceof ISurveyRegularStep) { continue; } if ($origin_table_name === 'Deployment') { $old_data = $old_deployment->{$origin_field_name}; $data = $old_data; if ($question instanceof IMultiValueQuestionTemplate) { $data = ''; $old_data = explode(',', $old_data); foreach ($old_data as $od) { $v = $question->getValueByValue($od); if (is_null($v)) { continue; } if (!empty($data)) { $data .= ','; } $data .= $v->getIdentifier(); } } if (empty($data)) { continue; } $step->addAnswer($survey_builder->buildAnswer($question, $data)); } } } } } }