Merge pull request #4004 from civicrm/4.4
[civicrm-core.git] / CRM / Activity / BAO / Activity.php
index a3b9d0642777f8be292e6b6c41ac3e171cbe7b4c..243fcd0ac6ba034785b4a072fc3a7cd48fe2b926 100644 (file)
@@ -542,7 +542,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
         );
       }
       else {
-        $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={" . CRM_Utils_Array::value('source_contact_id', $params) . "}&context=home";
+        $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home";
         if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
           $url = CRM_Utils_System::url('civicrm/activity', $q);
           if ($activity->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Print PDF Letter', 'name')) {
@@ -566,7 +566,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
           $url = CRM_Utils_System::url('civicrm/activity/view', $q);
           if (CRM_Core_Permission::check('delete activities')) {
             $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity',
-              "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={" . CRM_Utils_Array::value('source_contact_id', $params) . "}&context=home"
+              "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"
             );
           }
         }
@@ -630,6 +630,12 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     return $result;
   }
 
+  /**
+   * @param $activity
+   * @param null $logMessage
+   *
+   * @return bool
+   */
   public static function logActivityAction($activity, $logMessage = NULL) {
     $session = CRM_Core_Session::singleton();
     $id = $session->get('userID');
@@ -676,6 +682,11 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
       'name'
     );
 
+    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
+    $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
+    $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
+    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
+
     $config = CRM_Core_Config::singleton();
 
     $randomNum = md5(uniqid());
@@ -746,9 +757,6 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     }
 
     $input['count'] = FALSE;
-
-    // skip bulk activities in activity tab
-    $input['activity_type_exclude_id'][$bulkActivityTypeID] = $bulkActivityTypeID;
     list($sqlClause, $params) = self::getActivitySQLClause($input);
 
     $query = "{$insertSQL}
@@ -772,7 +780,7 @@ LEFT JOIN  civicrm_case_activity ON ( civicrm_case_activity.activity_id = tbl.ac
     $activityContactTempTable = "civicrm_temp_activity_contact_{$randomNum}";
     $query = "CREATE TEMPORARY TABLE {$activityContactTempTable} (
                 activity_id int unsigned, contact_id int unsigned, record_type_id varchar(16),
-                 contact_name varchar(255), is_deleted int unsigned, INDEX index_activity_id( activity_id ) )
+                 contact_name varchar(255), is_deleted int unsigned, counter int unsigned, INDEX index_activity_id( activity_id ) )
                 ENGINE=MYISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
 
     CRM_Core_DAO::executeQuery($query);
@@ -786,11 +794,33 @@ SELECT     ac.activity_id,
            c.sort_name,
            c.is_deleted
 FROM       {$activityTempTable}
-INNER JOIN civicrm_activity a ON ( a.id = {$activityTempTable}.activity_id  )
+INNER JOIN civicrm_activity a ON ( a.id = {$activityTempTable}.activity_id )
 INNER JOIN civicrm_activity_contact ac ON ( ac.activity_id = {$activityTempTable}.activity_id )
 INNER JOIN civicrm_contact c ON c.id = ac.contact_id
+WHERE ac.record_type_id != %1
 ";
-    CRM_Core_DAO::executeQuery($query);
+    $params = array(1 => array($targetID, 'Integer'));
+    CRM_Core_DAO::executeQuery($query, $params);
+
+    // for each activity insert one target contact
+    // if we load all target contacts the performance will suffer a lot for mass-activities;
+    $query = "
+INSERT INTO {$activityContactTempTable} ( activity_id, contact_id, record_type_id, contact_name, is_deleted, counter )
+SELECT     ac.activity_id,
+           ac.contact_id,
+           ac.record_type_id,
+           c.sort_name,
+           c.is_deleted,
+           count(ac.contact_id)
+FROM       {$activityTempTable}
+INNER JOIN civicrm_activity a ON ( a.id = {$activityTempTable}.activity_id )
+INNER JOIN civicrm_activity_contact ac ON ( ac.activity_id = {$activityTempTable}.activity_id )
+INNER JOIN civicrm_contact c ON c.id = ac.contact_id
+WHERE ac.record_type_id = %1
+GROUP BY ac.activity_id
+";
+
+    CRM_Core_DAO::executeQuery($query, $params);
 
     // step 3: Combine all temp tables to get final query for activity selector
     // sort by the original sort order, stored in fixed_sort_order
@@ -799,13 +829,13 @@ SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.contact_id,
            {$activityContactTempTable}.record_type_id,
            {$activityContactTempTable}.contact_name,
