Merge pull request #14995 from mfb/context-fix
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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
52 *
53 * @throws \CRM_Core_Exception
54 */
55 public static function addToForm(&$form, $subType = NULL, $subName = NULL, $groupCount = 1) {
56 $entityName = $form->getDefaultEntity();
e4f5c851 57 $entityID = $form->getEntityId();
2c024dc6
MWMC
58 // FIXME: If the form has been converted to use entityFormTrait then getEntitySubTypeId() will exist.
59 // However, if it is only partially converted (ie. we've switched customdata to use CRM_Custom_Form_CustomData)
60 // it won't, so we check if we have a subtype before calling the function.
61 $entitySubType = NULL;
62 if ($subType) {
63 $entitySubType = $form->getEntitySubTypeId($subType);
64 }
6452294d 65
48c242dc
MWMC
66 if ($form->getAction() == CRM_Core_Action::VIEW) {
67 // Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template)
68 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityName, NULL, $entityID, 0, $entitySubType);
69 CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID);
70 }
71 else {
72 // Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template)
73 if (!empty($_POST['hidden_custom'])) {
74 self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
75 self::buildQuickForm($form);
76 self::setDefaultValues($form);
77 }
6452294d
MW
78 }
79 // need to assign custom data type and subtype to the template
80 $form->assign('customDataType', $entityName);
9f1073d2 81 $form->assign('customDataSubType', $entitySubType);
e4f5c851 82 $form->assign('entityID', $entityID);
6452294d
MW
83 }
84
6a488035
TO
85 /**
86 * @param CRM_Core_Form $form
87 * @param null|string $subName
88 * @param null|string $subType
89 * @param null|int $groupCount
e8646905 90 * @param string $type
6a488035 91 * @param null|int $entityID
77b97be7 92 * @param null $onlySubType
6452294d
MW
93 *
94 * @throws \CRM_Core_Exception
6a488035 95 */
2da40d21 96 public static function preProcess(
40b55be5 97 &$form, $subName = NULL, $subType = NULL,
63dbed83 98 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
6a488035
TO
99 ) {
100 if ($type) {
101 $form->_type = $type;
102 }
103 else {
104 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
105 }
106
107 if (isset($subType)) {
108 $form->_subType = $subType;
109 }
110 else {
111 $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form);
112 }
113
114 if ($form->_subType == 'null') {
115 $form->_subType = NULL;
116 }
117
118 if (isset($subName)) {
119 $form->_subName = $subName;
120 }
121 else {
122 $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form);
123 }
124
125 if ($form->_subName == 'null') {
126 $form->_subName = NULL;
127 }
128
129 if ($groupCount) {
130 $form->_groupCount = $groupCount;
131 }
132 else {
133 $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form);
134 }
135
136 $form->assign('cgCount', $form->_groupCount);
137
138 //carry qf key, since this form is not inhereting core form.
a3d827a7 139 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String')) {
6a488035
TO
140 $form->assign('qfKey', $qfKey);
141 }
142
143 if ($entityID) {
144 $form->_entityId = $entityID;
145 }
146 else {
147 $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form);
148 }
149
a3d827a7
CW
150 $typeCheck = CRM_Utils_Request::retrieve('type', 'String');
151 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive');
481a74f4 152 if (isset($typeCheck) && $urlGroupId) {
6a488035 153 $form->_groupID = $urlGroupId;
0db6c3e1
TO
154 }
155 else {
6a488035
TO
156 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
157 }
158
159 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
160 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
161
162 $subType = $form->_subType;
163 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
164 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
165 }
8ef12e64 166
5b15263a
EM
167 self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
168 }
169
170 /**
171 * @param CRM_Core_Form $form
172 *
173 * @return array
174 */
175 public static function setDefaultValues(&$form) {
be2fb01f 176 $defaults = [];
5b15263a
EM
177 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
178 return $defaults;
179 }
180
181 /**
182 * @param CRM_Core_Form $form
183 */
184 public static function buildQuickForm(&$form) {
185 $form->addElement('hidden', 'hidden_custom', 1);
186 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
187 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
188 }
189
190 /**
1203c8df 191 * Add the group data as a formatted array to the form.
192 *
193 * @param CRM_Core_Form $form
194 * @param string $subType
041ecc95 195 * @param int $gid
1203c8df 196 * @param bool $onlySubType
197 * @param bool $getCachedTree
5b15263a
EM
198 *
199 * @return array
1203c8df 200 * @throws \CRM_Core_Exception
5b15263a 201 */
1203c8df 202 public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
e1d48b72 203 $singleRecord = NULL;
1b56bd01 204 if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
e1d48b72 205 $singleRecord = $form->_groupCount;
206 }
207 $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
1b56bd01 208 // when a new record is being added for multivalued custom fields.
209 if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
210 !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
e1d48b72 211 $singleRecord = 'new';
212 }
5b15263a
EM
213
214 $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
0b330e6d 215 NULL,
6a488035
TO
216 $form->_entityId,
217 $gid,
218 $subType,
219 $form->_subName,
63dbed83 220 $getCachedTree,
e1d48b72 221 $onlySubType,
222 FALSE,
223 TRUE,
224 $singleRecord
6a488035
TO
225 );
226
227 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
ec3de1a9 228 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
6a488035
TO
229 }
230 // we should use simplified formatted groupTree
231 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
232
233 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
234 $keys = array_keys($groupTree);
235 foreach ($keys as $key) {
236 $form->_groupTree[$key] = $groupTree[$key];
237 }
be2fb01f 238 return [$form, $groupTree];
6a488035
TO
239 }
240 else {
241 $form->_groupTree = $groupTree;
be2fb01f 242 return [$form, $groupTree];
6a488035
TO
243 }
244 }
245
6a488035 246}