2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Text field that automatically checks that the value entered is unique for the given
|
|
|
|
* set of fields in a given set of tables
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class UniqueTextField extends TextField {
|
|
|
|
|
|
|
|
protected $restrictedField;
|
|
|
|
protected $restrictedTable;
|
|
|
|
protected $restrictedMessage;
|
|
|
|
|
|
|
|
function __construct($name, $restrictedField, $restrictedTable, $restrictedMessage, $title = null, $value = "", $maxLength = null ){
|
|
|
|
$this->maxLength = $maxLength;
|
|
|
|
|
|
|
|
$this->restrictedField = $restrictedField;
|
|
|
|
|
|
|
|
$this->restrictedTable = $restrictedTable;
|
|
|
|
$this->restrictedMessage = $restrictedMessage;
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Field() {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/UniqueFields.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
$restrictedValues = array();
|
|
|
|
$restrictedInputs = array();
|
|
|
|
|
|
|
|
// if the restrictedFields and tables have been specified,
|
|
|
|
// then get the restricted values
|
|
|
|
if( !empty( $this->restrictedField ) && !empty( $this->restrictedTable ) ) {
|
|
|
|
$result = DB::query("SELECT `{$this->restrictedField}` FROM `{$this->restrictedTable}`");
|
|
|
|
|
|
|
|
$count = 1;
|
|
|
|
|
|
|
|
while( $restrictedValue = $result->nextRecord() )
|
|
|
|
$restrictedValues[$restrictedValue[$this->restrictedField]] = 1;
|
|
|
|
|
|
|
|
$result = DB::query("SELECT `{$this->restrictedField}` FROM `{$this->restrictedTable}_Live`");
|
|
|
|
|
|
|
|
while( $restrictedValue = $result->nextRecord() )
|
|
|
|
$restrictedValues[$restrictedValue[$this->restrictedField]] = 1;
|
|
|
|
|
|
|
|
// remove the initial value of this field
|
|
|
|
$restrictedValues = array_diff_assoc( $restrictedValues, array( $this->attrValue() => 1 ) );
|
|
|
|
|
|
|
|
foreach( $restrictedValues as $restrictedValue => $discard )
|
|
|
|
$restrictedInputs[] = "<input type=\"hidden\" id=\"".$this->id()."-restricted-".($count++)."\" value=\"$restrictedValue\" name=\"restricted-values[".$this->id()."]\" />";
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:00:59 +02:00
|
|
|
$fieldSize = $this->maxLength ? min( $this->maxLength, 30 ) : 30;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->maxLength){
|
2007-09-16 04:00:59 +02:00
|
|
|
return /*implode("", $restrictedInputs).*/"<input class=\"".$this->class."\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" maxlength=\"$this->maxLength\" size=\"$fieldSize\" /><input type=\"hidden\" name=\"restricted-messages[".$this->id()."]\" id=\"".$this->id()."-restricted-message\" value=\"{$this->restrictedMessage}\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
2007-09-16 04:00:59 +02:00
|
|
|
return /*implode("", $restrictedInputs).*/"<input class=\"".$this->class."\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" size=\"30\" /><input type=\"hidden\" name=\"restricted-messages[".$this->id()."]\" id=\"".$this->id()."-restricted-message\" value=\"{$this->restrictedMessage}\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|