CRM-20521 Convert returning groups and mailings for CiviMail new mailing screen into...
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 4 May 2017 11:23:31 +0000 (21:23 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 1 Aug 2017 22:23:42 +0000 (08:23 +1000)
CRM/Mailing/Info.php
ang/crmMailing/BlockRecipients.html
ang/crmMailing/Recipients.html [deleted file]
ang/crmMailing/Recipients.js
ang/crmMailing/ViewRecipCtrl.js

index 78557a74088be695efd12ec1bb60dfb2d91c789c..c73f45d26d31772d51e1e517c066fe5c0e4e6875 100644 (file)
@@ -90,30 +90,12 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
     $session = CRM_Core_Session::singleton();
     $contactID = $session->get('userID');
 
-    // Get past mailings.
-    // CRM-16155 - Limit to a reasonable number.
-    $civiMails = civicrm_api3('Mailing', 'get', array(
-      'is_completed' => 1,
-      'mailing_type' => array('IN' => array('standalone', 'winner')),
-      'domain_id' => CRM_Core_Config::domainID(),
-      'return' => array('id', 'name', 'scheduled_date'),
-      'sequential' => 1,
-      'options' => array(
-        'limit' => 500,
-        'sort' => 'is_archived asc, scheduled_date desc',
-      ),
-    ));
     // Generic params.
     $params = array(
       'options' => array('limit' => 0),
       'sequential' => 1,
     );
 
-    $groupNames = civicrm_api3('Group', 'get', $params + array(
-      'is_active' => 1,
-      'check_permissions' => TRUE,
-      'return' => array('title', 'visibility', 'group_type', 'is_hidden'),
-    ));
     $headerfooterList = civicrm_api3('MailingComponent', 'get', $params + array(
       'is_active' => 1,
       'return' => array('name', 'component_type', 'is_default', 'body_html', 'body_text'),
@@ -145,9 +127,9 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
       ->addSetting(array(
         'crmMailing' => array(
           'templateTypes' => CRM_Mailing_BAO_Mailing::getTemplateTypes(),
-          'civiMails' => $civiMails['values'],
+          'civiMails' => array(),
           'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents),
-          'groupNames' => $groupNames['values'],
+          'groupNames' => array(),
           'headerfooterList' => $headerfooterList['values'],
           'mesTemplate' => $mesTemplate['values'],
           'emailAdd' => $emailAdd['values'],
index 354e26925c176b4f8fb2ea7f4481ea22594b2afe..83af7a5741984d2398b4076c15d89a192e3dfc96 100644 (file)
@@ -4,16 +4,13 @@
       <a href="" ng-click="previewRecipients()" title="{{ts('Preview a List of Recipients')}}">{{getRecipientsEstimate()}}</a>
     </div>
   </div>
-  <select
+  <input
+    type="hidden"
     crm-mailing-recipients
     ng-model="mailing.recipients"
     crm-mandatory-groups="crmMailingConst.groupNames | filter:{is_hidden:1}"
-    crm-avail-groups="crmMailingConst.groupNames | filter:isMailingList | filter:{is_hidden:0}"
-    crm-avail-mailings="crmMailingConst.civiMails"
     crm-ui-id="{{crmMailingBlockRecipients.id}}"
     name="{{crmMailingBlockRecipients.name}}"
-    ng-required="true"
-    multiple>
-  </select>
+    ng-required="true"/>
   <a crm-icon="fa-wrench" ng-click="editOptions(mailing)" class="crm-hover-button" title="{{ts('Edit Recipient Options')}}"></a>
 </div>
diff --git a/ang/crmMailing/Recipients.html b/ang/crmMailing/Recipients.html
deleted file mode 100644 (file)
index 5fb6117..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<!--
-Directive: crmMailingRecipients
--->
-<select multiple>
-  <optgroup label="{{ts('Mandatory Group')}}">
-    <option ng-repeat="grp in mandatoryGroups" value="{{grp.id}} civicrm_group include">{{ts('Search Results')}}</option>
-  </optgroup>
-  <optgroup label="{{ts('Include Group')}}">
-    <option ng-repeat="grp in groups|orderBy:'title'" value="{{grp.id}} civicrm_group include">{{grp.title}}</option>
-  </optgroup>
-  <optgroup label="{{ts('Exclude Group')}}">
-    <option ng-repeat="grp in groups|orderBy:'title'" value="{{grp.id}} civicrm_group exclude">{{grp.title}}</option>
-  </optgroup>
-  <optgroup label="{{ts('Include Past Recipients From')}}">
-    <option ng-repeat="ml in mailings|orderBy:'name'" value="{{ml.id}} civicrm_mailing include">{{ml.name}} ({{parseDate(ml.scheduled_date) | date:'mediumDate'}})</option>
-  </optgroup>
-  <optgroup label="{{ts('Exclude Past Recipients From')}}">
-    <option ng-repeat="ml in mailings|orderBy:'name'" value="{{ml.id}} civicrm_mailing exclude">{{ml.name}} ({{parseDate(ml.scheduled_date) | date:'mediumDate'}})</option>
-  </optgroup>
-</select>
index 60cd1b88e21a928385e12020515671d6c595f327..af7046c9387f9279d96bab1a2be2f3380497daf4 100644 (file)
@@ -6,9 +6,6 @@
       restrict: 'AE',
       require: 'ngModel',
       scope: {
-        crmAvailGroups: '@', // available groups
-        crmAvailMailings: '@', // available mailings
-        crmMandatoryGroups: '@', // hard-coded/mandatory groups
         ngRequired: '@'
       },
       templateUrl: '~/crmMailing/Recipients.html',
           }
         }
 
+        var rcpAjaxState = {
+         input: '',
+         entity: 'civicrm_group',
+         type: 'include',
+         page_n: 0,
+         page_i: 0,
+        };
+
         $(element).select2({
+          width: '36em',
           dropdownAutoWidth: true,
           placeholder: "Groups or Past Recipients",
           formatResult: formatItem,
           formatSelection: formatItem,
           escapeMarkup: function(m) {
             return m;
-          }
+          },
+          multiple: true,
+          initSelection: function(el, cb) {
+            var values = el.val().split(',');
+
+            var gids = [];
+            var mids = [];
+
+            for (var i in values) {
+              var dv = convertValueToObj(values[i]);
+              if (dv.entity_type == 'civicrm_group') {
+                gids.push(dv.entity_id);
+              }
+              else if (dv.entity_type == 'civicrm_mailing') {
+                mids.push(dv.entity_id);
+              }
+            }
+
+            CRM.api3('Group', 'getlist', { params: { id: { IN: gids } }, extra: ["is_hidden"] }).then(
+              function(glist) {
+                CRM.api3('Mailing', 'getlist', { params: { id: { IN: mids } } }).then(
+                  function(mlist) {
+                    var datamap = [];
+
+                    var groupNames = [];
+                    var civiMails = [];
+
+                    $(glist.values).each(function (idx, group) {
+                      var key = group.id + ' civicrm_group include';
+                      groupNames.push({id: parseInt(group.id), title: group.label, is_hidden: group.extra.is_hidden});
+
+                      if (values.indexOf(key) >= 0) {
+                        datamap.push({id: key, text: group.label});
+                      }
+
+                      key = group.id + ' civicrm_group exclude';
+                      if (values.indexOf(key) >= 0) {
+                        datamap.push({id: key, text: group.label});
+                      }
+                    });
+
+                    $(mlist.values).each(function (idx, group) {
+                      var key = group.id + ' civicrm_mailing include';
+                      civiMails.push({id: parseInt(group.id), name: group.label});
+
+                      if (values.indexOf(key) >= 0) {
+                        datamap.push({id: key, text: group.label});
+                      }
+
+                      key = group.id + ' civicrm_mailing exclude';
+                      if (values.indexOf(key) >= 0) {
+                        datamap.push({id: key, text: group.label});
+                      }
+                    });
+
+                    scope.$parent.crmMailingConst.groupNames = groupNames;
+                    scope.$parent.crmMailingConst.civiMails = civiMails;
+
+                    refreshMandatory();
+
+                    cb(datamap);
+                  });
+              });
+          },
+          ajax: {
+            url: CRM.url('civicrm/ajax/rest'),
+            quietMillis: 300,
+            data: function(input, page_num) {
+              if (page_num <= 1) {
+                rcpAjaxState = {
+                  input: input,
+                  entity: 'civicrm_group',
+                  type: 'include',
+                  page_n: 0,
+                };
+              }
+
+              rcpAjaxState.page_i = page_num - rcpAjaxState.page_n;
+
+              var params = {
+                input: input,
+                page_num: rcpAjaxState.page_i,
+                params: { is_hidden: 0 },
+              };
+              return params;
+            },
+            transport: function(params) {
+              switch(rcpAjaxState.entity) {
+              case 'civicrm_group':
+                CRM.api3('Group', 'getlist', params.data).then(params.success, params.error);
+                break;
+
+              case 'civicrm_mailing':
+                params.data.params.options = { sort: "is_archived asc, scheduled_date desc" };
+                CRM.api3('Mailing', 'getlist', params.data).then(params.success, params.error);
+                break;
+              }
+            },
+            results: function(data) {
+              results = {
+                children: $.map(data.values, function(obj) {
+                  return {   id: obj.id + ' ' + rcpAjaxState.entity + ' ' + rcpAjaxState.type,
+                             text: obj.label };
+                })
+              };
+
+              if (rcpAjaxState.page_i == 1 && data.count) {
+                results.text = ts((rcpAjaxState.type == 'include'? 'Include ' : 'Exclude ') +
+                  (rcpAjaxState.entity == 'civicrm_group'? 'Group' : 'Mailing'));
+              }
+
+              more = data.more_results || !(rcpAjaxState.entity == 'civicrm_mailing' && rcpAjaxState.type == 'exclude');
+
+              if (more && !data.more_results) {
+                if (rcpAjaxState.type == 'include') {
+                  rcpAjaxState.type = 'exclude';
+                } else {
+                 rcpAjaxState.type = 'include';
+                 rcpAjaxState.entity = 'civicrm_mailing';
+                }
+                rcpAjaxState.page_n += rcpAjaxState.page_i;
+              }
+
+              return { more: more, results: [ results ] };
+            },
+          },
         });
 
         $(element).on('select2-selecting', function(e) {
index d07bb83e71ac9a6e058409df5bf5fc4d169fd844..652b89e3a8881743a31b6351a1d41ed9eaf21d6e 100644 (file)
@@ -5,20 +5,24 @@
       var first = true;
       var names = '';
       _.each(mailing.recipients.groups.include, function(id) {
-        if (!first) {
-          names = names + ', ';
-        }
         var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
-        names = names + group[0].title;
-        first = false;
+        if (group.length) {
+          if (!first) {
+            names = names + ', ';
+          }
+          names = names + group[0].title;
+          first = false;
+        }
       });
       _.each(mailing.recipients.mailings.include, function(id) {
-        if (!first) {
-          names = names + ', ';
-        }
         var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
-        names = names + oldMailing[0].name;
-        first = false;
+        if (oldMailing.length) {
+          if (!first) {
+            names = names + ', ';
+          }
+          names = names + oldMailing[0].name;
+          first = false;
+        }
       });
       return names;
     };
       var first = true;
       var names = '';
       _.each(mailing.recipients.groups.exclude, function(id) {
-        if (!first) {
-          names = names + ', ';
-        }
         var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
-        names = names + group[0].title;
-        first = false;
+        if (group.length) {
+          if (!first) {
+            names = names + ', ';
+          }
+          names = names + group[0].title;
+          first = false;
+        }
       });
       _.each(mailing.recipients.mailings.exclude, function(id) {
-        if (!first) {
-          names = names + ', ';
-        }
         var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
-        names = names + oldMailing[0].name;
-        first = false;
+        if (oldMailing.length) {
+          if (!first) {
+            names = names + ', ';
+          }
+          names = names + oldMailing[0].name;
+          first = false;
+        }
       });
       return names;
     };