dataObj->hasFilter()) { $form = $this->_getFilterForm(); if ($form->validate()) { $this->dataObj->addFilter($form->getValues()); } } $options = array(); $options['order'] = array($this->list->getOrderBy()); $stmt = $this->dataObj->getListStatement($options); $fp = fopen('php://output', 'w'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-type: text/comma-separated-values'); header('Content-Disposition: attachment; filename="' . $this->_getExportFileName() . '"'); $columns = $this->_getColumns4ExportCsv(); fputcsv($fp, $columns); $blankRow = array_combine(array_keys($columns), array_fill(0, count($columns), '')); while ($row = $stmt->fetch()) { $row = $this->_getRow4ExportCsv($row); $row = array_intersect_key($row, $columns); $row = array_merge($blankRow, $row); fputcsv($fp, $row); } fclose($fp); exit; } protected function _getExportFileName() { if (null == $this->_exportFileName) { $this->_exportFileName = $this->dataObj->itemsName . ' ' . date('M d Y g:i A') . '.csv'; } return $this->_exportFileName; } protected function _getColumns4ExportCsv() { if (null === $this->_columns4ExportCsv) { $this->_columns4ExportCsv = array( 'added' => 'Submitted', 'donateType' => 'Donation Type', 'name' => 'Name', 'address' => 'Address', 'city' => 'City', 'state' => 'State', 'zip' => 'Zip', 'needs' => 'I wish to donate to a specific need', 'totalAmount' => 'Total donation amount' ); $fields = array_keys(Qs_Array::exclude( $this->dataObj->table->getMetaData(), 'id', 'added', 'donateType', 'changed', 'needs', 'totalAmount', 'name', 'address', 'city', 'state', 'zip' )); foreach ($fields as $field) { $this->_columns4ExportCsv[$field] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $field)); } } return $this->_columns4ExportCsv; } protected function _getRow4ExportCsv(&$data) { $row = str_replace(array("\n", "\r"), ' ', $data); return $row; } protected function _getMailData(array $data) { throw new Qs_Exception('_getMailData() not implemented'); } protected function _getFilterFields() { if (null === $this->_filterFields) { $this->_filterFields = $this->dataObj->getFilterFields(); $this->_filterFields = array_flip($this->_filterFields); $columns = $this->_getColumns4ExportCsv(); $this->_filterFields = array_intersect_key($columns, $this->_filterFields); } return $this->_filterFields; } }