From 124a6e3934c6e6b5283edad01d077cb4ef32b9af Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 10 Aug 2008 23:53:56 +0000 Subject: [PATCH] (merged from branches/roa. use "svn log -c -g " for detailed commit message) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60268 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 6 ++ forms/TableListField.php | 68 +++++++++++++++---- search/SearchContext.php | 5 +- .../Includes/TableListField_Item_export.ss | 1 + 4 files changed, 64 insertions(+), 16 deletions(-) create mode 100755 templates/Includes/TableListField_Item_export.ss diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 53b0eb312..79a906c65 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -2625,6 +2625,12 @@ class DataObject extends ViewableData implements DataObjectInterface { * view of this object. */ public static $summary_fields = null; + + /** + * User defined permissions for search result table by ModelAdmin to act on. + * Such as print search + */ + public static $results_permissions = null; } diff --git a/forms/TableListField.php b/forms/TableListField.php index d09e27ee5..9131fb656 100755 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -165,6 +165,14 @@ class TableListField extends FormField { */ public $fieldFormatting = array(); + /** + * @var array Specify custom escape for the fields, e.g. to escape all accurance of "\r", "\r\n" and "\n" of the field + * value, we need to set this field as array("\r"=>"", "\r\n"=>"", "\n"=>"") ; + * Example: setFieldEscape(array("\""=>"\"\"","\r"=>"", "\r\n"=>"", "\n"=>"") is needed for exporting the table to a .csv + * file + */ + public $fieldEscape = array(); + /** * @var string */ @@ -312,7 +320,7 @@ JS // get query $dataQuery = $this->getQuery(); - // we don't limit when doing certain actions + // we don't limit when doing certain actions T if(!isset($_REQUEST['methodName']) || !in_array($_REQUEST['methodName'],array('printall','export'))) { $dataQuery->limit(array( 'limit' => $SQL_limit, @@ -335,9 +343,9 @@ JS function Items() { $fieldItems = new DataObjectSet(); if($items = $this->sourceItems()) foreach($items as $item) { + $fieldItem = new TableListField_Item($item, $this); if($item) $fieldItems->push(new TableListField_Item($item, $this)); } - return $fieldItems; } @@ -740,7 +748,6 @@ JS $record = $records->nextRecord(); $this->totalCount = $record['TotalCount']; } - return $this->totalCount; } @@ -782,6 +789,13 @@ JS $this->csvSeparator = $csvSeparator; } + /** + * Get the CSV separator character. Defaults to , + */ + function getCsvSeparator() { + return $this->csvSeparator; + } + /** * Remove the header row from the CSV export */ @@ -812,21 +826,32 @@ JS // get data $dataQuery = $this->getCsvQuery(); $records = $dataQuery->execute(); + $sourceClass = $this->sourceClass; $dataobject = new $sourceClass(); $items = $dataobject->buildDataObjectSet($records, 'DataObjectSet'); - if($items) { - foreach($items as $item) { - $columnData = array(); - foreach($csvColumns as $columnName => $columnTitle) { - $tmpColumnData = "\"" . str_replace("\"", "\"\"", $item->$columnName) . "\""; - $tmpColumnData = str_replace(array("\r", "\n"), "", $tmpColumnData); - $columnData[] = $tmpColumnData; - } - $fileData .= implode($separator, $columnData); - $fileData .= "\n"; + $fieldItems = new DataObjectSet(); + + if($items&&$items->count()) foreach($items as $item) { + $fieldItem = new TableListField_Item($item, $this); + if($item) $fieldItems->push(new TableListField_Item($item, $this)); + } + + $this->setFieldFormatting(array()); + $this->setFieldEscape(array( + "\""=>"\"\"", + "\r\n"=>"", + "\r"=>"", + "\n"=>"", + )); + + if($fieldItems) { + + foreach($fieldItems as $fieldItem) { + $fileData .= $fieldItem->renderwith("TableListField_Item_export"); } + HTTP::sendFileToBrowser($fileData, $fileName); } else { user_error("No records found", E_USER_ERROR); @@ -838,7 +863,9 @@ JS * We need to instanciate this button manually as a normal button has no means of adding inline onclick-behaviour. */ function ExportLink() { - return Controller::join_links($this->Link(), 'export'); + $exportLink = Controller::join_links($this->Link(), 'export'); + if($this->extraLinkParams) $exportLink .= "?" . http_build_query($this->extraLinkParams); + return $exportLink; } function printall() { @@ -904,6 +931,10 @@ JS $this->fieldFormatting = $formatting; } + function setFieldEscape($escape){ + $this->fieldEscape = $escape; + } + /** * @return String */ @@ -1057,14 +1088,21 @@ class TableListField_Item extends ViewableData { $format = str_replace('__VAL__', '$value', $format); eval('$value = "' . $format . '";'); } + + //escape + if($escape = $this->parent->fieldEscape){ + foreach($escape as $search => $replace){ + $value = str_replace($search, $replace, $value); + } + } $fields[] = new ArrayData(array( "Name" => $fieldName, "Title" => $fieldTitle, "Value" => $value, + "CsvSeparator" => $this->parent->getCsvSeparator(), )); } - return new DataObjectSet($fields); } diff --git a/search/SearchContext.php b/search/SearchContext.php index bb1778a60..bfc7186e2 100644 --- a/search/SearchContext.php +++ b/search/SearchContext.php @@ -114,7 +114,10 @@ class SearchContext extends Object { $SQL_sort = (!empty($sort)) ? Convert::raw2sql($sort) : singleton($this->modelClass)->stat('default_sort'); $query->orderby($SQL_sort); foreach($searchParams as $key => $value) { - //if ($value != '0') { + /*We add $value!='' here to not include a filter like this: $fieldname like '%%', which abviously filter out some + records with the $fieldname set to null. and this is not the search intention. + */ + if ($value != '0'&&$value!='') { $key = str_replace('__', '.', $key); $filter = $this->getFilter($key); if ($filter) { diff --git a/templates/Includes/TableListField_Item_export.ss b/templates/Includes/TableListField_Item_export.ss new file mode 100755 index 000000000..3c7dbbe7a --- /dev/null +++ b/templates/Includes/TableListField_Item_export.ss @@ -0,0 +1 @@ +<% control Fields %>"$Value"<% if Last %>\n<% else %>$CsvSeparator<% end_if %><% end_control %> \ No newline at end of file