Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / CRM / Activity / BAO / Activity.php
index 53f9e19e2d426bf01161c5bc1a8b967c2d41307e..efb8f57d1e37de28458c5c98b7d895c8a91c8a8c 100644 (file)
@@ -40,7 +40,7 @@
 class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
   /**
-   * static field for all the activity information that we can potentially export
+   * Static field for all the activity information that we can potentially export
    *
    * @var array
    * @static
@@ -48,7 +48,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   static $_exportableFields = NULL;
 
   /**
-   * static field for all the activity information that we can potentially import
+   * Static field for all the activity information that we can potentially import
    *
    * @var array
    * @static
@@ -71,18 +71,12 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   }
 
   /**
-   * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. Typically the valid params are only
-   * contact_id. We'll tweak this function to be more full featured over a period
-   * of time. This is the inverse function of create. It also stores all the retrieved
-   * values in the default array
+   * Fetch object based on array of properties
    *
    * @param array $params (reference ) an assoc array of name/value pairs
    * @param array $defaults (reference ) an assoc array to hold the flattened values
    *
-   * @internal param string $activityType activity type
-   *
-   * @return object CRM_Core_BAO_Meeting object
+   * @return CRM_Activity_DAO_Activity object
    * @access public
    */
   public static function retrieve(&$params, &$defaults) {
@@ -146,10 +140,9 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   }
 
   /**
-   * Function to delete the activity
+   * Delete the activity
    *
    * @param array $params associated array
-   *
    * @param bool $moveToTrash
    *
    * @return void
@@ -256,10 +249,8 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   /**
    * Delete activity assignment record
    *
-   * @param $activityId
-   * @param null $recordTypeID
-   *
-   * @internal param int $id activity id
+   * @param int $activityId
+   * @param int $recordTypeID
    *
    * @return null
    * @access public
@@ -274,15 +265,11 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   }
 
   /**
-   * Function to process the activities
+   * Process the activities
    *
    * @param array $params associated array of the submitted values
    *
    * @throws CRM_Core_Exception
-   * @internal param object $form form object
-   * @internal param array $ids array of ids
-   * @internal param string $activityType activity Type
-   * @internal param bool $record true if it is Record Activity
    * @access public
    *
    * @return $this|null|object
@@ -656,7 +643,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   }
 
   /**
-   * function to get the list Activities
+   * Get the list Activities
    *
    * @param array   $input            array of parameters
    *    Keys include
@@ -830,9 +817,11 @@ SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.record_type_id,
            {$activityContactTempTable}.contact_name,
            {$activityContactTempTable}.is_deleted,
-           {$activityContactTempTable}.counter
+           {$activityContactTempTable}.counter,
+           re.parent_id as is_recurring_activity
 FROM       {$activityTempTable}
 INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$activityContactTempTable}.activity_id
+LEFT JOIN civicrm_recurring_entity re on {$activityContactTempTable}.activity_id = re.entity_id
 ORDER BY    fixed_sort_order
         ";
 
@@ -859,6 +848,7 @@ ORDER BY    fixed_sort_order
       $values[$activityID]['status_id'] = $dao->status_id;
       $values[$activityID]['subject'] = $dao->subject;
       $values[$activityID]['campaign_id'] = $dao->campaign_id;
+      $values[$activityID]['is_recurring_activity'] = $dao->is_recurring_activity;
 
       if ($dao->campaign_id) {
         $values[$activityID]['campaign'] = $allCampaigns[$dao->campaign_id];
@@ -916,7 +906,7 @@ ORDER BY    fixed_sort_order
    * user has permission. To decide whether we are going to include
    * component related activities w/ core activity retrieve process.
    *
-   * return an array of component id and name.
+   * @return array of component id and name.
    * @static
    **/
   static function activityComponents() {
@@ -945,7 +935,7 @@ ORDER BY    fixed_sort_order
   }
 
   /**
-   * function to get the activity Count
+   * Get the activity Count
    *
    * @param array   $input            array of parameters
    *    Keys include
@@ -981,7 +971,7 @@ LEFT JOIN   civicrm_case_activity ON ( civicrm_case_activity.activity_id = tbl.a
   }
 
   /**
-   * function to get the activity sql clause to pick activities
+   * Get the activity sql clause to pick activities
    *
    * @param array   $input            array of parameters
    *    Keys include
@@ -1161,7 +1151,7 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   }
 
   /**
-   * send the message to all the contacts and also insert a
+   * Send the message to all the contacts and also insert a
    * contact activity in each contacts record
    *
    * @param array $contactDetails the array of contact details to send the email
@@ -1177,7 +1167,6 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
    * @param array $contactIds contact ids
    * @param string $additionalDetails the additional information of CC and BCC appended to the activity Details
    *
-   * @internal param string $message the message contents
    * @return array               ( sent, activityId) if any email is sent and activityId
    * @access public
    * @static
@@ -1366,11 +1355,11 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   }
 
   /**
-   * @param $contactDetails
-   * @param $activityParams
+   * @param array $contactDetails
+   * @param array $activityParams
    * @param array $smsParams
    * @param $contactIds
-   * @param null $userID
+   * @param int $userID
    *
    * @return array
    * @throws CRM_Core_Exception
@@ -1386,16 +1375,12 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
       $userID = $session->get('userID');
     }
 
-    $text = &$activityParams['text_message'];
-    $html = &$activityParams['html_message'];
+    $text = &$activityParams['sms_text_message'];
 
     // CRM-4575
     // token replacement of addressee/email/postal greetings
     // get the tokens added in subject and message
     $messageToken = CRM_Utils_Token::getTokens($text);
-    $messageToken = array_merge($messageToken,
-      CRM_Utils_Token::getTokens($html)
-    );
 
     //create the meta level record first ( sms activity )
     $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type',
@@ -1403,13 +1388,7 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
       'name'
     );
 
-    // CRM-6265: save both text and HTML parts in details (if present)
-    if ($html and $text) {
-      $details = "-ALTERNATIVE ITEM 0-\n$html\n-ALTERNATIVE ITEM 1-\n$text\n-ALTERNATIVE END-\n";
-    }
-    else {
-      $details = $html ? $html : $text;
-    }
+    $details = $text;
 
     $activitySubject = $activityParams['activity_subject'];
     $activityParams = array(
@@ -1465,9 +1444,6 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
       $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty);
       $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty);
 
-      $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty);
-      $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty);
-
       // Only send if the phone is of type mobile
       $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
       if ($values['phone_type_id'] == CRM_Utils_Array::value('Mobile', $phoneTypes)) {
@@ -1480,7 +1456,6 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
       $sendResult = self::sendSMSMessage(
         $contactId,
         $tokenText,
-        $tokenHtml,
         $smsParams,
         $activityID,
         $userID
@@ -1509,15 +1484,14 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   }
 
   /**
-   * send the sms message to a specific contact
+   * Send the sms message to a specific contact
    *
    * @param int $toID the contact id of the recipient
    * @param $tokenText
    * @param $tokenHtml
    * @param array $smsParams the params used for sending sms
-   *
    * @param int $activityID the activity ID that tracks the message
-   * @param null $userID
+   * @param int $userID
    *
    * @return mixed                    true on success or PEAR_Error object
    * @access public
@@ -1525,7 +1499,6 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
    */
   static function sendSMSMessage($toID,
     &$tokenText,
-    &$tokenHtml,
     $smsParams = array(),
     $activityID,
     $userID = null
@@ -1558,13 +1531,12 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
       );
     }
 
