}
static function mergeTagList() {
- $name = CRM_Utils_Type::escape($_GET['s'], 'String');
+ $name = CRM_Utils_Type::escape($_GET['s'], 'String');
$fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
- $limit = CRM_Utils_Type::escape($_GET['limit'], 'Integer');
+ $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
// build used-for clause to be used in main query
$usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
({$usedForClause})
LIMIT $limit";
$dao = CRM_Core_DAO::executeQuery($query);
+ $result = array();
while ($dao->fetch()) {
- $warning = 0;
+ $row = array(
+ 'id' => $dao->id,
+ 'text' => ($dao->parent ? "{$dao->parent} :: " : '') . $dao->name,
+ );
+ // Add warning about used_for types
if (!empty($dao->used_for)) {
$usedForTagB = explode(',', $dao->used_for);
sort($usedForTagB);
$usedForDiff = array_diff($usedForTagA, $usedForTagB);
if (!empty($usedForDiff)) {
- $warning = 1;
+ $row['warning'] = TRUE;
}
}
- $tag = addcslashes($dao->name, '"') . "|{$dao->id}|{$warning}\n";
- echo $tag = $dao->parent ? (addcslashes($dao->parent, '"') . ' :: ' . $tag) : $tag;
+ $result[] = $row;
}
+ print json_encode($result);
CRM_Utils_System::civiExit();
}
$result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
}
+ $result['message'] = ts('"%1" has been merged with "%2". All records previously tagged "%1" are now tagged "%2".',
+ array(1 => $result['tagA'], 2 => $result['tagB'])
+ );
+
echo json_encode($result);
CRM_Utils_System::civiExit();
}
* passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue"
* @param options {object|void} Override defaults, keys include 'title', 'message',
* see jQuery.dialog for full list of available params
+ * @param cancelLabel {string}
*/
CRM.confirm = function (buttons, options, cancelLabel) {
var dialog, callbacks = {};
}
$.each(callbacks, function (label, callback) {
settings.buttons[label] = function () {
- callback.call(dialog);
- dialog.dialog('close');
+ if (callback.call(dialog) !== false) {
+ dialog.dialog('close');
+ }
};
});
dialog = $('<div class="crm-container crm-confirm-dialog"></div>')
</div>
{/if}
-<div id="mergeTagDialog" style="display:none;">
- {ts}Begin typing name of tag to merge into.{/ts}<br/>
- <input type="text" id="tag_name"/>
- <input type="hidden" id="tag_name_id" value="">
- <div id="used_for_warning" class="status message"></div>
-</div>
-
</div>
{literal}
<script type="text/javascript">
-cj("#mergeTagDialog").hide( );
-cj( function($) {
- cj('.merge_tag').click(function(e){
- var tagId = cj(this).closest('tr').attr('id').split('-');
- mergeTag(tagId[1]);
- return false;
- });
-});
-
-function mergeTag( fromId ) {
- var fromTag = cj('#tag-' + fromId).children('td.crm-tag-name').text();
- cj('#used_for_warning').html('');
-
- cj("#mergeTagDialog").dialog({
- title: "Merge tag '" + fromTag + "' into:",
- modal: true,
- bgiframe: true,
- close: function(event, ui) { cj("#tag_name").unautocomplete( ); },
- overlay: {
- opacity: 0.5,
- background: "black"
- },
-
- open:function() {
- cj("#tag_name").val( "" );
- cj("#tag_name_id").val( null );
-
- var tagUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTagList' h=0 q='fromId='}"{literal} + fromId;
-
- cj("#tag_name").autocomplete( tagUrl, {
- width: 260,
- selectFirst: false,
- matchContains: true
- });
-
- cj("#tag_name").focus();
- cj("#tag_name").result(function(event, data, formatted) {
- cj("input[id=tag_name_id]").val(data[1]);
- if ( data[2] == 1 ) {
- cj('#used_for_warning').html("Warning: '" + fromTag + "' has different used-for options than the selected tag, which would be merged into the selected tag. Click Ok to proceed.");
- } else {
- cj('#used_for_warning').html('');
- }
- });
- },
+cj(function($) {
+ var tag;
+ $('.merge_tag').click(function() {
+ tag = $(this).crmEditableEntity();
+ mergeTagDialog();
+ return false;
+ });
- buttons: {
- "Ok": function() {
- if ( ! cj("#tag_name").val( ) ) {
- alert('{/literal}{ts escape="js"}Select valid tag from the list{/ts}{literal}.');
- return false;
- }
- var toId = cj("#tag_name_id").val( );
- if ( ! toId ) {
- alert('{/literal}{ts escape="js"}Select valid tag from the list{/ts}{literal}.');
- return false;
+ function mergeTagDialog() {
+ var tagUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTagList' h=0}"{literal};
+ var title = {/literal}'{ts escape="js" 1="%1"}Merge tag %1 into:{/ts}'{literal};
+ CRM.confirm(doMergeTag, {
+ title: ts(title, {1: tag.name}),
+ message: '<input name="select_merge_tag" class="big" />',
+ open: function() {
+ var dialog = this;
+ $('input[name=select_merge_tag]', dialog)
+ .select2({
+ placeholder: {/literal}'{ts escape="js"}- select tag -{/ts}'{literal},
+ minimumInputLength: 1,
+ ajax: {
+ url: tagUrl,
+ data: function(term) {
+ return {s: term, fromId: tag.id};
+ },
+ results: function(response) {
+ return {results: response};
+ }
+ }
+ })
+ .change(function() {
+ $('.messages', dialog).remove();
+ if ($(this).val() && $(this).select2('data').warning) {
+ $(dialog).append('<div class="messages status">{/literal}{ts escape='js'}Note: the selected tag is used by additional entities.{/ts}{literal}</div>');
+ }
+ });
+ },
+ close: function() {
+ $('input[name=select_merge_tag]', this).select2('destroy');
+ $(this).dialog('destroy').remove();
+ }
+ });
+ }
+
+ function doMergeTag() {
+ var toId = $("input[name=select_merge_tag]").val();
+ if (!toId) {
+ return false;
+ }
+ /* send synchronous request so that disabling any actions for slow servers*/
+ var postUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTags' h=0 }"{literal};
+ var data = {fromId: tag.id, toId: toId, key:{/literal}"{crmKey name='civicrm/ajax/mergeTags'}"{literal}};
+ cj.ajax({
+ type: "POST",
+ url: postUrl,
+ data: data,
+ dataType: "json",
+ success: function(values) {
+ if ( values.status == true ) {
+ $('#tag-' + toId).children('td.crm-tag-used_for').text(values.tagB_used_for);
+ $('#tag-' + tag.id).html('<td colspan="8"><div class="status message"><div class="icon inform-icon"></div>' + values.message + '</div></td>');
}
-
- /* send synchronous request so that disabling any actions for slow servers*/
- var postUrl = {/literal}"{crmURL p='civicrm/ajax/mergeTags' h=0 }"{literal};
- var data = {fromId: fromId, toId: toId, key:{/literal}"{crmKey name='civicrm/ajax/mergeTags'}"{literal}};
- cj.ajax({ type : "POST",
- url : postUrl,
- data : data,
- dataType : "json",
- success : function( values ) {
- if ( values.status == true ) {
- cj('#tag-' + toId).children('td.crm-tag-used_for').text(values.tagB_used_for);
- var msg = "'" + values.tagA + "' has been merged with '" + values.tagB + "'. All records previously tagged with '" + values.tagA + "' are now tagged with '" + values.tagB + "'.";
- cj('#tag-' + fromId).html('<td colspan="8"><div class="status message"><div class="icon inform-icon"></div>' + msg + '</div></td>');
- }
- }
- });
-
- cj(this).dialog("close");
- },
-
- "Cancel": function() {
- cj(this).dialog("close");
}
- }
- });
-}
+ });
+ }
+});
</script>
{/literal}