/**
* @param array $filters
- * Filter by property (e.g. 'id', 'entity_value').
+ * Filter by property (e.g. 'id').
* @return array
* Array(scalar $id => Mapping $mapping).
*/
$_action_mapping = $event->getMappings();
}
- if ($filters === NULL) {
+ if (empty($filters)) {
return $_action_mapping;
}
-
- $result = array();
- foreach ($_action_mapping as $mappingId => $mapping) {
- $match = TRUE;
- foreach ($filters as $filterField => $filterValue) {
- if ($mapping->{$filterField} != $filterValue) {
- $match = FALSE;
- }
- }
- if ($match) {
- $result[$mappingId] = $mapping;
- }
+ elseif (isset($filters['id'])) {
+ return array(
+ $filters['id'] => $_action_mapping[$filters['id']],
+ );
}
- return $result;
+ else {
+ throw new CRM_Core_Exception("getMappings() called with unsupported filter: " . implode(', ', array_keys($filters)));
+ }
+ }
+
+ /**
+ * @param string|int $id
+ * @return \Civi\ActionSchedule\Mapping|NULL
+ */
+ public static function getMapping($id) {
+ $mappings = self::getMappings();
+ return isset($mappings[$id]) ? $mappings[$id] : NULL;
}
/**
*/
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 */
-
- $entityValueLabels[$mapping->id] = $mapping->getValueLabels();
- // Not sure why: everything *except* contact-dates have a $valueLabel.
- if ($mapping->id !== CRM_Core_ActionScheduleTmp::CONTACT_MAPPING_ID) {
- $valueLabel = array('- ' . strtolower($mapping->entity_value_label) . ' -');
- $entityValueLabels[$mapping->id] = $valueLabel + $entityValueLabels[$mapping->id];
- }
-
- if ($mapping->id == $id) {
- $dateFieldLabels = $mapping->getDateFields();
- $entityRecipientLabels = array($mapping->entity_recipient => $mapping->getRecipientTypes());
- $entityRecipientNames = array_combine(array_keys($entityRecipientLabels[$mapping->entity_recipient]), array_keys($entityRecipientLabels[$mapping->entity_recipient]));
- }
-
- $statusLabel = array('- ' . strtolower($mapping->entity_status_label) . ' -');
- $entityStatusLabels[$mapping->id] = $entityValueLabels[$mapping->id];
- foreach ($entityStatusLabels[$mapping->id] as $kkey => & $vval) {
+ $statusLabel = array('- ' . strtolower($mapping->getStatusHeader()) . ' -');
+ $entityStatusLabels[$mapping->getId()] = $entityValueLabels[$mapping->getId()];
+ foreach ($entityStatusLabels[$mapping->getId()] as $kkey => & $vval) {
$vval = $statusLabel + $mapping->getStatusLabels($kkey);
}
}
+ $entityRecipientLabels = $selectedMapping->getRecipientTypes() + self::getAdditionalRecipients();
+
return array(
- 'sel1' => CRM_Utils_Array::collect('entity_label', $mappings),
+ 'sel1' => CRM_Utils_Array::collectMethod('getLabel', $mappings),
'sel2' => $entityValueLabels,
'sel3' => $entityStatusLabels,
- 'sel4' => $dateFieldLabels,
+ 'sel4' => $selectedMapping->getDateFields(),
'sel5' => $entityRecipientLabels,
- 'entityMapping' => CRM_Utils_Array::collect('entity', $mappings),
- 'recipientMapping' => $entityRecipientNames,
+ 'entityMapping' => CRM_Utils_Array::collectMethod('getEntity', $mappings),
+ 'recipientMapping' => array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)),
);
}
foreach ($mappings as $mapping) {
/** @var \Civi\ActionSchedule\Mapping $mapping */
$dateFieldLabels = $mapping->getDateFields();
- $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit);
+ $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit) + self::getAdditionalRecipients();
}
return array(
if ($filterMapping and $filterValue) {
$where .= " AND cas.entity_value = %1 AND cas.mapping_id = %2";
$queryParams[1] = array($filterValue, 'Integer');
- $queryParams[2] = array($filterMapping->id, 'Integer');
+ $queryParams[2] = array($filterMapping->getId(), 'String');
}
$where .= " AND cas.used_for IS NULL";
$query .= $where;
$list[$dao->id]['start_action_condition'] = $dao->start_action_condition;
$list[$dao->id]['entityDate'] = ucwords(str_replace('_', ' ', $dao->entityDate));
$list[$dao->id]['absolute_date'] = $dao->absolute_date;
- $list[$dao->id]['entity'] = $filterMapping->entity_label;
+ $list[$dao->id]['entity'] = $filterMapping->getLabel();
$list[$dao->id]['value'] = implode(', ', CRM_Utils_Array::subset(
$filterMapping->getValueLabels(),
explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityValueIds)
entity_value = %2";
$params = array(
- 1 => array($mappingID, 'Integer'),
+ 1 => array($mappingID, 'String'),
2 => array($id, 'Integer'),
);
return CRM_Core_DAO::singleValueQuery($queryString, $params);
protected static function createMailingActivity($actionSchedule, $mapping, $contactID, $entityID, $caseID) {
$session = CRM_Core_Session::singleton();
- if ($mapping->entity == 'civicrm_membership') {
+ if ($mapping->getEntity() == 'civicrm_membership') {
$activityTypeID
= CRM_Core_OptionGroup::getValue('activity_type', 'Membership Renewal Reminder', 'name');
}
}
/**
- * @param $mapping
- * @param $actionSchedule
+ * @param \Civi\ActionSchedule\MappingInterface $mapping
+ * @param \CRM_Core_DAO_ActionSchedule $actionSchedule
* @return string
*/
protected static function prepareMailingQuery($mapping, $actionSchedule) {
- $select = CRM_Utils_SQL_Select::from('civicrm_action_log reminder', array('mode' => 'out'))
- ->select("reminder.id as reminderID, reminder.contact_id as contactID, reminder.entity_table as entityTable, reminder.*, e.id as entityID, e.*")
- ->where("reminder.action_schedule_id = #casScheduleId")
- ->param('casScheduleId', $actionSchedule->id)
- ->where("reminder.action_date_time IS NULL");
+ $select = CRM_Utils_SQL_Select::from('civicrm_action_log reminder')
+ ->select("reminder.id as reminderID, reminder.contact_id as contactID, reminder.entity_table as entityTable, reminder.*, e.id AS entityID")
+ ->join('e', "!casMailingJoinType !casMappingEntity e ON !casEntityJoinExpr")
+ ->select("e.id as entityID, e.*")
+ ->where("reminder.action_schedule_id = #casActionScheduleId")
+ ->where("reminder.action_date_time IS NULL")
+ ->param(array(
+ 'casActionScheduleId' => $actionSchedule->id,
+ 'casMailingJoinType' => ($actionSchedule->limit_to == 0) ? 'LEFT JOIN' : 'INNER JOIN',
+ 'casMappingId' => $mapping->getId(),
+ 'casMappingEntity' => $mapping->getEntity(),
+ 'casEntityJoinExpr' => 'e.id = reminder.entity_id',
+ ));
if ($actionSchedule->limit_to == 0) {
- $entityJoinClause = "LEFT JOIN {$mapping->entity} e ON e.id = reminder.entity_id";
$select->where("e.id = reminder.entity_id OR reminder.entity_table = 'civicrm_contact'");
}
- else {
- $entityJoinClause = "INNER JOIN {$mapping->entity} e ON e.id = reminder.entity_id";
- }
- if ($mapping->entity == 'civicrm_activity') {
- $entityJoinClause .= ' AND e.is_current_revision = 1 AND e.is_deleted = 0 ';
- }
- $select->join('a', $entityJoinClause);
-
- if ($mapping->entity == 'civicrm_activity') {
- $compInfo = CRM_Core_Component::getEnabledComponents();
- $select->select('ov.label as activity_type, e.id as activity_id');
-
- $JOIN_TYPE = ($actionSchedule->limit_to == 0) ? 'LEFT JOIN' : 'INNER JOIN';
- $select->join("og", "$JOIN_TYPE civicrm_option_group og ON og.name = 'activity_type'");
- $select->join("ov", "$JOIN_TYPE civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id");
-
- // if CiviCase component is enabled, join for caseId.
- if (array_key_exists('CiviCase', $compInfo)) {
- $select->select("civicrm_case_activity.case_id as case_id");
- $select->join('civicrm_case_activity', "LEFT JOIN `civicrm_case_activity` ON `e`.`id` = `civicrm_case_activity`.`activity_id`");
- }
- }
-
- if ($mapping->entity == 'civicrm_participant') {
- $select->select('ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone');
-
- $JOIN_TYPE = ($actionSchedule->limit_to == 0) ? 'LEFT JOIN' : 'INNER JOIN';
- $select->join('participant_stuff', "
-$JOIN_TYPE civicrm_event ev ON e.event_id = ev.id
-$JOIN_TYPE civicrm_option_group og ON og.name = 'event_type'
-$JOIN_TYPE civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id
-LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
-LEFT JOIN civicrm_address address ON address.id = lb.address_id
-LEFT JOIN civicrm_email email ON email.id = lb.email_id
-LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
-");
- }
- if ($mapping->entity == 'civicrm_membership') {
- $select->select('mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, ms.name as status, mt.name as type');
-
- $JOIN_TYPE = ($actionSchedule->limit_to == 0) ? 'LEFT JOIN' : 'INNER JOIN';
- $select->join('mt', "$JOIN_TYPE civicrm_membership_type mt ON e.membership_type_id = mt.id");
- $select->join('ms', "$JOIN_TYPE civicrm_membership_status ms ON e.status_id = ms.id");
- }
+ \Civi\Core\Container::singleton()->get('dispatcher')
+ ->dispatch(
+ \Civi\ActionSchedule\Events::MAILING_QUERY,
+ new \Civi\ActionSchedule\Event\MailingQueryEvent($actionSchedule, $mapping, $select)
+ );
return $select->toSQL();
}
/**
- * @param TokenRow $tokenRow
+ * @param \Civi\Token\TokenRow $tokenRow
* @param CRM_Core_DAO_ActionSchedule $schedule
* @param int $toContactID
* @throws CRM_Core_Exception
}
/**
- * @param TokenRow $tokenRow
+ * @param \Civi\Token\TokenRow $tokenRow
* @param CRM_Core_DAO_ActionSchedule $schedule
* @param int $toContactID
* @return array
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'),
+ );
+ }
+
}