Skip to content

Commit

Permalink
Fix Issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Lott committed Aug 17, 2017
1 parent 02eb3a5 commit f82276b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
43 changes: 42 additions & 1 deletion CRM/CsvImportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions ang/CsvImportHelper/Main.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,21 @@ <h2>{{ ts('Data to process') }}</h2>
</li>
</ul>
<input crm-entityref="{entity: 'Contact', select: {allowClear:true, placeholder:ts('Or Select Different Contact')}}" ng-change="chooseContact2()" ng-model="row.contact_id" />
<em>or</em>
<button ng-click="createNewContact()" >Create New</button>
</div>
<!-- too many candidates -->
<div ng-if="row.contact_id == 0 && row.resolution.length == 0 && row.state == 'multiple'">
{{ ts('More than 10 candidate contacts.') }}
<input crm-entityref="{entity: 'Contact', select: {allowClear:true, placeholder:ts('Select Contact')}}" ng-change="chooseContact2()" ng-model="row.contact_id" />
<em>or</em>
<button ng-click="createNewContact()" >Create New</button>
</div>
<div ng-if="row.resolution.length == 0">
{{ ts('No matches.') }}
<input crm-entityref="{entity: 'Contact', select: {allowClear:true, placeholder:ts('Select Contact')}}" ng-change="chooseContact2()" ng-model="row.contact_id" />
<em>or</em>
<button ng-click="createNewContact()" >Create New</button>
</div>
</td>
</tr>
Expand Down
13 changes: 13 additions & 0 deletions ang/CsvImportHelper/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions api/v3/CsvHelper/Createmissingcontacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand All @@ -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');
}

2 changes: 1 addition & 1 deletion docs/wishlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <del>Add a button to create a new contact when there's several similar candidates.</del>

- Use it with organisations as well as individuals.

Expand Down
6 changes: 3 additions & 3 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<url desc="Support">https://artfulrobot.uk</url>
<url desc="Licensing">http://www.gnu.org/licenses/gpl-3.0.html</url>
</urls>
<releaseDate>2017-04-27</releaseDate>
<version>1.1.5-beta</version>
<develStage>beta</develStage>
<releaseDate>2017-08-17</releaseDate>
<version>1.1.6</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
<ver>4.6</ver>
Expand Down

0 comments on commit f82276b

Please sign in to comment.