Merge pull request #6618 from mallezie/crm-17106
authorcolemanw <coleman@civicrm.org>
Fri, 28 Aug 2015 14:18:49 +0000 (10:18 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 28 Aug 2015 14:18:49 +0000 (10:18 -0400)
crm-17106 add custom fields to message template

CRM/Activity/BAO/Query.php
CRM/Campaign/BAO/Query.php
CRM/Contribute/BAO/Query.php
CRM/Event/BAO/Query.php
CRM/Mailing/BAO/Query.php
CRM/Member/BAO/Query.php
CRM/Pledge/BAO/Query.php
CRM/Utils/Date.php
api/v3/utils.php
templates/CRM/common/jsortable.tpl
tests/phpunit/api/v3/EventTest.php

index 147dd69d4e7a3af231496f23d4e887b59b69bdf9..046afd02bc9b300dda5fb709555eb5874f7b242e 100644 (file)
@@ -197,6 +197,8 @@ class CRM_Activity_BAO_Query {
       case 'activity_engagement_level':
       case 'activity_subject':
       case 'activity_id':
+      case 'activity_campaign_id':
+
         $qillName = $name;
         if (in_array($name, array('activity_engagement_level', 'activity_id'))) {
           $name = $qillName = str_replace('activity_', '', $name);
@@ -205,6 +207,10 @@ class CRM_Activity_BAO_Query {
           $name = str_replace('activity_', '', $name);
           $qillName = str_replace('_id', '', $qillName);
         }
+        if ($name == 'activity_campaign_id') {
+          $name  = 'campaign_id';
+        }
+
         $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
 
         $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.$name", $op, $value, $dataType);
@@ -300,16 +306,6 @@ class CRM_Activity_BAO_Query {
         $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
         break;
 
-      case 'activity_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_activity',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
-        break;
-
       case 'activity_result':
         if (is_array($value)) {
           $safe = NULL;
index f38273cfb8126908a4b960beaeda82f1c25444b4..1066794f666c8f27c87be154258fea5ac06d6d89 100755 (executable)
@@ -568,57 +568,4 @@ INNER JOIN  civicrm_custom_group grp on fld.custom_group_id = grp.id
     return $voterClause;
   }
 
-  /**
-   * Build the campaign clause for component serach.
-   *
-   */
-  public static function componentSearchClause(&$params, &$query) {
-    $op = CRM_Utils_Array::value('op', $params, '=');
-    $campaign = CRM_Utils_Array::value('campaign', $params);
-    $tableName = CRM_Utils_Array::value('tableName', $params);
-    $grouping = CRM_Utils_Array::value('grouping', $params);
-    if (CRM_Utils_System::isNull($campaign) || empty($tableName)) {
-      return;
-    }
-
-    // fixme - what is the purpose of this code? $campaign should be
-    // an integer, not an array
-    if (is_array($campaign)) {
-      foreach (array(
-                 'current_campaign',
-                 'past_campaign',
-               ) as $ignore) {
-        $index = array_search($ignore, $campaign);
-        if ($index !== FALSE) {
-          unset($campaign[$index]);
-        }
-      }
-    }
-
-    $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
-
-    $campaignIds = $campaignTitles = array();
-    if (is_array($campaign)) {
-      foreach ($campaign as $campId) {
-        $campaignIds[$campId] = $campId;
-        $campaignTitles[$campId] = $allCampaigns[$campId];
-      }
-    }
-    else {
-      $campaignIds = $campaign;
-      if (array_key_exists($campaignIds, $allCampaigns)) {
-        $campaignTitles[$campaignIds] = $allCampaigns[$campaignIds];
-      }
-    }
-    $query->_qill[$grouping][] = ts('Campaigns %1',
-        array(1 => $op)
-      ) . ' ' . implode(' ' . ts('or') . ' ', $campaignTitles);
-    $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("{$tableName}.campaign_id",
-      $op,
-      $campaignIds,
-      'Integer'
-    );
-    $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
-  }
-
 }
index 7e82e7a80a022e0014df26fd5ce796edab68cf20..8836a34c6b5d0d74cc7492db49cb60f6b05245c6 100644 (file)
@@ -360,6 +360,7 @@ class CRM_Contribute_BAO_Query {
       case 'contribution_contact_id':
       case (strpos($name, '_amount') !== FALSE):
       case (strpos($name, '_date') !== FALSE && $name != 'contribution_fulfilled_date'):
+      case 'contribution_campaign_id':
         $qillName = $name;
         $pseudoExtraParam = NULL;
         // @todo including names using a switch statement & then using an 'if' to filter them out is ... odd!
@@ -372,11 +373,12 @@ class CRM_Contribute_BAO_Query {
               'contribution_check_number',
               'contribution_payment_instrument_id',
               'contribution_contact_id',
+              'contribution_campaign_id',
             )
           )
         ) {
           $name = str_replace('contribution_', '', $name);
-          if (!in_array($name, array('source', 'id', 'contact_id'))) {
+          if (!in_array($name, array('source', 'id', 'contact_id', 'campaign_id'))) {
             $qillName = str_replace('contribution_', '', $qillName);
           }
         }
@@ -517,16 +519,6 @@ class CRM_Contribute_BAO_Query {
         $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
         return;
 
-      case 'contribution_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_contribution',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
-        return;
-
       case 'contribution_batch_id':
         $batches = CRM_Contribute_PseudoConstant::batch();
         $query->_where[$grouping][] = " civicrm_entity_batch.batch_id $op $value";
index b9d18e13815deffbc208028dba28fc36df7c754b..1f5dc93c51234111bb7a367fe608dca7e4df9c72 100644 (file)
@@ -346,6 +346,8 @@ class CRM_Event_BAO_Query {
       case 'participant_is_pay_later':
       case 'participant_fee_amount':
       case 'participant_fee_level':
+      case 'participant_campaign_id':
+
         $qillName = $name;
         if (in_array($name, array(
               'participant_status_id',
@@ -356,6 +358,7 @@ class CRM_Event_BAO_Query {
               'participant_fee_amount',
               'participant_fee_level',
               'participant_is_pay_later',
+              'participant_campaign_id',
             ))) {
           $name = str_replace('participant_', '', $name);
           if ($name == 'is_pay_later') {
@@ -411,16 +414,6 @@ class CRM_Event_BAO_Query {
         list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Event_DAO_Event', $name, $value, $op);
         $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
         return;
-
-      case 'participant_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_participant',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
-        return;
     }
   }
 
index 13fed90d9ba9ac3cfd48f25ce82134ae914766eb..983a690f3fe86fecf1aa6b3496bf8a8d2b87fa52 100644 (file)
@@ -384,13 +384,11 @@ class CRM_Mailing_BAO_Query {
         return;
 
       case 'mailing_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_mailing',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
+        $name = 'campaign_id';
+        CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'Integer');
+        list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op);
+        $query->_qill[$grouping][] = ts('Campaign %1 %2', array(1 => $op, 2 => $value));
+        $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
         $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
         return;
     }
index 0ff5dd528363ad28ad84cc412210a39ddbcbd901..524302bde8a5c81d6f4d1d67dddeb0f4b69f2845 100644 (file)
@@ -209,6 +209,8 @@ class CRM_Member_BAO_Query {
       case 'membership_type':
       case 'membership_type_id':
       case 'member_id':
+      case 'member_campaign_id':
+
         if (strpos($name, 'status') !== FALSE) {
           $name = 'status_id';
           $qillName = 'Membership Status(s)';
@@ -217,6 +219,10 @@ class CRM_Member_BAO_Query {
           $name = 'id';
           $qillName = 'Membership ID';
         }
+        elseif ($name == 'member_campaign_id') {
+          $name = 'campaign_id';
+          $qillName = 'Campaign';
+        }
         else {
           $name = 'membership_type_id';
           $qillName = 'Membership Type(s)';
@@ -290,16 +296,6 @@ class CRM_Member_BAO_Query {
         }
         $query->_tables['civicrm_membership'] = $query->_whereTables['civicrm_membership'] = 1;
         return;
-
-      case 'member_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_membership',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
-        return;
     }
   }
 
index e4b176c9fb5cbb6f646b21ba0a6954bc74d59ead..6d5f4f2c37d7d1fd820b4d3006a129bbbb6b5152 100644 (file)
@@ -355,21 +355,13 @@ class CRM_Pledge_BAO_Query {
         $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
         return;
 
-      case 'pledge_campaign_id':
-        $campParams = array(
-          'op' => $op,
-          'campaign' => $value,
-          'grouping' => $grouping,
-          'tableName' => 'civicrm_pledge',
-        );
-        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
-        return;
-
       case 'pledge_contact_id':
+      case 'pledge_campaign_id':
         $name = str_replace('pledge_', '', $name);
         $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.$name", $op, $value, 'Integer');
         list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Pledge_DAO_Pledge', $name, $value, $op);
-        $query->_qill[$grouping][] = ts('Contact ID %1 %2', array(1 => $op, 2 => $value));
+        $label = ($name == 'campaign_id') ? 'Campaign' : 'Contact ID';
+        $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $label, 2 => $op, 3 => $value));
         $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
         return;
     }
index eeab873f1885d22e3caf0686a0f3bf7dcf314073..e018f9611b4c0d93e95ccd297f0fb697fc6c0d15 100644 (file)
@@ -1490,12 +1490,20 @@ class CRM_Utils_Date {
         break;
 
       case 'week':
+        $weekFirst = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'weekBegins', NULL, 0);
+        $thisDay = $now['wday'];
+        if ($weekFirst > $thisDay) {
+          $diffDay = $thisDay - $weekFirst + 7;
+        }
+        else {
+          $diffDay = $thisDay - $weekFirst;
+        }
         switch ($relativeTerm) {
           case 'this':
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay), $from);
             $to = self::intervalAdd('day', 6, $from);
             break;
 
@@ -1503,7 +1511,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']) - 7, $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay) - 7, $from);
             $to = self::intervalAdd('day', 6, $from);
             break;
 
@@ -1511,7 +1519,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from);
             $to = self::intervalAdd('day', 6, $from);
             break;
 
@@ -1519,7 +1527,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from);
             $to = self::intervalAdd('day', 13, $from);
             break;
 
@@ -1527,7 +1535,7 @@ class CRM_Utils_Date {
             $to['d'] = $now['mday'];
             $to['M'] = $now['mon'];
             $to['Y'] = $now['year'];
-            $to = self::intervalAdd('day', -1 * ($now['wday']) - 1, $to);
+            $to = self::intervalAdd('day', -1 * ($diffDay) - 1, $to);
             unset($from);
             break;
 
@@ -1535,7 +1543,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay), $from);
             unset($to);
             break;
 
