(NFC) Apply upcoming civicrm/coder policies (batch 2)
[civicrm-core.git] / CRM / Case / Form / Case.php
index 8c2312c1c734b1e88ebb3654eb806fe5b505be2f..f988aba14562869db00e0c638ae5926524712c41 100644 (file)
@@ -83,10 +83,43 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
    */
   public $_caseTypeId = NULL;
 
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'Case';
+  }
+
+  /**
+   * Get the entity id being edited.
+   *
+   * @return int|null
+   */
+  public function getEntityId() {
+    return $this->_caseId;
+  }
+
+  /**
+   * Get the entity subtype ID being edited
+   *
+   * @param $subTypeId
+   *
+   * @return int|null
+   */
+  public function getEntitySubTypeId($subTypeId) {
+    if ($subTypeId) {
+      return $subTypeId;
+    }
+    return $this->_caseTypeId;
+  }
+
   /**
    * Build the form object.
    */
   public function preProcess() {
+    if (empty($this->_action)) {
+      $this->_action = CRM_Core_Action::ADD;
+    }
 
     $this->_caseId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
 
@@ -110,15 +143,15 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
     }
 
     if (!$this->_caseId) {
-      $caseAttributes = array(
+      $caseAttributes = [
         'case_type_id' => ts('Case Type'),
         'status_id' => ts('Case Status'),
         'medium_id' => ts('Activity Medium'),
-      );
+      ];
 
       foreach ($caseAttributes as $key => $label) {
         if (!CRM_Case_BAO_Case::buildOptions($key, 'create')) {
-          CRM_Core_Error::fatal(ts('You do not have any active %1', array(1 => $label)));
+          CRM_Core_Error::fatal(ts('You do not have any active %1', [1 => $label]));
         }
       }
     }
@@ -159,7 +192,7 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
       $contact = new CRM_Contact_DAO_Contact();
       $contact->id = $this->_currentlyViewedContactId;
       if (!$contact->find(TRUE)) {
-        CRM_Core_Error::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId)));
+        CRM_Core_Error::statusBounce(ts('Client contact does not exist: %1', [1 => $this->_currentlyViewedContactId]));
       }
       $this->assign('clientName', $contact->display_name);
     }
@@ -167,18 +200,15 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
     $session = CRM_Core_Session::singleton();
     $this->_currentUserId = $session->get('userID');
 
-    //when custom data is included in this page
+    //Add activity custom data is included in this page
     CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity');
     $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
     $className::preProcess($this);
     $activityGroupTree = $this->_groupTree;
 
-    // for case custom fields to populate with defaults
-    if (!empty($_POST['hidden_custom'])) {
-      $params = CRM_Utils_Request::exportValues();
-      CRM_Custom_Form_CustomData::preProcess($this, NULL, CRM_Utils_Array::value('case_type_id', $params, $this->_caseTypeId), 1, 'Case', $this->_caseId);
-      CRM_Custom_Form_CustomData::buildQuickForm($this);
-    }
+    // Add case custom data to form
+    $caseTypeId = CRM_Utils_Array::value('case_type_id', CRM_Utils_Request::exportValues(), $this->_caseTypeId);
+    CRM_Custom_Form_CustomData::addToForm($this, $caseTypeId);
 
     // so that grouptree is not populated with case fields, since the grouptree is used
     // for populating activity custom fields.
@@ -208,44 +238,42 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
       if ($this->_action & CRM_Core_Action::RENEW) {
         $title = ts('Restore');
       }
-      $this->addButtons(array(
-          array(
-            'type' => 'next',
-            'name' => $title,
-            'spacing' => '         ',
-            'isDefault' => TRUE,
-          ),
-          array(
-            'type' => 'cancel',
-            'name' => ts('Cancel'),
-          ),
-        )
-      );
+      $this->addButtons([
+        [
+          'type' => 'next',
+          'name' => $title,
+          'spacing' => '         ',
+          'isDefault' => TRUE,
+        ],
+        [
+          'type' => 'cancel',
+          'name' => ts('Cancel'),
+        ],
+      ]);
       return;
     }
 
-    //need to assign custom data type and subtype to the template
-    $this->assign('customDataType', 'Case');
-
+    // Add the activity custom data to the form
     CRM_Custom_Form_CustomData::buildQuickForm($this);
