Merge pull request #2264 from dlobo/CRM-13606
[civicrm-core.git] / CRM / Core / BAO / ActionSchedule.php
index 5542307c9ce7904e5b65fc8f949de301666710e9..decb24753ccc6b39de118bee79d6c4270db2dd4b 100755 (executable)
@@ -1,7 +1,7 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.3                                                |
+  | CiviCRM version 4.4                                                |
   +--------------------------------------------------------------------+
   | Copyright (C) 2011 Marty Wright                                    |
   | Licensed to CiviCRM under the Academic Free License version 3.0.   |
@@ -463,6 +463,8 @@ WHERE   cas.entity_value = $id AND
         'toName' => $contact['display_name'],
         'toEmail' => $email,
         'subject' => $messageSubject,
+        'entity' => 'action_schedule',
+        'entity_id' => $scheduleID,
       );
 
       if (!$html || $contact['preferred_mail_format'] == 'Text' ||
@@ -495,7 +497,7 @@ WHERE   cas.entity_value = $id AND
    * @static
    *
    */
-  static function add(&$params, &$ids) {
+  static function add(&$params, $ids = array()) {
     $actionSchedule = new CRM_Core_DAO_ActionSchedule();
     $actionSchedule->copyValues($params);
 
@@ -682,7 +684,7 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
             $stateProvince = CRM_Core_PseudoConstant::stateProvince();
             $loc['street_address'] = $dao->street_address;
             $loc['city'] = $dao->city;
-            $loc['state_province'] = CRM_Utils_array::value($dao->state_province_id, $stateProvince);
+            $loc['state_province'] = CRM_Utils_Array::value($dao->state_province_id, $stateProvince);
             $loc['postal_code'] = $dao->postal_code;
             $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Address::format($loc);
           }
@@ -898,10 +900,44 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
         $where[] = "e.status_id IN ({$mStatus})";
       }
 
+      // CRM-13577 Introduce Smart Groups Handling
+      if ($actionSchedule->group_id) {
+
+        // Need to check if its a smart group or not
+        // Then decide which table to join onto the query
+        $group = CRM_Contact_DAO_Group::getTableName();
+
+        // Get the group information
+        $sql = "
+SELECT     $group.id, $group.cache_date, $group.saved_search_id, $group.children
+FROM       $group
+WHERE      $group.id = {$actionSchedule->group_id}
+";
+
+        $groupDAO = CRM_Core_DAO::executeQuery($sql);
+        $isSmartGroup = FALSE;
+        if (
+          $groupDAO->fetch() &&
+          !empty($groupDAO->saved_search_id)
+        ) {
+          // Check that the group is in place in the cache and up to date
+          CRM_Contact_BAO_GroupContactCache::check($actionSchedule->group_id);
+          // Set smart group flag
+          $isSmartGroup = TRUE;
+        }
+      }
+      // CRM-13577 End Introduce Smart Groups Handling
+
       if ($limitTo) {
         if ($actionSchedule->group_id) {
-          $join[] = "INNER JOIN civicrm_group_contact grp ON {$contactField} = grp.contact_id AND grp.status = 'Added'";
-          $where[] = "grp.group_id IN ({$actionSchedule->group_id})";
+          // CRM-13577 If smart group then use Cache table
+          if ($isSmartGroup) {
+            $join[] = "INNER JOIN civicrm_group_contact_cache grp ON {$contactField} = grp.contact_id";
+            $where[] = "grp.group_id IN ({$actionSchedule->group_id})";
+          } else {
+            $join[] = "INNER JOIN civicrm_group_contact grp ON {$contactField} = grp.contact_id AND grp.status = 'Added'";
+            $where[] = "grp.group_id IN ({$actionSchedule->group_id})";
+          }
         }
         elseif (!empty($actionSchedule->recipient_manual)) {
           $rList = CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String');
@@ -911,8 +947,14 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
       else {
         $addGroup = $addWhere = '';
         if ($actionSchedule->group_id) {
-          $addGroup = " INNER JOIN civicrm_group_contact grp ON c.id = grp.contact_id AND grp.status = 'Added'";
-          $addWhere = " grp.group_id IN ({$actionSchedule->group_id})";
+          // CRM-13577 If smart group then use Cache table
+          if ($isSmartGroup) {
+            $addGroup = " INNER JOIN civicrm_group_contact_cache grp ON c.id = grp.contact_id";
+            $addWhere = " grp.group_id IN ({$actionSchedule->group_id})";
+          } else {
+            $addGroup = " INNER JOIN civicrm_group_contact grp ON c.id = grp.contact_id AND grp.status = 'Added'";
+            $addWhere = " grp.group_id IN ({$actionSchedule->group_id})";
+          }
         }
         if (!empty($actionSchedule->recipient_manual)) {
           $rList = CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String');