| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | require_once 'packages/When/When.php'; |
| 37 | |
| 38 | class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { |
| 39 | |
| 40 | public $schedule = array(); |
| 41 | public $scheduleId = NULL; |
| 42 | public $scheduleDBParams = array(); |
| 43 | |
| 44 | public $dateColumns = array(); |
| 45 | public $overwriteColumns = array(); |
| 46 | public $intervalDateColumns = array(); |
| 47 | |
| 48 | public $excludeDates = array(); |
| 49 | |
| 50 | public $recursion = NULL; |
| 51 | public $recursionCounter = NULL; |
| 52 | |
| 53 | public $isGenRecurringEntity = TRUE; |
| 54 | |
| 55 | static $_tableDAOMapper = |
| 56 | array( |
| 57 | 'civicrm_event' => 'CRM_Event_DAO_Event', |
| 58 | 'civicrm_price_set_entity' => 'CRM_Price_DAO_PriceSetEntity', |
| 59 | 'civicrm_uf_join' => 'CRM_Core_DAO_UFJoin', |
| 60 | 'civicrm_tell_friend' => 'CRM_Friend_DAO_Friend', |
| 61 | 'civicrm_pcp_block' => 'CRM_PCP_DAO_PCPBlock', |
| 62 | 'civicrm_activity' => 'CRM_Activity_DAO_Activity', |
| 63 | ); |
| 64 | |
| 65 | static function add(&$params) { |
| 66 | if (CRM_Utils_Array::value('id', $params)) { |
| 67 | CRM_Utils_Hook::pre('edit', 'RecurringEntity', $params['id'], $params); |
| 68 | } |
| 69 | else { |
| 70 | CRM_Utils_Hook::pre('create', 'RecurringEntity', NULL, $params); |
| 71 | } |
| 72 | |
| 73 | $daoRecurringEntity = new CRM_Core_DAO_RecurringEntity(); |
| 74 | $daoRecurringEntity->copyValues($params); |
| 75 | $result = $daoRecurringEntity->save(); |
| 76 | |
| 77 | if (CRM_Utils_Array::value('id', $params)) { |
| 78 | CRM_Utils_Hook::post('edit', 'RecurringEntity', $daoRecurringEntity->id, $daoRecurringEntity); |
| 79 | } |
| 80 | else { |
| 81 | CRM_Utils_Hook::post('create', 'RecurringEntity', $daoRecurringEntity->id, $daoRecurringEntity); |
| 82 | } |
| 83 | return $result; |
| 84 | } |
| 85 | |
| 86 | static function quickAdd($parentId, $entityId, $entityTable) { |
| 87 | $params = |
| 88 | array( |
| 89 | 'parent_id' => $parentId, |
| 90 | 'entity_id' => $entityId, |
| 91 | 'entity_table' => $entityTable |
| 92 | ); |
| 93 | return self::add($params); |
| 94 | } |
| 95 | |
| 96 | function mode($mode) { |
| 97 | $this->mode = $mode; |
| 98 | $this->parent_id = $this->entity_id; |
| 99 | $this->save(); |
| 100 | } |
| 101 | |
| 102 | // generate all new entities based on object vars |
| 103 | function generate() { |
| 104 | // fixme: check if entityid & entitytable set |
| 105 | $this->generateRecursiveDates(); |
| 106 | CRM_Core_Error::debug_var('$this->recursionDates1', $this->recursionDates); |
| 107 | |
| 108 | return $this->generateEntities(); |
| 109 | } |
| 110 | |
| 111 | function generateRecursion2() { |
| 112 | // return if already generated |
| 113 | if (is_a($this->recursion, 'When')) { |
| 114 | return $this->recursion; |
| 115 | } |
| 116 | |
| 117 | if ($this->scheduleId) { |
| 118 | // get params by ID |
| 119 | $this->recursion = $this->getRecursionFromReminder($this->scheduleId); |
| 120 | } else if (!empty($this->schedule)) { |
| 121 | $this->scheduleDBParams = $this->mapFormValuesToDB($this->schedule);//call using obj |
| 122 | } |
| 123 | |
| 124 | CRM_Core_Error::debug_var('$this->scheduleDBParams', $this->scheduleDBParams); |
| 125 | if (!empty($this->scheduleDBParams)) { |
| 126 | $this->recursion = $this->getRecursionFromReminderByDBParams($this->scheduleDBParams); |
| 127 | } |
| 128 | return $this->recursion; |
| 129 | } |
| 130 | |
| 131 | // generate new DAOs and along with entries in recurring_entity table |
| 132 | function generateEntities() { |
| 133 | $newEntities = array(); |
| 134 | if (!empty($this->recursionDates)) { |
| 135 | // save an entry with initiating entity-id & entity-table |
| 136 | if (!$this->find(TRUE)) { |
| 137 | $this->parent_id = $this->entity_id; |
| 138 | $this->save(); |
| 139 | } |
| 140 | |
| 141 | foreach ($this->recursionDates as $key => $dateCols) { |
| 142 | $newCriteria = $dateCols; |
| 143 | foreach ($this->overwriteColumns as $col => $val) { |
| 144 | $newCriteria[$col] = $val; |
| 145 | } |
| 146 | CRM_Core_Error::debug_var('$newCriteria', $newCriteria); |
| 147 | $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table, |
| 148 | array('id' => $this->entity_id), |
| 149 | $newCriteria, |
| 150 | $this->isGenRecurringEntity |
| 151 | ); |
| 152 | $newEntities[] = $obj->id; |
| 153 | } |
| 154 | } |
| 155 | return $newEntities; |
| 156 | } |
| 157 | |
| 158 | function generateRecursiveDates() { |
| 159 | $this->generateRecursion2(); |
| 160 | |
| 161 | $recursionDates = array(); |
| 162 | if (is_a($this->recursion, 'When')) { |
| 163 | $initialCount = CRM_Utils_Array::value('start_action_offset', $this->scheduleDBParams); |
| 164 | |
| 165 | $exRangeStart = $exRangeEnd = NULL; |
| 166 | if (!empty($this->excludeDateRangeColumns)) { |
| 167 | $exRangeStart = $this->excludeDateRangeColumns[0]; |
| 168 | $exRangeEnd = $this->excludeDateRangeColumns[1]; |
| 169 | } |
| 170 | |
| 171 | $count = 1; |
| 172 | CRM_Core_Error::debug_var('$this->intervalDateColumns', $this->intervalDateColumns); |
| 173 | while ($result = $this->recursion->next()) { |
| 174 | $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); |
| 175 | |
| 176 | foreach ($this->dateColumns as $col) { |
| 177 | $recursionDates[$count][$col] = $baseDate; |
| 178 | } |
| 179 | foreach ($this->intervalDateColumns as $col => $interval) { |
| 180 | $newDate = new DateTime($baseDate); |
| 181 | $newDate->add($interval); |
| 182 | $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s')); |
| 183 | } |
| 184 | if ($exRangeStart) { |
| 185 | $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd'); |
| 186 | $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd'); |
| 187 | } |
| 188 | |
| 189 | $skip = FALSE; |
| 190 | foreach ($this->excludeDates as $exDate) { |
| 191 | $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd'); |
| 192 | if (!$exRangeStart) { |
| 193 | if ($exDate == $result->format('Ymd')) { |
| 194 | $skip = TRUE; |
| 195 | break; |
| 196 | } |
| 197 | } else { |
| 198 | if (($exDate == $exRangeStartDate) || |
| 199 | ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate)) |
| 200 | ) { |
| 201 | $skip = TRUE; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if ($skip) { |
| 208 | unset($recursionDates[$count]); |
| 209 | if ($initialCount && ($initialCount > 0)) { |
| 210 | // lets increase the counter, so we get correct number of occurrences |
| 211 | $initialCount++; |
| 212 | $this->recursion->count($initialCount); |
| 213 | $this->recursionCounter = $initialCount; |
| 214 | } |
| 215 | continue; |
| 216 | } |
| 217 | $count++; |
| 218 | } |
| 219 | } |
| 220 | $this->recursionDates = $recursionDates; |
| 221 | CRM_Core_Error::debug_var('$recursionDates', $recursionDates); |
| 222 | |
| 223 | return $recursionDates; |
| 224 | } |
| 225 | |
| 226 | // MODE = 3 (ALL) |
| 227 | static public function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) { |
| 228 | $entities = array(); |
| 229 | |
| 230 | if (!$initiatorId) { |
| 231 | $initiatorId = $parentId; |
| 232 | } |
| 233 | |
| 234 | $queryParams = array( |
| 235 | 1 => array($parentId, 'Integer'), |
| 236 | 2 => array($entityTable, 'String'), |
| 237 | 3 => array($initiatorId, 'Integer'), |
| 238 | ); |
| 239 | |
| 240 | if (!$mode) { |
| 241 | $mode = CRM_Core_DAO::singleValueQuery("SELECT mode FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
| 242 | } |
| 243 | |
| 244 | $query = "SELECT * |
| 245 | FROM civicrm_recurring_entity |
| 246 | WHERE parent_id = %1 AND entity_table = %2"; |
| 247 | if (!$includeParent) { |
| 248 | $query .= " AND entity_id != " . ($initiatorId ? "%3" : "%1"); |
| 249 | } |
| 250 | |
| 251 | if ($mode == '1') { // MODE = SINGLE |
| 252 | $query .= " AND entity_id = %3"; |
| 253 | } else if ($mode == '2') { // MODE = FUTURE |
| 254 | $recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
| 255 | if ($recurringEntityID) { |
| 256 | $query .= $includeParent ? " AND id >= %4" : " AND id > %4"; |
| 257 | $query .= " ORDER BY id ASC"; // FIXME: change to order by dates |
| 258 | $queryParams[4] = array($recurringEntityID, 'Integer'); |
| 259 | } else { |
| 260 | // something wrong, return empty |
| 261 | return array(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 266 | while ($dao->fetch()) { |
| 267 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['table'] = $dao->entity_table; |
| 268 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['id'] = $dao->entity_id; |
| 269 | } |
| 270 | return $entities; |
| 271 | } |
| 272 | |
| 273 | static public function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) { |
| 274 | $parentId = self::getParentFor($entityId, $entityTable); |
| 275 | if ($parentId) { |
| 276 | return self::getEntitiesForParent($parentId, $entityTable, $includeParent, $mode, $entityId); |
| 277 | } |
| 278 | return array(); |
| 279 | } |
| 280 | |
| 281 | static public function getParentFor($entityId, $entityTable, $includeParent = TRUE) { |
| 282 | $query = " |
| 283 | SELECT parent_id |
| 284 | FROM civicrm_recurring_entity |
| 285 | WHERE entity_id = %1 AND entity_table = %2"; |
| 286 | if (!$includeParent) { |
| 287 | $query .= " AND parent_id != %1"; |
| 288 | } |
| 289 | $parentId = |
| 290 | CRM_Core_DAO::singleValueQuery($query, |
| 291 | array( |
| 292 | 1 => array($entityId, 'Integer'), |
| 293 | 2 => array($entityTable, 'String'), |
| 294 | ) |
| 295 | ); |
| 296 | return $parentId; |
| 297 | } |
| 298 | |
| 299 | static public function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) { |
| 300 | $daoName = self::$_tableDAOMapper[$entityTable]; |
| 301 | $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams); |
| 302 | |
| 303 | if ($newObject->id && $createRecurringEntity) { |
| 304 | $object = new $daoName( ); |
| 305 | foreach ($fromCriteria as $key => $value) { |
| 306 | $object->$key = $value; |
| 307 | } |
| 308 | $object->find(TRUE); |
| 309 | |
| 310 | CRM_Core_BAO_RecurringEntity::quickAdd($object->id, $newObject->id, $entityTable); |
| 311 | } |
| 312 | return $newObject; |
| 313 | } |
| 314 | |
| 315 | static public function triggerUpdate($obj) { |
| 316 | // if DB version is earlier than 4.6 skip any processing |
| 317 | static $currentVer = NULL; |
| 318 | if (!$currentVer) { |
| 319 | $currentVer = CRM_Core_BAO_Domain::version(); |
| 320 | } |
| 321 | if (version_compare($currentVer, '4.6.alpha1') < 0) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | static $processedEntities = array(); |
| 326 | if (empty($obj->id) || empty($obj->__table)) { |
| 327 | return FALSE; |
| 328 | } |
| 329 | $key = "{$obj->__table}_{$obj->id}"; |
| 330 | |
| 331 | if (array_key_exists($key, $processedEntities)) { |
| 332 | // already processed |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | // get related entities |
| 337 | $repeatingEntities = self::getEntitiesFor($obj->id, $obj->__table, FALSE, NULL); |
| 338 | if (empty($repeatingEntities)) { |
| 339 | // return if its not a recurring entity parent |
| 340 | return NULL; |
| 341 | } |
| 342 | // mark being processed |
| 343 | $processedEntities[$key] = 1; |
| 344 | |
| 345 | // to make sure we not copying to source itself |
| 346 | unset($repeatingEntities[$key]); |
| 347 | |
| 348 | foreach($repeatingEntities as $key => $val) { |
| 349 | $entityID = $val['id']; |
| 350 | $entityTable = $val['table']; |
| 351 | |
| 352 | $processedEntities[$key] = 1; |
| 353 | |
| 354 | if (array_key_exists($entityTable, self::$_tableDAOMapper)) { |
| 355 | $daoName = self::$_tableDAOMapper[$entityTable]; |
| 356 | |
| 357 | // FIXME: generalize me |
| 358 | $skipData = array('start_date' => NULL, |
| 359 | 'end_date' => NULL, |
| 360 | ); |
| 361 | |
| 362 | $updateDAO = CRM_Core_DAO::cascadeUpdate($daoName, $obj->id, $entityID, $skipData); |
| 363 | CRM_Core_DAO::freeResult(); |
| 364 | } else { |
| 365 | CRM_Core_Error::fatal("DAO Mapper missing for $entityTable."); |
| 366 | } |
| 367 | } |
| 368 | // done with processing. lets unset static var. |
| 369 | unset($processedEntities); |
| 370 | } |
| 371 | |
| 372 | function mapFormValuesToDB($formParams = array()){ |
| 373 | $dbParams = array(); |
| 374 | if(CRM_Utils_Array::value('used_for', $formParams)){ |
| 375 | $dbParams['used_for'] = $formParams['used_for']; |
| 376 | } |
| 377 | |
| 378 | if(CRM_Utils_Array::value('parent_event_id', $formParams)){ |
| 379 | $dbParams['entity_value'] = $formParams['parent_event_id']; |
| 380 | } |
| 381 | |
| 382 | if(CRM_Utils_Array::value('repetition_start_date', $formParams)) { |
| 383 | $repetitionStartDate = $formParams['repetition_start_date']; |
| 384 | if (CRM_Utils_Array::value('repetition_start_date_time', $formParams)){ |
| 385 | $repetitionStartDate = $repetitionStartDate . " " . $formParams['repetition_start_date_time']; |
| 386 | } |
| 387 | $repetition_start_date = new DateTime($repetitionStartDate); |
| 388 | $repetition_start_date->modify('+1 day'); |
| 389 | $dbParams['entity_status'] = CRM_Utils_Date::processDate($repetition_start_date->format('Y-m-d H:i:s')); |
| 390 | } |
| 391 | |
| 392 | if(CRM_Utils_Array::value('repetition_frequency_unit', $formParams)){ |
| 393 | $dbParams['repetition_frequency_unit'] = $formParams['repetition_frequency_unit']; |
| 394 | } |
| 395 | |
| 396 | if(CRM_Utils_Array::value('repetition_frequency_interval', $formParams)){ |
| 397 | $dbParams['repetition_frequency_interval'] = $formParams['repetition_frequency_interval']; |
| 398 | } |
| 399 | |
| 400 | //For Repeats on:(weekly case) |
| 401 | if($formParams['repetition_frequency_unit'] == 'week'){ |
| 402 | if(CRM_Utils_Array::value('start_action_condition', $formParams)){ |
| 403 | $repeats_on = CRM_Utils_Array::value('start_action_condition', $formParams); |
| 404 | $dbParams['start_action_condition'] = implode(",", array_keys($repeats_on)); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | //For Repeats By:(monthly case) |
| 409 | if($formParams['repetition_frequency_unit'] == 'month'){ |
| 410 | if($formParams['repeats_by'] == 1){ |
| 411 | if(CRM_Utils_Array::value('limit_to', $formParams)){ |
| 412 | $dbParams['limit_to'] = $formParams['limit_to']; |
| 413 | } |
| 414 | } |
| 415 | if($formParams['repeats_by'] == 2){ |
| 416 | if(CRM_Utils_Array::value('start_action_date_1', $formParams) && CRM_Utils_Array::value('start_action_date_2', $formParams)){ |
| 417 | $dbParams['start_action_date'] = $formParams['start_action_date_1']." ".$formParams['start_action_date_2']; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | //For "Ends" - After: |
| 423 | if($formParams['ends'] == 1){ |
| 424 | if(CRM_Utils_Array::value('start_action_offset', $formParams)){ |
| 425 | $dbParams['start_action_offset'] = $formParams['start_action_offset']; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | //For "Ends" - On: |
| 430 | if($formParams['ends'] == 2){ |
| 431 | if(CRM_Utils_Array::value('repeat_absolute_date', $formParams)){ |
| 432 | $dbParams['absolute_date'] = CRM_Utils_Date::processDate($formParams['repeat_absolute_date']); |
| 433 | } |
| 434 | } |
| 435 | return $dbParams; |
| 436 | } |
| 437 | |
| 438 | static public function getScheduleReminderDetailsById($scheduleReminderId){ |
| 439 | $query = "SELECT * |
| 440 | FROM civicrm_action_schedule WHERE 1"; |
| 441 | if($scheduleReminderId){ |
| 442 | $query .= " |
| 443 | AND id = %1"; |
| 444 | } |
| 445 | $dao = CRM_Core_DAO::executeQuery($query, |
| 446 | array( |
| 447 | 1 => array($scheduleReminderId, 'Integer') |
| 448 | ) |
| 449 | ); |
| 450 | $dao->fetch(); |
| 451 | return $dao; |
| 452 | } |
| 453 | |
| 454 | function getRecursionFromReminder($scheduleReminderId){ |
| 455 | if($scheduleReminderId){ |
| 456 | //Get all the details from schedule reminder table |
| 457 | $scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId); |
| 458 | $scheduleReminderDetails = (array) $scheduleReminderDetails; |
| 459 | $recursionDetails = self::getRecursionFromReminderByDBParams($scheduleReminderDetails); |
| 460 | } |
| 461 | return $recursionDetails; |
| 462 | } |
| 463 | |
| 464 | function getRecursionFromReminderByDBParams($scheduleReminderDetails = array()){ |
| 465 | $r = new When(); |
| 466 | //If there is some data for this id |
| 467 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
| 468 | if($scheduleReminderDetails['entity_status']){ |
| 469 | $currDate = date('Y-m-d H:i:s', strtotime($scheduleReminderDetails['entity_status'])); |
| 470 | }else{ |
| 471 | $currDate = date("Y-m-d H:i:s"); |
| 472 | } |
| 473 | $start = new DateTime($currDate); |
| 474 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
| 475 | $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; |
| 476 | if($repetition_frequency_unit == "day"){ |
| 477 | $repetition_frequency_unit = "dai"; |
| 478 | } |
| 479 | $repetition_frequency_unit = $repetition_frequency_unit.'ly'; |
| 480 | $r->recur($start, $repetition_frequency_unit); |
| 481 | } |
| 482 | |
| 483 | if($scheduleReminderDetails['repetition_frequency_interval']){ |
| 484 | $r->interval($scheduleReminderDetails['repetition_frequency_interval']); |
| 485 | }else{ |
| 486 | $r->errors[] = 'Repeats every: is a required field'; |
| 487 | } |
| 488 | |
| 489 | //week |
| 490 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'week'){ |
| 491 | if($scheduleReminderDetails['start_action_condition']){ |
| 492 | $startActionCondition = $scheduleReminderDetails['start_action_condition']; |
| 493 | $explodeStartActionCondition = explode(',', $startActionCondition); |
| 494 | $buildRuleArray = array(); |
| 495 | foreach($explodeStartActionCondition as $key => $val){ |
| 496 | $buildRuleArray[] = strtoupper(substr($val, 0, 2)); |
| 497 | } |
| 498 | $r->wkst('MO')->byday($buildRuleArray); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | //month |
| 503 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'month'){ |
| 504 | if($scheduleReminderDetails['start_action_date']){ |
| 505 | $startActionDate = explode(" ", $scheduleReminderDetails['start_action_date']); |
| 506 | switch ($startActionDate[0]) { |
| 507 | case 'first': |
| 508 | $startActionDate1 = 1; |
| 509 | break; |
| 510 | case 'second': |
| 511 | $startActionDate1 = 2; |
| 512 | break; |
| 513 | case 'third': |
| 514 | $startActionDate1 = 3; |
| 515 | break; |
| 516 | case 'fourth': |
| 517 | $startActionDate1 = 4; |
| 518 | break; |
| 519 | case 'last': |
| 520 | $startActionDate1 = -1; |
| 521 | break; |
| 522 | } |
| 523 | $concatStartActionDateBits = $startActionDate1.strtoupper(substr($startActionDate[1], 0, 2)); |
| 524 | $r->byday(array($concatStartActionDateBits)); |
| 525 | }else if($scheduleReminderDetails['limit_to']){ |
| 526 | $r->bymonthday(array($scheduleReminderDetails['limit_to'])); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | //Ends |
| 531 | if($scheduleReminderDetails['start_action_offset']){ |
| 532 | if($scheduleReminderDetails['start_action_offset'] > 30){ |
| 533 | $r->errors[] = 'Occurrences should be less than or equal to 30'; |
| 534 | } |
| 535 | $r->count($scheduleReminderDetails['start_action_offset']); |
| 536 | } |
| 537 | |
| 538 | if(CRM_Utils_Array::value('absolute_date', $scheduleReminderDetails)) { |
| 539 | $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']); |
| 540 | $endDate = new DateTime($absoluteDate[0].' '.$absoluteDate[1]); |
| 541 | $r->until($endDate); |
| 542 | } |
| 543 | |
| 544 | if(!$scheduleReminderDetails['start_action_offset'] && !$scheduleReminderDetails['absolute_date']){ |
| 545 | $r->errors[] = 'Ends: is a required field'; |
| 546 | } |
| 547 | }else{ |
| 548 | $r->errors[] = 'Repeats: is a required field'; |
| 549 | } |
| 550 | return $r; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Get Reminder id based on event id |
| 555 | */ |
| 556 | static public function getReminderDetailsByEventId($eventId, $used_for){ |
| 557 | if($eventId){ |
| 558 | $query = " |
| 559 | SELECT * |
| 560 | FROM civicrm_action_schedule |
| 561 | WHERE entity_value = %1"; |
| 562 | if($used_for){ |
| 563 | $query .= " AND used_for = %2"; |
| 564 | } |
| 565 | $params = array( |
| 566 | 1 => array($eventId, 'Integer'), |
| 567 | 2 => array($used_for, 'String') |
| 568 | ); |
| 569 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 570 | $dao->fetch(); |
| 571 | } |
| 572 | return $dao; |
| 573 | } |
| 574 | |
| 575 | static public function getInterval($startDate, $endDate) { |
| 576 | if ($startDate && $endDate) { |
| 577 | $startDate = new DateTime($startDate); |
| 578 | $endDate = new DateTime($endDate); |
| 579 | |
| 580 | return $startDate->diff($endDate); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static public function generateRecursions($recursionObj, $params = array(), $excludeDates = array()) { |
| 585 | $newParams = $recursionResult = array(); |
| 586 | if (is_a($recursionObj, 'When')) { |
| 587 | $initialCount = CRM_Utils_Array::value('start_action_offset', $params); |
| 588 | $interval = CRM_Utils_Array::value('interval', $params); |
| 589 | |
| 590 | $count = 1; |
| 591 | while ($result = $recursionObj->next()) { |
| 592 | $recursionResult[$count]['start_date'] = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); |
| 593 | |
| 594 | if($interval){ |
| 595 | $endDate = new DateTime($recursionResult[$count]['start_date']); |
| 596 | $endDate->add($interval); |
| 597 | $recursionResult[$count]['end_date'] = CRM_Utils_Date::processDate($endDate->format('Y-m-d H:i:s')); |
| 598 | } |
| 599 | |
| 600 | $skip = FALSE; |
| 601 | foreach ($excludeDates as $date) { |
| 602 | $date = CRM_Utils_Date::processDate($date, NULL, FALSE, 'Ymd'); |
| 603 | if (($date == $result->format('Ymd')) || |
| 604 | ($endDate && ($date > $result->format('Ymd')) && ($date <= $endDate->format('Ymd'))) |
| 605 | ) { |
| 606 | $skip = TRUE; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if ($skip) { |
| 612 | unset($recursionResult[$count]); |
| 613 | if ($initialCount && ($initialCount > 0)) { |
| 614 | // lets increase the counter, so we get correct number of occurrences |
| 615 | $initialCount++; |
| 616 | $recursionObj->count($initialCount); |
| 617 | } |
| 618 | continue; |
| 619 | } |
| 620 | $count++; |
| 621 | } |
| 622 | } |
| 623 | return $recursionResult; |
| 624 | } |
| 625 | |
| 626 | static public function delEntityRelations($entityId, $entityTable){ |
| 627 | if(!$entityId && !$entityTable){ |
| 628 | return FALSE; |
| 629 | } |
| 630 | $parentID = self::getParentFor($entityId, $entityTable); |
| 631 | if($parentID){ |
| 632 | $dao = new CRM_Core_DAO_RecurringEntity(); |
| 633 | $dao->parent_id = $parentID; |
| 634 | return $dao->delete(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static public function getParticipantCountforEvent($listOfRelatedEntities = array()){ |
| 639 | if(!empty($listOfRelatedEntities)){ |
| 640 | $implodeRelatedEntities = implode(',', array_map(function($entity){ |
| 641 | return $entity['id']; |
| 642 | }, $listOfRelatedEntities)); |
| 643 | if($implodeRelatedEntities){ |
| 644 | $query = "SELECT p.event_id as event_id, |
| 645 | concat_ws(' ', e.title, concat_ws(' - ', DATE_FORMAT(e.start_date, '%b %d %Y %h:%i %p'), DATE_FORMAT(e.end_date, '%b %d %Y %h:%i %p'))) as event_data, |
| 646 | count(p.id) as participant_count |
| 647 | FROM civicrm_participant p, civicrm_event e |
| 648 | WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities}) |
| 649 | GROUP BY p.event_id"; |
| 650 | $dao = CRM_Core_DAO::executeQuery($query); |
| 651 | $participantDetails = array(); |
| 652 | while($dao->fetch()) { |
| 653 | $participantDetails['countByID'][$dao->event_id] = $dao->participant_count; |
| 654 | $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | return $participantDetails; |
| 659 | } |
| 660 | |
| 661 | static function testActivityGeneration() { |
| 662 | //Activity set initial params |
| 663 | $daoActivity = new CRM_Activity_DAO_Activity(); |
| 664 | $daoActivity->activity_type_id = 1; |
| 665 | $daoActivity->subject = "Initial Activity"; |
| 666 | $daoActivity->activity_date_time = date('YmdHis'); |
| 667 | $daoActivity->save(); |
| 668 | |
| 669 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
| 670 | $recursion->entity_id = $daoActivity->id; |
| 671 | $recursion->entity_table = 'civicrm_activity'; |
| 672 | $recursion->dateColumns = array('activity_date_time'); |
| 673 | $recursion->scheduleDBParams = array( |
| 674 | 'entity_value' => $daoActivity->id, |
| 675 | 'entity_status' => $daoActivity->activity_date_time, |
| 676 | 'start_action_date' => 'fourth saturday', |
| 677 | 'repetition_frequency_unit' => 'month', |
| 678 | 'repetition_frequency_interval' => 3, |
| 679 | 'start_action_offset' => 5, |
| 680 | 'used_for' => 'activity' |
| 681 | ); |
| 682 | |
| 683 | $generatedEntities = $recursion->generate(); |
| 684 | |
| 685 | // try changing something |
| 686 | $recursion->mode(3); // sets ->mode var & saves in DB |
| 687 | |
| 688 | // lets change subject of initial activity that we created in begining |
| 689 | $daoActivity->find(TRUE); |
| 690 | $daoActivity->subject = 'I changed it'; |
| 691 | $daoActivity->save(); |
| 692 | } |
| 693 | |
| 694 | static function testEventGeneration(){ |
| 695 | //Event set initial params |
| 696 | $daoEvent = new CRM_Event_DAO_Event(); |
| 697 | $daoEvent->title = 'Test event for Recurring Entity'; |
| 698 | $daoEvent->event_type_id = 3; |
| 699 | $daoEvent->is_public = 1; |
| 700 | $daoEvent->start_date = date('YmdHis', strtotime('2014-09-24 10:30:00')); |
| 701 | $daoEvent->end_date = date('YmdHis', strtotime('2014-09-26 10:30:00')); |
| 702 | $daoEvent->created_date = date('YmdHis'); |
| 703 | $daoEvent->is_active = 1; |
| 704 | $daoEvent->save(); |
| 705 | |
| 706 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
| 707 | $recursion->entity_id = $daoEvent->id; |
| 708 | $recursion->entity_table = 'civicrm_event'; |
| 709 | $recursion->dateColumns = array('start_date'); |
| 710 | $recursion->scheduleDBParams = array ( |
| 711 | 'entity_value' => $daoEvent->id, |
| 712 | 'entity_status' => $daoEvent->start_date, |
| 713 | 'start_action_condition' => 'wednesday', |
| 714 | 'repetition_frequency_unit' => 'week', |
| 715 | 'repetition_frequency_interval' => 1, |
| 716 | 'start_action_offset' => 4, |
| 717 | 'used_for' => 'event' |
| 718 | ); |
| 719 | |
| 720 | $interval = $recursion->getInterval($daoEvent->start_date, $daoEvent->end_date); |
| 721 | $recursion->intervalDateColumns = array('end_date' => $interval); |
| 722 | |
| 723 | $recursion->excludeDates = array(date('Ymd', strtotime('2014-10-02')), '20141008');// = array('date1', date2, date2) |
| 724 | $recursion->excludeDateRangeColumns = array('start_date', 'end_date'); |
| 725 | |
| 726 | $generatedEntities = $recursion->generate(); |
| 727 | CRM_Core_Error::debug_var('$generatedEntities', $generatedEntities); |
| 728 | |
| 729 | // try changing something |
| 730 | $recursion->mode(3); // sets ->mode var & saves in DB |
| 731 | |
| 732 | $daoEvent->find(TRUE); |
| 733 | $daoEvent->title = 'I changed event'; |
| 734 | $daoEvent->save(); |
| 735 | } |
| 736 | } |