2008-09-26 04:22:51 +02:00
< ? php
/**
* Special ComplexTableField for editing a many_many relation .
* @ package forms
* @ subpackage fields - relational
*/
class ManyManyComplexTableField extends HasManyComplexTableField {
private $manyManyParentClass ;
public $itemClass = 'ManyManyComplexTableField_Item' ;
2009-03-13 11:05:57 +01:00
function __construct ( $controller , $name , $sourceClass , $fieldList = null , $detailFormFields = null , $sourceFilter = " " , $sourceSort = " " , $sourceJoin = " " ) {
2008-09-26 04:22:51 +02:00
parent :: __construct ( $controller , $name , $sourceClass , $fieldList , $detailFormFields , $sourceFilter , $sourceSort , $sourceJoin );
2008-10-08 05:35:27 +02:00
$classes = array_reverse ( ClassInfo :: ancestry ( $this -> controllerClass ()));
2008-09-26 04:22:51 +02:00
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 ;
2010-10-13 02:51:33 +02:00
$this -> sourceJoin .= " LEFT JOIN \" $manyManyTable\ " ON ( \ " $source\ " . \ " ID \" = \" $manyManyTable\ " . \ " { $sourceField } ID \" AND \" { $this -> manyManyParentClass } ID \" = ' $parentID ') " ;
2008-09-26 04:22:51 +02:00
$this -> joinField = 'Checked' ;
}
2009-02-02 00:49:53 +01:00
function getQuery () {
$query = parent :: getQuery ();
2009-09-17 02:16:12 +02:00
$query -> select [] = " IF( \" { $this -> manyManyParentClass } ID \" IS NULL, '0', '1') AS Checked " ;
2009-02-02 00:49:53 +01:00
return $query ;
2008-09-26 04:22:51 +02:00
}
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 {
function MarkingCheckbox () {
$name = $this -> parent -> Name () . '[]' ;
if ( $this -> parent -> IsReadOnly )
return " <input class= \" checkbox \" type= \" checkbox \" name= \" $name\ " value = \ " { $this -> item -> ID } \" disabled= \" disabled \" /> " ;
else if ( $this -> item -> { $this -> parent -> joinField })
return " <input class= \" checkbox \" type= \" checkbox \" name= \" $name\ " value = \ " { $this -> item -> ID } \" checked= \" checked \" /> " ;
else
return " <input class= \" checkbox \" type= \" checkbox \" name= \" $name\ " value = \ " { $this -> item -> ID } \" /> " ;
}
}
?>