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