@@ -1543,7 +1551,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']) - 1, $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay) - 1, $from);
             unset($to);
             break;
 
@@ -1561,7 +1569,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay), $from);
             $to['d'] = $now['mday'];
             $to['M'] = $now['mon'];
             $to['Y'] = $now['year'];
@@ -1574,7 +1582,7 @@ class CRM_Utils_Date {
             $to['M'] = $now['mon'];
             $to['Y'] = $now['year'];
             //CRM-14550 QA Fix
-            $to = self::intervalAdd('day', -1 * ($now['wday']) + 6, $to);
+            $to = self::intervalAdd('day', -1 * ($diffDay) + 6, $to);
             unset($from);
             break;
 
@@ -1582,7 +1590,7 @@ class CRM_Utils_Date {
             $from['d'] = $now['mday'];
             $from['M'] = $now['mon'];
             $from['Y'] = $now['year'];
-            $from = self::intervalAdd('day', -1 * ($now['wday']) + 7, $from);
+            $from = self::intervalAdd('day', -1 * ($diffDay) + 7, $from);
             $to = self::intervalAdd('day', 6, $from);
             break;
 
index d2b8495f83030c74122eeae65e66674e337a7856..11ed468c96fb3795951132f0e7f2802fc0ae5e12 100644 (file)
@@ -641,7 +641,7 @@ function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFiel
         );
       }
       else {
-        $query->where(CRM_Core_DAO::createSQLFilter('a.' . $column_name, $value, $type));
+        $query->where(CRM_Core_DAO::createSQLFilter("{$table_name}.{$column_name}", $value, $type));
       }
     }
   }
