<?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. |
}
}
-
return array(
'sel1' => $sel1,
'sel2' => $sel2,
'toName' => $contact['display_name'],
'toEmail' => $email,
'subject' => $messageSubject,
+ 'entity' => 'action_schedule',
+ 'entity_id' => $scheduleID,
);
if (!$html || $contact['preferred_mail_format'] == 'Text' ||
* @static
*
*/
- static function add(&$params, &$ids) {
+ static function add(&$params, $ids = array()) {
$actionSchedule = new CRM_Core_DAO_ActionSchedule();
$actionSchedule->copyValues($params);
$extraJoin = "
INNER JOIN civicrm_option_group og ON og.name = 'activity_type'
INNER JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id";
- $extraOn = 'AND e.is_current_revision = 1 AND e.is_deleted = 0';
+ $extraOn = ' AND e.is_current_revision = 1 AND e.is_deleted = 0 ';
if ($actionSchedule->limit_to == 0) {
$extraJoin = "
LEFT JOIN civicrm_option_group og ON og.name = 'activity_type'
$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);
}
$status = implode(',', $status);
if (!CRM_Utils_System::isNull($mapping->entity_recipient)) {
- $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient);
+ $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name');
}
$from = "{$mapping->entity} e";
$rList = implode(',', $rList);
switch ($recipientOptions[$actionSchedule->recipient]) {
- case 'Participant Role':
+ case 'participant_role':
$where[] = "e.role_id IN ({$rList})";
break;
$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');
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');
$fromClause = "FROM $from";
$joinClause = !empty($join) ? implode(' ', $join) : '';
$whereClause = 'WHERE ' . implode(' AND ', $where);
- $limitWhere .= implode(' AND ', $limitWhere);
+ $limitWhereClause = '';
+ if (!empty($limitWhere)) {
+ $limitWhereClause = ' AND ' . implode(' AND ', $limitWhere);
+ }
$query = "
INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id)
{$fromClause}
{$joinClause}
LEFT JOIN {$reminderJoinClause}
-{$whereClause} {$limitWhere} AND {$dateClause} {$notINClause}
+{$whereClause} {$limitWhereClause} AND {$dateClause} {$notINClause}
";
-
CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer')));
if ($limitTo == 0) {
$additionWhere = ' WHERE ';
if ($actionSchedule->start_action_date) {
- $additionWhere = $whereClause;
+ $additionWhere = $whereClause . ' AND ';
}
+ $contactTable = "civicrm_contact c";
+ $addSelect = "SELECT c.id as contact_id, c.id as entity_id, 'civicrm_contact' as entity_table, {$actionSchedule->id} as action_schedule_id";
+ $additionReminderClause = "civicrm_action_log reminder ON reminder.contact_id = c.id AND
+ reminder.entity_id = c.id AND
+ reminder.entity_table = 'civicrm_contact' AND
+ reminder.action_schedule_id = {$actionSchedule->id}";
$insertAdditionalSql ="
INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id)
- SELECT c.id as contact_id, c.id as entity_id, 'civicrm_contact' as entity_table, {$actionSchedule->id} as action_schedule_id
-FROM (civicrm_contact c, {$table})
-LEFT JOIN civicrm_action_log reminder ON reminder.contact_id = c.id AND
- reminder.entity_id = c.id AND
- reminder.entity_table = 'civicrm_contact' AND
- reminder.action_schedule_id = {$actionSchedule->id}
+{$addSelect}
+FROM ({$contactTable}, {$table})
+LEFT JOIN {$additionReminderClause}
{$addGroup}
-{$additionWhere} (reminder.id IS NULL AND c.is_deleted = 0 AND c.is_deceased = 0 AND {$addWhere})
+{$additionWhere} c.is_deleted = 0 AND c.is_deceased = 0 AND {$addWhere}
AND {$dateClause}
AND c.id NOT IN (
SELECT rem.contact_id
WHERE rem.action_schedule_id = {$actionSchedule->id}
AND rem.entity_table = '{$mapping->entity}'
)
+GROUP BY c.id
";
CRM_Core_DAO::executeQuery($insertAdditionalSql);
}
$havingClause = "HAVING TIMEDIFF({$now}, latest_log_time) >= TIME('{$hrs}:00:00')";
$groupByClause = 'GROUP BY reminder.contact_id, reminder.entity_id, reminder.entity_table';
$selectClause .= ', MAX(reminder.action_date_time) as latest_log_time';
-
$sqlInsertValues = "{$selectClause}
{$fromClause}
{$joinClause}
INNER JOIN {$reminderJoinClause}
-{$whereClause} AND {$repeatEventClause}
+{$whereClause} {$limitWhereClause} AND {$repeatEventClause}
{$groupByClause}
{$havingClause}";
INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id) VALUES ' . $valString;
CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer')));
}
+
+ if ($limitTo == 0) {
+ $addSelect .= ', MAX(reminder.action_date_time) as latest_log_time';
+ $sqlEndEventCheck = "
+SELECT * FROM {$table}
+{$whereClause} AND {$repeatEventClause} LIMIT 1";
+
+ $daoCheck = CRM_Core_DAO::executeQuery($sqlEndEventCheck);
+ if ($daoCheck->fetch()) {
+ $valSqlAdditionInsert = "
+{$addSelect}
+FROM {$contactTable}
+{$addGroup}
+INNER JOIN {$additionReminderClause}
+WHERE {$addWhere} AND c.is_deleted = 0 AND c.is_deceased = 0
+GROUP BY reminder.contact_id
+{$havingClause}
+";
+ $daoForVals = CRM_Core_DAO::executeQuery($valSqlAdditionInsert);
+ $addValues = array();
+ while ($daoForVals->fetch()) {
+ $addValues[] = "( {$daoForVals->contact_id}, {$daoForVals->entity_id}, '{$daoForVals->entity_table}',{$daoForVals->action_schedule_id} )";
+ }
+ $valString = implode(',', $addValues);
+
+ if ($valString) {
+ $query = '
+ INSERT INTO civicrm_action_log (contact_id, entity_id, entity_table, action_schedule_id) VALUES ' . $valString;
+ CRM_Core_DAO::executeQuery($query);
+ }
+ }
+ }
}
}
}
if (!CRM_Utils_Array::value($recipientType, $eventContacts)) {
return $options;
}
- if ($eventContacts[$recipientType] == 'Participant Role') {
+ if ($eventContacts[$recipientType] == 'participant_role') {
$options = CRM_Event_PseudoConstant::participantRole();
}
break;