From f82276bd121597268654852157bb26dc12f6cae4 Mon Sep 17 00:00:00 2001 From: Rich Lott Date: Thu, 17 Aug 2017 09:05:20 +0100 Subject: [PATCH] Fix Issue #3 --- CRM/CsvImportHelper.php | 43 +++++++++++++++++++++- ang/CsvImportHelper/Main.html | 6 +++ ang/CsvImportHelper/Main.js | 13 +++++++ api/v3/CsvHelper/Createmissingcontacts.php | 15 ++++++-- docs/wishlist.md | 2 +- info.xml | 6 +-- 6 files changed, 77 insertions(+), 8 deletions(-) diff --git a/CRM/CsvImportHelper.php b/CRM/CsvImportHelper.php index 76029be..1980dcb 100644 --- a/CRM/CsvImportHelper.php +++ b/CRM/CsvImportHelper.php @@ -538,7 +538,7 @@ public static function loadCacheRecordsPost(&$return_values) { * Load single row from civicrm_csv_match_cache. * * @param integer $id - * @return array of records. + * @return array */ public static function loadCacheRecord($id) { @@ -684,6 +684,47 @@ public static function createMissingContacts() { $dao->free(); } + /** + * Create new contact (Individual) for the specified row. + * + * @param int $record_id id from our cache table. + * @return int new Contact ID. + */ + public static function createMissingContact($record_id) { + $record = CRM_CsvImportHelper::loadCacheRecord($record_id); + + $params = [ + 'contact_type' => 'Individual', + 'first_name' => $record['fname'], + 'last_name' => $record['lname'], + ]; + + $contact = civicrm_api3('Contact', 'create', $params); + + if (!empty($record['email'])) { + $params = [ + 'contact_id' => $contact['id'], + 'email' => $record['email'], + ]; + $email = civicrm_api3('Email', 'create', $params); + } + + // Now update record. + static::updateSet([ + 'fname' => $record['fname'], + 'lname' => $record['lname'], + 'email' => $record['email'], + 'state' => 'found', + 'contact_id' => $contact['id'], + 'resolution' => serialize( + [[ + 'contact_id' => (string) $contact['id'], + 'match' => ts('Created'), + 'name' => "$record[lname], $record[fname]", + ]]), + ]); + return $contact['id']; + } static public function getSummary($counts = NULL) { // Summarise data diff --git a/ang/CsvImportHelper/Main.html b/ang/CsvImportHelper/Main.html index 4471fc5..649f613 100644 --- a/ang/CsvImportHelper/Main.html +++ b/ang/CsvImportHelper/Main.html @@ -61,15 +61,21 @@

{{ ts('Data to process') }}

+ or +
{{ ts('More than 10 candidate contacts.') }} + or +
{{ ts('No matches.') }} + or +
diff --git a/ang/CsvImportHelper/Main.js b/ang/CsvImportHelper/Main.js index c76320a..c7d0d53 100644 --- a/ang/CsvImportHelper/Main.js +++ b/ang/CsvImportHelper/Main.js @@ -131,6 +131,19 @@ contact_id: 0, }, this.$parent.rowIndex); }; + $scope.createNewContact = function(event) { + console.log("createNewContact", this); + var rowIndex = this.$parent.rowIndex; + var cacheId = this.$parent.row.id; + + crmStatus( {start: ts('Creating new contact...'), success: ts('Contact Created')}, + crmApi('CsvHelper', 'createMissingContacts', { id: cacheId })) + .then(function(result) { + console.log("CREATE NEW ", result); + // The result contains the new row. + $scope.csvRecords[rowIndex] = result.values; + }); + }; $scope.countNoMatch = function() { return $scope.csvRecords.reduce(function(a, record) { return (record.state == 'impossible') ? a + 1 : a; diff --git a/api/v3/CsvHelper/Createmissingcontacts.php b/api/v3/CsvHelper/Createmissingcontacts.php index 56f00de..ffb1fc2 100644 --- a/api/v3/CsvHelper/Createmissingcontacts.php +++ b/api/v3/CsvHelper/Createmissingcontacts.php @@ -10,6 +10,7 @@ */ function _civicrm_api3_csv_helper_Createmissingcontacts_spec(&$spec) { //$spec['magicword']['api.required'] = 1; + $spec['id']['description'] = 'If an import cache ID is given only this contact will be created'; } /** @@ -22,8 +23,16 @@ function _civicrm_api3_csv_helper_Createmissingcontacts_spec(&$spec) { * @throws API_Exception */ function civicrm_api3_csv_helper_Createmissingcontacts($params) { - CRM_CsvImportHelper::createMissingContacts(); - $return_values = CRM_CsvImportHelper::loadCacheRecords(); - return civicrm_api3_create_success($return_values, $params, 'CsvHelper', 'rescan'); + if (!empty($params['id'])) { + CRM_CsvImportHelper::createMissingContact($params['id']); + // Return a single cache record. + $return_values = CRM_CsvImportHelper::loadCacheRecord($params['id']); + } + else { + CRM_CsvImportHelper::createMissingContacts(); + // Return all the records. + $return_values = CRM_CsvImportHelper::loadCacheRecords(); + } + return civicrm_api3_create_success($return_values, $params, 'CsvHelper', 'createmissingcontacts'); } diff --git a/docs/wishlist.md b/docs/wishlist.md index 8daea3a..7acf1fb 100644 --- a/docs/wishlist.md +++ b/docs/wishlist.md @@ -7,7 +7,7 @@ If you can [submit a PR](https://help.github.com/articles/about-pull-requests/), even better! Or if you want to [hire me to develop a new feature](https://artfulrobot.uk), just ask and I'll do you a quote. -- Add a button to create a new contact when there's several similar candidates. +- ✔ implemented Add a button to create a new contact when there's several similar candidates. - Use it with organisations as well as individuals. diff --git a/info.xml b/info.xml index 4f35540..0792477 100644 --- a/info.xml +++ b/info.xml @@ -17,9 +17,9 @@ https://artfulrobot.uk http://www.gnu.org/licenses/gpl-3.0.html - 2017-04-27 - 1.1.5-beta - beta + 2017-08-17 + 1.1.6 + stable 4.7 4.6