From 9c29864827496adf1a40f9c9045fbc09fe8cbfdb Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Sun, 2 Dec 2007 21:27:49 +0000 Subject: [PATCH] Relation Tables Reorganisation (merged from branches/2.2.0@45907, r45142) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@46095 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/HasManyComplexTableField.php | 89 ++++++++++++++++++++++++++++-- forms/HasOneComplexTableField.php | 87 ++--------------------------- 2 files changed, 89 insertions(+), 87 deletions(-) diff --git a/forms/HasManyComplexTableField.php b/forms/HasManyComplexTableField.php index f28663428..6cb617968 100644 --- a/forms/HasManyComplexTableField.php +++ b/forms/HasManyComplexTableField.php @@ -1,11 +1,83 @@ Markable = true; - function getParentIdName( $parentClass, $childClass ) { - return $this->getParentIdNameRelation( $childClass, $parentClass, 'has_one' ); + $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 getControllerID() { @@ -28,7 +100,16 @@ class HasManyComplexTableField extends HasOneComplexTableField { $saveDest->setByIDList( $items ); } - + + function setAddTitle( $addTitle ) { + if( is_string( $addTitle ) ) + $this->addTitle = $addTitle; + } + + function Title() { + return $this->addTitle ? $this->addTitle : parent::Title(); + } + function ExtraData() { $items = array(); foreach( $this->unpagedSourceItems as $item ) { diff --git a/forms/HasOneComplexTableField.php b/forms/HasOneComplexTableField.php index 05fca0da2..e874766d2 100644 --- a/forms/HasOneComplexTableField.php +++ b/forms/HasOneComplexTableField.php @@ -1,83 +1,13 @@ Markable = true; - $this->joinField = $this->getParentIdName( $this->controller->ClassName, $this->sourceClass ); + function getParentIdName( $parentClass, $childClass ) { + return $this->getParentIdNameRelation( $parentClass, $childClass, 'has_one' ); } - - 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}; } @@ -94,16 +24,7 @@ class HasOneComplexTableField extends ComplexTableField { $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;