3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * this class builds custom data
37 class CRM_Custom_Form_CustomData
{
40 * Generic wrapper to add custom data to a form via a single line in preProcess.
42 * $this->getDefaultEntity() must be defined for the form class for this to work.
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());
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
53 * @throws \CRM_Core_Exception
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);
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);
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);
73 * @param CRM_Core_Form $form
74 * @param null|string $subName
75 * @param null|string $subType
76 * @param null|int $groupCount
78 * @param null|int $entityID
79 * @param null $onlySubType
81 * @throws \CRM_Core_Exception
83 public static function preProcess(
84 &$form, $subName = NULL, $subType = NULL,
85 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
91 $form->_type
= CRM_Utils_Request
::retrieve('type', 'String', $form);
94 if (isset($subType)) {
95 $form->_subType
= $subType;
98 $form->_subType
= CRM_Utils_Request
::retrieve('subType', 'String', $form);
101 if ($form->_subType
== 'null') {
102 $form->_subType
= NULL;
105 if (isset($subName)) {
106 $form->_subName
= $subName;
109 $form->_subName
= CRM_Utils_Request
::retrieve('subName', 'String', $form);
112 if ($form->_subName
== 'null') {
113 $form->_subName
= NULL;
117 $form->_groupCount
= $groupCount;
120 $form->_groupCount
= CRM_Utils_Request
::retrieve('cgcount', 'Positive', $form);
123 $form->assign('cgCount', $form->_groupCount
);
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);
131 $form->_entityId
= $entityID;
134 $form->_entityId
= CRM_Utils_Request
::retrieve('entityID', 'Positive', $form);
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;
143 $form->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $form);
146 $gid = (isset($form->_groupID
)) ?
$form->_groupID
: NULL;
147 $getCachedTree = isset($form->_getCachedTree
) ?
$form->_getCachedTree
: TRUE;
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
));
154 self
::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
158 * @param CRM_Core_Form $form
162 public static function setDefaultValues(&$form) {
164 CRM_Core_BAO_CustomGroup
::setDefaults($form->_groupTree
, $defaults, FALSE, FALSE, $form->get('action'));
169 * @param CRM_Core_Form $form
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
);
181 * @param $onlySubType
182 * @param $getCachedTree
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
;
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';
198 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($form->_type
,
211 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
212 $form->_customValueCount
= CRM_Core_BAO_CustomGroup
::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId
);
214 // we should use simplified formatted groupTree
215 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $form->_groupCount
, $form);
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];
222 return array($form, $groupTree);
225 $form->_groupTree
= $groupTree;
226 return array($form, $groupTree);