Merge pull request #24139 from eileenmcnaughton/var
[civicrm-core.git] / CRM / Event / ActionMapping.php
index 77fa8d77edfb6fe72abc2b8920a6944a1cf2a694..c474a8dba6aaf87b5afd7ffaf7a5533fd66bdebd 100644 (file)
@@ -74,10 +74,10 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping {
    */
   public function getDateFields() {
     return [
-      'start_date' => ts('Event Start Date'),
-      'end_date' => ts('Event End Date'),
-      'registration_start_date' => ts('Registration Start Date'),
-      'registration_end_date' => ts('Registration End Date'),
+      'start_date' => ts('Event Start'),
+      'end_date' => ts('Event End'),
+      'registration_start_date' => ts('Registration Start'),
+      'registration_end_date' => ts('Registration End'),
     ];
   }
 
@@ -140,7 +140,7 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping {
     $query['casContactIdField'] = 'e.contact_id';
     $query['casEntityIdField'] = 'e.id';
     $query['casContactTableAlias'] = NULL;
-    $query['casDateField'] = str_replace('event_', 'r.', $schedule->start_action_date);
+    $query['casDateField'] = str_replace('event_', 'r.', ($schedule->start_action_date ?? ''));
     if (empty($query['casDateField']) && $schedule->absolute_date) {
       $query['casDateField'] = "'" . CRM_Utils_Type::escape($schedule->absolute_date, 'String') . "'";
     }
@@ -149,8 +149,9 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping {
     if ($schedule->recipient_listing && $schedule->limit_to) {
       switch ($schedule->recipient) {
         case 'participant_role':
-          $query->where("e.role_id IN (#recipList)")
-            ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing));
+          $regex = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', \CRM_Utils_Array::explodePadded($schedule->recipient_listing)) . "([[:cntrl:]]|$)";
+          $query->where("e.role_id REGEXP (@regex)")
+            ->param('regex', $regex);
           break;
 
         default:
@@ -159,6 +160,7 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping {
     }
 
     // build where clause
+    // FIXME: This handles scheduled reminder of type "Event Name" and "Event Type", gives incorrect result on "Event Template".
     if (!empty($selectedValues)) {
       $valueField = ($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? 'event_type_id' : 'id';
       $query->where("r.{$valueField} IN (@selectedValues)")
@@ -186,4 +188,49 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping {
     return $query;
   }
 
+  /**
+   * Determine whether a schedule based on this mapping should
+   * send to additional contacts.
+   *
+   * @param string $entityId Either an event ID/event type ID, or a set of event IDs/types separated
+   *  by the separation character.
+   */
+  public function sendToAdditional($entityId): bool {
+    $selectedValues = (array) \CRM_Utils_Array::explodePadded($entityId);
+    switch ($this->id) {
+      case self::EVENT_TYPE_MAPPING_ID:
+        $valueTable = 'e';
+        $valueField = 'event_type_id';
+        $templateReminder = FALSE;
+        break;
+
+      case self::EVENT_NAME_MAPPING_ID:
+        $valueTable = 'e';
+        $valueField = 'id';
+        $templateReminder = FALSE;
+        break;
+
+      case self::EVENT_TPL_MAPPING_ID:
+        $valueTable = 't';
+        $valueField = 'id';
+        $templateReminder = TRUE;
+        break;
+    }
+    // Don't send to additional recipients if this event is deleted or a template.
+    $query = new \CRM_Utils_SQL_Select('civicrm_event e');
+    $query
+      ->select('e.id')
+      ->where("e.is_template = 0")
+      ->where("e.is_active = 1");
+    if ($templateReminder) {
+      $query->join('r', 'INNER JOIN civicrm_event t ON e.template_title = t.template_title AND t.is_template = 1');
+    }
+    $sql = $query
+      ->where("{$valueTable}.{$valueField} IN (@selectedValues)")
+      ->param('selectedValues', $selectedValues)
+      ->toSQL();
+    $dao = \CRM_Core_DAO::executeQuery($sql);
+    return (bool) $dao->N;
+  }
+
 }