From e4f4dc2251ca019c33ced4494eeae97da420a9b0 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 29 Apr 2014 23:42:55 -0700 Subject: [PATCH] CRM-13966 - Add ajax save support for new tagSet code --- CRM/Core/Form/Tag.php | 131 ++++---------------------------- CRM/Tag/Form/Tag.php | 2 +- js/Common.js | 6 +- js/crm.ajax.js | 3 +- templates/CRM/Tag/Form/Tag.tpl | 32 ++++---- templates/CRM/common/Tagset.tpl | 50 +++++++++--- 6 files changed, 79 insertions(+), 145 deletions(-) diff --git a/CRM/Core/Form/Tag.php b/CRM/Core/Form/Tag.php index 05adaefbe5..52399b78b6 100644 --- a/CRM/Core/Form/Tag.php +++ b/CRM/Core/Form/Tag.php @@ -44,7 +44,7 @@ class CRM_Core_Form_Tag { * Function to build tag widget if correct parent is passed * * @param CRM_Core_Form $form form object - * @param string $parentName parent name ( tag name) + * @param string $parentNames parent name ( tag name) * @param string $entityTable entitytable 'eg: civicrm_contact' * @param int $entityId entityid 'eg: contact id' * @param boolean $skipTagCreate true if tag need be created using ajax @@ -67,25 +67,15 @@ class CRM_Core_Form_Tag { $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name'); // check if parent exists - $entityTags = array(); if ($parentId) { $tagsetItem = 'parentId_' . $parentId; - $tagset[$tagsetItem]['parentName'] = $parentNameItem; + $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; $tagset[$tagsetItem]['parentID'] = $parentId; - //tokeninput url - $qparams = "parentId={$parentId}"; - - if ($searchMode) { - $qparams .= '&search=1'; - } - - $tagUrl = CRM_Utils_System::url('civicrm/ajax/taglist', $qparams, FALSE, NULL, FALSE); - - $tagset[$tagsetItem]['tagUrl'] = $tagUrl; + // Fixme: these 3 variables may be unused + $tagset[$tagsetItem]['parentName'] = $parentNameItem; $tagset[$tagsetItem]['entityTable'] = $entityTable; $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate; - $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; switch ($entityTable) { case 'civicrm_activity': @@ -109,117 +99,26 @@ class CRM_Core_Form_Tag { $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName; if ($tagsetElementName) { - $form->addEntityRef("{$tagsetElementName}[{$parentId}]", NULL, array( + $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array( 'entity' => 'tag', 'multiple' => TRUE, - 'create' => TRUE, - 'api' => array('params' => array('parent_id' => $parentId))) - ); + 'create' => !$skipTagCreate, + 'api' => array('params' => array('parent_id' => $parentId)), + 'data-entity_table' => $entityTable, + 'data-entity_id' => $entityId, + 'class' => "crm-$mode-tagset", + )); } if ($entityId) { $tagset[$tagsetItem]['entityId'] = $entityId; $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable); - } - else { - - switch ($entityTable) { - case 'civicrm_activity': - if (!empty($form->_submitValues['activity_taglist']) && !empty($form->_submitValues['activity_taglist'][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues['activity_taglist'][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - break; - - case 'civicrm_case': - if (!empty($form->_submitValues['case_taglist']) && !empty($form->_submitValues['case_taglist'][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues['case_taglist'][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - break; - case 'civicrm_file': - $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); - for ($i = 1; $i <= $numAttachments; $i++) { - $tagset[$i] = $tagset[$tagsetItem]; - $tagset[$i]['tagsetElementName'] = "attachment_taglist_$i"; - $form->add('text', "attachment_taglist_{$i}[{$parentId}]", NULL); - if (!empty($form->_submitValues["attachment_taglist_$i"]) && !empty($form->_submitValues["attachment_taglist_$i"][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues["attachment_taglist_$i"][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - } - unset($tagset[$tagsetItem]); - break; - - default: - if (!empty($form->_formValues['contact_tags'])) { - $contactTags = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', TRUE, FALSE, $parentId); - - foreach (array_keys($form->_formValues['contact_tags']) as $tagId) { - if (!empty($contactTags[$tagId])) { - $tagName = $tagId; - if (is_numeric($tagId)) { - $tagName = $contactTags[$tagId]; - } - - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - } + if ($entityTags) { + $form->setDefaults(array("{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags)))); } } - - if (!empty($entityTags)) { - // assign as simple array for display in smarty - $tagset[$tagsetItem]['entityTagsArray'] = $entityTags; - // assign as json for js widget - $tagset[$tagsetItem]['entityTags'] = json_encode(array_values($entityTags)); - - if (!empty($form->_entityTagValues)) { - $form->_entityTagValues = CRM_Utils_Array::crmArrayMerge($entityTags, $form->_entityTagValues); - } - else { - $form->_entityTagValues = $entityTags; - } + else { + $skipEntityAction = TRUE; } } } diff --git a/CRM/Tag/Form/Tag.php b/CRM/Tag/Form/Tag.php index 5492a05e51..2b2a97ae63 100644 --- a/CRM/Tag/Form/Tag.php +++ b/CRM/Tag/Form/Tag.php @@ -112,7 +112,7 @@ class CRM_Tag_Form_Tag extends CRM_Core_Form { CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, $this->_entityTable, $this->_entityID); if ($this->_action & CRM_Core_Action::BROWSE) { - $this->freeze(); + //$this->freeze(); } else { $this->addButtons(array( diff --git a/js/Common.js b/js/Common.js index ae938704ed..42d4cafd69 100644 --- a/js/Common.js +++ b/js/Common.js @@ -368,7 +368,7 @@ CRM.validate = CRM.validate || { return {id: "0", term: term, label: term + ' (' + ts('new tag') + ')'}; } }; - selectParams.tokenSeparators = [',', ' ']; + selectParams.tokenSeparators = [',']; selectParams.createSearchChoicePosition = 'bottom'; } $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)) @@ -383,12 +383,12 @@ CRM.validate = CRM.validate || { data = $el.select2('data'), item = {id: created.id, label: e.object.term}; if (val === "0") { - $el.select2('data', item); + $el.select2('data', item, true); } else if ($.isArray(val) && $.inArray("0", val) > -1) { _.remove(data, {id: "0"}); data.push(item); - $el.select2('data', data); + $el.select2('data', data, true); } }); } diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 066269b15a..78205449cf 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -71,7 +71,8 @@ entity: 'api3', action: 'call', json: JSON.stringify(entity) - } + }; + status = action; } var ajax = $.ajax({ url: CRM.url('civicrm/ajax/rest'), diff --git a/templates/CRM/Tag/Form/Tag.tpl b/templates/CRM/Tag/Form/Tag.tpl index 94f5750e82..9da84a2604 100644 --- a/templates/CRM/Tag/Form/Tag.tpl +++ b/templates/CRM/Tag/Form/Tag.tpl @@ -31,10 +31,25 @@ }
diff --git a/templates/CRM/common/Tagset.tpl b/templates/CRM/common/Tagset.tpl index 8d19c9a2b3..8d877b8643 100644 --- a/templates/CRM/common/Tagset.tpl +++ b/templates/CRM/common/Tagset.tpl @@ -24,22 +24,50 @@ +--------------------------------------------------------------------+ *} {if empty($tagsetType)} - {capture assign="tagsetType"}contact{/capture} + {assign var="tagsetType" value="contact"}contact{/capture} {/if} {foreach from=$tagsetInfo.$tagsetType item=tagset}
-
- {assign var=elemName value = $tagset.tagsetElementName} - {assign var=parID value = $tagset.parentID} - {assign var=editTagSet value=false} + {assign var="elemName" value=$tagset.tagsetElementName} + {assign var="parID" value=$tagset.parentID} + {$form.$elemName.$parID.label} {$form.$elemName.$parID.html} - {if $action ne 4 } - {assign var=editTagSet value=true} - {if $action eq 16 and !($permission eq 'edit') } - {assign var=editTagSet value=false} - {/if} - {/if}
+ {if !$tagset.skipEntityAction} + + {/if}
{/foreach} -- 2.25.1