From dfbe9b91cb1f97bced1a57d11ce319dfbe2637d8 Mon Sep 17 00:00:00 2001 From: priyankakaran26 Date: Fri, 12 Sep 2014 21:48:04 +0100 Subject: [PATCH] towards recursion objects --- CRM/Core/BAO/RecurringEntity.php | 59 +++----------------- CRM/Core/Form/RecurringEntity.php | 44 ++++++++------- CRM/Core/Page/AJAX/RecurringEntity.php | 74 ++++++++++++-------------- 3 files changed, 67 insertions(+), 110 deletions(-) diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index b720688255..d8a659bc01 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -95,20 +95,20 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { function mode($mode) { $this->mode = $mode; - $this->parent_id = $this->entity_id; + if (!$this->parent_id) { + $this->parent_id = $this->entity_id; + } $this->save(); } // generate all new entities based on object vars function generate() { - // fixme: check if entityid & entitytable set $this->generateRecursiveDates(); - CRM_Core_Error::debug_var('$this->recursionDates1', $this->recursionDates); return $this->generateEntities(); } - function generateRecursion2() { + function generateRecursion() { // return if already generated if (is_a($this->recursion, 'When')) { return $this->recursion; @@ -121,7 +121,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { $this->scheduleDBParams = $this->mapFormValuesToDB($this->schedule);//call using obj } - CRM_Core_Error::debug_var('$this->scheduleDBParams', $this->scheduleDBParams); if (!empty($this->scheduleDBParams)) { $this->recursion = $this->getRecursionFromReminderByDBParams($this->scheduleDBParams); } @@ -130,6 +129,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { // generate new DAOs and along with entries in recurring_entity table function generateEntities() { + // fixme: check if entityid & entitytable set + $newEntities = array(); if (!empty($this->recursionDates)) { // save an entry with initiating entity-id & entity-table @@ -143,7 +144,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { foreach ($this->overwriteColumns as $col => $val) { $newCriteria[$col] = $val; } - CRM_Core_Error::debug_var('$newCriteria', $newCriteria); $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table, array('id' => $this->entity_id), $newCriteria, @@ -156,7 +156,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } function generateRecursiveDates() { - $this->generateRecursion2(); + $this->generateRecursion(); $recursionDates = array(); if (is_a($this->recursion, 'When')) { @@ -169,7 +169,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } $count = 1; - CRM_Core_Error::debug_var('$this->intervalDateColumns', $this->intervalDateColumns); while ($result = $this->recursion->next()) { $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); @@ -218,7 +217,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } } $this->recursionDates = $recursionDates; - CRM_Core_Error::debug_var('$recursionDates', $recursionDates); return $recursionDates; } @@ -581,48 +579,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } } - static public function generateRecursions($recursionObj, $params = array(), $excludeDates = array()) { - $newParams = $recursionResult = array(); - if (is_a($recursionObj, 'When')) { - $initialCount = CRM_Utils_Array::value('start_action_offset', $params); - $interval = CRM_Utils_Array::value('interval', $params); - - $count = 1; - while ($result = $recursionObj->next()) { - $recursionResult[$count]['start_date'] = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); - - if($interval){ - $endDate = new DateTime($recursionResult[$count]['start_date']); - $endDate->add($interval); - $recursionResult[$count]['end_date'] = CRM_Utils_Date::processDate($endDate->format('Y-m-d H:i:s')); - } - - $skip = FALSE; - foreach ($excludeDates as $date) { - $date = CRM_Utils_Date::processDate($date, NULL, FALSE, 'Ymd'); - if (($date == $result->format('Ymd')) || - ($endDate && ($date > $result->format('Ymd')) && ($date <= $endDate->format('Ymd'))) - ) { - $skip = TRUE; - break; - } - } - - if ($skip) { - unset($recursionResult[$count]); - if ($initialCount && ($initialCount > 0)) { - // lets increase the counter, so we get correct number of occurrences - $initialCount++; - $recursionObj->count($initialCount); - } - continue; - } - $count++; - } - } - return $recursionResult; - } - static public function delEntityRelations($entityId, $entityTable){ if(!$entityId && !$entityTable){ return FALSE; @@ -724,7 +680,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { $recursion->excludeDateRangeColumns = array('start_date', 'end_date'); $generatedEntities = $recursion->generate(); - CRM_Core_Error::debug_var('$generatedEntities', $generatedEntities); // try changing something $recursion->mode(3); // sets ->mode var & saves in DB diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index 04096d2a41..fb809c9a92 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -212,12 +212,7 @@ class CRM_Core_Form_RecurringEntity { $dbParams = CRM_Core_BAO_RecurringEntity::mapFormValuesToDB($params); $actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($dbParams); - //Build Recursion Object - if($actionScheduleObj->id){ - $recursionObject = CRM_Core_BAO_RecurringEntity::getRecursionFromReminder($actionScheduleObj->id); - } - - //TO DO - Exclude date functionality + //exclude dates $excludeDateList = array(); if(CRM_Utils_Array::value('copyExcludeDates', $params) && CRM_Utils_Array::value('parent_event_id', $params)){ //Since we get comma separated values lets get them in array @@ -260,7 +255,7 @@ class CRM_Core_Form_RecurringEntity { } } } - + //Delete relations if any from recurring entity tables before inserting new relations for this entity id if($params['parent_event_id']){ $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($params['parent_event_id'], 'civicrm_event', FALSE); @@ -287,12 +282,23 @@ class CRM_Core_Form_RecurringEntity { CRM_Core_BAO_RecurringEntity::delEntityRelations($params['parent_event_id'], 'civicrm_event'); } - //Give call to create recursions + $recursion = new CRM_Core_BAO_RecurringEntity(); + $recursion->dateColumns = array('start_date'); + $recursion->scheduleId = $actionScheduleObj->id; + + if (!empty($excludeDateList)) { + $recursion->excludeDates = $excludeDateList; + $recursion->excludeDateRangeColumns = array('start_date', 'end_date'); + } + if ($params['parent_event_end_date']) { - $params['interval'] = CRM_Core_BAO_RecurringEntity::getInterval($params['parent_event_start_date'], $params['parent_event_end_date']); + $interval = $recursion->getInterval($params['parent_event_start_date'], $params['parent_event_end_date']); + $recursion->intervalDateColumns = array('end_date' => $interval); } - $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params, $excludeDateList); + $recurResult = $recursion->generateRecursiveDates(); + + //$recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params, $excludeDateList); if(!empty($recurResult) && $params['parent_event_id']) { CRM_Core_Form_RecurringEntity::addEntityThroughRecursion($recurResult, $params['parent_event_id']); } @@ -303,22 +309,22 @@ class CRM_Core_Form_RecurringEntity { static public function addEntityThroughRecursion($recursionResult = array(), $currEntityID){ if(!empty($recursionResult) && $currEntityID){ - $parent_event_id = CRM_Core_BAO_RecurringEntity::getParentFor($currEntityID, 'civicrm_event'); - if(!$parent_event_id){ - $parent_event_id = $currEntityID; + $parentEventId = CRM_Core_BAO_RecurringEntity::getParentFor($currEntityID, 'civicrm_event'); + if(!$parentEventId){ + $parentEventId = $currEntityID; } // add first entry just for parent - CRM_Core_BAO_RecurringEntity::quickAdd($parent_event_id, $parent_event_id, 'civicrm_event'); + CRM_Core_BAO_RecurringEntity::quickAdd($parentEventId, $parentEventId, 'civicrm_event'); foreach ($recursionResult as $key => $value) { $newEventObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity('civicrm_event', - array('id' => $parent_event_id), + array('id' => $parentEventId), $value); CRM_Core_BAO_RecurringEntity::copyCreateEntity('civicrm_price_set_entity', array( - 'entity_id' => $parent_event_id, + 'entity_id' => $parentEventId, 'entity_table' => 'civicrm_event' ), array( @@ -329,7 +335,7 @@ class CRM_Core_Form_RecurringEntity { CRM_Core_BAO_RecurringEntity::copyCreateEntity('civicrm_uf_join', array( - 'entity_id' => $parent_event_id, + 'entity_id' => $parentEventId, 'entity_table' => 'civicrm_event' ), array( @@ -340,7 +346,7 @@ class CRM_Core_Form_RecurringEntity { CRM_Core_BAO_RecurringEntity::copyCreateEntity('civicrm_tell_friend', array( - 'entity_id' => $parent_event_id, + 'entity_id' => $parentEventId, 'entity_table' => 'civicrm_event' ), array( @@ -350,7 +356,7 @@ class CRM_Core_Form_RecurringEntity { CRM_Core_BAO_RecurringEntity::copyCreateEntity('civicrm_pcp_block', array( - 'entity_id' => $parent_event_id, + 'entity_id' => $parentEventId, 'entity_table' => 'civicrm_event' ), array( diff --git a/CRM/Core/Page/AJAX/RecurringEntity.php b/CRM/Core/Page/AJAX/RecurringEntity.php index b440475774..160361b433 100644 --- a/CRM/Core/Page/AJAX/RecurringEntity.php +++ b/CRM/Core/Page/AJAX/RecurringEntity.php @@ -39,49 +39,45 @@ class CRM_Core_Page_AJAX_RecurringEntity { $params = $formValues = $genericResult = array(); $formValues = $_REQUEST; if(!empty($formValues)){ - $dbParams = CRM_Core_BAO_RecurringEntity::mapFormValuesToDB($formValues); - if(!empty($dbParams)){ - $recursionObject = CRM_Core_BAO_RecurringEntity::getRecursionFromReminderByDBParams($dbParams); - // Check if there were any errors - if($recursionObject->errors){ - $genericResult['errors'] = $recursionObject->errors; - }else{ - if(CRM_Utils_Array::value('event_id', $formValues)){ - $parent_event_id = CRM_Core_BAO_RecurringEntity::getParentFor($formValues['event_id'], 'civicrm_event'); - if(!$parent_event_id){ - $parent_event_id = $formValues['event_id']; - } - //Show the list of participants registered for the events if any - $getConnectedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($parent_event_id, 'civicrm_event', FALSE); - if($getConnectedEntities){ - $participantDetails = CRM_Core_BAO_RecurringEntity::getParticipantCountforEvent($getConnectedEntities); - if(!empty($participantDetails['countByName'])){ - $genericResult['participantData'] = $participantDetails['countByName']; - } - } - $startDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $parent_event_id, 'start_date'); - $endDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $parent_event_id, 'end_date'); + $recursion = new CRM_Core_BAO_RecurringEntity(); + $recursion->dateColumns = array('start_date'); + $recursion->schedule = $formValues; + if (!empty($formValues['exclude_date_list'])) { + $recursion->excludeDates = $formValues['exclude_date_list']; + $recursion->excludeDateRangeColumns = array('start_date', 'end_date'); + } + + $parentEventId = CRM_Core_BAO_RecurringEntity::getParentFor($formValues['event_id'], 'civicrm_event'); + if(!$parentEventId){ + $parentEventId = $formValues['event_id']; + } + + $endDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $parentEventId, 'end_date'); + if ($endDate) { + $startDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $parentEventId, 'start_date'); + $interval = $recursion->getInterval($startDate, $endDate); + $recursion->intervalDateColumns = array('end_date' => $interval); + } + + $result = $recursion->generateRecursiveDates(); - if($endDate) { - $params['interval'] = CRM_Core_BAO_RecurringEntity::getInterval($startDate, $endDate); - } - $params['start_action_offset'] = $formValues['start_action_offset']; - } - $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params, $formValues['exclude_date_list']); - $count = 1; - foreach ($recurResult as $key => $value) { - $genericResult[$count]['start_date'] = date('M d, Y h:i:s A \o\n l', strtotime($value['start_date'])); - if($value['end_date']){ - $genericResult[$count]['end_date'] = date('M d, Y h:i:s A \o\n l', strtotime($value['end_date'])); - } - $count++; - } + foreach ($result as $key => $value) { + $result[$key]['start_date'] = date('M d, Y h:i:s A \o\n l', strtotime($value['start_date'])); + if($value['end_date']){ + $result[$key]['end_date'] = date('M d, Y h:i:s A \o\n l', strtotime($value['end_date'])); + } + } + + //Show the list of participants registered for the events if any + $getConnectedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($parentEventId, 'civicrm_event', FALSE); + if($getConnectedEntities){ + $participantDetails = CRM_Core_BAO_RecurringEntity::getParticipantCountforEvent($getConnectedEntities); + if(!empty($participantDetails['countByName'])){ + $result['participantData'] = $participantDetails['countByName']; } } } - echo json_encode($genericResult); + echo json_encode($result); CRM_Utils_System::civiExit(); } - } - -- 2.25.1