Merge branch 'angular-tests' of https://github.com/giant-rabbit/civicrm-core into...
[civicrm-core.git] / CRM / Core / BAO / ActionSchedule.php
old mode 100755 (executable)
new mode 100644 (file)
index 54919f0..6c6095c
@@ -1,7 +1,7 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.5                                                |
+  | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
   | Copyright (C) 2011 Marty Wright                                    |
   | Licensed to CiviCRM under the Academic Free License version 3.0.   |
 class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
 
   /**
-   * @param null $id
+   * @param int $id
    *
    * @return array
    */
-  static function getMapping($id = NULL) {
+  public static function getMapping($id = NULL) {
     static $_action_mapping;
 
     if ($id && !is_null($_action_mapping) && isset($_action_mapping[$id])) {
@@ -72,7 +72,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
    * Get all fields of the type Date
    */
 
-  static function getDateFields() {
+  public static function getDateFields() {
     $allFields = CRM_Core_BAO_CustomField::getFields('');
     $dateFields = array('birth_date' => ts('Birth Date'));
     foreach ($allFields as $fieldID => $field) {
@@ -90,9 +90,8 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
    *
    * @return array  associated array of all the drop downs in the form
    * @static
-   * @access public
    */
-  static function getSelection($id = NULL) {
+  public static function getSelection($id = NULL) {
     $mapping = self::getMapping();
     $activityStatus = CRM_Core_PseudoConstant::activityStatus();
     $activityType   = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
@@ -112,7 +111,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
     $sel1 = $sel2 = $sel3 = $sel4 = $sel5 = array();
     $options = array(
       'manual' => ts('Choose Recipient(s)'),
-      'group' => ts('Select Group'),
+      'group' => ts('Select Group'),
     );
 
     $entityMapping = array();
@@ -262,16 +261,16 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
   }
 
   /**
-   * @param null $id
+   * @param int $id
    *
    * @return array
    */
-  static function getSelection1($id = NULL) {
+  public static function getSelection1($id = NULL) {
     $mapping = self::getMapping($id);
     $sel4 = $sel5 = array();
     $options = array(
       'manual' => ts('Choose Recipient(s)'),
-      'group' => ts('Select Group'),
+      'group' => ts('Select Group'),
     );
 
     $recipientMapping = array_combine(array_keys($options), array_keys($options));
@@ -319,13 +318,12 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
    * @param bool $namesOnly return simple list of names
    *
    * @param null $entityValue
-   * @param null $id
+   * @param int $id
    *
    * @return array  (reference)   reminder list
    * @static
-   * @access public
    */
-  static function &getList($namesOnly = FALSE, $entityValue = NULL, $id = NULL) {
+  public static function &getList($namesOnly = FALSE, $entityValue = NULL, $id = NULL) {
     $activity_type = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
     $activity_status = CRM_Core_PseudoConstant::activityStatus();
 
@@ -414,16 +412,16 @@ AND   cas.entity_value = $id AND
   }
 
   /**
-   * @param $contactId
+   * @param int $contactId
    * @param $to
-   * @param $scheduleID
+   * @param int $scheduleID
    * @param $from
-   * @param $tokenParams
+   * @param array $tokenParams
    *
    * @return bool|null
    * @throws CRM_Core_Exception
    */
-  static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams) {
+  public static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams) {
     $email = $to['email'];
     $phoneNumber = $to['phone'];
     $schedule = new CRM_Core_DAO_ActionSchedule();
@@ -539,7 +537,6 @@ AND   cas.entity_value = $id AND
 
         CRM_Activity_BAO_Activity::sendSMSMessage($contactId,
           $sms_text,
-          $html,
           $smsParams,
           $activity->id,
           $userID
@@ -579,17 +576,16 @@ AND   cas.entity_value = $id AND
   }
 
   /**
-   * Function to add the schedules reminders in the db
+   * Add the schedules reminders in the db
    *
    * @param array $params (reference ) an assoc array of name/value pairs
    * @param array $ids    the array that holds all the db ids
    *
-   * @return object CRM_Core_DAO_ActionSchedule
-   * @access public
+   * @return CRM_Core_DAO_ActionSchedule
    * @static
    *
    */
-  static function add(&$params, $ids = array()) {
+  public static function add(&$params, $ids = array()) {
     $actionSchedule = new CRM_Core_DAO_ActionSchedule();
     $actionSchedule->copyValues($params);
 
@@ -604,11 +600,10 @@ AND   cas.entity_value = $id AND
    * @param array $params   (reference ) an assoc array of name/value pairs
    * @param array $values (reference ) an assoc array to hold the flattened values
    *
-   * @return object CRM_Core_DAO_ActionSchedule object on success, null otherwise
-   * @access public
+   * @return CRM_Core_DAO_ActionSchedule object on success, null otherwise
    * @static
    */
-  static function retrieve(&$params, &$values) {
+  public static function retrieve(&$params, &$values) {
     if (empty($params)) {
       return NULL;
     }
@@ -627,14 +622,13 @@ AND   cas.entity_value = $id AND
   }
 
   /**
-   * Function to delete a Reminder
+   * Delete a Reminder
    *
    * @param  int  $id     ID of the Reminder to be deleted.
    *
-   * @access public
    * @static
    */
-  static function del($id) {
+  public static function del($id) {
     if ($id) {
       $dao = new CRM_Core_DAO_ActionSchedule();
       $dao->id = $id;
@@ -647,7 +641,7 @@ AND   cas.entity_value = $id AND
   }
 
   /**
-   * update the is_active flag in the db
+   * Update the is_active flag in the db
    *
    * @param int      $id        id of the database record
    * @param boolean  $is_active value we want to set the is_active field
@@ -655,17 +649,17 @@ AND   cas.entity_value = $id AND
    * @return Object             DAO object on success, null otherwise
    * @static
    */
-  static function setIsActive($id, $is_active) {
+  public static function setIsActive($id, $is_active) {
     return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_ActionSchedule', $id, 'is_active', $is_active);
   }
 
   /**
-   * @param $mappingID
+   * @param int $mappingID
    * @param $now
    *
    * @throws CRM_Core_Exception
    */
-  static function sendMailings($mappingID, $now) {
+  public static function sendMailings($mappingID, $now) {
     $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
     $fromEmailAddress = "$domainValues[0] <$domainValues[1]>";
 
@@ -684,10 +678,11 @@ AND   cas.entity_value = $id AND
     while ($actionSchedule->fetch()) {
       $extraSelect = $extraJoin = $extraWhere = $extraOn = '';
 
-    if ($actionSchedule->from_email)
-            $fromEmailAddress = "$actionSchedule->from_name <$actionSchedule->from_email>";
-
+      if ($actionSchedule->from_email)
+        $fromEmailAddress = "$actionSchedule->from_name <$actionSchedule->from_email>";
 
+      $activityTypeID = false;
+      $activityStatusID = false;
       if ($actionSchedule->record_activity) {
         if ($mapping->entity == 'civicrm_membership') {
           $activityTypeID =
@@ -703,6 +698,7 @@ AND   cas.entity_value = $id AND
       }
 
       if ($mapping->entity == 'civicrm_activity') {
+        $compInfo = CRM_Core_Component::getEnabledComponents();
         $tokenEntity = 'activity';
         $tokenFields = array('activity_id', 'activity_type', 'subject', 'details', 'activity_date_time');
         $extraSelect = ', ov.label as activity_type, e.id as activity_id';
@@ -715,6 +711,14 @@ INNER JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.optio
 LEFT JOIN civicrm_option_group og ON og.name = 'activity_type'
 LEFT JOIN civicrm_option_value ov ON e.activity_type_id = ov.value AND ov.option_group_id = og.id";
         }
+
+        //join for caseId
+        // if CiviCase component is enabled
+        if (array_key_exists('CiviCase', $compInfo)) {
+          $extraSelect .= ", civicrm_case_activity.case_id as case_id";
+          $extraJoin .= "
+            LEFT JOIN `civicrm_case_activity` ON `e`.`id` = `civicrm_case_activity`.`activity_id`";
+        }
       }
 
       if ($mapping->entity == 'civicrm_participant') {
@@ -874,7 +878,7 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
         CRM_Core_BAO_ActionLog::create($logParams);
 
         // insert activity log record if needed
-        if ($actionSchedule->record_activity) {
+        if ($actionSchedule->record_activity && !$isError) {
           $activityParams = array(
             'subject' => $actionSchedule->title,
             'details' => $actionSchedule->body_html,
@@ -887,6 +891,14 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
             'source_record_id' => $dao->entityID,
           );
           $activity = CRM_Activity_BAO_Activity::create($activityParams);
+          
+          //file reminder on case if source activity is a case activity
+          if (!empty($dao->case_id)) {
+            $caseActivityParams = array();
+            $caseActivityParams['case_id'] = $dao->case_id;
+            $caseActivityParams['activity_id'] = $activity->id;
+            CRM_Case_BAO_Case::processCaseActivity($caseActivityParams);
+          }
         }
       }
 
@@ -895,13 +907,13 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
   }
 
   /**
-   * @param $mappingID
+   * @param int $mappingID
    * @param $now
    * @param array $params
    *
    * @throws API_Exception
    */
-  static function buildRecipientContacts($mappingID, $now, $params = array()) {
+  public static function buildRecipientContacts($mappingID, $now, $params = array()) {
     $actionSchedule = new CRM_Core_DAO_ActionSchedule();
     $actionSchedule->mapping_id = $mappingID;
     $actionSchedule->is_active = 1;
@@ -934,7 +946,12 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL
       $anniversary = false;
 
       if (!CRM_Utils_System::isNull($mapping->entity_recipient)) {
-        $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name');
+        if ($mapping->entity_recipient == 'event_contacts') {
+          $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name', TRUE, FALSE, 'name');
+        }
+        else {
+          $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name');
+        }
       }
       $from = "{$mapping->entity} e";
 
@@ -1210,12 +1227,8 @@ LEFT JOIN {$reminderJoinClause}
 {$whereClause} {$limitWhereClause} AND {$dateClause} {$notINClause}
 ";
       CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer')));
-
-      if (!is_null($limitTo) && $limitTo == 0 && (!empty($addGroup) || !empty($addWhere))) {
-        $additionWhere = ' WHERE ';
-        if ($actionSchedule->start_action_date) {
-          $additionWhere = $whereClause . ' AND ';
-        }
+      $isSendToAdditionalContacts = (!is_null($limitTo) && $limitTo == 0 && (!empty($addGroup) || !empty($addWhere))) ? TRUE : FALSE;
+      if ($isSendToAdditionalContacts) {
         $contactTable = "civicrm_contact c";
         $addSelect  = "SELECT c.id as contact_id, c.id as entity_id, 'civicrm_contact' as entity_table, {$actionSchedule->id} as action_schedule_id";
         $additionReminderClause = "civicrm_action_log reminder ON reminder.contact_id = c.id AND
@@ -1299,7 +1312,7 @@ INNER JOIN {$reminderJoinClause}
           CRM_Core_DAO::executeQuery($query, array(1 => array($actionSchedule->id, 'Integer')));
         }
 
-        if (!is_null($limitTo) && $limitTo == 0) {
+        if ($isSendToAdditionalContacts) {
           $addSelect .= ', MAX(reminder.action_date_time) as latest_log_time';
           $sqlEndEventCheck = "
 SELECT * FROM {$table}
@@ -1339,7 +1352,7 @@ GROUP BY reminder.contact_id
    *
    * @return null|string
    */
-  static function permissionedRelationships($field) {
+  public static function permissionedRelationships($field) {
     $query = '
 SELECT    cm.id AS owner_id, cm.contact_id AS owner_contact, m.id AS slave_id, m.contact_id AS slave_contact, cmt.relationship_type_id AS relation_type, rel.contact_id_a, rel.contact_id_b, rel.is_permission_a_b, rel.is_permission_b_a
 FROM      civicrm_membership m
@@ -1375,7 +1388,7 @@ WHERE     m.owner_membership_id IS NOT NULL AND
    *
    * @return array
    */
-  static function processQueue($now = NULL, $params = array()) {
+  public static function processQueue($now = NULL, $params = array()) {
     $now = $now ? CRM_Utils_Time::setTime($now) : CRM_Utils_Time::getTime();
 
     $mappings = self::getMapping();
@@ -1392,12 +1405,12 @@ WHERE     m.owner_membership_id IS NOT NULL AND
   }
 
   /**
-   * @param $id
-   * @param $mappingID
+   * @param int $id
+   * @param int $mappingID
    *
    * @return null|string
    */
-  static function isConfigured($id, $mappingID) {
+  public static function isConfigured($id, $mappingID) {
     $queryString = "SELECT count(id) FROM civicrm_action_schedule
                         WHERE  mapping_id = %1 AND
                                entity_value = %2";
@@ -1410,12 +1423,12 @@ WHERE     m.owner_membership_id IS NOT NULL AND
   }
 
   /**
-   * @param $mappingID
+   * @param int $mappingID
    * @param $recipientType
    *
    * @return array
    */
-  static function getRecipientListing($mappingID, $recipientType) {
+  public static function getRecipientListing($mappingID, $recipientType) {
     $options = array();
     if (!$mappingID || !$recipientType) {
       return $options;