-           {$activityContactTempTable}.is_deleted
+           {$activityContactTempTable}.is_deleted,
+           {$activityContactTempTable}.counter
 FROM       {$activityTempTable}
 INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$activityContactTempTable}.activity_id
 ORDER BY    fixed_sort_order
         ";
 
-
     $dao = CRM_Core_DAO::executeQuery($query);
 
     //CRM-3553, need to check user has access to target groups.
@@ -818,14 +848,7 @@ ORDER BY    fixed_sort_order
 
     //get all campaigns.
     $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
-
     $values = array();
-    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
-    $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
-    $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
-    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
-
-
     while ($dao->fetch()) {
       $activityID = $dao->activity_id;
       $values[$activityID]['activity_id'] = $dao->activity_id;
@@ -847,6 +870,7 @@ ORDER BY    fixed_sort_order
 
       if (empty($values[$activityID]['target_contact_name'])) {
         $values[$activityID]['target_contact_name'] = array();
+        $values[$activityID]['target_contact_counter'] = $dao->counter;
       }
 
       // if deleted, wrap in <del>
@@ -873,7 +897,7 @@ ORDER BY    fixed_sort_order
         $values[$activityID]['case_subject'] = $dao->case_subject;
       }
       else {
-        $values[$activityID]['recipients'] = ts('(recipients)');
+        $values[$activityID]['recipients'] =  ts('(%1 recipients)', array(1 => $dao->counter));
         $values[$activityID]['mailingId'] = false;
         if (
           $accessCiviMail &&
@@ -937,14 +961,6 @@ ORDER BY    fixed_sort_order
    * @static
    */
   static function &getActivitiesCount($input) {
-    // skip bulk activities in activity tab
-    $bulkActivityTypeID = CRM_Core_OptionGroup::getValue(
-      'activity_type',
-      'Bulk Email',
-      'name'
-    );
-    $input['activity_type_exclude_id'][$bulkActivityTypeID] = $bulkActivityTypeID;
-
     $input['count'] = TRUE;
     list($sqlClause, $params) = self::getActivitySQLClause($input);
 
@@ -1148,18 +1164,20 @@ 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
    * contact activity in each contacts record
    *
-   * @param array  $contactDetails the array of contact details to send the email
-   * @param string $subject      the subject of the message
-   * @param string $message      the message contents
+   * @param array $contactDetails the array of contact details to send the email
+   * @param string $subject the subject of the message
+   * @param $text
+   * @param $html
    * @param string $emailAddress use this 'to' email address instead of the default Primary address
-   * @param int    $userID       use this userID if set
+   * @param int $userID use this userID if set
    * @param string $from
-   * @param array  $attachments  the array of attachments if any
-   * @param string $cc           cc recipient
-   * @param string $bcc          bcc recipient
-   * @param array $contactIds    contact ids
+   * @param array $attachments the array of attachments if any
+   * @param string $cc cc recipient
+   * @param string $bcc bcc recipient
+   * @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
@@ -1347,6 +1365,16 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
     return array($sent, $activity->id);
   }
 
+  /**
+   * @param $contactDetails
+   * @param $activityParams
+   * @param array $smsParams
+   * @param $contactIds
+   * @param null $userID
+   *
+   * @return array
+   * @throws CRM_Core_Exception
+   */
   static function sendSMS(&$contactDetails,
     &$activityParams,
     &$smsParams = array(),
@@ -1483,9 +1511,13 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   /**
    * send the sms message to a specific contact
    *
-   * @param int    $toID              the contact id of the recipient
-   * @param int    $activityID        the activity ID that tracks the message
-   * @param array  $smsParams         the params used for sending sms
+   * @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
    *
    * @return mixed                    true on success or PEAR_Error object
    * @access public
@@ -1555,13 +1587,20 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
   /**
    * send the message to a specific contact
    *
-   * @param string $from         the name and email of the sender
-   * @param int    $toID         the contact id of the recipient
-   * @param string $subject      the subject of the message
-   * @param string $message      the message contents
+   * @param string $from the name and email of the sender
+   * @param $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 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
@@ -1632,7 +1671,9 @@ LEFT JOIN civicrm_activity_contact src ON (src.activity_id = ac.activity_id AND
    * scheme. Adding weight is super important and should be done in the
    * next week or so, before this can be called complete.
    *
-   * @param NULL
+   * @param bool $status
+   *
+   * @internal param $NULL
    *
    * @return array    array of importable Fields
    * @access public
@@ -1863,6 +1904,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;
@@ -1903,7 +1948,9 @@ SELECT  display_name
   /**
    * Function to get total count of prior revision of currently viewd activity
    *
-   * @param int  $activityId   current activity id
+   * @param $activityID
+   *
+   * @internal param int $activityId current activity id
    *
    * @return int $params  count of prior activities otherwise false.
    * @access public
@@ -1941,7 +1988,10 @@ AND id < {$activityID}
    * Function to get all prior activities of currently viewe
    * d activity
    *
-   * @param int  $activityId   current activity id
+   * @param $activityID
+   * @param bool $onlyPriorRevisions
+   *
+   * @internal param int $activityId current activity id
    *
    * @return array $result  prior activities info.
    * @access public
@@ -1990,7 +2040,9 @@ AND cl.modified_id  = c.id
   /**
    * Function to find the latest revision of a given activity
    *
-   * @param int  $activityId    prior activity id
+   * @param $activityID
+   *
+   * @internal param int $activityId prior activity id
    *
    * @return int $params  current activity id.
    * @access public
@@ -2024,7 +2076,11 @@ AND cl.modified_id  = c.id
    *
    * @activityId int activity id of parent activity
    *
-   * @param array  $activity details
+   * @param $activityId
+   * @param $params
+   *
+   * @return $this|null|object
+   * @internal param array $activity details
    *
    * @access public
    */
@@ -2061,7 +2117,9 @@ AND cl.modified_id  = c.id
   /**
    * Function to get Activity specific File according activity type Id.
    *
-   * @param int  $activityTypeId  activity id
+   * @param int $activityTypeId activity id
+   *
+   * @param string $crmDir
    *
    * @return if file exists returns $activityTypeFile activity filename otherwise false.
    *
@@ -2460,28 +2518,19 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
         elseif (!empty($values['recipients'])) {
           $contactActivities[$activityId]['target_contact'] = $values['recipients'];
         }
-        elseif (!$values['target_contact_name']) {
-          $contactActivities[$activityId]['target_contact'] = '<em>n/a</em>';
-        }
-        elseif (!empty($values['target_contact_name'])) {
-          $count = 0;
-          $contactActivities[$activityId]['target_contact'] = '';
+        elseif (isset($values['target_contact_counter']) && $values['target_contact_counter']) {
           foreach ($values['target_contact_name'] as $tcID => $tcName) {
-            if ($tcID && $count < 5) {
-              $contactActivities[$activityId]['target_contact'] .= CRM_Utils_System::href($tcName,
-                'civicrm/contact/view', "reset=1&cid={$tcID}");
-              $count++;
-              if ($count) {
-                $contactActivities[$activityId]['target_contact'] .= ";&nbsp;";
-              }
+            $contactActivities[$activityId]['target_contact'] .= CRM_Utils_System::href($tcName,
+              'civicrm/contact/view', "reset=1&cid={$tcID}");
+          }
 
-              if ($count == 4) {
-                $contactActivities[$activityId]['target_contact'] .= "(" . ts('more') . ")";
-                break;
-              }
-            }
+          if ($extraCount = $values['target_contact_counter'] - 1) {
+            $contactActivities[$activityId]['target_contact'] .= ";<br />" . "(" . ts('%1 more', array(1 => $extraCount)) . ")";
           }
         }
+        elseif (!$values['target_contact_name']) {
+          $contactActivities[$activityId]['target_contact'] = '<em>n/a</em>';
+        }
 
         if (empty($values['assignee_contact_name'])) {
           $contactActivities[$activityId]['assignee_contact'] = '<em>n/a</em>';
@@ -2559,6 +2608,9 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
    * Used to copy custom fields and attachments from an existing activity to another.
    * see CRM_Case_Page_AJAX::_convertToCaseActivity() for example
    */
+  /**
+   * @param $params
+   */
   static function copyExtendedActivityData($params) {
     // attach custom data to the new activity
     $customParams = $htmlType = array();
@@ -2596,6 +2648,13 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
     CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $params['activityID'], 'civicrm_activity', $params['mainActivityId']);
   }
 
+  /**
+   * @param $activityId
+   * @param null $recordTypeID
+   * @param string $column
+   *
+   * @return null
+   */
   public static function getActivityContact($activityId, $recordTypeID = NULL, $column = 'contact_id') {
     $activityContact = new CRM_Activity_BAO_ActivityContact();
     $activityContact->activity_id = $activityId;
@@ -2608,6 +2667,11 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
     return NULL;
   }
 
+  /**
+   * @param $activityId
+   *
+   * @return null
+   */
   public static function getSourceContactID($activityId) {
     static $sourceID = NULL;
     if (!$sourceID) {
@@ -2618,6 +2682,9 @@ INNER JOIN  civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.n
     return self::getActivityContact($activityId, $sourceID);
   }
 
+  /**
+   * @param $params
+   */
   function setApiFilter(&$params) {
     if (CRM_Utils_Array::value('target_contact_id', $params)) {
       $this->selectAdd();