X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2Fangular-crmMailingAB%2Fservices.js;h=e23c905b1feab15f1aedd091497d84679e1bdc1e;hb=a740a53116a223821ca8a3d9041b098c44ff5630;hp=fd656bf346c1bf9bde7fda3cde9711fbd626b767;hpb=c9e9a71e45c718f2e3f83c1aeb1640d5b318c4b4;p=civicrm-core.git diff --git a/js/angular-crmMailingAB/services.js b/js/angular-crmMailingAB/services.js index fd656bf346..e23c905b1f 100644 --- a/js/angular-crmMailingAB/services.js +++ b/js/angular-crmMailingAB/services.js @@ -1,22 +1,18 @@ (function (angular, $, _) { - // FIXME: surely there's already some helper which can do this in one line? - // @return string "YYYY-MM-DD hh:mm:ss" - var createNow = function () { - var currentdate = new Date(); - var yyyy = currentdate.getFullYear(); - var mm = currentdate.getMonth() + 1; - mm = mm < 10 ? '0' + mm : mm; - var dd = currentdate.getDate(); - dd = dd < 10 ? '0' + dd : dd; - var hh = currentdate.getHours(); - hh = hh < 10 ? '0' + hh : hh; - var min = currentdate.getMinutes(); - min = min < 10 ? '0' + min : min; - var sec = currentdate.getSeconds(); - sec = sec < 10 ? '0' + sec : sec; - return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + sec; - }; + function OptionGroup(values) { + this.get = function get(value) { + var r = _.where(values, {value: '' + value}); + return r.length > 0 ? r[0] : null; + }; + this.getByName = function get(name) { + var r = _.where(values, {name: '' + name}); + return r.length > 0 ? r[0] : null; + }; + this.getAll = function getAll() { + return values; + }; + } angular.module('crmMailingAB').factory('crmMailingABCriteria', function () { // TODO Get data from server @@ -25,15 +21,17 @@ '2': {value: '2', name: 'From names', label: ts('Test different "From" lines')}, '3': {value: '3', name: 'Two different emails', label: ts('Test entirely different emails')} }; - return { - get: function get(value) { - var r = _.where(values, {value: '' + value}); - return r.length > 0 ? r[0] : null; - }, - getAll: function getAll() { - return values; - } + return new OptionGroup(values); + }); + + angular.module('crmMailingAB').factory('crmMailingABStatus', function () { + // TODO Get data from server + var values = { + '1': {value: '1', name: 'Draft', label: ts('Draft')}, + '2': {value: '2', name: 'Testing', label: ts('Testing')}, + '3': {value: '3', name: 'Final', label: ts('Final')} }; + return new OptionGroup(values); }); // CrmMailingAB is a data-model which combines an AB test (APIv3 "MailingAB"), three mailings (APIv3 "Mailing"), @@ -44,7 +42,7 @@ // abtest.load().then(function(){ // alert("Mailing A is named "+abtest.mailings.a.name); // }); - angular.module('crmMailingAB').factory('CrmMailingAB', function (crmApi, crmMailingMgr, $q, CrmAttachments) { + angular.module('crmMailingAB').factory('CrmMailingAB', function (crmApi, crmMailingMgr, $q, CrmAttachments, crmNow) { function CrmMailingAB(id) { this.id = id; this.mailings = {}; @@ -73,13 +71,13 @@ crmMailingAB.mailings.b = crmMailingMgr.create(); crmMailingAB.mailings.c = crmMailingMgr.create(); crmMailingAB.attachments.a = new CrmAttachments(function () { - return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_a']}; + return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_a}; }); crmMailingAB.attachments.b = new CrmAttachments(function () { - return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_b']}; + return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_b}; }); crmMailingAB.attachments.c = new CrmAttachments(function () { - return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab['mailing_id_c']}; + return {entity_table: 'civicrm_mailing', entity_id: crmMailingAB.ab.mailing_id_c}; }); var dfr = $q.defer(); @@ -104,7 +102,7 @@ .then(function () { return crmApi('MailingAB', 'create', crmMailingAB.ab) .then(function (abResult) { - crmMailingAB.ab.id = abResult.id; + crmMailingAB.id = crmMailingAB.ab.id = abResult.id; }); }) .then(function () { @@ -113,14 +111,30 @@ }, // Schedule the test // @return Promise CrmMailingAB - // Note: Submission may cause the server state to change. Consider abtest.submit().then(abtest.load) - submit: function submit(newStatus) { + // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...) + submitTest: function submitTest() { + var crmMailingAB = this; + var params = { + id: this.ab.id, + status: 'Testing', + approval_date: crmNow(), + scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : crmNow() + }; + return crmApi('MailingAB', 'submit', params) + .then(function () { + return crmMailingAB; + }); + }, + // Schedule the final mailing + // @return Promise CrmMailingAB + // Note: Submission may cause the server state to change. Consider abtest.submit().then(...abtest.load()...) + submitFinal: function submitFinal() { var crmMailingAB = this; var params = { - id: this.id, - status: newStatus, - approval_date: createNow(), - scheduled_date: this.mailings.a.scheduled_date ? this.mailings.a.scheduled_date : createNow() + id: this.ab.id, + status: 'Final', + approval_date: crmNow(), + scheduled_date: this.mailings.c.scheduled_date ? this.mailings.c.scheduled_date : crmNow() }; return crmApi('MailingAB', 'submit', params) .then(function () {