diff --git a/forms/HasManyComplexTableField.php b/forms/HasManyComplexTableField.php new file mode 100644 index 000000000..fafef6079 --- /dev/null +++ b/forms/HasManyComplexTableField.php @@ -0,0 +1,61 @@ +getParentIdNameRelation( $childClass, $parentClass, 'has_one' ); + } + + function getControllerID() { + return $this->controller->ID; + } + + function saveInto( DataObject $record ) { + $fieldName = $this->name; + $saveDest = $record->$fieldName(); + + if( ! $saveDest ) + user_error( "HasManyComplexTableField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR ); + + $items = array(); + + if( $list = $this->value[ $this->htmlListField ] ) + $items = explode( ',', $list ); + + $saveDest->setByIDList( $items ); + } + + function ExtraData() { + $items = array(); + foreach( $this->unpagedSourceItems as $item ) { + if( $item->{$this->joinField} == $this->controller->ID ) + $items[] = $item->ID; + } + $list = implode( ',', $items ); + $inputId = $this->id() . '_' . $this->htmlListEndName; + return <<
+HTML; + } +} + +class HasManyComplexTableField_Item extends ComplexTableField_Item { + + function MarkingCheckbox() { + $name = $this->parent->Name() . '[]'; + + $joinVal = $this->item->{$this->parent->joinField}; + $parentID = $this->parent->getControllerID(); + + if( $this->parent->IsReadOnly || ! $this->Can( 'edit' ) || ( $joinVal > 0 && $joinVal != $parentID ) ) + return "item->ID}\" disabled=\"disabled\"/>"; + else if( $joinVal == $parentID ) + return "item->ID}\" checked=\"checked\"/>"; + else + return "item->ID}\"/>"; + } +} + +?> \ No newline at end of file diff --git a/forms/HasOneComplexTableField.php b/forms/HasOneComplexTableField.php new file mode 100644 index 000000000..1f7ccd782 --- /dev/null +++ b/forms/HasOneComplexTableField.php @@ -0,0 +1,132 @@ +Markable = true; + + $this->joinField = $this->getParentIdName( $this->controller->ClassName, $this->sourceClass ); + } + + function getQuery( $limitClause = null ) { + if( $this->customQuery ) { + $query = $this->customQuery; + $query->select[] = "{$this->sourceClass}.ID AS ID"; + $query->select[] = "{$this->sourceClass}.ClassName AS ClassName"; + $query->select[] = "{$this->sourceClass}.ClassName AS RecordClassName"; + } + else { + $query = singleton( $this->sourceClass )->extendedSQL( $this->sourceFilter, $this->sourceSort, $limitClause, $this->sourceJoin ); + + // Add more selected fields if they are from joined table. + + $SNG = singleton( $this->sourceClass ); + foreach( $this->FieldList() as $k => $title ) { + if( ! $SNG->hasField( $k ) && ! $SNG->hasMethod( 'get' . $k ) ) + $query->select[] = $k; + } + } + return clone $query; + } + + function sourceItems() { + if($this->sourceItems) + return $this->sourceItems; + + $limitClause = ''; + if( isset( $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] ) && is_numeric( $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] ) ) + $limitClause = $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] . ", $this->pageSize"; + else + $limitClause = "0, $this->pageSize"; + + $dataQuery = $this->getQuery( $limitClause ); + $records = $dataQuery->execute(); + $items = new DataObjectSet(); + foreach( $records as $record ) { + if( ! get_class( $record ) ) + $record = new DataObject( $record ); + $items->push( $record ); + } + + $dataQuery = $this->getQuery(); + $records = $dataQuery->execute(); + $unpagedItems = new DataObjectSet(); + foreach( $records as $record ) { + if( ! get_class( $record ) ) + $record = new DataObject( $record ); + $unpagedItems->push( $record ); + } + $this->unpagedSourceItems = $unpagedItems; + + $this->totalCount = ( $this->unpagedSourceItems ) ? $this->unpagedSourceItems->TotalItems() : null; + + return $items; + } + + function getControllerJoinID() { + return $this->controller->{$this->joinField}; + } + + function saveInto( DataObject $record ) { + $fieldName = $this->name; + $fieldNameID = $fieldName . 'ID'; + + if( $val = $this->value[ $this->htmlListField ] ) + $record->$fieldNameID = $val; + else + $record->$fieldNameID = 0; + + $record->write(); + } + + function setAddTitle( $addTitle ) { + if( is_string( $addTitle ) ) + $this->addTitle = $addTitle; + } + + function Title() { + return $this->addTitle ? $this->addTitle : parent::Title(); + } + + function ExtraData() { + $val = $this->getControllerJoinID() ? $this->getControllerJoinID() : ''; + $inputId = $this->id() . '_' . $this->htmlListEndName; + return <<