-    $message = $tokenHtml ? $tokenHtml : $tokenText;
     $recipient = $smsParams['To'];
     $smsParams['contact_id'] = $toID;
     $smsParams['parent_activity_id'] = $activityID;
 
     $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id']));
-    $sendResult = $providerObj->send($recipient, $smsParams, $message, NULL, $userID);
+    $sendResult = $providerObj->send($recipient, $smsParams, $tokenText, NULL, $userID);
     if (PEAR::isError($sendResult)) {
       return $sendResult;
     }
@@ -1585,22 +1557,20 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   }
 
   /**
-   * send the message to a specific contact
+   * Send the message to a specific contact
    *
    * @param string $from the name and email of the sender
-   * @param $fromID
+   * @param int $fromID
    * @param int $toID the contact id of the recipient
    * @param string $subject the subject of the message
    * @param $text_message
    * @param $html_message
    * @param string $emailAddress use this 'to' email address instead of the default Primary address
    * @param int $activityID the activity ID that tracks the message
-   *
    * @param null $attachments
    * @param null $cc
    * @param null $bcc
    *
-   * @internal param string $message the message contents
    * @return boolean             true if successfull else false.
    * @access public
    * @static
@@ -1665,7 +1635,7 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   }
 
   /**
-   * combine all the importable fields from the lower levels object
+   * Combine all the importable fields from the lower levels object
    *
    * The ordering is important, since currently we do not have a weight
    * scheme. Adding weight is super important and should be done in the
@@ -1673,8 +1643,6 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
    *
    * @param bool $status
    *
-   * @internal param $NULL
-   *
    * @return array    array of importable Fields
    * @access public
    * @static
@@ -1727,8 +1695,7 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   /**
    * To get the Activities of a target contact
    *
-   * @param $contactId    Integer  ContactId of the contact whose activities
-   *                               need to find
+   * @param int $contactId Id of the contact whose activities need to find
    *
    * @return array    array of activity fields
    * @access public
@@ -1799,13 +1766,11 @@ WHERE      activity.id IN ($activityIds)";
   }
 
   /**
-   * Function to add activity for Membership/Event/Contribution
+   * Add activity for Membership/Event/Contribution
    *
    * @param object $activity (reference) particular component object
    * @param string $activityType for Membership Signup or Renewal
-   *
-   *
-   * @param null $targetContactID
+   * @param int $targetContactID
    *
    * @return bool
    * @static
@@ -1839,7 +1804,7 @@ SELECT  display_name
         $subject .= " (by {$displayName})";
       }
 
-      $subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $activity->status_id);
+      $subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $activity->status_id, 'label');
       // CRM-72097 changed from start date to today
       $date = date('YmdHis');
       $component = 'Membership';
@@ -1887,7 +1852,6 @@ SELECT  display_name
       ),
       'subject' => $subject,
       'activity_date_time' => $date,
-      'details' => $activity->details,
       'is_test' => $activity->is_test,
       'status_id' => CRM_Core_OptionGroup::getValue('activity_status',
         'Completed',
@@ -1905,6 +1869,10 @@ SELECT  display_name
       $activityParams['target_contact_id'][] = $activity->contact_id;
     }
 
+    // CRM-14945
+    if (property_exists($activity, 'details')) {
+      $activityParams['details'] = $activity->details;
+    }
     //CRM-4027
     if ($targetContactID) {
       $activityParams['target_contact_id'][] = $targetContactID;
@@ -1916,7 +1884,7 @@ SELECT  display_name
   }
 
   /**
-   * Function to get Parent activity for currently viewed activity
+   * Get Parent activity for currently viewed activity
    *
    * @param int  $activityId   current activity id
    *
@@ -1943,11 +1911,9 @@ SELECT  display_name
   }
 
   /**
-   * Function to get total count of prior revision of currently viewd activity
-   *
-   * @param $activityID
+   * Get total count of prior revision of currently viewd activity
    *
-   * @internal param int $activityId current activity id
+   * @param $activityID current activity id
    *
    * @return int $params  count of prior activities otherwise false.
    * @access public
@@ -1982,14 +1948,11 @@ AND id < {$activityID}
   }
 
   /**
-   * Function to get all prior activities of currently viewe
-   * d activity
+   * Get all prior activities of currently viewed activity
    *
-   * @param $activityID
+   * @param $activityID current activity id
    * @param bool $onlyPriorRevisions
    *
-   * @internal param int $activityId current activity id
-   *
    * @return array $result  prior activities info.
    * @access public
    */
