- *\r
- *\r
- * @return void\r
- */\r
- public function mode($mode) {\r
- if ($this->entity_id && $this->entity_table) {\r
- if ($this->find(TRUE)) {\r
- $this->mode = $mode;\r
- }\r
- else {\r
- $this->parent_id = $this->entity_id;\r
- $this->mode = $mode;\r
- }\r
- $this->save();\r
- }\r
- }\r
-\r
- /**\r
- * This function generates all new entities based on object vars\r
- *\r
- * @return array\r
- */\r
- public function generate() {\r
- $this->generateRecursiveDates();\r
-\r
- return $this->generateEntities();\r
- }\r
-\r
- /**\r
- * This function builds a "When" object based on schedule/reminder params\r
- *\r
- * @return object When object\r
- */\r
- public function generateRecursion() {\r
- // return if already generated\r
- if (is_a($this->recursion, 'When')) {\r
- return $this->recursion;\r
- }\r
-\r
- if ($this->scheduleId) {\r
- // get params by ID\r
- $this->schedule = $this->getScheduleParams($this->scheduleId);\r
- }\r
- elseif (!empty($this->scheduleFormValues)) {\r
- $this->schedule = $this->mapFormValuesToDB($this->scheduleFormValues);\r
- }\r
-\r
- if (!empty($this->schedule)) {\r
- $this->recursion = $this->getRecursionFromSchedule($this->schedule);\r
- }\r
- return $this->recursion;\r
- }\r
-\r
- /**\r
- * Generate new DAOs and along with entries in civicrm_recurring_entity table\r
- *\r
- * @return array\r
- */\r
- public function generateEntities() {\r
- self::setStatus(self::RUNNING);\r
-\r
- $newEntities = array();\r
- $findCriteria = array();\r
- if (!empty($this->recursionDates)) {\r
- if ($this->entity_id) {\r
- $findCriteria = array('id' => $this->entity_id);\r
-\r
- // save an entry with initiating entity-id & entity-table\r
- if ($this->entity_table && !$this->find(TRUE)) {\r
- $this->parent_id = $this->entity_id;\r
- $this->save();\r
- }\r
- }\r
- if (empty($findCriteria)) {\r
- CRM_Core_Error::fatal("Find criteria missing to generate form. Make sure entity_id and table is set.");\r
- }\r
-\r
- $count = 0;\r
- foreach ($this->recursionDates as $key => $dateCols) {\r
- $newCriteria = $dateCols;\r
- foreach ($this->overwriteColumns as $col => $val) {\r
- $newCriteria[$col] = $val;\r
- }\r
- // create main entities\r
- $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table,\r
- $findCriteria,\r
- $newCriteria,\r
- $this->isRecurringEntityRecord\r
- );\r
-\r
- if (is_a($obj, 'CRM_Core_DAO') && $obj->id) {\r
- $newCriteria = array();\r
- $newEntities[$this->entity_table][$count] = $obj->id;\r
-\r
- foreach ($this->linkedEntities as $linkedInfo) {\r
- foreach ($linkedInfo['linkedColumns'] as $col) {\r
- $newCriteria[$col] = $obj->id;\r
- }\r
- // create linked entities\r
- $linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($linkedInfo['table'],\r
- $linkedInfo['findCriteria'],\r
- $newCriteria,\r
- CRM_Utils_Array::value('isRecurringEntityRecord', $linkedInfo, TRUE)\r
- );\r
-\r
- if (is_a($linkedObj, 'CRM_Core_DAO') && $linkedObj->id) {\r
- $newEntities[$linkedInfo['table']][$count] = $linkedObj->id;\r
- }\r
- }\r
- }\r
- $count++;\r
- }\r
- }\r
-\r
- self::$status = NULL;\r
- return $newEntities;\r
- }\r
-\r
- /**\r
- * This function iterates through when object criterias and\r
- * generates recursive dates based on that\r
- *\r
- * @return array array of dates\r
- */\r
- public function generateRecursiveDates() {\r
- $this->generateRecursion();\r
-\r
- $recursionDates = array();\r
- if (is_a($this->recursion, 'When')) {\r
- $initialCount = CRM_Utils_Array::value('start_action_offset', $this->schedule);\r
-\r
- $exRangeStart = $exRangeEnd = NULL;\r
- if (!empty($this->excludeDateRangeColumns)) {\r
- $exRangeStart = $this->excludeDateRangeColumns[0];\r
- $exRangeEnd = $this->excludeDateRangeColumns[1];\r
- }\r
-\r
- $count = 1;\r
- while ($result = $this->recursion->next()) {\r
- $skip = FALSE;\r
- if ($result == $this->recursion_start_date) {\r
- // skip the recursion-start-date from the list we going to generate\r
- $skip = TRUE;\r
- }\r
- $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s'));\r
-\r
- foreach ($this->dateColumns as $col) {\r
- $recursionDates[$count][$col] = $baseDate;\r
- }\r
- foreach ($this->intervalDateColumns as $col => $interval) {\r
- $newDate = new DateTime($baseDate);\r
- $newDate->add($interval);\r
- $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s'));\r
- }\r
- if ($exRangeStart) {\r
- $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd');\r
- $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd');\r
- }\r
-\r
- foreach ($this->excludeDates as $exDate) {\r
- $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd');\r
- if (!$exRangeStart) {\r
- if ($exDate == $result->format('Ymd')) {\r
- $skip = TRUE;\r
- break;\r
- }\r
- }\r
- else {\r
- if (($exDate == $exRangeStartDate) ||\r
- ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate))\r
- ) {\r
- $skip = TRUE;\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if ($skip) {\r
- unset($recursionDates[$count]);\r
- if ($initialCount && ($initialCount > 0)) {\r
- // lets increase the counter, so we get correct number of occurrences\r
- $initialCount++;\r
- $this->recursion->count($initialCount);\r
- }\r
- continue;\r
- }\r
- $count++;\r
- }\r
- }\r
- $this->recursionDates = $recursionDates;\r
-\r
- return $recursionDates;\r
- }\r
-\r
- /**\r
- * This function gets all the children for a particular parent entity\r
- *\r
+ *
+ *
+ * @return void
+ */
+ public function mode($mode) {
+ if ($this->entity_id && $this->entity_table) {
+ if ($this->find(TRUE)) {
+ $this->mode = $mode;
+ }
+ else {
+ $this->parent_id = $this->entity_id;
+ $this->mode = $mode;
+ }
+ $this->save();
+ }
+ }
+
+ /**
+ * This function generates all new entities based on object vars.
+ *
+ * @return array
+ */
+ public function generate() {
+ $this->generateRecursiveDates();
+
+ return $this->generateEntities();
+ }
+
+ /**
+ * This function builds a "When" object based on schedule/reminder params
+ *
+ * @return object
+ * When object
+ */
+ public function generateRecursion() {
+ // return if already generated
+ if (is_a($this->recursion, 'When')) {
+ return $this->recursion;
+ }
+
+ if ($this->scheduleId) {
+ // get params by ID
+ $this->schedule = $this->getScheduleParams($this->scheduleId);
+ }
+ elseif (!empty($this->scheduleFormValues)) {
+ $this->schedule = $this->mapFormValuesToDB($this->scheduleFormValues);
+ }
+
+ if (!empty($this->schedule)) {
+ $this->recursion = $this->getRecursionFromSchedule($this->schedule);
+ }
+ return $this->recursion;
+ }
+
+ /**
+ * Generate new DAOs and along with entries in civicrm_recurring_entity table.
+ *
+ * @return array
+ */
+ public function generateEntities() {
+ self::setStatus(self::RUNNING);
+
+ $newEntities = array();
+ $findCriteria = array();
+ if (!empty($this->recursionDates)) {
+ if ($this->entity_id) {
+ $findCriteria = array('id' => $this->entity_id);
+
+ // save an entry with initiating entity-id & entity-table
+ if ($this->entity_table && !$this->find(TRUE)) {
+ $this->parent_id = $this->entity_id;
+ $this->save();
+ }
+ }
+ if (empty($findCriteria)) {
+ CRM_Core_Error::fatal("Find criteria missing to generate form. Make sure entity_id and table is set.");
+ }
+
+ $count = 0;
+ foreach ($this->recursionDates as $key => $dateCols) {
+ $newCriteria = $dateCols;
+ foreach ($this->overwriteColumns as $col => $val) {
+ $newCriteria[$col] = $val;
+ }
+ // create main entities
+ $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table,
+ $findCriteria,
+ $newCriteria,
+ $this->isRecurringEntityRecord
+ );
+
+ if (is_a($obj, 'CRM_Core_DAO') && $obj->id) {
+ $newCriteria = array();
+ $newEntities[$this->entity_table][$count] = $obj->id;
+
+ foreach ($this->linkedEntities as $linkedInfo) {
+ foreach ($linkedInfo['linkedColumns'] as $col) {
+ $newCriteria[$col] = $obj->id;
+ }
+ // create linked entities
+ $linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($linkedInfo['table'],
+ $linkedInfo['findCriteria'],
+ $newCriteria,
+ CRM_Utils_Array::value('isRecurringEntityRecord', $linkedInfo, TRUE)
+ );
+
+ if (is_a($linkedObj, 'CRM_Core_DAO') && $linkedObj->id) {
+ $newEntities[$linkedInfo['table']][$count] = $linkedObj->id;
+ }
+ }
+ }
+ $count++;
+ }
+ }
+
+ self::$status = NULL;
+ return $newEntities;
+ }
+
+ /**
+ * This function iterates through when object criterias and
+ * generates recursive dates based on that
+ *
+ * @return array
+ * array of dates
+ */
+ public function generateRecursiveDates() {
+ $this->generateRecursion();
+
+ $recursionDates = array();
+ if (is_a($this->recursion, 'When')) {
+ $initialCount = CRM_Utils_Array::value('start_action_offset', $this->schedule);
+
+ $exRangeStart = $exRangeEnd = NULL;
+ if (!empty($this->excludeDateRangeColumns)) {
+ $exRangeStart = $this->excludeDateRangeColumns[0];
+ $exRangeEnd = $this->excludeDateRangeColumns[1];
+ }
+
+ $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;
+ }
+ $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s'));
+
+ foreach ($this->dateColumns as $col) {
+ $recursionDates[$count][$col] = $baseDate;
+ }
+ foreach ($this->intervalDateColumns as $col => $interval) {
+ $newDate = new DateTime($baseDate);
+ $newDate->add($interval);
+ $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s'));
+ }
+ if ($exRangeStart) {
+ $exRangeStartDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeStart, $recursionDates[$count]), NULL, FALSE, 'Ymd');
+ $exRangeEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeEnd, $recursionDates[$count]), NULL, FALSE, 'Ymd');
+ }
+
+ foreach ($this->excludeDates as $exDate) {
+ $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd');
+ if (!$exRangeStart) {
+ if ($exDate == $result->format('Ymd')) {
+ $skip = TRUE;
+ break;
+ }
+ }
+ else {
+ if (($exDate == $exRangeStartDate) ||
+ ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate))
+ ) {
+ $skip = TRUE;
+ break;
+ }
+ }
+ }
+
+ 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++;
+ }
+ }
+ $this->recursionDates = $recursionDates;
+
+ return $recursionDates;
+ }
+
+ /**
+ * This function gets all the children for a particular parent entity.
+ *