Version: current

     1 <?php
     2 /**
     3  * Special ComplexTableField for editing a many_many relation.
     4  *
     5  * This field  allows you to show a **many-to-many** relation with a group of
     6  * DataObjects as a (readonly) tabular list (similiar to {@link ComplexTableField}).
     7  * Its most useful when you want to manage the relationship itself
     8  * thanks to the check boxes present on each line of the table.
     9  *
    10  * See {@link ComplexTableField} for more documentation on the base-class.
    11  * See {@link HasManyComplexTableField} for more documentation on the relation table base-class.
    12  *
    13  * Note: This class relies on the fact that both sides of the relation have database tables.
    14  * If you are only creating a class as a logical extension (that is, it doesn't have any database fields),
    15  * then you will need to create a dummy static $db array because SilverStripe won't create a database
    16  * table unless needed.
    17  *
    18  * <b>Usage</b>
    19  *
    20  * <code>
    21  * $tablefield = new ManyManyComplexTableField(
    22  *     $this,
    23  *     'MyFruits',
    24  *     'Fruit',
    25  *     array(
    26  *  'Name' => 'Name',
    27  *  'Color' => 'Color'
    28  *     ),
    29  *     'getCMSFields_forPopup'
    30  * );
    31  * </code>
    32  *
    33  * @package forms
    34  * @subpackage fields-relational
    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\""; // necessary for Postgres
    70
    71         return $query;
    72     }
    73
    74     function getParentIdName($parentClass, $childClass) {
    75         return $this->getParentIdNameRelation($parentClass, $childClass, 'many_many');
    76     }
    77 }
    78
    79 /**
    80  * One record in a {@link ManyManyComplexTableField}.
    81  * @package forms
    82  * @subpackage fields-relational
    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. -