public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $valid_steps = $this->Report()->Template()->Steps()->column();
     $entity_survey_temp = $this->Report()->Template()->EntitySurveys();
     foreach ($entity_survey_temp as $est) {
         $est_steps = $est->Steps()->column();
         $valid_steps = array_merge($valid_steps, $est_steps);
     }
     $questionList = SurveyMultiValueQuestionTemplate::get()->filter(array('StepID' => $valid_steps))->sort('Label')->map('ID', 'Label')->toArray();
     $questionSelect = DropdownField::create('QuestionID', 'Question')->setSource($questionList);
     $fields->replaceField('QuestionID', $questionSelect);
     return $fields;
 }
    private function buildSurveyBuilderHeaders($flat_fields = array(), $flat_fields_entity = array())
    {
        $survey_header_query = <<<SQL
SELECT SS.Name, Q.ID AS QuestionID, Q.Name, Q.ClassName FROM SurveyTemplate S
INNER JOIN SurveyStepTemplate SS ON SS.SurveyTemplateID = S.ID
INNER JOIN SurveyQuestionTemplate Q ON Q.StepID = SS.ID
WHERE S.ClassName = 'SurveyTemplate' AND Q.ClassName <> 'SurveyLiteralContentQuestionTemplate'
ORDER BY SS.`Order`, Q.`Order`;
SQL;
        $res = DB::query($survey_header_query);
        $template_1 = array();
        $template_1['SurveyID'] = null;
        $template_1['CreatedByID'] = null;
        $template_1['Email'] = null;
        $template_1['Created'] = null;
        $template_1['LastEdited'] = null;
        foreach ($res as $row) {
            $name = $row['Name'];
            if (in_array($name, $flat_fields)) {
                $q = SurveyMultiValueQuestionTemplate::get()->byID(intval($row['QuestionID']));
                if (is_null($q)) {
                    continue;
                }
                foreach ($q->Values() as $v) {
                    $header = sprintf('%s - %s', $name, $v->Value);
                    $template_1[$header] = null;
                }
            } else {
                $template_1[$name] = null;
            }
        }
        $entity_survey_header_query = <<<SQL
SELECT SS.Name, Q.ID AS QuestionID, Q.Name, Q.ClassName FROM SurveyTemplate S
INNER JOIN EntitySurveyTemplate ES ON ES.ID = S.ID
INNER JOIN SurveyStepTemplate SS ON SS.SurveyTemplateID = S.ID
INNER JOIN SurveyQuestionTemplate Q ON Q.StepID = SS.ID
WHERE
S.ClassName = 'EntitySurveyTemplate'
AND Q.ClassName <> 'SurveyLiteralContentQuestionTemplate'
AND ES.EntityName = 'Deployment'
ORDER BY SS.`Order`, Q.`Order`;
SQL;
        $res = DB::query($entity_survey_header_query);
        $template_2 = array('DeploymentID' => null);
        foreach ($res as $row) {
            $name = $row['Name'];
            if (in_array($name, $flat_fields_entity)) {
                $q = SurveyMultiValueQuestionTemplate::get()->byID(intval($row['QuestionID']));
                if (is_null($q)) {
                    continue;
                }
                foreach ($q->Values() as $v) {
                    $header = sprintf('%s - %s', $name, $v->Value);
                    $template_2[$header] = null;
                }
            } else {
                if ($row['ClassName'] === 'SurveyRadioButtonMatrixTemplateQuestion') {
                    $q = SurveyRadioButtonMatrixTemplateQuestion::get()->byID(intval($row['QuestionID']));
                    if (is_null($q)) {
                        continue;
                    }
                    foreach ($q->Rows() as $r) {
                        $header = sprintf('%s - %s', $name, $r->Value);
                        $template_2[$header] = null;
                    }
                } else {
                    $template_2[$name] = null;
                }
            }
        }
        return array($template_1, $template_2);
    }