+ /**
+ * Purge any datasource related assets when the datasource is dropped.
+ *
+ * This is the datasource's chance to delete any tables etc that it created
+ * which will now not be used.
+ *
+ * @param array $newParams
+ * If the dataSource is being updated to another variant of the same
+ * class (eg. the csv upload was set to no column headers and they
+ * have resubmitted WITH skipColumnHeader (first row is a header) then
+ * the dataSource is still CSV and the params for the new intance
+ * are passed in. When changing from csv to SQL (for example) newParams is
+ * empty.
+ *
+ * @return array
+ * The details to update the DataSource key in the userJob metadata to.
+ * Generally and empty array but it the datasource decided (for example)
+ * that the table it created earlier is still consistent with the new params
+ * then it might decided not to drop the table and would want to retain
+ * some metadata.
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ *
+ * @noinspection PhpUnusedParameterInspection
+ */
+ public function purge(array $newParams = []) :array {
+ // The old name is still stored...
+ $oldTableName = $this->getTableName();
+ if ($oldTableName) {
+ CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS ' . $oldTableName);
+ }
+ return [];
+ }
+
+ /**
+ * Add a status columns to the import table.
+ *
+ * We add
+ * _id - primary key
+ * _status
+ * _statusMsg
+ *
+ * Note that
+ * 1) the use of the preceding underscore has 2 purposes - it avoids clashing
+ * with an id field (code comments from 14 years ago suggest perhaps there
+ * could be cases where it still clashes but time didn't tell in this case)
+ * 2) the show fields query used to get the column names excluded the
+ * administrative fields, relying on this convention.
+ * 3) we have the capitalisation on _statusMsg - @param string $tableName
+ *
+ * @throws \API_Exception
+ * @todo change to _status_message
+ */
+ protected function addTrackingFieldsToTable(string $tableName): void {
+ CRM_Core_DAO::executeQuery("
+ ALTER TABLE $tableName
+ ADD COLUMN _entity_id INT,
+ " . $this->getAdditionalTrackingFields() . "
+ ADD COLUMN _status VARCHAR(32) DEFAULT 'NEW' NOT NULL,
+ ADD COLUMN _status_message TEXT,
+ ADD COLUMN _id INT PRIMARY KEY NOT NULL AUTO_INCREMENT"
+ );
+ }
+
+ /**
+ * Get any additional import specific tracking fields.
+ *
+ * @throws \API_Exception
+ */
+ private function getAdditionalTrackingFields(): string {
+ $sql = '';
+ $fields = $this->getParser()->getTrackingFields();
+ foreach ($fields as $fieldName => $spec) {
+ $sql .= 'ADD COLUMN _' . $fieldName . ' ' . $spec . ',';
+ }
+ return $sql;
+ }
+
+ /**
+ * Get the import parser.
+ *
+ * @return CRM_Import_Parser
+ *
+ * @throws \API_Exception
+ */
+ private function getParser() {
+ $parserClass = '';
+ foreach (CRM_Core_BAO_UserJob::getTypes() as $type) {
+ if ($this->getUserJob()['type_id'] === $type['id']) {
+ $parserClass = $type['class'];
+ }
+ }
+ /* @var \CRM_Import_Parser */
+ $parser = new $parserClass();
+ $parser->setUserJobID($this->getUserJobID());
+ return $parser;
+ }
+
+ /**
+ * Has the import job completed.
+ *
+ * @return bool
+ * True if no rows remain to be imported.
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ public function isCompleted(): bool {
+ return (bool) $this->getRowCount(['new']);
+ }
+
+ /**
+ * Update the status of the import row to reflect the processing outcome.
+ *
+ * @param int $id
+ * @param string $status
+ * @param string $message
+ * @param int|null $entityID
+ * Optional created entity ID
+ * @param array $additionalFields
+ * Optional array e.g ['related_contact' => 4]
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ public function updateStatus(int $id, string $status, string $message, ? int $entityID = NULL, array $additionalFields = []): void {
+ $sql = 'UPDATE ' . $this->getTableName() . ' SET _status = %1, _status_message = %2 ';
+ $params = [1 => [$status, 'String'], 2 => [$message, 'String']];
+ if ($entityID) {
+ $sql .= ', _entity_id = %3';
+ $params[3] = [$entityID, 'Integer'];
+ }
+ $nextParam = 4;
+ foreach ($additionalFields as $fieldName => $value) {
+ $sql .= ', _' . $fieldName . ' = %' . $nextParam;
+ $params[$nextParam] = is_numeric($value) ? [$value, 'Int'] : [json_encode($value), 'String'];
+ $nextParam++;
+ }
+ CRM_Core_DAO::executeQuery($sql . ' WHERE _id = ' . $id, $params);
+ }
+
+ /**
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ private function instantiateQueryObject(): void {
+ $query = 'SELECT * FROM ' . $this->getTableName() . ' ' . $this->getStatusClause();
+ if ($this->limit) {
+ $query .= ' LIMIT ' . $this->limit . ($this->offset ? (' OFFSET ' . $this->offset) : NULL);
+ }
+ $this->queryResultObject = CRM_Core_DAO::executeQuery($query);
+ }
+
+ /**
+ * Get the mapping of constants to database status codes.
+ *
+ * @return array[]
+ */
+ protected function getStatusMapping(): array {
+ return [
+ CRM_Import_Parser::VALID => ['imported', 'new'],
+ CRM_Import_Parser::ERROR => ['error', 'invalid'],
+ CRM_Import_Parser::DUPLICATE => ['duplicate'],
+ CRM_Import_Parser::NO_MATCH => ['invalid_no_match'],
+ CRM_Import_Parser::UNPARSED_ADDRESS_WARNING => ['warning_unparsed_address'],
+ 'new' => ['new'],
+ ];
+ }
+
+ /**
+ * Get the status filter clause.
+ *
+ * @return string
+ */
+ private function getStatusClause(): string {
+ if (!empty($this->statuses)) {
+ $statuses = [];
+ foreach ($this->statuses as $status) {
+ foreach ($this->getStatusMapping()[$status] as $statusName) {
+ $statuses[] = '"' . $statusName . '"';
+ }
+ }
+ return ' WHERE _status IN (' . implode(',', $statuses) . ')';
+ }
+ return '';
+ }
+