<commit_before><?php

/**
 * @package userforms
 */

class UserDefinedForm extends Page {

	/**
	 * @var string
	 */
	private static $description = 'Adds a customizable form.';

	/**
	 * @var string Required Identifier
	 */
	private static $required_identifier = null;

	/**
	 * Prevent translatable module from attepmting to translate FieldEditor
	 *
	 * @var array
	 * @config
	 */
	private static $translate_excluded_fields = array(
		'Fields'
	);

	/**
	 * @var array Fields on the user defined form page.
	 */
	private static $db = array(
		"SubmitButtonText" => "Varchar",
		"ClearButtonText" => "Varchar",
		"OnCompleteMessage" => "HTMLText",
		"ShowClearButton" => "Boolean",
		'DisableSaveSubmissions' => 'Boolean',
		'EnableLiveValidation' => 'Boolean',
		'HideFieldLabels' => 'Boolean',
		'DisableAuthenicatedFinishAction' => 'Boolean',
		'DisableCsrfSecurityToken' => 'Boolean'
	);

	/**
	 * @var array Default values of variables when this page is created
	 */
	private static $defaults = array(
		'Content' => '$UserDefinedForm',
		'DisableSaveSubmissions' => 0,
		'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
	);

	/**
	 * @var array
	 */
	private static $has_many = array(
		"Fields" => "EditableFormField",
		"Submissions" => "SubmittedForm",
		"EmailRecipients" => "UserDefinedForm_EmailRecipient"
	);

	/**
	 * Temporary storage of field ids when the form is duplicated.
	 * Example layout: array('EditableCheckbox3' => 'EditableCheckbox14')
	 * @var array
	 */
	protected $fieldsFromTo = array();

