<commit_before><?php
/**
 * Gift voucher products, when purchased will send out a voucher code to the customer via email.
 */
class GiftVoucherProduct extends Product{

	private static $db = array(
		"VariableAmount" => "Boolean",
		"MinimumAmount" => "Currency"
	);

	private static $singular_name = "Gift Voucher";
	private static $plural_name = "Gift Vouchers";

	private static $order_item = "GiftVoucher_OrderItem";

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Pricing",
			new OptionsetField("VariableAmount", "Price", array(
				0 => "Fixed",
				1 => "Allow customer to choose"
			)),
			"BasePrice"
		);
		$fields->addFieldsToTab("Root.Pricing", array(
			//text field, because of CMS js validation issue
			$minimumamount = new TextField("MinimumAmount", "Minimum Amount")
		));
		$fields->removeByName("CostPrice");
		$fields->removeByName("Variations");
		$fields->removeByName("Model");
		return $fields;
	}

	public function canPurchase($member = null, $quantity = 1) {
		if(!self::config()->get('global_allow_purchase')){
			return false;
		}
		if(!$this->dbObject('AllowPurchase')->getValue()){
			return false;
		}
		if(!$this->isPublished()){
			return false;
		}
		return true;
	}

}

class GiftVoucherProduct_Controller extends Product_Controller{

	private static $allowed_actions = array(
		'Form'
	);
    
	public function Form() {
		$form = parent::Form();
		if($this->VariableAmount){
			$form->setSaveableFields(array(
				"UnitPrice"
			));
			$form->Fields()->push(
				//TODO: set minimum amount
				$giftamount = new CurrencyField("UnitPrice", _t('GiftVoucherProduct.Amount', 'Amount'), $this->BasePrice)
			);
			$giftamount->setForm($form);
		}
		$form->setValidator($validator = new GiftVoucherFormValidator(array(
			"Quantity", "UnitPrice"
		)));
		return $form;
	}

}

class GiftVoucherFormValidator extends RequiredFields{

	public function php($data) {
		$valid =  parent::php($data);
		if($valid){
			$controller = $this->form->Controller();
			if($controller->VariableAmount){
				$giftvalue = $data['UnitPrice'];
				if($controller->MinimumAmount > 0 && $giftvalue < $controller->MinimumAmount){
					$this->validationError("UnitPrice", "Gift value must be at least ".$controller->MinimumAmount);
					return false;
				}
				if($giftvalue <= 0){
					$this->validationError("UnitPrice", "Gift value must be greater than 0");
					return false;
				}
			}
		}
		return $valid;
	}

}
<commit_msg>Added ability to translate MinimumAmountError<commit_after><?php
/**
 * Gift voucher products, when purchased will send out a voucher code to the customer via email.
 */
class GiftVoucherProduct extends Product{

	private static $db = array(
		"VariableAmount" => "Boolean",
		"MinimumAmount" => "Currency"
	);

	private static $singular_name = "Gift Voucher";
	private static $plural_name = "Gift Vouchers";

	private static $order_item = "GiftVoucher_OrderItem";

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Pricing",
			new OptionsetField("VariableAmount", "Price", array(
				0 => "Fixed",
				1 => "Allow customer to choose"
			)),
			"BasePrice"
		);
		$fields->addFieldsToTab("Root.Pricing", array(
			//text field, because of CMS js validation issue
			$minimumamount = new TextField("MinimumAmount", "Minimum Amount")
		));
		$fields->removeByName("CostPrice");
		$fields->removeByName("Variations");
		$fields->removeByName("Model");
		return $fields;
	}

	public function canPurchase($member = null, $quantity = 1) {
		if(!self::config()->get('global_allow_purchase')){
			return false;
		}
		if(!$this->dbObject('AllowPurchase')->getValue()){
			return false;
		}
		if(!$this->isPublished()){
			return false;
		}
		return true;
	}

}

class GiftVoucherProduct_Controller extends Product_Controller{

	private static $allowed_actions = array(
		'Form'
	);
    
	public function Form() {
		$form = parent::Form();
		if($this->VariableAmount){
			$form->setSaveableFields(array(
				"UnitPrice"
			));
			$form->Fields()->push(
				//TODO: set minimum amount
				$giftamount = new CurrencyField("UnitPrice", _t('GiftVoucherProduct.Amount', 'Amount'), $this->BasePrice)
			);
			$giftamount->setForm($form);
		}
		$form->setValidator($validator = new GiftVoucherFormValidator(array(
			"Quantity", "UnitPrice"
		)));
		return $form;
	}

}

class GiftVoucherFormValidator extends RequiredFields{

	public function php($data) {
		$valid =  parent::php($data);
		if($valid){
			$controller = $this->form->Controller();
			if($controller->VariableAmount){
				$giftvalue = $data['UnitPrice'];
				if($controller->MinimumAmount > 0 && $giftvalue < $controller->MinimumAmount){
					$this->validationError("UnitPrice", _t('GiftVoucherProduct.MinimumAmountError', 'Gift value must be at least {MinimumAmount}', array('MinimumAmount' => $controller->MinimumAmount)));
					return false;
				}
				if($giftvalue <= 0){
					$this->validationError("UnitPrice", "Gift value must be greater than 0");
					return false;
				}
			}
		}
		return $valid;
	}

}

{'fraction_non_alphanumeric': 0.15435847208619, 'fraction_numerical': 0.0023506366307541626, 'mean_word_length': 4.391763463569165, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 62, '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': '501590', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1614, 'n_words': 361}