'Company Name', 'address' => 'Address 1', 'address2' => 'Address 2', 'city' => 'City', 'stateFull' => 'State', 'zip' => 'Zip', 'phone' => 'Phone Number', 'fax' => 'Fax Number', 'websiteUrl' => 'Website', ]; protected $_exportFileName; protected $_hasFilter = true; protected $_filterFields = ['Company Name', 'City', 'Address']; protected function _getDefaultLinks() { $links = parent::_getDefaultLinks(); $filter = $this->_dataObj->getFilter(); foreach ($filter as $field => $value) { if ($value) { $params[$field] = $value; } } $params['action'] = 'exportCsv'; $links['exportCsv'] = ['title' => 'Export to CSV', 'url' => $this->url($params)]; return $links; } protected function _getColumns4ExportCsv() { if (null === $this->_columns4ExportCsv) { $fields = array_keys(Qs_Array::excludeKey($this->_getDataObj()->getTableMeta(), 'id', 'changed')); foreach ($fields as $field) { $this->_columns4ExportCsv[$field] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $field)); } } return $this->_columns4ExportCsv; } protected function _doExportCsv() { $dataObj = $this->_getDataObj(); if ($dataObj->hasFilter()) { $form = $this->_getFilterForm(); if ($form->validate()) { $dataObj->addFilter($form->getValues()); } } $options = []; $options['order'] = [$this->_getList()->getOrderBy()]; $this->_getDataObj()->setSelectOptions($options); $select = $this->_getDataObj()->getListSelect(); $this->_getDataObj()->addStateFullname($select); $stmt = $select->query(); $columns = $this->_getColumns4ExportCsv(); $blankRow = array_combine(array_keys($columns), array_fill(0, count($columns), '')); $rowCallback = function () use ($stmt, $columns, $blankRow) { $row = $stmt->fetch(); if ($row) { $row = $this->_getRow4ExportCsv($row); $row = array_intersect_key($row, $columns); $row = array_merge($blankRow, $row); } return $row; }; $helper = new ExportCsv([ 'fileName' => $this->getConfig('itemsName') . ' {date}.csv', 'columns' => $columns, 'rowCallback' => $rowCallback, ]); $helper->export(); $this->_postExportCsv(); exit; } protected function _postExportCsv() { $this->_log(); return $this; } protected function _getRow4ExportCsv(&$data) { $row = str_replace(["\n", "\r"], ' ', $data); $row['name'] = $row['name'] . ' (' . $row['activeIndividualsCount'] . ')'; return $row; } protected function _doNew() { $data = []; if ($userId = Qs_Request::getRequestValue('userId')) { $table = new Qs_Db_Table('UserRegistrationCompany'); $data = (array) $table->search($userId); } $form = $this->_getNewForm(['defaults' => $data]); $form->setDefaults(); $this->_addFormItem($form); $this->_postNew(); return $this; } }