1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 class ManyManyComplexTableField extends HasManyComplexTableField {
37
38 private $manyManyParentClass;
39
40 public $itemClass = 'ManyManyComplexTableField_Item';
41
42 function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
43
44 parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
45
46 $singleton = singleton($this->controllerClass());
47 if ($manymany = $singleton->many_many($name)) {
48 $this->manyManyParentClass = $manymany[0];
49 $this->parentClass = $manymany[0];
50 $this->setParentIdName($manymany[2]);
51 $manyManyTable = $manymany[4];
52 }
53
54 $tableClasses = ClassInfo::dataClassesFor($this->sourceClass);
55 $source = array_shift($tableClasses);
56 $sourceField = $this->sourceClass;
57 if($this->manyManyParentClass == $sourceField)
58 $sourceField = 'Child';
59 $parentID = $this->controller->ID;
60
61 $this->sourceJoin .= " LEFT JOIN \"$manyManyTable\" ON (\"$source\".\"ID\" = \"$manyManyTable\".\"{$sourceField}ID\" AND \"{$this->manyManyParentClass}ID\" = '$parentID')";
62
63 $this->joinField = 'Checked';
64 }
65
66 function getQuery() {
67 $query = parent::getQuery();
68 $query->select[] = "CASE WHEN \"{$this->manyManyParentClass}ID\" IS NULL THEN '0' ELSE '1' END AS \"Checked\"";
69 $query->groupby[] = "\"{$this->manyManyParentClass}ID\"";
70
71 return $query;
72 }
73
74 function getParentIdName($parentClass, $childClass) {
75 return $this->getParentIdNameRelation($parentClass, $childClass, 'many_many');
76 }
77 }
78
79 80 81 82 83
84 class ManyManyComplexTableField_Item extends ComplexTableField_Item {
85
86 function MarkingCheckbox() {
87 $name = $this->parent->Name() . '[]';
88
89 if($this->parent->IsReadOnly)
90 return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
91 else if($this->item->{$this->parent->joinField})
92 return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\"/>";
93 else
94 return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\"/>";
95 }
96 }
97
98 ?>
99
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-