From 6fd5424ba9dca6e9e0caad8ce0bf09c41f27948a Mon Sep 17 00:00:00 2001 From: kurund Date: Tue, 7 Jan 2014 16:42:33 -0800 Subject: [PATCH] fixes to case view tagset and tags CRM-14052 ---------------------------------------- * CRM-14052: Tagset fixes http://issues.civicrm.org/jira/browse/CRM-14052 --- CRM/Case/Form/CaseView.php | 9 ++++-- CRM/Case/Page/AJAX.php | 31 +++++++++++------- CRM/Core/Form/Tag.php | 47 +++++++++++++++++++++------- templates/CRM/Case/Form/CaseView.tpl | 17 +++++----- 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php index 771c3e7c1a..4f887b45f8 100644 --- a/CRM/Case/Form/CaseView.php +++ b/CRM/Case/Form/CaseView.php @@ -433,7 +433,12 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { $this->setDefaults(array('case_tag' => $tags)); foreach ($tags as $tid) { - $tags[$tid] = $allTags[$tid]; + if (isset($allTags[$tid])) { + $tags[$tid] = $allTags[$tid]; + } + else { + unset($tags[$tid]); + } } $this->assign('tags', implode(', ', array_filter($tags))); @@ -453,7 +458,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { else { $this->assign('showTagsets', FALSE); } - CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, FALSE, TRUE); + CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, TRUE, TRUE); $this->addButtons(array( array( diff --git a/CRM/Case/Page/AJAX.php b/CRM/Case/Page/AJAX.php index 968a570c65..b05803690f 100644 --- a/CRM/Case/Page/AJAX.php +++ b/CRM/Case/Page/AJAX.php @@ -72,6 +72,7 @@ class CRM_Case_Page_AJAX { $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Integer'); $tags = CRM_Utils_Type::escape($_POST['tag'], 'String'); + $tagList = $_POST['taglist']; if (empty($caseId)) { echo 'false'; @@ -83,20 +84,28 @@ class CRM_Case_Page_AJAX { $tagIds = explode(',', $tags); } - $params = array( - 'entity_id' => $caseId, - 'entity_table' => 'civicrm_case', - ); - - CRM_Core_BAO_EntityTag::del($params); - - foreach ($tagIds as $tagid) { - if (is_numeric($tagid)) { - $params['tag_id'] = $tagid; - CRM_Core_BAO_EntityTag::add($params); + $skipEntityDelete = FALSE; + if (!empty($tagIds)) { + $skipEntityDelete = TRUE; + $params = array( + 'entity_id' => $caseId, + 'entity_table' => 'civicrm_case', + ); + + CRM_Core_BAO_EntityTag::del($params); + + foreach ($tagIds as $tagid) { + if (is_numeric($tagid)) { + $params['tag_id'] = $tagid; + CRM_Core_BAO_EntityTag::add($params); + } } } + if (!empty($tagList)) { + CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject, $skipEntityDelete); + } + $session = CRM_Core_Session::singleton(); $activityParams = array(); diff --git a/CRM/Core/Form/Tag.php b/CRM/Core/Form/Tag.php index 6e1057cab1..4d1a454d00 100644 --- a/CRM/Core/Form/Tag.php +++ b/CRM/Core/Form/Tag.php @@ -234,13 +234,30 @@ class CRM_Core_Form_Tag { /** * Function to save entity tags when it is not save used AJAX * + * @param array $params associated array + * @param int $entityId entity id, eg: contact id, activity id, case id, file id + * @param string $entityTable entity table + * @param object $form form object + * @param boolean $skipDelete TRUE if you need to skip delete action in tag entity table + * + * @return void + * @access public + * @static */ - static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form) { + static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form, $skipDelete = FALSE) { + if ($form && !empty($form->_entityTagValues)) { + $existingTags = $form->_entityTagValues; + } + else { + $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable); + } + $allTagIds = array(); foreach ($params as $parentId => $value) { - if (!$value) { + if (!$value || empty($value)) { continue; } + $tagsIDs = explode(',', $value); $insertValues = array(); $insertSQL = NULL; @@ -252,11 +269,11 @@ class CRM_Core_Form_Tag { $tagValue = explode(':::', $tagId); if (isset($tagValue[1]) && $tagValue[1] == 'value') { - $params = array( + $tagParams = array( 'name' => $tagValue[0], 'parent_id' => $parentId, ); - $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray); + $tagObject = CRM_Core_BAO_Tag::add($tagParams, CRM_Core_DAO::$_nullArray); $tagId = $tagObject->id; } } @@ -265,23 +282,31 @@ class CRM_Core_Form_Tag { if ($form && $form->_action != CRM_Core_Action::UPDATE) { $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) "; } - elseif (!$form || !array_key_exists($tagId, $form->_entityTagValues)) { + elseif (!array_key_exists($tagId, $existingTags)) { $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) "; } } if (!empty($insertValues)) { - $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table ) VALUES ' . implode(', ', $insertValues) . ';'; + $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table ) + VALUES ' . implode(', ', $insertValues) . ';'; CRM_Core_DAO::executeQuery($insertSQL); } } } - // delete tags that are missing - if (!empty($allTagIds)) { - $validTagIds = implode(',', $allTagIds); - $deleteSQL = "DELETE FROM civicrm_entity_tag WHERE entity_id={$entityId} AND entity_table='{$entityTable}' - AND tag_id NOT IN ({$validTagIds})"; + // delete tags that are missing from civicrm_entity_tag table + if (!$skipDelete) { + $inClause = ''; + if (!empty($allTagIds)) { + $validTagIds = implode(',', $allTagIds); + $inClause = " AND civicrm_entity_tag.tag_id NOT IN ({$validTagIds})"; + } + + $deleteSQL = " +DELETE FROM civicrm_entity_tag USING civicrm_entity_tag, civicrm_tag +WHERE civicrm_entity_tag.tag_id = civicrm_tag.id AND civicrm_tag.parent_id IS NOT NULL +AND civicrm_entity_tag.entity_id={$entityId} AND civicrm_entity_tag.entity_table='{$entityTable}' {$inClause}"; CRM_Core_DAO::executeQuery($deleteSQL); } } diff --git a/templates/CRM/Case/Form/CaseView.tpl b/templates/CRM/Case/Form/CaseView.tpl index 1b4c0a191a..60334a4154 100644 --- a/templates/CRM/Case/Form/CaseView.tpl +++ b/templates/CRM/Case/Form/CaseView.tpl @@ -781,7 +781,7 @@ function addRole() {
{assign var="tagExits" value=0} {if $tags} -
{$tags}
+
  {$tags}
{assign var="tagExits" value=1} {/if} @@ -853,18 +853,15 @@ function addTags() { } }); - var tagList = ''; - cj("#manageTags input[name^=case_taglist]").each(function( ) { - if (!tagsChecked) { - tagsChecked = cj(this).val() + ''; - } - else { - tagsChecked = tagsChecked + ',' + cj(this).val(); - } + var tagList = {}; + cj("#manageTags input[name^=case_taglist]").each(function(){ + var tsId = cj(this).attr('id').split('_'); + tagList[tsId[2]] = cj(this).val(); }); var postUrl = {/literal}"{crmURL p='civicrm/case/ajax/processtags' h=0 }"{literal}; - var data = 'case_id=' + caseID + '&tag=' + tagsChecked + '&key=' + {/literal}"{crmKey name='civicrm/case/ajax/processtags'}"{literal}; + var key = {/literal}"{crmKey name='civicrm/case/ajax/processtags'}"{literal}; + var data = {'case_id': caseID, 'tag': tagsChecked, 'taglist': tagList, 'key': key}; cj.ajax({ type: "POST", url: postUrl, data: data, async: false }); cj(this).dialog("close"); -- 2.25.1