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
This commit is contained in:
Ingo Schommer 2008-11-25 04:31:02 +00:00 committed by Sam Minnee
parent 3aa7ad3212
commit dde4a07058

View File

@ -439,6 +439,8 @@ class ModelAdmin_CollectionController extends Controller {
$model = singleton($this->modelClass); $model = singleton($this->modelClass);
$source = $model->summaryFields(); $source = $model->summaryFields();
// select all fields by default
$value = array(); $value = array();
if($source) foreach ($source as $fieldName => $label){ if($source) foreach ($source as $fieldName => $label){
$value[] = $fieldName; $value[] = $fieldName;
@ -535,18 +537,28 @@ class ModelAdmin_CollectionController extends Controller {
return $context->getQuery($searchCriteria); 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); $model = singleton($this->modelClass);
$summaryFields = $model->summaryFields(); $summaryFields = $model->summaryFields();
$resultAssembly = $searchCriteria['ResultAssembly']; if($selectedOnly) {
if(!is_array($resultAssembly)) { $resultAssembly = $searchCriteria['ResultAssembly'];
$explodedAssembly = split(' *, *', $resultAssembly); if(!is_array($resultAssembly)) {
$resultAssembly = array(); $explodedAssembly = split(' *, *', $resultAssembly);
foreach($explodedAssembly as $item) $resultAssembly[$item] = true; $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 // @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))); $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 = '<a href=\"' . $this->Link() . '/$ID/edit\">$value</a>'; $url = '<a href=\"' . $this->Link() . '/$ID/edit\">$value</a>';
$tf->setFieldFormatting(array_combine(array_keys($summaryFields), array_fill(0,count($summaryFields), $url))); $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['url']);
unset($filteredCriteria['action_search']); unset($filteredCriteria['action_search']);
if(isset($filteredCriteria['Investors__PEFirm__IsPECMember']) && !$filteredCriteria['Investors__PEFirm__IsPECMember']) unset($filteredCriteria['Investors__PEFirm__IsPECMember']); if(isset($filteredCriteria['Investors__PEFirm__IsPECMember']) && !$filteredCriteria['Investors__PEFirm__IsPECMember']) unset($filteredCriteria['Investors__PEFirm__IsPECMember']);
$form->setFormAction($this->Link() . '/ResultsForm?' . http_build_query($filteredCriteria)); $form->setFormAction($this->Link() . '/ResultsForm?' . http_build_query($filteredCriteria));
return $form; return $form;
} }