index ee34619dd060e2f7207104fa46bc961ffc08b6a1..db86e0876cdd71c1019ace4e6b2e7f648f08149a 100644 (file)
           "bAutoWidth": false,
           "aoColumns": columns,
           "bSort": true,
+          "sDom": 'ti',
           "oLanguage":{
             "sEmptyTable": noRecordFoundMsg,
             "sZeroRecords": noRecordFoundMsg
index 10ca65627161d655c6aa5fcec9236dfb6585ede1..7560a5087e4a586fe5f5ebfc309da84b4a0a7e16 100644 (file)
@@ -449,6 +449,37 @@ class api_v3_EventTest extends CiviUnitTestCase {
     ));
   }
 
+  /**
+   * Test searching on custom fields with less than or equal.
+   *
+   * See CRM-17101.
+   */
+  public function testEventGetCustomFieldLte() {
+    // create custom group with custom field on event
+    $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
+
+    // Create an event, with a custom value.
+    $params = $this->_params;
+    $params['title'] = "My test event.";
+    $params['start_date'] = "2015-03-14";
+    // Just assume that an event type 1 exists.
+    $params['event_type_id'] = 1;
+    $params['custom_' . $ids['custom_field_id']] = "AAAA";
+
+    $save_result = $this->callApiSuccess($this->_entity, 'create', $params);
+
+    // Retrieve the activity, search for custom field < 'BBBB'
+    $get_result = $this->callAPISuccess($this->_entity, 'get', array(
+      'return.custom_' . $ids['custom_field_id'] => 1,
+      'custom_' . $ids['custom_field_id'] => array('<=' => 'BBBB'),
+    ));
+
+    // Expect that we find the saved event.
+    $this->assertArrayKeyExists($save_result['id'], $get_result['values']);
+
+    $this->callAPISuccess($this->_entity, 'Delete', array('id' => $save_result['id']));
+  }
+
   /**
    * Test searching on custom fields with netsted call with id param.
    *