Filter groups according to included profiles
[civicrm-core.git] / CRM / Profile / Form.php
index 74ed2ca620c674b3c07c955d56003d8a9cb53592..6f63c185288c06e591c90b40df9ed238392b47a4 100644 (file)
@@ -331,7 +331,7 @@ class CRM_Profile_Form extends CRM_Core_Form {
     }
     $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate');
 
-    $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0));
+    $gids = explode(',', (CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0) ?? ''));
 
     if ((count($gids) > 1) && !$this->_profileIds && empty($this->_profileIds)) {
       if (!empty($gids)) {
@@ -689,8 +689,9 @@ class CRM_Profile_Form extends CRM_Core_Form {
    * Build the form object.
    *
    */
-  public function buildQuickForm() {
+  public function buildQuickForm(): void {
     $this->add('hidden', 'gid', $this->_gid);
+    $this->assign('deleteRecord', $this->isDeleteMode());
 
     switch ($this->_mode) {
       case self::MODE_CREATE:
@@ -713,13 +714,10 @@ class CRM_Profile_Form extends CRM_Core_Form {
       return;
     }
 
-    if (($this->_multiRecord & CRM_Core_Action::DELETE)) {
+    if ($this->isDeleteMode()) {
       if (!$this->_recordExists) {
         CRM_Core_Session::setStatus(ts('The record %1 doesnot exists', [1 => $this->_recordId]), ts('Record doesnot exists'), 'alert');
       }
-      else {
-        $this->assign('deleteRecord', TRUE);
-      }
       return;
     }
 
@@ -786,7 +784,7 @@ class CRM_Profile_Form extends CRM_Core_Form {
     //lets have single status message,
     $this->assign('statusMessage', $statusMessage);
     if ($return) {
-      return FALSE;
+      return;
     }
 
     $this->assign('id', $this->_id);
@@ -846,7 +844,7 @@ class CRM_Profile_Form extends CRM_Core_Form {
         continue;
       }
 
-      list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2);
+      [$prefixName, $index] = CRM_Utils_System::explode('-', $name, 2);
 
       CRM_Core_BAO_UFGroup::buildProfile($this, $field, $this->_mode);
 
@@ -1008,7 +1006,7 @@ class CRM_Profile_Form extends CRM_Core_Form {
     }
 
     foreach ($fields as $key => $value) {
-      list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $key, 3);
+      [$fieldName, $locTypeId, $phoneTypeId] = CRM_Utils_System::explode('-', $key, 3);
       if ($fieldName == 'state_province' && !empty($fields["country-{$locTypeId}"])) {
         // Validate Country - State list
         $countryId = $fields["country-{$locTypeId}"];
@@ -1271,6 +1269,9 @@ class CRM_Profile_Form extends CRM_Core_Form {
       $params['customRecordValues'][$this->_recordId] = array_keys($this->_multiRecordFields);
     }
 
+    // sudoman hack: re-insert filtered group memberships
+    $params = CRM_Contact_Form_Edit_TagsAndGroups::reInsertFilteredGroupMemberships([$this->_gid], $this->_id, TRUE, $params);
+
     $this->_id = CRM_Contact_BAO_Contact::createProfileContact(
       $params,
       $profileFields,
@@ -1370,4 +1371,11 @@ class CRM_Profile_Form extends CRM_Core_Form {
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
   }
 
+  /**
+   * @return int|string
+   */
+  private function isDeleteMode() {
+    return ($this->_multiRecord & CRM_Core_Action::DELETE);
+  }
+
 }