@@ -2035,13 +1998,11 @@ AND cl.modified_id  = c.id
   }
 
   /**
-   * Function to find the latest revision of a given activity
-   *
-   * @param $activityID
+   * Find the latest revision of a given activity
    *
-   * @internal param int $activityId prior activity id
+   * @param int $activityID  prior activity id
    *
-   * @return int $params  current activity id.
+   * @return int current activity id.
    * @access public
    */
   static function getLatestActivityId($activityID) {
@@ -2069,15 +2030,12 @@ AND cl.modified_id  = c.id
   }
 
   /**
-   * Function to create a follow up a given activity
-   *
-   * @activityId int activity id of parent activity
+   * Create a follow up a given activity
    *
-   * @param $activityId
-   * @param $params
+   * @param $activityId int activity id of parent activity
+   * @param array $params
    *
    * @return $this|null|object
-   * @internal param array $activity details
    *
    * @access public
    */
@@ -2112,13 +2070,12 @@ AND cl.modified_id  = c.id
   }
 
   /**
-   * Function to get Activity specific File according activity type Id.
+   * Get Activity specific File according activity type Id.
    *
    * @param int $activityTypeId activity id
-   *
    * @param string $crmDir
    *
-   * @return if file exists returns $activityTypeFile activity filename otherwise false.
+   * @return string|bool if file exists returns $activityTypeFile activity filename otherwise false.
    *
    * @static
    */
@@ -2147,7 +2104,7 @@ AND cl.modified_id  = c.id
   }
 
   /**
-   * Function to restore the activity
+   * Restore the activity
    *
    * @param array  $params  associated array
    *
@@ -2307,7 +2264,7 @@ AND cl.modified_id  = c.id
   }
 
   /**
-   * Does user has sufficient permission for view/edit activity record.
+   * Does user has sufficient permission for view/edit activity record?
    *
    * @param  int   $activityId activity record id.
    * @param  int   $action     edit/view
@@ -2595,18 +2552,19 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
           'Activity',
           $values['activity_id']
         );
+
+        $contactActivities[$activityId]['is_recurring_activity'] = $values['is_recurring_activity'];
       }
     }
 
     return $contactActivities;
   }
 
-  /*
-   * Used to copy custom fields and attachments from an existing activity to another.
-   * see CRM_Case_Page_AJAX::_convertToCaseActivity() for example
-   */
   /**
-   * @param $params
+   * Used to copy custom fields and attachments from an existing activity to another.
+   * @see CRM_Case_Page_AJAX::_convertToCaseActivity() for example
+   *
+   * @param array $params
    */
   static function copyExtendedActivityData($params) {
     // attach custom data to the new activity
@@ -2646,8 +2604,8 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
   }
 
   /**
-   * @param $activityId
-   * @param null $recordTypeID
+   * @param int $activityId
+   * @param int $recordTypeID
    * @param string $column
    *
    * @return null
@@ -2665,7 +2623,7 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
   }
 
   /**
-   * @param $activityId
+   * @param int $activityId
    *
    * @return null
    */
@@ -2680,7 +2638,7 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
   }
 
   /**
-   * @param $params
+   * @param array $params
    */
   function setApiFilter(&$params) {
     if (CRM_Utils_Array::value('target_contact_id', $params)) {