$id = UserJob::create(FALSE)
->setValues([
'created_id' => CRM_Core_Session::getLoggedInContactID(),
- 'type_id:name' => $this->getUserJobType(),
+ 'job_type' => $this->getUserJobType(),
'status_id:name' => 'draft',
// This suggests the data could be cleaned up after this.
'expires_date' => '+ 1 week',
return $this->getDataSourceObject()->setLimit($limit)->setStatuses($statuses)->getRows();
}
+ /**
+ * Get the datasource rows ready for csv output.
+ *
+ * @param array $statuses
+ * @param int $limit
+ *
+ * @return array
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ protected function getOutputRows($statuses = [], int $limit = 0) {
+ $statuses = (array) $statuses;
+ $dataSource = $this->getDataSourceObject()->setLimit($limit)->setStatuses($statuses)->setStatuses($statuses);
+ $dataSource->setSelectFields(array_merge(['_id', '_status_message'], $dataSource->getDataFieldNames()));
+ return $dataSource->getRows();
+ }
+
+ /**
+ * Get the column headers for the output csv.
+ *
+ * @return array
+ */
+ protected function getOutputColumnsHeaders(): array {
+ $headers = $this->getColumnHeaders();
+ array_unshift($headers, ts('Reason'));
+ array_unshift($headers, ts('Line Number'));
+ return $headers;
+ }
+
/**
* Get the number of rows with the specified status.
*
*/
public static function outputCSV(): void {
$userJobID = CRM_Utils_Request::retrieveValue('user_job_id', 'Integer', NULL, TRUE);
- $status = CRM_Utils_Request::retrieveValue('status', 'String', NULL, TRUE);
+ $status = (int) CRM_Utils_Request::retrieveValue('status', 'String', NULL, TRUE);
$saveFileName = CRM_Import_Parser::saveFileName($status);
$form = new CRM_Import_Forms();
$form->getUserJob();
$writer = Writer::createFromFileObject(new SplTempFileObject());
- $headers = $form->getColumnHeaders();
- if ($headers) {
- array_unshift($headers, ts('Reason'));
- array_unshift($headers, ts('Line Number'));
- $writer->insertOne($headers);
- }
- $writer->addFormatter(['CRM_Import_Forms', 'reorderOutput']);
- // Note this might be more inefficient that iterating the result
+ $headers = $form->getOutputColumnsHeaders();
+ $writer->insertOne($headers);
+ // Note this might be more inefficient by iterating the result
// set & doing insertOne - possibly something to explore later.
- $writer->insertAll($form->getDataRows($status));
-
- CRM_Utils_System::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
- CRM_Utils_System::setHttpHeader('Content-Description', 'File Transfer');
- CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv; charset=UTF-8');
+ $writer->insertAll($form->getOutputRows($status));
$writer->output($saveFileName);
CRM_Utils_System::civiExit();
}
- /**
- * When outputting the row as a csv, more the last 2 rows to the start.
- *
- * This is because the id and status message fields are at the end. It may make sense
- * to move them to the start later, when order code cleanup has happened...
- *
- * @param array $record
- */
- public static function reorderOutput(array $record): array {
- $rowNumber = array_pop($record);
- $message = array_pop($record);
- // Also pop off the status - but we are not going to use this at this stage.
- array_pop($record);
- // Related entities
- array_pop($record);
- // Entity_id
- array_pop($record);
- array_unshift($record, $message);
- array_unshift($record, $rowNumber);
- return $record;
- }
-
/**
* Get the url to download the relevant csv file.
* @param string $status
]);
}
+ /**
+ * Get the url to download the relevant csv file.
+ * @param string $status
+ *
+ * @return string
+ */
+
+ /**
+ *
+ * @return array
+ */
+ public function getTrackingSummary(): array {
+ $summary = [];
+ $fields = $this->getParser()->getTrackingFields();
+ $row = $this->getDataSourceObject()->setAggregateFields($fields)->getRow();
+ foreach ($fields as $fieldName => $field) {
+ $summary[] = [
+ 'field_name' => $fieldName,
+ 'description' => $field['description'],
+ 'value' => $row[$fieldName],
+ ];
+ }
+
+ return $summary;
+ }
+
/**
* Get the fields available for import selection.
*
* @return \CRM_Contact_Import_Parser_Contact|\CRM_Contribute_Import_Parser_Contribution
*/
protected function getParser() {
+ foreach (CRM_Core_BAO_UserJob::getTypes() as $jobType) {
+ if ($jobType['id'] === $this->getUserJob()['job_type']) {
+ $className = $jobType['class'];
+ $classObject = new $className();
+ $classObject->setUserJobID($this->getUserJobID());
+ return $classObject;
+ };
+ }
return NULL;
}
$this->_columnNames = $this->getColumnHeaders();
$this->_dataValues = array_values($this->getDataRows([], 2));
$this->assign('columnNames', $this->getColumnHeaders());
- $this->assign('showColumnNames', $this->getSubmittedValue('skipColumnHeader'));
+ $this->assign('showColumnNames', $this->getSubmittedValue('skipColumnHeader') || $this->getSubmittedValue('dataSource') !== 'CRM_Import_DataSource');
$this->assign('highlightedFields', $this->getHighlightedFields());
$this->assign('columnCount', $this->_columnCount);
$this->assign('dataValues', $this->_dataValues);