CRM-17645 - Convert case autocompletes to use EntityRef
[civicrm-core.git] / CRM / Case / Form / ActivityToCase.php
index 5ce068eff3903c54f11a6e444c6d9efdfad73eea..3a981f898af4dcdceef40ca2bb3ac7b58d1621b7 100644 (file)
@@ -68,18 +68,20 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form {
     // If this contact has an open case, supply it as a default
     $cid = CRM_Utils_Request::retrieve('cid', 'Integer');
     if ($cid) {
-      $cases = CRM_Case_BAO_Case::getUnclosedCases(array('contact_id' => $cid), $this->_currentCaseId);
-      foreach ($cases as $id => $details) {
-        $defaults['file_on_case_unclosed_case_id'] = $id;
-        $value = array(
-          'label' => $details['sort_name'] . ' - ' . $details['case_type'],
-          'extra' => array('contact_id' => $cid),
-        );
-        $this->updateElementAttr('file_on_case_unclosed_case_id', array('data-value' => json_encode($value)));
+      $cases = civicrm_api3('CaseContact', 'get', array(
+        'contact_id' => $cid,
+        'case_id' => array('!=' => $this->_currentCaseId),
+        'case_id.status_id' => array('!=' => "Closed"),
+        'case_id.is_deleted' => 0,
+        'case_id.end_date' => array('IS NULL' => 1),
+        'options' => array('limit' => 1),
+        'return' => 'case_id',
+      ));
+      foreach ($cases['values'] as $record) {
+        $defaults['file_on_case_unclosed_case_id'] = $record['case_id'];
         break;
       }
     }
-
     return $defaults;
   }
 
@@ -87,7 +89,18 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form {
    * Build the form object.
    */
   public function buildQuickForm() {
-    $this->add('text', 'file_on_case_unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE);
+    $this->addEntityRef('file_on_case_unclosed_case_id', ts('Select Case'), array(
+      'entity' => 'Case',
+      'api' => array(
+        'extra' => array('contact_id'),
+        'params' => array(
+          'case_id' => array('!=' => $this->_currentCaseId),
+          'case_id.is_deleted' => 0,
+          'case_id.status_id' => array('!=' => "Closed"),
+          'case_id.end_date' => array('IS NULL' => 1),
+        ),
+      ),
+    ), TRUE);
     $this->addEntityRef('file_on_case_target_contact_id', ts('With Contact(s)'), array('multiple' => TRUE));
     $this->add('text', 'file_on_case_activity_subject', ts('Subject'), array('size' => 50));
   }