3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * this class builds custom data
21 class CRM_Custom_Form_CustomData
{
24 * Generic wrapper to add custom data to a form via a single line in preProcess.
26 * $this->getDefaultEntity() must be defined for the form class for this to work.
28 * If the postProcess form cannot use the api & instead uses a BAO function it will need.
29 * $params['custom'] = CRM_Core_BAO_CustomField::postProcess($submitted, $this->_id, $this->getDefaultEntity());
31 * @param CRM_Core_Form $form
32 * @param null|string $entitySubType values stored in civicrm_custom_group.extends_entity_column_value
33 * e.g Student for contact type
34 * @param null|string $subName value in civicrm_custom_group.extends_entity_column_id
35 * @param null|int $groupCount number of entities that could have custom data
36 * @param null|int $contact_id contact ID associated with the custom data.
38 * @throws \CRM_Core_Exception
40 public static function addToForm(&$form, $entitySubType = NULL, $subName = NULL, $groupCount = 1, $contact_id = NULL) {
41 $entityName = $form->getDefaultEntity();
42 $entityID = $form->getEntityId();
43 // If the form has been converted to use entityFormTrait then getEntitySubTypeId() will exist.
44 if (method_exists($form, 'getEntitySubTypeId') && empty($entitySubType)) {
45 $entitySubType = $form->getEntitySubTypeId();
48 if ($form->getAction() == CRM_Core_Action
::VIEW
) {
49 // Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template)
50 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($entityName, NULL, $entityID, 0, $entitySubType);
51 CRM_Core_BAO_CustomGroup
::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID);
54 // Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template)
55 if (!empty($_POST['hidden_custom'])) {
56 self
::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
57 self
::buildQuickForm($form);
58 self
::setDefaultValues($form);
61 // need to assign custom data type and subtype to the template
62 $form->assign('customDataType', $entityName);
63 $form->assign('customDataSubType', $entitySubType);
64 $form->assign('entityID', $entityID);
65 $form->assign('cid', $contact_id);
69 * @param CRM_Core_Form $form
70 * @param null|string $subName
71 * @param null|string $subType
72 * @param null|int $groupCount
74 * @param null|int $entityID
75 * @param null $onlySubType
77 * @throws \CRM_Core_Exception
79 public static function preProcess(
80 &$form, $subName = NULL, $subType = NULL,
81 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
87 $form->_type
= CRM_Utils_Request
::retrieve('type', 'String', $form);
90 if (isset($subType)) {
91 $form->_subType
= $subType;
94 $form->_subType
= CRM_Utils_Request
::retrieve('subType', 'String', $form);
97 if ($form->_subType
== 'null') {
98 $form->_subType
= NULL;
101 if (isset($subName)) {
102 $form->_subName
= $subName;
105 $form->_subName
= CRM_Utils_Request
::retrieve('subName', 'String', $form);
108 if ($form->_subName
== 'null') {
109 $form->_subName
= NULL;
113 $form->_groupCount
= $groupCount;
116 $form->_groupCount
= CRM_Utils_Request
::retrieve('cgcount', 'Positive', $form);
119 $form->assign('cgCount', $form->_groupCount
);
121 //carry qf key, since this form is not inhereting core form.
122 if ($qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String')) {
123 $form->assign('qfKey', $qfKey);
127 $form->_entityId
= $entityID;
130 $form->_entityId
= CRM_Utils_Request
::retrieve('entityID', 'Positive', $form);
133 $typeCheck = CRM_Utils_Request
::retrieve('type', 'String');
134 $urlGroupId = CRM_Utils_Request
::retrieve('groupID', 'Positive');
135 if (isset($typeCheck) && $urlGroupId) {
136 $form->_groupID
= $urlGroupId;
139 $form->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $form);
142 $gid = (isset($form->_groupID
)) ?
$form->_groupID
: NULL;
143 $getCachedTree = $form->_getCachedTree ??
TRUE;
145 $subType = $form->_subType
;
146 if (!is_array($subType) && strstr($subType, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
147 $subType = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, ',', trim($subType, CRM_Core_DAO
::VALUE_SEPARATOR
));
150 self
::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
154 * @param CRM_Core_Form $form
158 public static function setDefaultValues(&$form) {
160 CRM_Core_BAO_CustomGroup
::setDefaults($form->_groupTree
, $defaults, FALSE, FALSE, $form->get('action'));
165 * @param CRM_Core_Form $form
167 public static function buildQuickForm(&$form) {
168 $form->addElement('hidden', 'hidden_custom', 1);
169 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount
);
170 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $form->_groupTree
);
174 * Add the group data as a formatted array to the form.
176 * @param CRM_Core_Form $form
177 * @param string $subType
179 * @param bool $onlySubType
180 * @param bool $getCachedTree
183 * @throws \CRM_Core_Exception
185 public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
186 $singleRecord = NULL;
187 if (!empty($form->_groupCount
) && !empty($form->_multiRecordDisplay
) && $form->_multiRecordDisplay
== 'single') {
188 $singleRecord = $form->_groupCount
;
190 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $form);
191 // when a new record is being added for multivalued custom fields.
192 if (isset($form->_groupCount
) && $form->_groupCount
== 0 && $mode == 'add' &&
193 !empty($form->_multiRecordDisplay
) && $form->_multiRecordDisplay
== 'single') {
194 $singleRecord = 'new';
197 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($form->_type
,
210 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
211 $form->_customValueCount
= CRM_Core_BAO_CustomGroup
::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId
);
213 // we should use simplified formatted groupTree
214 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $form->_groupCount
, $form);
216 if (isset($form->_groupTree
) && is_array($form->_groupTree
)) {
217 $keys = array_keys($groupTree);
218 foreach ($keys as $key) {
219 $form->_groupTree
[$key] = $groupTree[$key];
221 return [$form, $groupTree];
224 $form->_groupTree
= $groupTree;
225 return [$form, $groupTree];