Merge pull request #1683 from pradpnayak/CRM-12970
[civicrm-core.git] / CRM / Activity / BAO / Activity.php
index 3591285da50087fb961dde11540bf8524b02e0ca..80ef94a95e53a5db815ad139275f579ec7adbd48 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -688,13 +688,20 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
     $sql = "CREATE TEMPORARY TABLE {$activityTempTable} ( ";
     $insertValueSQL = array();
+    // The activityTempTable contains the sorted rows
+    // so in order to maintain the sort order as-is we add an auto_increment
+    // field; we can sort by this later to ensure the sort order stays correct.
+    $sql .= " fixed_sort_order INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,";
     foreach ($tableFields as $name => $desc) {
       $sql .= "$name $desc,\n";
       $insertValueSQL[] = $name;
     }
 
+    // add unique key on activity_id just to be sure
+    // this cannot be primary key because we need that for the auto_increment
+    // fixed_sort_order field
     $sql .= "
-          PRIMARY KEY ( activity_id )
+          UNIQUE KEY ( activity_id )
         ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
         ";
 
@@ -717,6 +724,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     }
 
     if (empty($order)) {
+      // context = 'activity' in Activities tab.
       $order = (CRM_Utils_Array::value('context', $input) == 'activity') ? " ORDER BY tbl.activity_date_time desc " : " ORDER BY tbl.status_id asc, tbl.activity_date_time asc ";
     }
 
@@ -770,6 +778,7 @@ WHERE      c.is_deleted = 0
     CRM_Core_DAO::executeQuery($query);
 
     // step 3: Combine all temp tables to get final query for activity selector
+    // sort by the original sort order, stored in fixed_sort_order
     $query = "
 SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.contact_id,
@@ -777,6 +786,7 @@ SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.contact_name
 FROM       {$activityTempTable}
 INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$activityContactTempTable}.activity_id
+ORDER BY    fixed_sort_order
         ";
 
 
@@ -1139,6 +1149,7 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
    * @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
    *
    * @return array               ( sent, activityId) if any email is sent and activityId
    * @access public
@@ -1155,7 +1166,8 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
     $attachments = NULL,
     $cc          = NULL,
     $bcc         = NULL,
-    $contactIds // FIXME a param with no default shouldn't be last
+    $contactIds, // FIXME a param with no default shouldn't be last
+    $additionalDetails = NULL
   ) {
     // get the contact details of logged in contact, which we set as from email
     if ($userID == NULL) {
@@ -1190,10 +1202,11 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
 
     // 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";
+      $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n";
     }
     else {
       $details = $html ? $html : $text;
+      $details .= $additionalDetails;
     }
 
     $activityParams = array(
@@ -1778,7 +1791,7 @@ SELECT  display_name
       $component = 'Membership';
     }
     elseif ($activity->__table == 'civicrm_participant') {
-      $event = CRM_Event_BAO_Event::getEvents(TRUE, $activity->event_id, TRUE, FALSE);
+      $event = CRM_Event_BAO_Event::getEvents(1, $activity->event_id, TRUE, FALSE);
 
       $roles = CRM_Event_PseudoConstant::participantRole();
       $status = CRM_Event_PseudoConstant::participantStatus();