Allow admin to change the names of filtered groups 4.6.29-fsf-mod1
authorAndrew Engelbrecht <andrew@fsf.org>
Mon, 18 Sep 2017 22:19:40 +0000 (18:19 -0400)
committerAndrew Engelbrecht <andrew@fsf.org>
Mon, 18 Sep 2017 22:19:40 +0000 (18:19 -0400)
This feature lets one mimic a custom field by allowing admins to change
the text that appears next to each group's check box, via drush.

The json format is a bit more nested, but indented json can be used. An
exmple is included in CRM/Contact/Form/Edit/TagsAndGroups.php.

CRM/Contact/Form/Edit/TagsAndGroups.php

index 23bda39101fb1859f603d022b1c34dcc1f5f34b1..b55a50085a258aa902a1d6f92fb492b3b1ebd263 100644 (file)
@@ -117,7 +117,26 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
         //
         // To set the variable, use the following method:
         //
-        // $ drush vset --format=string groups_field_filters "{'profile': {'468': [25, 41]}, 'contribution': {'14': [25, 41]}, 'event': {'49': [25, 41]}}"
+        // $ drush vset --format=string groups_field_filters '
+        // {
+        //     "profile": {
+        //         "468": {
+        //             "groups": [41, 25],
+        //             "titles": ["Yes, sign me up!", "Sign me up to the other group!"]
+        //         }
+        //     },
+        //     "contribution": {
+        //         "14": {
+        //             "groups": [41, 25],
+        //             "titles": ["Yes, sign me up!"]
+        //         }
+        //     },
+        //     "event": {
+        //         "49": {
+        //             "groups": [25]
+        //         }
+        //     }
+        // }'
         //
         // The "string" format must be used and not the "json" format.
         // Otherwise the json will be converted to a PHP array, which will not
@@ -146,23 +165,30 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
 
           case 'CRM_Profile_Form_Edit':
             $page_id = $form->get('gid'); // gid, not id
-            $filter = $groups_field_filters['profile'][strval($page_id)];
+            $filter_config = $groups_field_filters['profile'][strval($page_id)];
             break;
 
           case 'CRM_Contribute_Form_Contribution_Main':
           case 'CRM_Contribute_Form_Contribution_Confirm':
           case 'CRM_Contribute_Form_Contribution_ThankYou':
             $page_id = $form->get('id'); // id, not gid
-            $filter = $groups_field_filters['contribution'][strval($page_id)];
+            $filter_config = $groups_field_filters['contribution'][strval($page_id)];
             break;
 
           case 'CRM_Event_Form_Registration_Register':
             $page_id = $form->get('id'); // id, not gid
-            $filter = $groups_field_filters['event'][strval($page_id)];
+            $filter_config = $groups_field_filters['event'][strval($page_id)];
             break;
         }
+
+        if (isset($filter_config)) {
+
+          $filter_ids = $filter_config['groups'];
+          $filter_titles = $filter_config['titles'];
+        }
         // sudoman hack continues further below
 
+
         $attributes['skiplabel'] = TRUE;
         $elements = array();
         $groupsOptions = array();
@@ -170,10 +196,10 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
 
           // sudoman hack continues
           // filter groups if a filter is set
-          if (isset($filter) and !in_array($id, $filter)) {
+          if (isset($filter_ids) and !in_array($id, $filter_ids)) {
             continue;
           }
-          // sudoman hack continues with calls to CRM_Contact_Form_Edit_TagsAndGroups::reInsertFilteredGroupMemberships
+          // sudoman hack continues further below
 
           // make sure that this group has public visibility
           if ($visibility &&
@@ -186,8 +212,22 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
             $groupsOptions[$id] = $group['title'];
           }
           else {
+
+            // sudoman hack continues
+            if (isset($filter_titles)) {
+              $custom_group_title = $filter_titles[array_search($id, $filter_ids)];
+            }
+            if (isset($custom_group_title)) {
+              $group_title = $custom_group_title;
+            } else {
+              $group_title = $group['title'];
+            }
+
+            // only slight modification here
             $form->_tagGroup[$fName][$id]['description'] = $group['description'];
-            $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
+            $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group_title, $attributes);
+
+            // sudoman hack continues with calls to CRM_Contact_Form_Edit_TagsAndGroups::reInsertFilteredGroupMemberships
           }
         }
 
@@ -384,7 +424,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       $contactID, $ignorePermission, $params) {
 
     $groups_field_filters = json_decode(variable_get('groups_field_filters', NULL), $assoc = TRUE);
-    $filter = $groups_field_filters[$page_type][strval($page_id)];
+    $filter_ids = $groups_field_filters[$page_type][strval($page_id)]['groups'];
 
     if ($contactID !== NULL) {
 
@@ -395,7 +435,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       foreach ($contactGroupList as $value) {
 
         $groupId = $value['group_id'];
-        if (!empty($filter) && !in_array($groupId, $filter)) {
+        if (!empty($filter_ids) && !in_array($groupId, $filter_ids)) {
 
           $params['group'][strval($groupId)] = 1;
         }