From 08de74861df37a39bc95c535d9e953679e832403 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 29 Jul 2021 13:13:13 -0700 Subject: [PATCH] msgtplui - Render using prefetched example data --- ext/msgtplui/ang/msgtplui/Edit.js | 35 ++++++++++++++++++++-------- ext/msgtplui/ang/msgtplui/Preview.js | 20 +++++++--------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/ext/msgtplui/ang/msgtplui/Edit.js b/ext/msgtplui/ang/msgtplui/Edit.js index 306739faf1..e6e785fe24 100644 --- a/ext/msgtplui/ang/msgtplui/Edit.js +++ b/ext/msgtplui/ang/msgtplui/Edit.js @@ -172,8 +172,8 @@ var revisionTypes = [ {name: 'original', label: ts('Original')}, {name: 'main', label: ts('Standard')}, - {name: 'txActive', label: ts('%1 - Current translation', {1: $ctrl.locales[$ctrl.lang]})}, - {name: 'txDraft', label: ts('%1 - Draft translation', {1: $ctrl.locales[$ctrl.lang]})} + {name: 'txActive', label: ts('%1 - Current translation', {1: $ctrl.locales[$ctrl.lang] || $ctrl.lang})}, + {name: 'txDraft', label: ts('%1 - Draft translation', {1: $ctrl.locales[$ctrl.lang] || $ctrl.lang})} ]; $ctrl.switchTab = function switchTab(tgt) { @@ -265,17 +265,32 @@ title: ts('Preview') }; - crmApi4('WorkflowMessageExample', 'get', { - // FIXME: workflow name - where: [["tags", "CONTAINS", "preview"], ["workflow", "=", $ctrl.records.main.workflow_name]], - limit: 25 - }).then(function(workflowMessageExamples) { - defaults.exampleName = workflowMessageExamples.length > 0 ? workflowMessageExamples[0].name : null; + crmApi4({ + examples: ['WorkflowMessageExample', 'get', { + // FIXME: workflow name + where: [["tags", "CONTAINS", "preview"], ["workflow", "=", $ctrl.records.main.workflow_name]], + select: ['name', 'title', 'workflow', 'data'] + }], + adhoc: ['WorkflowMessage', 'getTemplateFields', { + workflow: $ctrl.records.main.workflow_name, + format: 'example' + }] + }).then(function(resp) { + console.log('resp',resp); + if ((!resp.examples || resp.examples.length === 0) && resp.adhoc) { + resp.examples = [{ + name: 'auto', + title: ts('Empty example'), + workflow: $ctrl.records.main.workflow_name, + data: {modelProps: resp.adhoc[0]} + }]; + } + defaults.exampleName = resp.examples.length > 0 ? (resp.examples)[0].name : null; var i = 0; - angular.forEach(workflowMessageExamples, function(ex) { + angular.forEach(resp.examples, function(ex) { ex.id = i++; }); - defaults.examples = workflowMessageExamples; + defaults.examples = resp.examples; var model = angular.extend({}, defaults, args); var options = CRM.utils.adjustDialogDefaults({ diff --git a/ext/msgtplui/ang/msgtplui/Preview.js b/ext/msgtplui/ang/msgtplui/Preview.js index afbdd5c0a8..d153d469e5 100644 --- a/ext/msgtplui/ang/msgtplui/Preview.js +++ b/ext/msgtplui/ang/msgtplui/Preview.js @@ -1,6 +1,6 @@ (function(angular, $, _) { - angular.module('msgtplui').controller('MsgtpluiPreviewCtrl', function($scope, crmUiHelp, crmStatus, crmApi4, $timeout, $q) { + angular.module('msgtplui').controller('MsgtpluiPreviewCtrl', function($scope, crmUiHelp, crmStatus, crmApi4, crmUiAlert, $timeout, $q) { var ts = $scope.ts = CRM.ts('msgtplui'); var hs = $scope.hs = crmUiHelp({file: 'CRM/Msgtplui/msgtplui'}); // See: templates/CRM/Msgtplui/msgtplui.hlp @@ -21,20 +21,16 @@ // $ctrl.preview = model.revisions[$ctrl.revisionId].rec; $ctrl.preview = {loading: true}; - var getting = crmApi4('WorkflowMessageExample', 'get', { - where: [["name", "=", model.examples[$ctrl.exampleId].name]], - select: ['workflow', 'data'], - chain: { - "render": ["WorkflowMessage", "render", { - "workflow": "$workflow", - "values": "$data.modelProps", - "messageTemplate": model.revisions[$ctrl.revisionId].rec - }] - } + var liveExample = model.examples[$ctrl.exampleId]; + var getting = crmApi4('WorkflowMessage', 'render', { + workflow: liveExample.workflow, + values: liveExample.data.modelProps, + messageTemplate: model.revisions[$ctrl.revisionId].rec }).then(function(response) { - $ctrl.preview = response[0].render[0]; + $ctrl.preview = response[0]; }, function(failure) { $ctrl.preview = {}; + crmUiAlert({title: ts('Render failed'), text: failure.error_message, type: 'error'}); }); return crmStatus({start: ts('Rendering...'), success: ''}, getting); }; -- 2.25.1