CRM-21507 Improve Activity Separation field logic
[civicrm-core.git] / CRM / Activity / Form / Activity.php
index 58d7eadff2fb64eb957ea421c1b5d15cfdb4c5f6..8c96bc205e620fcd7f97138f869d5fc35b19b9eb 100644 (file)
@@ -119,6 +119,23 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
   protected $unsavedWarn = TRUE;
 
+  /*
+   * Is it possible to create separate activities with this form?
+   *
+   * When TRUE, the form will ask whether the user wants to create separate
+   * activities (if the user has specified multiple contacts in the "with"
+   * field).
+   *
+   * When FALSE, the form will create one activity with all contacts together
+   * and won't ask the user anything.
+   *
+   * Note: This is a class property so that child classes can turn off this
+   * behavior (e.g. in CRM_Case_Form_Activity)
+   *
+   * @var boolean
+   */
+  protected $supportsActivitySeparation = TRUE;
+
   /**
    * Explicitly declare the entity api name.
    *
@@ -702,8 +719,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     }
     $this->assign('surveyActivity', $this->_isSurveyActivity);
 
-    // this option should be available only during add mode
-    if ($this->_action != CRM_Core_Action::UPDATE) {
+    // Add the "Activity Separation" field
+    $actionIsAdd = $this->_action != CRM_Core_Action::UPDATE;
+    $separationIsPossible = $this->supportsActivitySeparation;
+    if ($actionIsAdd && $separationIsPossible) {
       $this->addRadio(
         'separation',
         ts('Activity Separation'),
@@ -839,12 +858,16 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     if ((!empty($fields['followup_activity_subject']) || !empty($fields['followup_date'])) && empty($fields['followup_activity_type_id'])) {
       $errors['followup_activity_subject'] = ts('Follow-up Activity type is a required field.');
     }
+
+    // Check that a value has been set for the "activity separation" field if needed
+    $separationIsPossible = $self->supportsActivitySeparation;
     $actionIsAdd = $self->_action == CRM_Core_Action::ADD;
     $hasMultipleTargetContacts = !empty($fields['target_contact_id']) && strpos($fields['target_contact_id'], ',') !== FALSE;
     $separationFieldIsEmpty = empty($fields['separation']);
-    if ($actionIsAdd && $hasMultipleTargetContacts && $separationFieldIsEmpty) {
+    if ($separationIsPossible && $actionIsAdd && $hasMultipleTargetContacts && $separationFieldIsEmpty) {
       $errors['separation'] = ts('Activity Separation is a required field.');
     }
+
     return $errors;
   }