Merge pull request #2712 from pratik-joshi/CRM-14363
[civicrm-core.git] / CRM / Case / Page / AJAX.php
index 92ce576c301868539c3c9c45ec442e99d4657020..7e7256cc744c4502e1fa5c8d11d7bb5c9d870f82 100644 (file)
@@ -41,30 +41,25 @@ class CRM_Case_Page_AJAX {
    * Retrieve unclosed cases.
    */
   static function unclosedCases() {
-    $criteria = explode('-', CRM_Utils_Type::escape(CRM_Utils_Array::value('s', $_GET), 'String'));
-
-    $limit = CRM_Utils_Array::value('limit', $_GET);
-    if ($limit) {
-      $limit = CRM_Utils_Type::escape($limit, 'Integer');
-    }
-
     $params = array(
-      'limit' => $limit,
-      'case_type' => trim(CRM_Utils_Array::value(1, $criteria)),
-      'sort_name' => trim(CRM_Utils_Array::value(0, $criteria)),
+      'limit' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10),
+      'sort_name' => CRM_Utils_Type::escape(CRM_Utils_Array::value('term', $_GET, ''), 'String'),
     );
 
     $excludeCaseIds = array();
-    if ($caseIdStr = CRM_Utils_Array::value('excludeCaseIds', $_GET)) {
-      $excludeIdStr = CRM_Utils_Type::escape($caseIdStr, 'String');
-      $excludeCaseIds = explode(',', $excludeIdStr);
+    if (!empty($_GET['excludeCaseIds'])) {
+      $excludeCaseIds = explode(',', CRM_Utils_Type::escape($_GET['excludeCaseIds'], 'String'));
     }
     $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds);
-
+    $results = array();
     foreach ($unclosedCases as $caseId => $details) {
-      echo $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ') ' . "|$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name'] . "\n";
+      $results[] = array(
+        'id' => $caseId,
+        'text' => $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ')',
+        'extra' => $details,
+      );
     }
-
+    print json_encode($results);
     CRM_Utils_System::civiExit();
   }
 
@@ -72,6 +67,7 @@ class CRM_Case_Page_AJAX {
 
     $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Integer');
     $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
+    $tagList = $_POST['taglist'];
 
     if (empty($caseId)) {
       echo 'false';
@@ -83,20 +79,26 @@ class CRM_Case_Page_AJAX {
       $tagIds = explode(',', $tags);
     }
 
-    $params = array(
-      'entity_id' => $caseId,
-      'entity_table' => 'civicrm_case',
-    );
+    if (!empty($tagIds)) {
+      $params = array(
+        'entity_id' => $caseId,
+        'entity_table' => 'civicrm_case',
+      );
 
-    CRM_Core_BAO_EntityTag::del($params);
+      CRM_Core_BAO_EntityTag::del($params);
 
-    foreach ($tagIds as $tagid) {
-      if (is_numeric($tagid)) {
-        $params['tag_id'] = $tagid;
-        CRM_Core_BAO_EntityTag::add($params);
+      foreach ($tagIds as $tagid) {
+        if (is_numeric($tagid)) {
+          $params['tag_id'] = $tagid;
+          CRM_Core_BAO_EntityTag::add($params);
+        }
       }
     }
 
+    if (!empty($tagList)) {
+      CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject);
+    }
+
     $session = CRM_Core_Session::singleton();
 
     $activityParams = array();