From dde4a07058c131df7ad5d87b4512b0176451024d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 25 Nov 2008 04:31:02 +0000 Subject: [PATCH] ENHANCEMENT Optionally getting all columns in ModelAdmin->getResultColumns() - not just the columns ticked in the UI (useful for CSV export) ENHANCEMENT Defaulting to show all columns in ModelAdmin CSV export, ignoring user selection of result table columns (users can always limit CSV columns by manually deleting them e.g. in Excel) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@66596 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/ModelAdmin.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/code/ModelAdmin.php b/code/ModelAdmin.php index c2d6dce2..fb227d0c 100644 --- a/code/ModelAdmin.php +++ b/code/ModelAdmin.php @@ -439,6 +439,8 @@ class ModelAdmin_CollectionController extends Controller { $model = singleton($this->modelClass); $source = $model->summaryFields(); + + // select all fields by default $value = array(); if($source) foreach ($source as $fieldName => $label){ $value[] = $fieldName; @@ -535,18 +537,28 @@ class ModelAdmin_CollectionController extends Controller { return $context->getQuery($searchCriteria); } - function getResultColumns($searchCriteria) { + /** + * Returns all columns used for tabular search results display. + * Defaults to all fields specified in {@link DataObject->summaryFields()}. + * + * @param array $searchCriteria Limit fields by populating the 'ResultsAssembly' key + * @param boolean $selectedOnly Limit by 'ResultsAssempty + */ + function getResultColumns($searchCriteria, $selectedOnly = true) { $model = singleton($this->modelClass); $summaryFields = $model->summaryFields(); - $resultAssembly = $searchCriteria['ResultAssembly']; - if(!is_array($resultAssembly)) { - $explodedAssembly = split(' *, *', $resultAssembly); - $resultAssembly = array(); - foreach($explodedAssembly as $item) $resultAssembly[$item] = true; + if($selectedOnly) { + $resultAssembly = $searchCriteria['ResultAssembly']; + if(!is_array($resultAssembly)) { + $explodedAssembly = split(' *, *', $resultAssembly); + $resultAssembly = array(); + foreach($explodedAssembly as $item) $resultAssembly[$item] = true; + } + return array_intersect_key($summaryFields, $resultAssembly); + } else { + return $summaryFields; } - - return array_intersect_key($summaryFields, $resultAssembly); } /** @@ -569,6 +581,10 @@ class ModelAdmin_CollectionController extends Controller { // @todo Remove records that can't be viewed by the current user $tf->setPermissions(array_merge(array('view','export'), TableListField::permissions_for_object($this->modelClass))); + // csv export settings (select all columns regardless of user checkbox settings in 'ResultsAssembly') + $exportFields = $this->getResultColumns($searchCriteria, false); + $tf->setFieldListCsv($exportFields); + $url = 'Link() . '/$ID/edit\">$value'; $tf->setFieldFormatting(array_combine(array_keys($summaryFields), array_fill(0,count($summaryFields), $url))); @@ -593,7 +609,7 @@ class ModelAdmin_CollectionController extends Controller { unset($filteredCriteria['url']); unset($filteredCriteria['action_search']); if(isset($filteredCriteria['Investors__PEFirm__IsPECMember']) && !$filteredCriteria['Investors__PEFirm__IsPECMember']) unset($filteredCriteria['Investors__PEFirm__IsPECMember']); - + $form->setFormAction($this->Link() . '/ResultsForm?' . http_build_query($filteredCriteria)); return $form; }