Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-05-17-16-30
[civicrm-core.git] / CRM / Core / BAO / ActionSchedule.php
index ea000abf1111d0a0e13c7e2d14c5a4e86b753c0e..d70488f0a99607112aafa2988807745191f6ab5a 100755 (executable)
@@ -649,7 +649,7 @@ LEFT JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option
 
       if ($mapping->entity == 'civicrm_participant') {
         $tokenEntity = 'event';
-        $tokenFields = array('event_type', 'title', 'event_id', 'start_date', 'end_date', 'summary', 'description', 'location', 'info_url', 'registration_url', 'fee_amount', 'contact_email', 'contact_phone');
+        $tokenFields = array('event_type', 'title', 'event_id', 'start_date', 'end_date', 'summary', 'description', 'location', 'info_url', 'registration_url', 'fee_amount', 'contact_email', 'contact_phone', 'balance');
         $extraSelect = ', ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone ';
 
         $extraJoin   = "
@@ -729,6 +729,10 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
           elseif (in_array($field, array('start_date','end_date','join_date','activity_date_time'))) {
             $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Date::customFormat($dao->$field);
           }
+          elseif ($field == 'balance') {
+            $info = CRM_Contribute_BAO_Contribution::getPaymentInfo($dao->entityID, 'event');
+            $entityTokenParams["{$tokenEntity}." . $field] = CRM_Utils_Array::value('balance', $info);
+          }
           else {
             $entityTokenParams["{$tokenEntity}." . $field] = $dao->$field;
           }
@@ -803,10 +807,13 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
     }
   }
 
-  static function buildRecipientContacts($mappingID, $now) {
+  static function buildRecipientContacts($mappingID, $now, $params = array()) {
     $actionSchedule = new CRM_Core_DAO_ActionSchedule();
     $actionSchedule->mapping_id = $mappingID;
     $actionSchedule->is_active = 1;
+    if(!empty($params)) {
+      _civicrm_api3_dao_set_filter($actionSchedule, $params, FALSE, 'ActionSchedule');
+    }
     $actionSchedule->find();
 
     while ($actionSchedule->fetch()) {
@@ -1107,6 +1114,12 @@ GROUP BY c.id
         elseif ($actionSchedule->repetition_frequency_unit == 'week') {
           $hrs = 24 * $actionSchedule->repetition_frequency_interval * 7;
         }
+        elseif ($actionSchedule->repetition_frequency_unit == 'month') {
+          $hrs = "24*(DATEDIFF(DATE_ADD(latest_log_time, INTERVAL 1 MONTH ), latest_log_time))";
+        }
+        elseif ($actionSchedule->repetition_frequency_unit == 'year') {
+          $hrs = "24*(DATEDIFF(DATE_ADD(latest_log_time, INTERVAL 1 YEAR ), latest_log_time))";
+        }
         else {
           $hrs = $actionSchedule->repetition_frequency_interval;
         }
@@ -1205,12 +1218,12 @@ WHERE     m.owner_membership_id IS NOT NULL AND
     return NULL;
   }
 
-  static function processQueue($now = NULL) {
+  static function processQueue($now = NULL, $params = array()) {
     $now = $now ? CRM_Utils_Time::setTime($now) : CRM_Utils_Time::getTime();
 
     $mappings = self::getMapping();
     foreach ($mappings as $mappingID => $mapping) {
-      self::buildRecipientContacts($mappingID, $now);
+      self::buildRecipientContacts($mappingID, $now, $params);
       self::sendMailings($mappingID, $now);
     }