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)
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-10-14 00:20:41 +02:00
|
|
|
* @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) {
|
2012-03-01 12:00:03 +01:00
|
|
|
$field = new UploadField($relationName, $title);
|
|
|
|
$field->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
|
2008-10-14 00:20:41 +02:00
|
|
|
} elseif($hasOneClass && singleton($hasOneClass) instanceof File) {
|
2012-03-01 12:00:03 +01:00
|
|
|
$field = new UploadField($relationName, $title);
|
2008-10-14 00:20:41 +02:00
|
|
|
} else {
|
2008-11-22 04:33:00 +01:00
|
|
|
$titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name";
|
2012-03-09 15:58:46 +01:00
|
|
|
$list = DataList::create($hasOneClass);
|
|
|
|
// Don't scaffold a dropdown for large tables, as making the list concrete
|
|
|
|
// might exceed the available PHP memory in creating too many DataObject instances
|
|
|
|
if($list->count() < 100) {
|
|
|
|
$field = new DropdownField($this->name, $title, $list->map("ID", $titleField), null, null, ' ');
|
|
|
|
} else {
|
|
|
|
$field = new NumericField($this->name, $title);
|
|
|
|
}
|
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|