* @copyright CiviCRM LLC (c) 2004-2019
*/
-require_once 'packages/When/When.php';
+use When\When;
/**
* Class CRM_Core_BAO_RecurringEntity.
class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
const RUNNING = 1;
- public $schedule = array();
+ public $schedule = [];
public $scheduleId = NULL;
- public $scheduleFormValues = array();
+ public $scheduleFormValues = [];
- public $dateColumns = array();
- public $overwriteColumns = array();
- public $intervalDateColumns = array();
- public $excludeDates = array();
+ public $dateColumns = [];
+ public $overwriteColumns = [];
+ public $intervalDateColumns = [];
+ public $excludeDates = [];
- public $linkedEntities = array();
+ public $linkedEntities = [];
public $isRecurringEntityRecord = TRUE;
protected $recursion = NULL;
protected $recursion_start_date = NULL;
- public static $_entitiesToBeDeleted = array();
+ public static $_entitiesToBeDeleted = [];
public static $status = NULL;
- static $_recurringEntityHelper
- = array(
- 'civicrm_event' => array(
+ public static $_recurringEntityHelper
+ = [
+ 'civicrm_event' => [
'helper_class' => 'CRM_Event_DAO_Event',
'delete_func' => 'delete',
'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents',
- ),
- 'civicrm_activity' => array(
+ ],
+ 'civicrm_activity' => [
'helper_class' => 'CRM_Activity_DAO_Activity',
'delete_func' => 'delete',
'pre_delete_func' => '',
- ),
- );
-
- static $_dateColumns
- = array(
- 'civicrm_event' => array(
- 'dateColumns' => array('start_date'),
- 'excludeDateRangeColumns' => array('start_date', 'end_date'),
- 'intervalDateColumns' => array('end_date'),
- ),
- 'civicrm_activity' => array(
- 'dateColumns' => array('activity_date_time'),
- ),
- );
-
- static $_tableDAOMapper
- = array(
+ ],
+ ];
+
+ public static $_dateColumns
+ = [
+ 'civicrm_event' => [
+ 'dateColumns' => ['start_date'],
+ 'excludeDateRangeColumns' => ['start_date', 'end_date'],
+ 'intervalDateColumns' => ['end_date'],
+ ],
+ 'civicrm_activity' => [
+ 'dateColumns' => ['activity_date_time'],
+ ],
+ ];
+
+ public static $_tableDAOMapper
+ = [
'civicrm_event' => 'CRM_Event_DAO_Event',
'civicrm_price_set_entity' => 'CRM_Price_DAO_PriceSetEntity',
'civicrm_uf_join' => 'CRM_Core_DAO_UFJoin',
'civicrm_pcp_block' => 'CRM_PCP_DAO_PCPBlock',
'civicrm_activity' => 'CRM_Activity_DAO_Activity',
'civicrm_activity_contact' => 'CRM_Activity_DAO_ActivityContact',
- );
-
- static $_updateSkipFields
- = array(
- 'civicrm_event' => array('start_date', 'end_date'),
- 'civicrm_tell_friend' => array('entity_id'),
- 'civicrm_pcp_block' => array('entity_id'),
- 'civicrm_activity' => array('activity_date_time'),
- );
-
- static $_linkedEntitiesInfo
- = array(
- 'civicrm_tell_friend' => array(
+ ];
+
+ public static $_updateSkipFields
+ = [
+ 'civicrm_event' => ['start_date', 'end_date'],
+ 'civicrm_tell_friend' => ['entity_id'],
+ 'civicrm_pcp_block' => ['entity_id'],
+ 'civicrm_activity' => ['activity_date_time'],
+ ];
+
+ public static $_linkedEntitiesInfo
+ = [
+ 'civicrm_tell_friend' => [
'entity_id_col' => 'entity_id',
'entity_table_col' => 'entity_table',
- ),
- 'civicrm_price_set_entity' => array(
+ ],
+ 'civicrm_price_set_entity' => [
'entity_id_col' => 'entity_id',
'entity_table_col' => 'entity_table',
'is_multirecord' => TRUE,
- ),
- 'civicrm_uf_join' => array(
+ ],
+ 'civicrm_uf_join' => [
'entity_id_col' => 'entity_id',
'entity_table_col' => 'entity_table',
'is_multirecord' => TRUE,
- ),
- 'civicrm_pcp_block' => array(
+ ],
+ 'civicrm_pcp_block' => [
'entity_id_col' => 'entity_id',
'entity_table_col' => 'entity_table',
- ),
- );
+ ],
+ ];
//Define global CLASS CONSTANTS for recurring entity mode types
const MODE_THIS_ENTITY_ONLY = 1;
*/
public static function quickAdd($parentId, $entityId, $entityTable) {
$params
- = array(
+ = [
'parent_id' => $parentId,
'entity_id' => $entityId,
'entity_table' => $entityTable,
- );
+ ];
return self::add($params);
}
public function generateEntities() {
self::setStatus(self::RUNNING);
- $newEntities = array();
- $findCriteria = array();
+ $newEntities = [];
+ $findCriteria = [];
if (!empty($this->recursionDates)) {
if ($this->entity_id) {
- $findCriteria = array('id' => $this->entity_id);
+ $findCriteria = ['id' => $this->entity_id];
// save an entry with initiating entity-id & entity-table
if ($this->entity_table && !$this->find(TRUE)) {
);
if (is_a($obj, 'CRM_Core_DAO') && $obj->id) {
- $newCriteria = array();
+ $newCriteria = [];
$newEntities[$this->entity_table][$count] = $obj->id;
foreach ($this->linkedEntities as $linkedInfo) {
public function generateRecursiveDates() {
$this->generateRecursion();
- $recursionDates = array();
- if (is_a($this->recursion, 'When')) {
+ $recursionDates = [];
+ if (is_a($this->recursion, 'When\When')) {
$initialCount = CRM_Utils_Array::value('start_action_offset', $this->schedule);
$exRangeStart = $exRangeEnd = NULL;
}
$count = 1;
- while ($result = $this->recursion->next()) {
- $skip = FALSE;
- if ($result == $this->recursion_start_date) {
- // skip the recursion-start-date from the list we going to generate
- $skip = TRUE;
- }
+ try {
+ $this->recursion->RFC5545_COMPLIANT = When::IGNORE;
+ $this->recursion->generateOccurrences();
+ }
+ catch (Exception $e) {
+ CRM_Core_Session::setStatus(ts($e->getMessage()));
+ return $recursionDates;
+ }
+ foreach ($this->recursion->occurrences as $result) {
$baseDate = $result->format('YmdHis');
foreach ($this->dateColumns as $col) {
}
}
- if ($skip) {
- unset($recursionDates[$count]);
- if ($initialCount && ($initialCount > 0)) {
- // lets increase the counter, so we get correct number of occurrences
- $initialCount++;
- $this->recursion->count($initialCount);
- }
- continue;
- }
$count++;
}
}
* @return array
* an array of child ids
*/
- static public function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) {
- $entities = array();
+ public static function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) {
+ $entities = [];
if (empty($parentId) || empty($entityTable)) {
return $entities;
}
$initiatorId = $parentId;
}
- $queryParams = array(
- 1 => array($parentId, 'Integer'),
- 2 => array($entityTable, 'String'),
- 3 => array($initiatorId, 'Integer'),
- );
+ $queryParams = [
+ 1 => [$parentId, 'Integer'],
+ 2 => [$entityTable, 'String'],
+ 3 => [$initiatorId, 'Integer'],
+ ];
if (!$mode) {
$mode = CRM_Core_DAO::singleValueQuery("SELECT mode FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams);
$recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams);
if ($recurringEntityID) {
$query .= $includeParent ? " AND id >= %4" : " AND id > %4";
- $query .= " ORDER BY id ASC"; // FIXME: change to order by dates
- $queryParams[4] = array($recurringEntityID, 'Integer');
+ // FIXME: change to order by dates
+ $query .= " ORDER BY id ASC";
+ $queryParams[4] = [$recurringEntityID, 'Integer'];
}
else {
// something wrong, return empty
- return array();
+ return [];
}
}
* @return array
* array of connected ids
*/
- static public function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) {
+ public static function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) {
$parentId = self::getParentFor($entityId, $entityTable);
if ($parentId) {
return self::getEntitiesForParent($parentId, $entityTable, $includeParent, $mode, $entityId);
}
- return array();
+ return [];
}
/**
* @return int
* unsigned $parentId Parent ID
*/
- static public function getParentFor($entityId, $entityTable, $includeParent = TRUE) {
+ public static function getParentFor($entityId, $entityTable, $includeParent = TRUE) {
if (empty($entityId) || empty($entityTable)) {
return NULL;
}
}
$parentId
= CRM_Core_DAO::singleValueQuery($query,
- array(
- 1 => array($entityId, 'Integer'),
- 2 => array($entityTable, 'String'),
- )
+ [
+ 1 => [$entityId, 'Integer'],
+ 2 => [$entityTable, 'String'],
+ ]
);
return $parentId;
}
* @param $entityTable
* @return array|null
*/
- static public function getPositionAndCount($entityId, $entityTable) {
+ public static function getPositionAndCount($entityId, $entityTable) {
$position = $count = 0;
$query = "
WHERE parent_id = (SELECT parent_id FROM civicrm_recurring_entity WHERE entity_id = %1 AND entity_table = %2) AND entity_table = %2";
$dao = CRM_Core_DAO::executeQuery($query,
- array(
- 1 => array($entityId, 'Integer'),
- 2 => array($entityTable, 'String'),
- )
+ [
+ 1 => [$entityId, 'Integer'],
+ 2 => [$entityTable, 'String'],
+ ]
);
while ($dao->fetch()) {
}
}
if ($count) {
- return array($position, $count);
+ return [$position, $count];
}
return NULL;
}
*
* @return object
*/
- static public function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) {
+ public static function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) {
$daoName = self::$_tableDAOMapper[$entityTable];
if (!$daoName) {
CRM_Core_Error::fatal("DAO Mapper missing for $entityTable.");
* @param object $event
* An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just updated.
*/
- static public function triggerUpdate($event) {
+ public static function triggerUpdate($event) {
// if DB version is earlier than 4.6 skip any processing
static $currentVer = NULL;
if (!$currentVer) {
return;
}
- static $processedEntities = array();
+ static $processedEntities = [];
$obj =& $event->object;
if (empty($obj->id) || empty($obj->__table)) {
return;
if (array_key_exists($entityTable, self::$_tableDAOMapper)) {
$daoName = self::$_tableDAOMapper[$entityTable];
- $skipData = array();
+ $skipData = [];
if (array_key_exists($entityTable, self::$_updateSkipFields)) {
$skipFields = self::$_updateSkipFields[$entityTable];
foreach ($skipFields as $sfield) {
* @param object $event
* An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just inserted.
*/
- static public function triggerInsert($event) {
+ public static function triggerInsert($event) {
$obj =& $event->object;
if (!array_key_exists($obj->__table, self::$_linkedEntitiesInfo)) {
return;
return;
}
- static $processedEntities = array();
+ static $processedEntities = [];
if (empty($obj->id) || empty($obj->__table)) {
return;
}
}
else {
// linked entity doesn't exist. lets create them
- $newCriteria = array(
+ $newCriteria = [
$idCol => $val['id'],
$tableCol => $val['table'],
- );
+ ];
$linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($obj->__table,
- array('id' => $obj->id),
+ ['id' => $obj->id],
$newCriteria,
TRUE
);
* @param object $event
* An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just deleted.
*/
- static public function triggerDelete($event) {
+ public static function triggerDelete($event) {
$obj =& $event->object;
// if DB version is earlier than 4.6 skip any processing
return;
}
- static $processedEntities = array();
+ static $processedEntities = [];
if (empty($obj->id) || empty($obj->__table) || !$event->result) {
return;
}
* @return bool|\CRM_Core_DAO_RecurringEntity
* @throws \Exception
*/
- static public function delEntity($entityId, $entityTable, $isDelLinkedEntities = FALSE) {
+ public static function delEntity($entityId, $entityTable, $isDelLinkedEntities = FALSE) {
if (empty($entityId) || empty($entityTable)) {
return FALSE;
}
*
* @return array
*/
- public function mapFormValuesToDB($formParams = array()) {
- $dbParams = array();
+ public function mapFormValuesToDB($formParams = []) {
+ $dbParams = [];
if (!empty($formParams['used_for'])) {
$dbParams['used_for'] = $formParams['used_for'];
}
}
}
if ($formParams['repeats_by'] == 2) {
- if (CRM_Utils_Array::value('entity_status_1', $formParams) && CRM_Utils_Array::value('entity_status_2', $formParams)) {
+ if (!empty($formParams['entity_status_1']) && !empty($formParams['entity_status_2'])) {
$dbParams['entity_status'] = $formParams['entity_status_1'] . " " . $formParams['entity_status_2'];
}
}
*
* @return object
*/
- static public function getScheduleReminderDetailsById($scheduleReminderId) {
+ public static function getScheduleReminderDetailsById($scheduleReminderId) {
$query = "SELECT *
FROM civicrm_action_schedule WHERE 1";
if ($scheduleReminderId) {
AND id = %1";
}
$dao = CRM_Core_DAO::executeQuery($query,
- array(
- 1 => array($scheduleReminderId, 'Integer'),
- )
+ [
+ 1 => [$scheduleReminderId, 'Integer'],
+ ]
);
$dao->fetch();
return $dao;
* @return array
*/
public function getScheduleParams($scheduleReminderId) {
- $scheduleReminderDetails = array();
+ $scheduleReminderDetails = [];
if ($scheduleReminderId) {
//Get all the details from schedule reminder table
$scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId);
* @return object
* When object
*/
- public function getRecursionFromSchedule($scheduleReminderDetails = array()) {
+ public function getRecursionFromSchedule($scheduleReminderDetails = []) {
$r = new When();
//If there is some data for this id
if ($scheduleReminderDetails['repetition_frequency_unit']) {
$repetition_frequency_unit = "dai";
}
$repetition_frequency_unit = $repetition_frequency_unit . 'ly';
- $r->recur($start, $repetition_frequency_unit);
+ $r->startDate($start)
+ ->exclusions([$start])
+ ->freq($repetition_frequency_unit);
}
if ($scheduleReminderDetails['repetition_frequency_interval']) {
if ($scheduleReminderDetails['start_action_condition']) {
$startActionCondition = $scheduleReminderDetails['start_action_condition'];
$explodeStartActionCondition = explode(',', $startActionCondition);
- $buildRuleArray = array();
+ $buildRuleArray = [];
foreach ($explodeStartActionCondition as $key => $val) {
$buildRuleArray[] = strtoupper(substr($val, 0, 2));
}
break;
}
$concatStartActionDateBits = $startActionDate1 . strtoupper(substr($startActionDate[1], 0, 2));
- $r->byday(array($concatStartActionDateBits));
+ $r->byday([$concatStartActionDateBits]);
}
elseif ($scheduleReminderDetails['limit_to']) {
- $r->bymonthday(array($scheduleReminderDetails['limit_to']));
+ $r->bymonthday([$scheduleReminderDetails['limit_to']]);
}
}
return $r;
}
-
/**
* This function gets time difference between the two datetime object.
*
* @return object
* DateTime object which contain time difference
*/
- static public function getInterval($startDate, $endDate) {
+ public static function getInterval($startDate, $endDate) {
if ($startDate && $endDate) {
$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);
if ($used_for) {
$query .= " AND used_for = %2";
}
- $params = array(
- 1 => array($entityId, 'Integer'),
- 2 => array($used_for, 'String'),
- );
+ $params = [
+ 1 => [$entityId, 'Integer'],
+ 2 => [$used_for, 'String'],
+ ];
$dao = CRM_Core_DAO::executeQuery($query, $params);
$dao->fetch();
}
* @return array
*/
public static function updateModeLinkedEntity($entityId, $linkedEntityTable, $mainEntityTable) {
- $result = array();
+ $result = [];
if ($entityId && $linkedEntityTable && $mainEntityTable) {
if (CRM_Utils_Array::value($linkedEntityTable, self::$_tableDAOMapper)) {
$dao = self::$_tableDAOMapper[$linkedEntityTable];
return NULL;
}
$entityTable = $linkedEntityTable;
- $params = array(
+ $params = [
'entity_id' => $entityId,
'entity_table' => $mainEntityTable,
- );
- $defaults = array();
+ ];
+ $defaults = [];
CRM_Core_DAO::commonRetrieve($dao, $params, $defaults);
if (!empty($defaults['id'])) {
$result['entityId'] = $defaults['id'];
* @return array
*/
public static function updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet) {
- $finalResult = array();
+ $finalResult = [];
if (!empty($linkedEntityTable)) {
$result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $linkedEntityTable, $entityTable);
$seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable);
foreach ($seriesEventRecords as $event) {
//Step-2: Save price set in other series events
- if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {//Remove existing priceset
+ //Remove existing priceset
+ if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {
CRM_Core_BAO_Discount::del($event['id'], $event['table']);
}
- CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); //Add new price set
+ //Add new price set
+ CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet);
}
}
foreach ($seriesEventRecords as $event) {
//Step-2: Save price set in other series events
if ($entityId < $event["id"]) {
- if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {//Remove existing priceset
+ //Remove existing priceset
+ if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {
CRM_Core_BAO_Discount::del($event['id'], $event['table']);
}
- CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); //Add new price set
+ //Add new price set
+ CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet);
}
}
}