Usage
*
*
* $tablefield = new ManyManyComplexTableField(
* $this,
* 'MyFruits',
* 'Fruit',
* array(
* 'Name' => 'Name',
* 'Color' => 'Color'
* ),
* 'getCMSFields_forPopup'
* );
*
*
* @deprecated 3.0 Use GridField with GridFieldConfig_RelationEditor
*
* @package forms
* @subpackage fields-relational
*/
class ManyManyComplexTableField extends HasManyComplexTableField {
private $manyManyParentClass;
public $itemClass = 'ManyManyComplexTableField_Item';
public function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor', Deprecation::SCOPE_CLASS);
parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
$classes = array_reverse(ClassInfo::ancestry($this->controllerClass()));
foreach($classes as $class) {
$singleton = singleton($class);
$manyManyRelations = $singleton->uninherited('many_many', true);
if(isset($manyManyRelations) && array_key_exists($this->name, $manyManyRelations)) {
$this->manyManyParentClass = $class;
$manyManyTable = $class . '_' . $this->name;
break;
}
$belongsManyManyRelations = $singleton->uninherited( 'belongs_many_many', true );
if( isset( $belongsManyManyRelations ) && array_key_exists( $this->name, $belongsManyManyRelations ) ) {
$this->manyManyParentClass = $class;
$manyManyTable = $belongsManyManyRelations[$this->name] . '_' . $this->name;
break;
}
}
$tableClasses = ClassInfo::dataClassesFor($this->sourceClass);
$source = array_shift($tableClasses);
$sourceField = $this->sourceClass;
if($this->manyManyParentClass == $sourceField)
$sourceField = 'Child';
$parentID = $this->controller->ID;
$this->sourceJoin .= " LEFT JOIN \"$manyManyTable\" ON (\"$source\".\"ID\" = \"$manyManyTable\".\"{$sourceField}ID\" AND \"{$this->manyManyParentClass}ID\" = '$parentID')";
$this->joinField = 'Checked';
}
public function getQuery() {
$query = parent::getQuery();
$query->selectField("CASE WHEN \"{$this->manyManyParentClass}ID\" IS NULL THEN '0' ELSE '1' END", "Checked");
$query->groupby[] = "\"{$this->manyManyParentClass}ID\""; // necessary for Postgres
return $query;
}
public function getParentIdName($parentClass, $childClass) {
return $this->getParentIdNameRelation($parentClass, $childClass, 'many_many');
}
}
/**
* One record in a {@link ManyManyComplexTableField}.
* @package forms
* @subpackage fields-relational
*/
class ManyManyComplexTableField_Item extends ComplexTableField_Item {
public function MarkingCheckbox() {
$name = $this->parent->getName() . '[]';
if($this->parent->IsReadOnly)
return "item->ID}\" disabled=\"disabled\"/>";
else if($this->item->{$this->parent->joinField})
return "item->ID}\" checked=\"checked\"/>";
else
return "item->ID}\"/>";
}
}