+
     // we don't want to show button on top of custom form
     $this->assign('noPreCustomButton', TRUE);
 
     $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject');
     if (!is_array($s)) {
-      $s = array();
+      $s = [];
     }
     $this->add('text', 'activity_subject', ts('Subject'),
-      array_merge($s, array(
+      array_merge($s, [
         'maxlength' => '128',
-      )), TRUE
+      ]), TRUE
     );
 
     $tags = CRM_Core_BAO_Tag::getColorTags('civicrm_case');
 
     if (!empty($tags)) {
       $this->add('select2', 'tag', ts('Tags'), $tags, FALSE,
-        array('class' => 'huge', 'multiple' => 'multiple')
+        ['class' => 'huge', 'multiple' => 'multiple']
       );
     }
 
@@ -253,19 +281,18 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
     $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
     CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
 
-    $this->addButtons(array(
-        array(
-          'type' => 'next',
-          'name' => ts('Save'),
-          'spacing' => '         ',
-          'isDefault' => TRUE,
-        ),
-        array(
-          'type' => 'cancel',
-          'name' => ts('Cancel'),
-        ),
-      )
-    );
+    $this->addButtons([
+      [
+        'type' => 'next',
+        'name' => ts('Save'),
+        'spacing' => '         ',
+        'isDefault' => TRUE,
+      ],
+      [
+        'type' => 'cancel',
+        'name' => ts('Cancel'),
+      ],
+    ]);
 
     $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
     $className::buildQuickForm($this);
@@ -281,8 +308,8 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
       return TRUE;
     }
     $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
-    $this->addFormRule(array($className, 'formRule'), $this);
-    $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
+    $this->addFormRule([$className, 'formRule'], $this);
+    $this->addFormRule(['CRM_Case_Form_Case', 'formRule'], $this);
   }
 
   /**
@@ -302,34 +329,30 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
   }
 
   /**
-   * Process the form submission.
+   * Wrapper for unit testing the post process submit function.
+   *
+   * @param $params
+   * @param $activityTypeFile
+   * @param $contactId
+   * @param $context
+   * @return CRM_Case_BAO_Case
    */
-  public function postProcess() {
-    $transaction = new CRM_Core_Transaction();
+  public function testSubmit($params, $activityTypeFile, $contactId, $context = "case") {
+    $this->controller = new CRM_Core_Controller();
 
-    // check if dedupe button, if so return.
-    $buttonName = $this->controller->getButtonName();
-    if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
-      return;
-    }
+    $this->_activityTypeFile = $activityTypeFile;
+    $this->_currentUserId = $contactId;
+    $this->_context = $context;
 
-    if ($this->_action & CRM_Core_Action::DELETE) {
-      $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
-      if ($caseDelete) {
-        CRM_Core_Session::setStatus(ts('You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.'), ts('Case Deleted'), 'success');
-      }
-      return;
-    }
+    return $this->submit($params);
+  }
 
-    if ($this->_action & CRM_Core_Action::RENEW) {
-      $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
-      if ($caseRestore) {
-        CRM_Core_Session::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
-      }
-      return;
-    }
-    // store the submitted values in an array
-    $params = $this->controller->exportValues($this->_name);
+  /**
+   * Submit the form with given params.
+   *
+   * @param $params
+   */
+  public function submit(&$params) {
     $params['now'] = date("Ymd");
 
     // 1. call begin post process
@@ -359,9 +382,9 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
     unset($params['id'], $params['custom']);
 
     // add tags if exists
-    $tagParams = array();
+    $tagParams = [];
     if (!empty($params['tag'])) {
-      $tagParams = array();
+      $tagParams = [];
       if (!is_array($params['tag'])) {
         $params['tag'] = explode(',', $params['tag']);
       }
@@ -401,7 +424,42 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
       $className::endPostProcess($this, $params);
     }
 
+    return $caseObj;
+  }
+
+  /**
+   * Process the form submission.
+   */
+  public function postProcess() {
+    $transaction = new CRM_Core_Transaction();
+
+    // check if dedupe button, if so return.
+    $buttonName = $this->controller->getButtonName();
+    if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
+      return;
+    }
+
+    if ($this->_action & CRM_Core_Action::DELETE) {
+      $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
+      if ($caseDelete) {
+        CRM_Core_Session::setStatus(ts('You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.'), ts('Case Deleted'), 'success');
+      }
+      return;
+    }
+
+    if ($this->_action & CRM_Core_Action::RENEW) {
+      $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
+      if ($caseRestore) {
+        CRM_Core_Session::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
+      }
+      return;
+    }
+    // store the submitted values in an array
+    $params = $this->controller->exportValues($this->_name);
+    $this->submit($params);
+
     CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success');
+
   }
 
 }