Merge pull request #5557 from monishdeb/4.6
[civicrm-core.git] / js / angular-crmMailingAB.js
index 77ff3583a4b6a12d2edc9fd9b4dd5bce1a98b66a..5b5cef669050f8c386d527f462c9468baff692a1 100644 (file)
@@ -1,6 +1,6 @@
 (function (angular, $, _) {
 
-  angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize', 'crmUi', 'crmAttachment', 'crmMailing', 'crmD3']);
+  angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'crmUi', 'crmAttachment', 'crmMailing', 'crmD3']);
   angular.module('crmMailingAB').config([
     '$routeProvider',
     function ($routeProvider) {
@@ -29,7 +29,7 @@
         }
       });
       $routeProvider.when('/abtest/:id', {
-        templateUrl: '~/crmMailingAB/edit.html',
+        templateUrl: '~/crmMailingAB/main.html',
         controller: 'CrmMailingABEditCtrl',
         resolve: {
           abtest: function ($route, CrmMailingAB) {
           }
         }
       });
-      $routeProvider.when('/abtest/:id/report', {
-        templateUrl: '~/crmMailingAB/report.html',
-        controller: 'CrmMailingABReportCtrl',
-        resolve: {
-          abtest: function ($route, CrmMailingAB) {
-            var abtest = new CrmMailingAB($route.current.params.id);
-            return abtest.load();
-          }
-        }
-      });
     }
   ]);
 
@@ -76,6 +66,7 @@
     var myAutosave = null;
     $scope.crmMailingABCriteria = crmMailingABCriteria;
     $scope.crmMailingConst = CRM.crmMailing;
+    $scope.checkPerm = CRM.checkPerm;
 
     $scope.isSubmitted = function isSubmitted() {
       return _.size(abtest.mailings.a.jobs) > 0 || _.size(abtest.mailings.b.jobs) > 0;
@@ -84,7 +75,7 @@
     $scope.sync = function sync() {
       abtest.mailings.a.name = ts('Test A (%1)', {1: abtest.ab.name});
       abtest.mailings.b.name = ts('Test B (%1)', {1: abtest.ab.name});
-      abtest.mailings.c.name = ts('Winner (%1)', {1: abtest.ab.name});
+      abtest.mailings.c.name = ts('Final (%1)', {1: abtest.ab.name});
 
       if (abtest.ab.testing_criteria) {
         // TODO review fields exposed in UI and make sure the sync rules match
       };
       var options = CRM.utils.adjustDialogDefaults({
         autoOpen: false,
-        title: ts('Select Winner (%1)', {
+        height: 'auto',
+        width: '40%',
+        title: ts('Select Final Mailing (Test %1)', {
           1: mailingName.toUpperCase()
         })
       });
   angular.module('crmMailingAB').controller('CrmMailingABReportCtrl', function ($scope, crmApi, crmMailingStats) {
     var ts = $scope.ts = CRM.ts(null);
 
-    var activeMailingStatus = null, activeMailings = null;
+    var CrmMailingABReportCnt = 1, activeMailings = null;
     $scope.getActiveMailings = function() {
-      if ($scope.abtest.ab.status != activeMailingStatus) {
-        activeMailingStatus = $scope.abtest.ab.status;
+      if ($scope.abtest.$CrmMailingABReportCnt != CrmMailingABReportCnt) {
+        $scope.abtest.$CrmMailingABReportCnt = ++CrmMailingABReportCnt;
         activeMailings = [
           {name: 'a', title: ts('Mailing A'), mailing: $scope.abtest.mailings.a, attachments: $scope.abtest.attachments.a},
           {name: 'b', title: ts('Mailing B'), mailing: $scope.abtest.mailings.b, attachments: $scope.abtest.attachments.b}
 
     $scope.statTypes = crmMailingStats.getStatTypes();
     $scope.statUrl = function statUrl(mailing, statType, view) {
-      return crmMailingStats.getUrl(mailing, statType, view);
+      return crmMailingStats.getUrl(mailing, statType, view, 'abtest/' + $scope.abtest.ab.id);
     };
+
+    $scope.checkPerm = CRM.checkPerm;
   });
 
   angular.module('crmMailingAB').controller('CrmMailingABWinnerDialogCtrl', function ($scope, $timeout, dialogService, crmMailingMgr, crmStatus) {
       // When using dialogService with a button bar, the major button actions
       // need to be registered with the dialog widget (and not embedded in
       // the body of the dialog).
-      var buttons = {};
-      buttons[ts('Select Winner')] = function () {
-        crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings[mailingName], [
-          'name',
-          'recipients',
-          'scheduled_date'
-        ]);
-        crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
-          .then(function () {
-            return crmStatus({start: ts('Submitting...'), success: ts('Submitted')},
-              abtest.submitFinal().then(function(){
-                return abtest.load();
-              }));
-          })
-          .then(function(){
-            dialogService.close('selectWinnerDialog', abtest);
-          });
-      };
-      buttons[ts('Cancel')] = function () {
-        dialogService.cancel('selectWinnerDialog');
-      };
+      var buttons = [
+        {
+          text: ts('Submit final mailing'),
+          icons: {primary: 'ui-icon-check'},
+          click: function () {
+            crmMailingMgr.mergeInto(abtest.mailings.c, abtest.mailings[mailingName], [
+              'name',
+              'recipients',
+              'scheduled_date'
+            ]);
+            crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
+              .then(function () {
+                return crmStatus({start: ts('Submitting...'), success: ts('Submitted')},
+                  abtest.submitFinal().then(function(r){
+                    delete abtest.$CrmMailingABReportCnt;
+                    return r;
+                  }));
+              })
+              .then(function(){
+                dialogService.close('selectWinnerDialog', abtest);
+              });
+          }
+        },
+        {
+          text: ts('Cancel'),
+          icons: {primary: 'ui-icon-close'},
+          click: function () {
+            dialogService.cancel('selectWinnerDialog');
+          }
+        }
+      ];
       dialogService.setButtons('selectWinnerDialog', buttons);
     }