Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-06-08-21-41-06
[civicrm-core.git] / CRM / Activity / Page / AJAX.php
index 6f657edeeeb7a1f7ef77ff1d77328d528bddf2be..fccec558853a6004f503792e9dc32ee3586b1a35 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  *
  */
 
@@ -42,25 +42,19 @@ class CRM_Activity_Page_AJAX {
     $userID = CRM_Utils_Type::escape($_GET['userID'], 'Integer');
     $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
 
-    $sortMapper = array(
-      0 => 'display_date',
-      1 => 'ca.subject',
-      2 => 'ca.activity_type_id',
-      3 => 'acc.sort_name',
-      4 => 'cc.sort_name',
-      5 => 'ca.status_id',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $params = $_POST;
+    $params = $_GET;
     if ($sort && $sortOrder) {
-      $params['sortname'] = $sort;
-      $params['sortorder'] = $sortOrder;
+      $params['sortBy'] = $sort . ' ' . $sortOrder;
     }
     $params['page'] = ($offset / $rowCount) + 1;
     $params['rp'] = $rowCount;
@@ -68,36 +62,22 @@ class CRM_Activity_Page_AJAX {
     // get the activities related to given case
     $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
 
-    $iFilteredTotal = $iTotal = $params['total'];
-    $selectorElements = array(
-      'display_date',
-      'subject',
-      'type',
-      'with_contacts',
-      'reporter',
-      'status',
-      'links',
-      'class',
-    );
-
-    echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($activities);
   }
 
   public static function getCaseGlobalRelationships() {
-    $sortMapper = array(
-      0 => 'sort_name',
-      1 => 'phone',
-      2 => 'email',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
+
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $params = $_GET;
 
-    $params = $_POST;
     //CRM-14466 initialize variable to avoid php notice
     $sortSQL = "";
     if ($sort && $sortOrder) {
@@ -112,34 +92,42 @@ class CRM_Activity_Page_AJAX {
     // limit the rows
     $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
 
-    $iFilteredTotal = $iTotal = $relGlobalTotalCount;
-    $selectorElements = array('sort_name', 'phone', 'email');
+    $relationships = array();
+    // after sort we can update username fields to be a url
+    foreach ($relGlobal as $key => $value) {
+      $relationship = array();
+      $relationship['sort_name'] = $value['sort_name'];
+      $relationship['phone'] = $value['phone'];
+      $relationship['email'] = $value['email'];
+
+      array_push($relationships, $relationship);
+    }
+
+    $params['total'] = count($relationships);
 
-    echo CRM_Utils_JSON::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
+    $globalRelationshipsDT = array();
+    $globalRelationshipsDT['data'] = $relationships;
+    $globalRelationshipsDT['recordsTotal'] = $params['total'];
+    $globalRelationshipsDT['recordsFiltered'] = $params['total'];
+
+    CRM_Utils_JSON::output($globalRelationshipsDT);
   }
 
   public static function getCaseClientRelationships() {
     $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
     $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
 
-    $sortMapper = array(
-      0 => 'relation',
-      1 => 'name',
-      2 => 'phone',
-      3 => 'email',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $params = $_POST;
-    if ($sort && $sortOrder) {
-      $sortSQL = $sort . ' ' . $sortOrder;
-    }
+    $params = $_GET;
 
     // Retrieve ALL client relationships
     $relClient = CRM_Contact_BAO_Relationship::getRelationship($contactID,
@@ -165,21 +153,27 @@ class CRM_Activity_Page_AJAX {
     $sort_type = "SORT_" . strtoupper($sortOrder);
     array_multisort($sortArray, constant($sort_type), $clientRelationships);
 
-    //limit the rows
-    $allClientRelationships = $clientRelationships;
-    $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
-
+    $relationships = array();
     // after sort we can update username fields to be a url
     foreach ($clientRelationships as $key => $value) {
-      $clientRelationships[$key]['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view',
+      $relationship = array();
+      $relationship['relation'] = $value['relation'];
+      $relationship['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view',
           'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
+      $relationship['phone'] = $value['phone'];
+      $relationship['email'] = $value['email'];
+
+      array_push($relationships, $relationship);
     }
 
-    $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
-    $selectorElements = array('relation', 'name', 'phone', 'email');
+    $params['total'] = count($relationships);
+
+    $clientRelationshipsDT = array();
+    $clientRelationshipsDT['data'] = $relationships;
+    $clientRelationshipsDT['recordsTotal'] = $params['total'];
+    $clientRelationshipsDT['recordsFiltered'] = $params['total'];
 
-    echo CRM_Utils_JSON::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($clientRelationshipsDT);
   }
 
 
@@ -187,24 +181,17 @@ class CRM_Activity_Page_AJAX {
     $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
     $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
 
-    $sortMapper = array(
-      0 => 'relation',
-      1 => 'name',
-      2 => 'phone',
-      3 => 'email',
-      4 => 'actions',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $params = $_POST;
-    if ($sort && $sortOrder) {
-      $sortSQL = $sort . ' ' . $sortOrder;
-    }
+    $params = $_GET;
 
     $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
     $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
@@ -218,6 +205,8 @@ class CRM_Activity_Page_AJAX {
       $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
     }
 
+    $relationships = array();
+
     foreach ($caseRelationships as $key => $value) {
       //calculate roles that don't have relationships
       if (!empty($caseRoles[$value['relation_type']])) {
@@ -225,7 +214,7 @@ class CRM_Activity_Page_AJAX {
         $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
         unset($caseRoles[$value['relation_type']]);
       }
-      // mark orginal case relationships record to use on setting edit links below
+      // mark original case relationships record to use on setting edit links below
       $caseRelationships[$key]['source'] = 'caseRel';
     }
 
@@ -263,18 +252,13 @@ class CRM_Activity_Page_AJAX {
     foreach ($caseRelationships as $key => $row) {
       $sortArray[$key] = $row[$sort];
     }
-
     $sort_type = "SORT_" . strtoupper($sortOrder);
     array_multisort($sortArray, constant($sort_type), $caseRelationships);
 
-    //limit rows display
-    $allCaseRelationships = $caseRelationships;
-    $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
+    $relationships = array();
 
     // set user name, email and edit columns links
-    // idx will count number of current row / needed by edit links
-    $idx = 1;
-    foreach ($caseRelationships as &$row) {
+    foreach ($caseRelationships as $key => &$row) {
       // Get rid of the "<br />(Case Manager)" from label
       list($typeLabel) = explode('<', $row['relation']);
       // view user links
@@ -293,8 +277,7 @@ class CRM_Activity_Page_AJAX {
         $contactType = $contactType == 'Contact' ? '' : $contactType;
         switch ($row['source']) {
           case 'caseRel':
-            $row['actions']
-              = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
+            $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
               '<span class="icon ui-icon-pencil"></span>' .
               '</a>' .
               '<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' .
@@ -303,20 +286,28 @@ class CRM_Activity_Page_AJAX {
             break;
 
           case 'caseRoles':
-            $row['actions']
-              = '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
+            $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
               '<span class="icon ui-icon-pencil"></span>' .
               '</a>';
             break;
         }
       }
-      $idx++;
+      unset($row['cid']);
+      unset($row['relation_type']);
+      unset($row['rel_id']);
+      unset($row['client_id']);
+      unset($row['source']);
+      array_push($relationships, $row);
     }
-    $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
-    $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
+    $params['total'] = count($relationships);
+
+    $caseRelationshipsDT = array();
+    $caseRelationshipsDT['data'] = $relationships;
+    $caseRelationshipsDT['recordsTotal'] = $params['total'];
+    $caseRelationshipsDT['recordsFiltered'] = $params['total'];
+
+    CRM_Utils_JSON::output($caseRelationshipsDT);
 
-    echo CRM_Utils_JSON::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
   }
 
   public static function convertToCaseActivity() {
@@ -451,26 +442,20 @@ class CRM_Activity_Page_AJAX {
   }
 
   public static function getContactActivity() {
-    $contactID = CRM_Utils_Type::escape($_POST['contact_id'], 'Integer');
+    $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
     $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
 
-    $sortMapper = array(
-      0 => 'activity_type',
-      1 => 'subject',
-      2 => 'source_contact_name',
-      3 => '',
-      4 => '',
-      5 => 'activity_date_time',
-      6 => 'status_id',
-    );
+    $sortMapper = array();
+    foreach ($_GET['columns'] as $key => $value) {
+      $sortMapper[$key] = $value['data'];
+    };
 
-    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
-    $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
+    $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
+    $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
+    $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
+    $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
 
-    $params = $_POST;
+    $params = $_GET;
     if ($sort && $sortOrder) {
       $params['sortBy'] = $sort . ' ' . $sortOrder;
     }
@@ -486,13 +471,9 @@ class CRM_Activity_Page_AJAX {
 
     foreach ($activities as $key => $value) {
       //Check if recurring activity
-      if (CRM_Utils_Array::value('is_recurring_activity', $value)) {
-        if ($key == $value['is_recurring_activity']) {
-          $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Parent)</span>';
-        }
-        else {
-          $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Child)</span>';
-        }
+      if (!empty($value['is_recurring_activity'])) {
+        $repeat = $value['is_recurring_activity'];
+        $activities[$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1])) . '</span>';
       }
     }
 
@@ -530,21 +511,7 @@ class CRM_Activity_Page_AJAX {
       );
     }
 
-    $iFilteredTotal = $iTotal = $params['total'];
-    $selectorElements = array(
-      'activity_type',
-      'subject',
-      'source_contact',
-      'target_contact',
-      'assignee_contact',
-      'activity_date',
-      'status',
-      'links',
-      'class',
-    );
-
-    echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($activities);
   }
 
 }