<commit_before><?php

/*
 * This file is part of Fork CMS.
 *
 * For the full copyright and license information, please view the license
 * file that was distributed with this source code.
 */

/**
 * Save a field via ajax.
 *
 * @author <NAME> <<EMAIL>>
 */
class BackendFormBuilderAjaxSaveField extends BackendBaseAJAXAction
{
	/**
	 * Execute the action
	 */
	public function execute()
	{
		parent::execute();

		// get parameters
		$formId = SpoonFilter::getPostValue('form_id', null, '', 'int');
		$fieldId = SpoonFilter::getPostValue('field_id', null, '', 'int');
		$type = SpoonFilter::getPostValue('type', array('checkbox', 'dropdown', 'heading', 'paragraph', 'radiobutton', 'submit', 'textarea', 'textbox'), '', 'string');
		$label = trim(SpoonFilter::getPostValue('label', null, '', 'string'));
		$values = trim(SpoonFilter::getPostValue('values', null, '', 'string'));
		$defaultValues = trim(SpoonFilter::getPostValue('default_values', null, '', 'string'));
		$required = SpoonFilter::getPostValue('required', array('Y','N'), 'N', 'string');
		$requiredErrorMessage = trim(SpoonFilter::getPostValue('required_error_message', null, '', 'string'));
		$validation = SpoonFilter::getPostValue('validation', array('email', 'numeric'), '', 'string');
		$validationParameter = trim(SpoonFilter::getPostValue('validation_parameter', null, '', 'string'));
		$errorMessage = trim(SpoonFilter::getPostValue('error_message', null, '', 'string'));

		// invalid form id
		if(!BackendFormBuilderModel::exists($formId)) $this->output(self::BAD_REQUEST, null, 'form does not exist');

		// invalid fieldId
		if($fieldId !== 0 && !BackendFormBuilderModel::existsField($fieldId, $formId)) $this->output(self::BAD_REQUEST, null, 'field does not exist');

		// invalid type
		if($type == '') $this->output(self::BAD_REQUEST, null, 'invalid type provided');

		// init
		$errors = array();

		// validate textbox
		if($type == 'textbox')
		{
			if($label == '') $errors['label'] = BL::getError('LabelIsRequired');
			if($required == 'Y' && $requiredErrorMessage == '') $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
			if($validation != '' && $errorMessage == '') $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
		}

		// validate textarea
		elseif($type == 'textarea')
		{
			if($label == '') $errors['label'] = BL::getError('LabelIsRequired');
			if($required == 'Y' && $requiredErrorMessage == '') $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
			if($validation != '' && $errorMessage == '') $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
		}

		// validate heading
		elseif($type == 'heading' && $values == '') $errors['values'] = BL::getError('ValueIsRequired');

		// validate paragraph
		elseif($type == 'paragraph' && $values == '') $errors['values'] = BL::getError('ValueIsRequired');<commit_msg>FormBuilder: reset validation if not a textbox.<commit_after><?php

/*
 * This file is part of Fork CMS.
 *
 * For the full copyright and license information, please view the license
 * file that was distributed with this source code.
 */

/**
 * Save a field via ajax.
 *
 * @author <NAME> <<EMAIL>>
 */
class BackendFormBuilderAjaxSaveField extends BackendBaseAJAXAction
{
	/**
	 * Execute the action
	 */
	public function execute()
	{
		parent::execute();

		// get parameters
		$formId = SpoonFilter::getPostValue('form_id', null, '', 'int');
		$fieldId = SpoonFilter::getPostValue('field_id', null, '', 'int');
		$type = SpoonFilter::getPostValue('type', array('checkbox', 'dropdown', 'heading', 'paragraph', 'radiobutton', 'submit', 'textarea', 'textbox'), '', 'string');
		$label = trim(SpoonFilter::getPostValue('label', null, '', 'string'));
		$values = trim(SpoonFilter::getPostValue('values', null, '', 'string'));
		$defaultValues = trim(SpoonFilter::getPostValue('default_values', null, '', 'string'));
		$required = SpoonFilter::getPostValue('required', array('Y','N'), 'N', 'string');
		$requiredErrorMessage = trim(SpoonFilter::getPostValue('required_error_message', null, '', 'string'));
		$validation = SpoonFilter::getPostValue('validation', array('email', 'numeric'), '', 'string');
		$validationParameter = trim(SpoonFilter::getPostValue('validation_parameter', null, '', 'string'));
		$errorMessage = trim(SpoonFilter::getPostValue('error_message', null, '', 'string'));

		// invalid form id
		if(!BackendFormBuilderModel::exists($formId)) $this->output(self::BAD_REQUEST, null, 'form does not exist');

		// invalid fieldId
		if($fieldId !== 0 && !BackendFormBuilderModel::existsField($fieldId, $formId)) $this->output(self::BAD_REQUEST, null, 'field does not exist');

		// invalid type
		if($type == '') $this->output(self::BAD_REQUEST, null, 'invalid type provided');

		// extra validation is only possible for textfields
		if($type != 'textbox')
		{
			$validation = '';
			$validationParameter = '';
			$errorMessage = '';
		}

		// init
		$errors = array();

		// validate textbox
		if($type == 'textbox')
		{
			if($label == '') $errors['label'] = BL::getError('LabelIsRequired');
			if($required == 'Y' && $requiredErrorMessage == '') $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
			if($validation != '' && $errorMessage == '') $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
		}

		// validate textarea
		elseif($type == 'textarea')
		{
			if($label == '') $errors['label'] = BL::getError('LabelIsRequired');
			if($required == 'Y' && $requiredErrorMessage == '') $errors['required_error_message'] = BL::getError('ErrorMessageIsRequired');
			if($validation != '' && $errorMessage == '') $errors['error_message'] = BL::getError('ErrorMessageIsRequired');
		}

		// validate heading
		elseif($type == 'heading' && $values == '') $errors['values'] = BL::getError('ValueIsRequired');

		// validate paragraph
		elseif($type == 'paragraph' && $values == '') $errors['values'] = BL::getError('ValueIsRequired');
{'fraction_non_alphanumeric': 0.1998663101604278, 'fraction_numerical': 0.00033422459893048126, 'mean_word_length': 5.801136363636363, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '3624289', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1766, 'n_words': 377}