Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33
34/**
35 * this class builds custom data
36 */
37class CRM_Custom_Form_CustomData {
38
6452294d
MW
39 /**
40 * Generic wrapper to add custom data to a form via a single line in preProcess.
41 *
42 * $this->getDefaultEntity() must be defined for the form class for this to work.
43 *
44 * If the postProcess form cannot use the api & instead uses a BAO function it will need.
45 * $params['custom'] = CRM_Core_BAO_CustomField::postProcess($submitted, $this->_id, $this->getDefaultEntity());
46 *
47 * @param CRM_Core_Form $form
48 * @param null|string $subType values stored in civicrm_custom_group.extends_entity_column_value
49 * e.g Student for contact type
50 * @param null|string $subName value in civicrm_custom_group.extends_entity_column_id
51 * @param null|int $groupCount number of entities that could have custom data
2fcb4a7f 52 * @param null|int $contact_id contact ID associated with the custom data.
6452294d
MW
53 *
54 * @throws \CRM_Core_Exception
55 */
2fcb4a7f 56 public static function addToForm(&$form, $subType = NULL, $subName = NULL, $groupCount = 1, $contact_id = NULL) {
6452294d 57 $entityName = $form->getDefaultEntity();
e4f5c851 58 $entityID = $form->getEntityId();
2c024dc6
MWMC
59 // FIXME: If the form has been converted to use entityFormTrait then getEntitySubTypeId() will exist.
60 // However, if it is only partially converted (ie. we've switched customdata to use CRM_Custom_Form_CustomData)
61 // it won't, so we check if we have a subtype before calling the function.
62 $entitySubType = NULL;
63 if ($subType) {
64 $entitySubType = $form->getEntitySubTypeId($subType);
65 }
6452294d 66
48c242dc
MWMC
67 if ($form->getAction() == CRM_Core_Action::VIEW) {
68 // Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template)
69 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityName, NULL, $entityID, 0, $entitySubType);
70 CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID);
71 }
72 else {
73 // Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template)
74 if (!empty($_POST['hidden_custom'])) {
75 self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
76 self::buildQuickForm($form);
77 self::setDefaultValues($form);
78 }
6452294d
MW
79 }
80 // need to assign custom data type and subtype to the template
81 $form->assign('customDataType', $entityName);
9f1073d2 82 $form->assign('customDataSubType', $entitySubType);
e4f5c851 83 $form->assign('entityID', $entityID);
2fcb4a7f 84 $form->assign('cid', $contact_id);
6452294d
MW
85 }
86
6a488035
TO
87 /**
88 * @param CRM_Core_Form $form
89 * @param null|string $subName
90 * @param null|string $subType
91 * @param null|int $groupCount
e8646905 92 * @param string $type
6a488035 93 * @param null|int $entityID
77b97be7 94 * @param null $onlySubType
6452294d
MW
95 *
96 * @throws \CRM_Core_Exception
6a488035 97 */
2da40d21 98 public static function preProcess(
40b55be5 99 &$form, $subName = NULL, $subType = NULL,
63dbed83 100 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
6a488035
TO
101 ) {
102 if ($type) {
103 $form->_type = $type;
104 }
105 else {
106 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
107 }
108
109 if (isset($subType)) {
110 $form->_subType = $subType;
111 }
112 else {
113 $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form);
114 }
115
116 if ($form->_subType == 'null') {
117 $form->_subType = NULL;
118 }
119
120 if (isset($subName)) {
121 $form->_subName = $subName;
122 }
123 else {
124 $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form);
125 }
126
127 if ($form->_subName == 'null') {
128 $form->_subName = NULL;
129 }
130
131 if ($groupCount) {
132 $form->_groupCount = $groupCount;
133 }
134 else {
135 $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form);
136 }
137
138 $form->assign('cgCount', $form->_groupCount);
139
140 //carry qf key, since this form is not inhereting core form.
a3d827a7 141 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String')) {
6a488035
TO
142 $form->assign('qfKey', $qfKey);
143 }
144
145 if ($entityID) {
146 $form->_entityId = $entityID;
147 }
148 else {
149 $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form);
150 }
151
a3d827a7
CW
152 $typeCheck = CRM_Utils_Request::retrieve('type', 'String');
153 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive');
481a74f4 154 if (isset($typeCheck) && $urlGroupId) {
6a488035 155 $form->_groupID = $urlGroupId;
0db6c3e1
TO
156 }
157 else {
6a488035
TO
158 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
159 }
160
161 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
162 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
163
164 $subType = $form->_subType;
165 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
166 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
167 }
8ef12e64 168
5b15263a
EM
169 self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
170 }
171
172 /**
173 * @param CRM_Core_Form $form
174 *
175 * @return array
176 */
177 public static function setDefaultValues(&$form) {
be2fb01f 178 $defaults = [];
5b15263a
EM
179 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
180 return $defaults;
181 }
182
183 /**
184 * @param CRM_Core_Form $form
185 */
186 public static function buildQuickForm(&$form) {
187 $form->addElement('hidden', 'hidden_custom', 1);
188 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
189 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
190 }
191
192 /**
1203c8df 193 * Add the group data as a formatted array to the form.
194 *
195 * @param CRM_Core_Form $form
196 * @param string $subType
041ecc95 197 * @param int $gid
1203c8df 198 * @param bool $onlySubType
199 * @param bool $getCachedTree
5b15263a
EM
200 *
201 * @return array
1203c8df 202 * @throws \CRM_Core_Exception
5b15263a 203 */
1203c8df 204 public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
e1d48b72 205 $singleRecord = NULL;
1b56bd01 206 if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
e1d48b72 207 $singleRecord = $form->_groupCount;
208 }
209 $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
1b56bd01 210 // when a new record is being added for multivalued custom fields.
211 if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
212 !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
e1d48b72 213 $singleRecord = 'new';
214 }
5b15263a
EM
215
216 $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
0b330e6d 217 NULL,
6a488035
TO
218 $form->_entityId,
219 $gid,
220 $subType,
221 $form->_subName,
63dbed83 222 $getCachedTree,
e1d48b72 223 $onlySubType,
224 FALSE,
225 TRUE,
226 $singleRecord
6a488035
TO
227 );
228
229 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
ec3de1a9 230 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
6a488035
TO
231 }
232 // we should use simplified formatted groupTree
233 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
234
235 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
236 $keys = array_keys($groupTree);
237 foreach ($keys as $key) {
238 $form->_groupTree[$key] = $groupTree[$key];
239 }
be2fb01f 240 return [$form, $groupTree];
6a488035
TO
241 }
242 else {
243 $form->_groupTree = $groupTree;
be2fb01f 244 return [$form, $groupTree];
6a488035
TO
245 }
246 }
247
6a488035 248}