Skip to content

Commit

Permalink
Set the correct parentID for new tags. Fixes #8524
Browse files Browse the repository at this point in the history
  • Loading branch information
kzalewski committed Apr 9, 2015
1 parent 1efa94f commit 6d4c10c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
35 changes: 23 additions & 12 deletions civicrm/custom/php/CRM/IMAP/AJAX.php
Expand Up @@ -24,6 +24,9 @@ class CRM_IMAP_AJAX
const DEFAULT_AUTH_GROUP = 'Authorized_Forwarders';
const DEFAULT_CONTACT_ID = 1;

const POSITION_PARENT_ID = 292;
const KEYWORD_PARENT_ID = 296;


/**
* Occasionally we need the raw database connection to do
Expand Down Expand Up @@ -490,10 +493,10 @@ private static function highlightItems($text, $items)
/**
* Add a group of tags to either a contact or an activity.
*/
private static function addEntityTags($entityId, $entityTable, $tags)
private static function addEntityTags($entityId, $entityTab, $parentId, $tags)
{
require_once 'api/api.php';
$existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable);
$existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTab);
$entityTagIds = array();

foreach ($tags as $tagId) {
Expand All @@ -512,10 +515,16 @@ private static function addEntityTags($entityId, $entityTable, $tags)
$tagId = strval($apires['id']);
}
else {
if ($parentId == self::KEYWORD_PARENT_ID) {
$used_for = 'civicrm_contact,civicrm_activity,civicrm_case';
}
else {
$used_for = $entityTab;
}
$params = array(
'name' => $tagValue[0],
'parent_id' => $parentId,
'used_for' => 'civicrm_contact,civicrm_activity,civicrm_case'
'used_for' => $used_for
);
$tagObj = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
$tagId = strval($tagObj->id);
Expand All @@ -532,13 +541,14 @@ private static function addEntityTags($entityId, $entityTable, $tags)
// New tag ids can be inserted directly into the db table.
$insertValues = array();
foreach ($entityTagIds as $tagId) {
$insertValues[] = "( $tagId, $entityId, '$entityTable' )";
$insertValues[] = "( $tagId, $entityId, '$entityTab' )";
$tagCount++;
}
$query = "INSERT INTO civicrm_entity_tag
( tag_id, entity_id, entity_table )
VALUES ".implode(',', $insertValues);
if (mysql_query($query, self::db()) === true) {
$tagCount++;
if (mysql_query($query, self::db()) === false) {
$tagCount = 0;
}
}
return $tagCount;
Expand Down Expand Up @@ -1336,7 +1346,8 @@ public static function KeywordSearch()
$name = self::get('name');

$query = "SELECT id, name FROM civicrm_tag
WHERE parent_id=296 AND name LIKE '$name%'";
WHERE parent_id=".self::KEYWORD_PARENT_ID."
AND name LIKE '$name%'";
$dbres = mysql_query($query, self::db());
if ($dbres === false) {
self::exitError('Tag query failed');
Expand Down Expand Up @@ -1376,21 +1387,21 @@ public static function TagAdd()
$success = true;

if (!$parentId) {
$parentId = '296';
$parentId = self::KEYWORD_PARENT_ID;
}

switch ($parentId) {
case '292': $tagType = 'Position'; break;
case '296': $tagType = 'Keyword'; break;
case self::POSITION_PARENT_ID: $tagType = 'Position'; break;
case self::KEYWORD_PARENT_ID: $tagType = 'Keyword'; break;
default: $tagType = 'Issue Code'; break;
}

foreach ($contactIds as $contactId) {
$ntags = self::addEntityTags($contactId, 'civicrm_contact', $tagIds);
$ntags = self::addEntityTags($contactId, 'civicrm_contact', $parentId, $tagIds);
}

foreach ($activityIds as $activityId) {
$ntags += self::addEntityTags($activityId, 'civicrm_activity', $tagIds);
$ntags += self::addEntityTags($activityId, 'civicrm_activity', $parentId, $tagIds);
}

if ($ntags > 0) {
Expand Down
23 changes: 9 additions & 14 deletions modules/nyss_imapper/nyss_imapper.js
Expand Up @@ -901,14 +901,11 @@ cj(document).ready(function()

});

// Step through the process, erm, process
// Step through the process
function Process(clear) {
//console.log('Process', messageId);

// assume we have errors
var error = true;

// ----
// Inputs
var messageId = cj('#message').val();
var activityId = cj('#activity').val();
Expand Down Expand Up @@ -1076,18 +1073,12 @@ cj(document).ready(function()
error = false;
}

if (newContacts.length != 0) {
// console.log("Add to New Contact",newContacts);
}
else {
// console.log("Add to Old Contact",contactId);
if (newContacts.length == 0) {
newContacts.push(contactId);
};
// console.log("newContacts",newContacts);

// did we add any tags ?
if (activity_tag.length != 0 || contact_tag.length != 0 || removedIssueCodes.length != 0 || addedIssueCodes.length != 0 || contact_position.length != 0) {
// console.log('activity_tags: ',activity_tag.length,'contact_tags: ',contact_tag.length,'removedIssueCodes: ',removedIssueCodes.length,'addedIssueCodes: ',addedIssueCodes.length,'contact_positions: ',contact_position.length);

// CONTACT POSITIONS
if (contact_position.length) {
Expand All @@ -1099,15 +1090,17 @@ cj(document).ready(function()
contactId: newContacts.toString(),
tags: contact_position
},
success: function(data,status) {
CRM.alert('Added position', 'Success', 'success');
success: function(data, status) {
var result = cj.parseJSON(data);
CRM.alert(result.message, 'Success', 'success');
},
error: function() {
var result = cj.parseJSON(data);
CRM.alert('Failed to add position', 'Error', 'error');
CRM.alert(result.message, 'Error', 'error');
}
});
}

// CONTACT ISSUE CODES
// if any of the data has changed in the issue codes tree we need to
// submit the new data to be processed on the ajax side
Expand Down Expand Up @@ -1149,6 +1142,7 @@ cj(document).ready(function()
}
});
}

// CONTACT KEYWORDS
if (contact_tag) {
cj.ajax({
Expand All @@ -1168,6 +1162,7 @@ cj(document).ready(function()
}
});
}

// ACTIVITY KEYWORDS
if (activity_tag) {
var activity_ids_array = activityId.split(',');
Expand Down

0 comments on commit 6d4c10c

Please sign in to comment.