CRM-13422 - Consolidate extra rules for contact reminders
[civicrm-core.git] / CRM / Core / BAO / ActionSchedule.php
index 736dc3bcc5ad7f19bdef6f321d07eee8d8054d8f..780da240d8fe1c38d2f931164bfa8ad11e76415e 100755 (executable)
@@ -67,6 +67,15 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
     }
   }
 
+  /**
+   * @param string|int $id
+   * @return \Civi\ActionSchedule\Mapping|NULL
+   */
+  public static function getMapping($id) {
+    $mappings = self::getMappings();
+    return isset($mappings[$id]) ? $mappings[$id] : NULL;
+  }
+
   /**
    * Retrieve list of selections/drop downs for Scheduled Reminder form
    *
@@ -78,31 +87,19 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
    */
   public static function getSelection($id = NULL) {
     $mappings = CRM_Core_BAO_ActionSchedule::getMappings();
+    $selectedMapping = $mappings[$id ? $id : 1];
 
-    $entityValueLabels = $entityStatusLabels = $dateFieldLabels = array();
-    $entityRecipientLabels = $entityRecipientNames = array();
-
-    if (!$id) {
-      $id = 1;
+    $entityValueLabels = array();
+    foreach ($mappings as $mapping) {
+      /** @var \Civi\ActionSchedule\Mapping $mapping */
+      $entityValueLabels[$mapping->getId()] = $mapping->getValueLabels();
+      $valueLabel = array('- ' . strtolower($mapping->getValueHeader()) . ' -');
+      $entityValueLabels[$mapping->getId()] = $valueLabel + $entityValueLabels[$mapping->getId()];
     }
 
+    $entityStatusLabels = array();
     foreach ($mappings as $mapping) {
       /** @var \Civi\ActionSchedule\Mapping $mapping */
-
-      $mappingId = $mapping->getId();
-      $entityValueLabels[$mappingId] = $mapping->getValueLabels();
-      // Not sure why: everything *except* contact-dates have a $valueLabel.
-      if ($mapping->getId() !== CRM_Contact_ActionMapping::CONTACT_MAPPING_ID) {
-        $valueLabel = array('- ' . strtolower($mapping->getValueHeader()) . ' -');
-        $entityValueLabels[$mapping->getId()] = $valueLabel + $entityValueLabels[$mapping->getId()];
-      }
-
-      if ($mapping->getId() == $id) {
-        $dateFieldLabels = $mapping->getDateFields();
-        $entityRecipientLabels = $mapping->getRecipientTypes();
-        $entityRecipientNames = array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels));
-      }
-
       $statusLabel = array('- ' . strtolower($mapping->getStatusHeader()) . ' -');
       $entityStatusLabels[$mapping->getId()] = $entityValueLabels[$mapping->getId()];
       foreach ($entityStatusLabels[$mapping->getId()] as $kkey => & $vval) {
@@ -110,21 +107,16 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
       }
     }
 
-    $entityLabels = array_map(function ($v) {
-      return $v->getLabel();
-    }, $mappings);
-    $entityNames = array_map(function ($v) {
-      return $v->getEntity();
-    }, $mappings);
+    $entityRecipientLabels = $selectedMapping->getRecipientTypes() + self::getAdditionalRecipients();
 
     return array(
-      'sel1' => $entityLabels,
+      'sel1' => CRM_Utils_Array::collectMethod('getLabel', $mappings),
       'sel2' => $entityValueLabels,
       'sel3' => $entityStatusLabels,
-      'sel4' => $dateFieldLabels,
+      'sel4' => $selectedMapping->getDateFields(),
       'sel5' => $entityRecipientLabels,
-      'entityMapping' => $entityNames,
-      'recipientMapping' => $entityRecipientNames,
+      'entityMapping' => CRM_Utils_Array::collectMethod('getEntity', $mappings),
+      'recipientMapping' => array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)),
     );
   }
 
@@ -143,7 +135,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
     foreach ($mappings as $mapping) {
       /** @var \Civi\ActionSchedule\Mapping $mapping */
       $dateFieldLabels = $mapping->getDateFields();
-      $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit);
+      $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit) + self::getAdditionalRecipients();
     }
 
     return array(
@@ -190,7 +182,7 @@ FROM civicrm_action_schedule cas
     if ($filterMapping and $filterValue) {
       $where .= " AND cas.entity_value = %1 AND cas.mapping_id = %2";
       $queryParams[1] = array($filterValue, 'Integer');
-      $queryParams[2] = array($filterMapping->getId(), 'Integer');
+      $queryParams[2] = array($filterMapping->getId(), 'String');
     }
     $where .= " AND cas.used_for IS NULL";
     $query .= $where;
@@ -435,7 +427,7 @@ FROM civicrm_action_schedule cas
                                entity_value = %2";
 
     $params = array(
-      1 => array($mappingID, 'Integer'),
+      1 => array($mappingID, 'String'),
       2 => array($id, 'Integer'),
     );
     return CRM_Core_DAO::singleValueQuery($queryString, $params);
@@ -568,7 +560,7 @@ FROM civicrm_action_schedule cas
   }
 
   /**
-   * @param TokenRow $tokenRow
+   * @param \Civi\Token\TokenRow $tokenRow
    * @param CRM_Core_DAO_ActionSchedule $schedule
    * @param int $toContactID
    * @throws CRM_Core_Exception
@@ -632,7 +624,7 @@ FROM civicrm_action_schedule cas
   }
 
   /**
-   * @param TokenRow $tokenRow
+   * @param \Civi\Token\TokenRow $tokenRow
    * @param CRM_Core_DAO_ActionSchedule $schedule
    * @param int $toContactID
    * @return array
@@ -719,4 +711,17 @@ FROM civicrm_action_schedule cas
     return NULL;
   }
 
+  /**
+   * Get the list of generic recipient types supported by all entities/mappings.
+   *
+   * @return array
+   *   array(mixed $value => string $label).
+   */
+  protected static function getAdditionalRecipients() {
+    return array(
+      'manual' => ts('Choose Recipient(s)'),
+      'group' => ts('Select Group'),
+    );
+  }
+
 }