CRM-16155 - Use enityRef for mailing campaigns widget
authorColeman Watts <coleman@civicrm.org>
Thu, 26 Mar 2015 18:17:37 +0000 (14:17 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 26 Mar 2015 18:21:31 +0000 (14:21 -0400)
CRM/Mailing/Info.php
js/angular-crm-ui.js
partials/crmMailing/summary.html
partials/crmMailingAB/setup.html

index 14e9ccee7a582299500edbabda83fd6605bb6ce0..ece34db0cb64e119ef49cabd33a41822f209197f 100644 (file)
@@ -92,6 +92,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
       ),
     );
 
+    $config = CRM_Core_Config::singleton();
     $session = CRM_Core_Session::singleton();
     $contactID = $session->get('userID');
 
@@ -105,18 +106,14 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
       'options' => array(
         'limit' => 500,
         'sort' => 'is_archived asc, scheduled_date desc',
-      )
+      ),
     ));
     // Generic params
     $params = array(
       'options' => array('limit' => 0),
       'sequential' => 1,
     );
-    
-    $campNames = civicrm_api3('Campaign', 'get', $params + array(
-      'is_active' => 1,
-      'return' => 'title',
-    ));
+
     $groupNames = civicrm_api3('Group', 'get', $params + array(
       'is_active' => 1,
       'return' => array('title', 'visibility', 'group_type', 'is_hidden'),
@@ -142,7 +139,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
     $mailGrp = civicrm_api3('MailingGroup', 'get', $params);
     $mailTokens = civicrm_api3('Mailing', 'gettokens', array(
       'entity' => array('contact', 'mailing'),
-      'sequential' => 1
+      'sequential' => 1,
     ));
     $fromAddress = civicrm_api3('OptionValue', 'get', $params + array(
       'option_group_id' => "from_email_address",
@@ -151,7 +148,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info {
       ->addSetting(array(
         'crmMailing' => array(
           'civiMails' => $civiMails['values'],
-          'campNames' => $campNames['values'],
+          'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents),
           'groupNames' => $groupNames['values'],
           'headerfooterList' => $headerfooterList['values'],
           'mesTemplate' => $mesTemplate['values'],
index f055ca6c7d6c3dd7d4c8caafebb3962af24e8ded..3e821bb7e56fdc13f3cf22642ed993cecc7ca7f5 100644 (file)
       };
     })
 
+    // Render a crmEntityRef widget
+    // usage: <input crm-entityref="{entity: 'Contact', select: {allowClear:true}}" ng-model="myobj.field" />
+    .directive('crmEntityref', function ($parse, $timeout) {
+      return {
+        require: '?ngModel',
+        scope: {
+          crmEntityref: '='
+        },
+        link: function (scope, element, attrs, ngModel) {
+          // In cases where UI initiates update, there may be an extra
+          // call to refreshUI, but it doesn't create a cycle.
+
+          ngModel.$render = function () {
+            $timeout(function () {
+              // ex: msg_template_id adds new item then selects it; use $timeout to ensure that
+              // new item is added before selection is made
+              element.select2('val', ngModel.$viewValue);
+            });
+          };
+          function refreshModel() {
+            var oldValue = ngModel.$viewValue, newValue = element.select2('val');
+            if (oldValue != newValue) {
+              scope.$parent.$apply(function () {
+                ngModel.$setViewValue(newValue);
+              });
+            }
+          }
+
+          function init() {
+            // TODO watch options
+            // TODO can we infer "entity" from model?
+            element.crmEntityRef(scope.crmEntityref || {});
+            element.on('change', refreshModel);
+            $timeout(ngModel.$render);
+          }
+
+          init();
+        }
+      };
+    })
+
     // example <div crm-ui-tab crm-title="ts('My Title')">...content...</div>
     // WISHLIST: use a full Angular component instead of an incomplete jQuery wrapper
     .directive('crmUiTab', function($parse) {
index d843e7840b2e7f5ad53a47f1dfd66718a03f84b2..6050e448a2c3a692ec7e8ed668baf02965a4f562 100644 (file)
@@ -17,17 +17,13 @@ FIXME: Don't hardcode table-based layout!
           name="mailingName" />
       </div>
     </div>
-    <div crm-ui-field="{name: 'subform.campaign', title: ts('Campaign'), help: hs({id: 'id-campaign_id', file: 'CRM/Campaign/Form/addCampaignToComponent'})}" ng-show="crmMailingConst.campNames.length > 0">
-      <select
+    <div crm-ui-field="{name: 'subform.campaign', title: ts('Campaign'), help: hs({id: 'id-campaign_id', file: 'CRM/Campaign/Form/addCampaignToComponent'})}" ng-show="crmMailingConst.campaignEnabled">
+      <input
+        crm-entityref="{entity: 'Campaign', select: {allowClear: true, placeholder: ts('Select Campaign')}}"
         crm-ui-id="subform.campaign"
         name="campaign"
-        ui-jq="select2"
-        ui-options="{dropdownAutoWidth : true, allowClear: true, placeholder: ts('Select Campaign')}"
         ng-model="mailing.campaign_id"
-        ng-options="campaign.id as campaign.title for campaign in crmMailingConst.campNames|orderBy:'title'"
-        >
-        <option value=""></option>
-      </select>
+      />
     </div>
   </div>
 </div>
index ba5d83a7753da06c76aefe303eaa5c0ee813a21b..bed1989d90393671d2d15fc21d01a8a416fd7dc3 100644 (file)
         placeholder="A/B Test Name"
         required/>
     </div>
-    <div crm-ui-field="{name: 'setupForm.campaign', title: ts('Campaign'), help: hs({id: 'id-campaign_id', file: 'CRM/Campaign/Form/addCampaignToComponent'})}" ng-show="crmMailingConst.campNames.length > 0"
+    <div crm-ui-field="{name: 'setupForm.campaign', title: ts('Campaign'), help: hs({id: 'id-campaign_id', file: 'CRM/Campaign/Form/addCampaignToComponent'})}" ng-show="crmMailingConst.campaignEnabled"
          ng-if="fields.campaign">
-      <select
+      <input
+        crm-entityref="{entity: 'Campaign', select: {allowClear: true, placeholder: ts('Select Campaign')}}"
         crm-ui-id="setupForm.campaign"
         name="campaign"
-        ui-jq="select2"
-        ui-options="{dropdownAutoWidth : true, allowClear: true, placeholder: ts('Select Campaign')}"
         ng-model="mailing.campaign_id"
-        ng-options="campaign.id as campaign.title for campaign in crmMailingConst.campNames|orderBy:'title'"
-        >
-        <option value=""></option>
-      </select>
+      />
     </div>
     <div crm-ui-field="{title: ts('Test Type')}" ng-if="fields.testing_criteria">
       <div ng-repeat="criteria in crmMailingABCriteria.getAll()">