Merge pull request #13425 from mfb/utf8mb4
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * this class builds custom data
36 */
37 class CRM_Custom_Form_CustomData {
38
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();
57 $entityID = $form->getEntityId();
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 }
65
66 // when custom data is included in this page
67 if (!empty($_POST['hidden_custom'])) {
68 self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
69 self::buildQuickForm($form);
70 self::setDefaultValues($form);
71 }
72 // need to assign custom data type and subtype to the template
73 $form->assign('customDataType', $entityName);
74 $form->assign('customDataSubType', $entitySubType);
75 $form->assign('entityID', $entityID);
76 }
77
78 /**
79 * @param CRM_Core_Form $form
80 * @param null|string $subName
81 * @param null|string $subType
82 * @param null|int $groupCount
83 * @param string $type
84 * @param null|int $entityID
85 * @param null $onlySubType
86 *
87 * @throws \CRM_Core_Exception
88 */
89 public static function preProcess(
90 &$form, $subName = NULL, $subType = NULL,
91 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
92 ) {
93 if ($type) {
94 $form->_type = $type;
95 }
96 else {
97 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
98 }
99
100 if (isset($subType)) {
101 $form->_subType = $subType;
102 }
103 else {
104 $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form);
105 }
106
107 if ($form->_subType == 'null') {
108 $form->_subType = NULL;
109 }
110
111 if (isset($subName)) {
112 $form->_subName = $subName;
113 }
114 else {
115 $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form);
116 }
117
118 if ($form->_subName == 'null') {
119 $form->_subName = NULL;
120 }
121
122 if ($groupCount) {
123 $form->_groupCount = $groupCount;
124 }
125 else {
126 $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form);
127 }
128
129 $form->assign('cgCount', $form->_groupCount);
130
131 //carry qf key, since this form is not inhereting core form.
132 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String')) {
133 $form->assign('qfKey', $qfKey);
134 }
135
136 if ($entityID) {
137 $form->_entityId = $entityID;
138 }
139 else {
140 $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form);
141 }
142
143 $typeCheck = CRM_Utils_Request::retrieve('type', 'String');
144 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive');
145 if (isset($typeCheck) && $urlGroupId) {
146 $form->_groupID = $urlGroupId;
147 }
148 else {
149 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
150 }
151
152 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
153 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
154
155 $subType = $form->_subType;
156 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
157 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
158 }
159
160 self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
161 }
162
163 /**
164 * @param CRM_Core_Form $form
165 *
166 * @return array
167 */
168 public static function setDefaultValues(&$form) {
169 $defaults = array();
170 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
171 return $defaults;
172 }
173
174 /**
175 * @param CRM_Core_Form $form
176 */
177 public static function buildQuickForm(&$form) {
178 $form->addElement('hidden', 'hidden_custom', 1);
179 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
180 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
181 }
182
183 /**
184 * @param $form
185 * @param $subType
186 * @param $gid
187 * @param $onlySubType
188 * @param $getCachedTree
189 *
190 * @return array
191 */
192 public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = FALSE) {
193 $singleRecord = NULL;
194 if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
195 $singleRecord = $form->_groupCount;
196 }
197 $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
198 // when a new record is being added for multivalued custom fields.
199 if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
200 !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
201 $singleRecord = 'new';
202 }
203
204 $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
205 NULL,
206 $form->_entityId,
207 $gid,
208 $subType,
209 $form->_subName,
210 $getCachedTree,
211 $onlySubType,
212 FALSE,
213 TRUE,
214 $singleRecord
215 );
216
217 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
218 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
219 }
220 // we should use simplified formatted groupTree
221 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
222
223 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
224 $keys = array_keys($groupTree);
225 foreach ($keys as $key) {
226 $form->_groupTree[$key] = $groupTree[$key];
227 }
228 return array($form, $groupTree);
229 }
230 else {
231 $form->_groupTree = $groupTree;
232 return array($form, $groupTree);
233 }
234 }
235
236 }