2008-10-14 00:20:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A special type Int field used for foreign keys in has_one relationships.
|
2008-10-16 14:05:08 +02:00
|
|
|
* @uses ImageField
|
|
|
|
* @uses SimpleImageField
|
2008-10-31 03:09:04 +01:00
|
|
|
* @uses FileIFrameField
|
2008-10-16 14:05:08 +02:00
|
|
|
* @uses DropdownField
|
2008-10-14 00:20:41 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param DataOject $object The object that the foreign key is stored on (should have a relation with $name)
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
|
|
|
class ForeignKey extends Int {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
|
|
|
protected $object;
|
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
public static $default_search_filter_class = 'ExactMatchMultiFilter';
|
2008-10-14 00:20:41 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
function __construct($name, $object = null) {
|
2008-10-14 00:20:41 +02:00
|
|
|
$this->object = $object;
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
|
|
|
$relationName = substr($this->name,0,-2);
|
|
|
|
$hasOneClass = $this->object->has_one($relationName);
|
|
|
|
|
|
|
|
if($hasOneClass && singleton($hasOneClass) instanceof Image) {
|
2008-10-14 23:47:22 +02:00
|
|
|
if(isset($params['ajaxSafe']) && $params['ajaxSafe']) {
|
2008-10-14 00:20:41 +02:00
|
|
|
$field = new ImageField($relationName, $title, $this->value);
|
|
|
|
} else {
|
|
|
|
$field = new SimpleImageField($relationName, $title, $this->value);
|
|
|
|
}
|
|
|
|
} elseif($hasOneClass && singleton($hasOneClass) instanceof File) {
|
2008-10-14 23:47:22 +02:00
|
|
|
if(isset($params['ajaxSafe']) && $params['ajaxSafe']) {
|
2008-10-31 03:09:04 +01:00
|
|
|
$field = new FileIFrameField($relationName, $title, $this->value);
|
2008-10-14 00:20:41 +02:00
|
|
|
} else {
|
|
|
|
$field = new FileField($relationName, $title, $this->value);
|
|
|
|
}
|
|
|
|
} else {
|
2008-11-22 04:33:00 +01:00
|
|
|
$titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name";
|
2009-01-05 07:19:48 +01:00
|
|
|
$map = new SQLMap(singleton($hasOneClass)->extendedSQL(), "ID", $titleField);
|
2008-10-14 00:20:41 +02:00
|
|
|
$field = new DropdownField($this->name, $title, $map, null, null, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
?>
|