	/**
	 * @return FieldList
	 */
	 public function getCMSFields() {

		$self = $this;

		$this->beforeUpdateCMSFields(function($fields) use ($self) {

			// define tabs
			$fields->findOrMakeTab('Root.FormContent', _t('UserDefinedForm.FORM', 'Form'));
			$fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
			$fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));

			// field editor
			$fields->addFieldToTab('Root.FormContent', new FieldEditor('Fields', 'Fields', '', $self ));

			// text to show on complete
			$onCompleteFieldSet = new CompositeField(
				$label = new LabelField('OnCompleteMessageLabel',_t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')),
				$editor = new HtmlEditorField( 'OnCompleteMessage', '', _t('UserDefinedForm.ONCOMPLETEMESSAGE', $self->OnCompleteMessage))
			);

			$onCompleteFieldSet->addExtraClass('field');

			$editor->setRows(3);
			$label->addExtraClass('left');

			// Set the summary fields of UserDefinedForm_EmailRecipient dynamically via config system
			Config::inst()->update(
				'UserDefinedForm_EmailRecipient',
				'summary_fields',
				array(
					'EmailAddress' => _t('UserDefinedForm.EMAILADDRESS', 'Email'),
					'EmailSubject' => _t('UserDefinedForm.EMAILSUBJECT', 'Subject'),
					'EmailFrom' => _t('UserDefinedForm.EMAILFROM', 'From'),
				)
			);

			// who do we email on submission
			$emailRecipients = new GridField('EmailRecipients', _t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $self->EmailRecipients(), GridFieldConfig_RecordEditor::create(10));
			$emailRecipients->getConfig()->getComponentByType('GridFieldAddNewButton')->setButtonName(
				_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
			);

			$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet);
			$fields->addFieldToTab('Root.FormOptions', $emailRecipients);
			$fields->addFieldsToTab('Root.FormOptions', $self->getFormOptions());


			// view the submissions
			$submissions = new GridField(
				'Submissions',
				_t('UserDefinedForm.SUBMISSIONS', 'Submissions'),
				 $self->Submissions()->sort('Created', 'DESC')
			);

			// make sure a numeric not a empty string is checked against this int column for SQL server
			$parentID = (!empty($self->ID)) ? $self->ID : 0;

			// get a list of all field names and values used for print and export CSV views of the GridField below.
			$columnSQL = <<<SQL
SELECT "Name", "Title"
FROM "SubmittedFormField"
LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID"
WHERE "SubmittedForm"."ParentID" = '$parentID'
ORDER BY "Title" ASC
SQL;
			$columns = DB::query($columnSQL)->map();

			$config = new GridFieldConfig();
			$config->addComponent(new GridFieldToolbarHeader());
			$config->addComponent($sort = new GridFieldSortableHeader());
			$config->addComponent($filter = new UserFormsGridFieldFilterHeader());
			$config->addComponent(new GridFieldDataColumns());
			$config->addComponent(new GridFieldEditButton());
			$config->addComponent(new GridState_Component());
			$config->addComponent(new GridFieldDeleteAction());
			$config->addComponent(new GridFieldPageCount('toolbar-header-right'));
			$config->addComponent($pagination = new GridFieldPaginator(25));
			$config->addComponent(new GridFieldDetailForm());
			$config->addComponent($excelexport = new GridFieldExportToExcelButton());
			$config->addComponent($export = new GridFieldExportButton());
			$config->addComponent($print = new GridFieldPrintButton());

			/**
			 * Support for {@link https://github.com/colymba/GridFieldBulkEditingTools}
			 */
			if(class_exists('GridFieldBulkManager')) {
				$config->addComponent(new GridFieldBulkManager());
			}

			$sort->setThrowExceptionOnBadDataType(false);
			$filter->setThrowExceptionOnBadDataType(false);
			$pagination->setThrowExceptionOnBadDataType(false);

			// attach every column to the print view form
			$columns['Created'] = 'Created';
			$filter->setColumns($columns);

			// excel export configuration
			$excelexport->setExportColumns($columns);

			// print configuration
			$print->setPrintHasHeader(true);
			$print->setPrintColumns($columns);

			// export configuration
			$export->setCsvHasHeader(true);
			$export->setExportColumns($columns);

			$submissions->setConfig($config);
			$fields->addFieldToTab('Root.Submissions', $submissions);
			$fields->addFieldToTab('Root.FormOptions', new CheckboxField('DisableSaveSubmissions', _t('UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')));

		});

		$fields = parent::getCMSFields();

		return $fields;
	}


	/**
	 * When publishing copy the editable form fields to the live database
	 * Not going to version emails and submissions as they are likely to
	 * persist over multiple versions.
	 *
	 * @return void
	 */
	public function doPublish() {
		$parentID = (!empty($this->ID)) ? $this->ID : 0;
		// remove fields on the live table which could have been orphaned.
		$live = Versioned::get_by_stage("EditableFormField", "Live", "\"EditableFormField\".\"ParentID\" = $parentID");

		if($live) {
			foreach($live as $field) {
				$field->doDeleteFromStage('Live');
			}<commit_msg>Ensure Excel export contains all columns<commit_after><?php

/**
 * @package userforms
 */

class UserDefinedForm extends Page {

	/**
	 * @var string
	 */
	private static $description = 'Adds a customizable form.';

	/**
	 * @var string Required Identifier
	 */
	private static $required_identifier = null;

	/**
	 * Prevent translatable module from attepmting to translate FieldEditor
	 *
	 * @var array
	 * @config
	 */
	private static $translate_excluded_fields = array(
		'Fields'
	);

	/**
	 * @var array Fields on the user defined form page.
	 */
	private static $db = array(
		"SubmitButtonText" => "Varchar",
		"ClearButtonText" => "Varchar",
		"OnCompleteMessage" => "HTMLText",
		"ShowClearButton" => "Boolean",
		'DisableSaveSubmissions' => 'Boolean',
		'EnableLiveValidation' => 'Boolean',
		'HideFieldLabels' => 'Boolean',
		'DisableAuthenicatedFinishAction' => 'Boolean',
		'DisableCsrfSecurityToken' => 'Boolean'
	);

	/**
	 * @var array Default values of variables when this page is created
	 */
	private static $defaults = array(
		'Content' => '$UserDefinedForm',
		'DisableSaveSubmissions' => 0,
		'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
	);

	/**
	 * @var array
	 */
	private static $has_many = array(
		"Fields" => "EditableFormField",
		"Submissions" => "SubmittedForm",
		"EmailRecipients" => "UserDefinedForm_EmailRecipient"
	);

	/**
	 * Temporary storage of field ids when the form is duplicated.
	 * Example layout: array('EditableCheckbox3' => 'EditableCheckbox14')
	 * @var array
	 */
	protected $fieldsFromTo = array();

	/**
	 * @return FieldList
	 */
	 public function getCMSFields() {

		$self = $this;

		$this->beforeUpdateCMSFields(function($fields) use ($self) {

			// define tabs
			$fields->findOrMakeTab('Root.FormContent', _t('UserDefinedForm.FORM', 'Form'));
			$fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
			$fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));

			// field editor
			$fields->addFieldToTab('Root.FormContent', new FieldEditor('Fields', 'Fields', '', $self ));

			// text to show on complete
			$onCompleteFieldSet = new CompositeField(
				$label = new LabelField('OnCompleteMessageLabel',_t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')),
				$editor = new HtmlEditorField( 'OnCompleteMessage', '', _t('UserDefinedForm.ONCOMPLETEMESSAGE', $self->OnCompleteMessage))
			);

			$onCompleteFieldSet->addExtraClass('field');

			$editor->setRows(3);
			$label->addExtraClass('left');

			// Set the summary fields of UserDefinedForm_EmailRecipient dynamically via config system
			Config::inst()->update(
				'UserDefinedForm_EmailRecipient',
				'summary_fields',
				array(
					'EmailAddress' => _t('UserDefinedForm.EMAILADDRESS', 'Email'),
					'EmailSubject' => _t('UserDefinedForm.EMAILSUBJECT', 'Subject'),
					'EmailFrom' => _t('UserDefinedForm.EMAILFROM', 'From'),
				)
			);

			// who do we email on submission
			$emailRecipients = new GridField('EmailRecipients', _t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $self->EmailRecipients(), GridFieldConfig_RecordEditor::create(10));
			$emailRecipients->getConfig()->getComponentByType('GridFieldAddNewButton')->setButtonName(
				_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
			);

			$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet);
			$fields->addFieldToTab('Root.FormOptions', $emailRecipients);
			$fields->addFieldsToTab('Root.FormOptions', $self->getFormOptions());


			// view the submissions
			$submissions = new GridField(
				'Submissions',
				_t('UserDefinedForm.SUBMISSIONS', 'Submissions'),
				 $self->Submissions()->sort('Created', 'DESC')
			);

			// make sure a numeric not a empty string is checked against this int column for SQL server
			$parentID = (!empty($self->ID)) ? $self->ID : 0;

			// get a list of all field names and values used for print and export CSV views of the GridField below.
			$columnSQL = <<<SQL
SELECT "Name", "Title"
FROM "SubmittedFormField"
LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID"
WHERE "SubmittedForm"."ParentID" = '$parentID'
ORDER BY "Title" ASC
SQL;
			$columns = DB::query($columnSQL)->map();

			$config = new GridFieldConfig();
			$config->addComponent(new GridFieldToolbarHeader());
			$config->addComponent($sort = new GridFieldSortableHeader());
			$config->addComponent($filter = new UserFormsGridFieldFilterHeader());
			$config->addComponent(new GridFieldDataColumns());
			$config->addComponent(new GridFieldEditButton());
			$config->addComponent(new GridState_Component());
			$config->addComponent(new GridFieldDeleteAction());
			$config->addComponent(new GridFieldPageCount('toolbar-header-right'));
			$config->addComponent($pagination = new GridFieldPaginator(25));
			$config->addComponent(new GridFieldDetailForm());
			$config->addComponent($excelexport = new GridFieldExportToExcelButton());
			$config->addComponent($export = new GridFieldExportButton());
			$config->addComponent($print = new GridFieldPrintButton());

			/**
			 * Support for {@link https://github.com/colymba/GridFieldBulkEditingTools}
			 */
			if(class_exists('GridFieldBulkManager')) {
				$config->addComponent(new GridFieldBulkManager());
			}

			$sort->setThrowExceptionOnBadDataType(false);
			$filter->setThrowExceptionOnBadDataType(false);
			$pagination->setThrowExceptionOnBadDataType(false);

			// attach every column to the print view form
			$columns['Created'] = 'Created';
			$filter->setColumns($columns);

			// print configuration
			$print->setPrintHasHeader(true);
			$print->setPrintColumns($columns);

			// export configuration
			$export->setCsvHasHeader(true);
			$export->setExportColumns($columns);

			// excel export configuration
			$excelexport->setExportColumns($columns);

			$submissions->setConfig($config);
			$fields->addFieldToTab('Root.Submissions', $submissions);
			$fields->addFieldToTab('Root.FormOptions', new CheckboxField('DisableSaveSubmissions', _t('UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')));

		});

		$fields = parent::getCMSFields();

		return $fields;
	}


	/**
	 * When publishing copy the editable form fields to the live database
	 * Not going to version emails and submissions as they are likely to
	 * persist over multiple versions.
	 *
	 * @return void
	 */
	public function doPublish() {
		$parentID = (!empty($this->ID)) ? $this->ID : 0;
		// remove fields on the live table which could have been orphaned.
		$live = Versioned::get_by_stage("EditableFormField", "Live", "\"EditableFormField\".\"ParentID\" = $parentID");

		if($live) {
			foreach($live as $field) {
				$field->doDeleteFromStage('Live');
			}
{'fraction_non_alphanumeric': 0.14837712519319937, 'fraction_numerical': 0.0016191948185765804, 'mean_word_length': 4.867012089810017, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 149, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737244', 'n_tokens_mistral': 4654, 'n_tokens_neox': 4008, 'n_words': 889}