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
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @param string $name
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param DataObject $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 {
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var DataObject
|
2008-10-14 00:20:41 +02:00
|
|
|
*/
|
|
|
|
protected $object;
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_search_filter_class = 'ExactMatchFilter';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $object = null) {
|
2008-10-14 00:20:41 +02:00
|
|
|
$this->object = $object;
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2015-09-04 05:49:22 +02:00
|
|
|
if(empty($this->object)) {
|
|
|
|
return null;
|
|
|
|
}
|
2008-10-14 00:20:41 +02:00
|
|
|
$relationName = substr($this->name,0,-2);
|
2015-02-25 16:21:33 +01:00
|
|
|
$hasOneClass = $this->object->hasOneComponent($relationName);
|
2008-10-14 00:20:41 +02:00
|
|
|
|
|
|
|
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) {
|
2012-05-23 12:45:04 +02:00
|
|
|
$field = new DropdownField($this->name, $title, $list->map('ID', $titleField));
|
|
|
|
$field->setEmptyString(' ');
|
2012-03-09 15:58:46 +01:00
|
|
|
} else {
|
2012-05-23 12:45:04 +02:00
|
|
|
$field = new NumericField($this->name, $title);
|
2012-03-09 15:58:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2015-09-04 05:49:22 +02:00
|
|
|
|
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
|
|
|
if($record instanceof DataObject) {
|
|
|
|
$this->object = $record;
|
|
|
|
}
|
|
|
|
parent::setValue($value, $record, $markChanged);
|
|
|
|
}
|
2008-10-14 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|