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 // 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;
63 $entitySubType = $form->getEntitySubTypeId($subType);
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);
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);
79 * @param CRM_Core_Form $form
80 * @param null|string $subName
81 * @param null|string $subType
82 * @param null|int $groupCount
84 * @param null|int $entityID
85 * @param null $onlySubType
87 * @throws \CRM_Core_Exception
89 public static function preProcess(
90 &$form, $subName = NULL, $subType = NULL,
91 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
97 $form->_type
= CRM_Utils_Request
::retrieve('type', 'String', $form);
100 if (isset($subType)) {
101 $form->_subType
= $subType;
104 $form->_subType
= CRM_Utils_Request
::retrieve('subType', 'String', $form);
107 if ($form->_subType
== 'null') {
108 $form->_subType
= NULL;
111 if (isset($subName)) {
112 $form->_subName
= $subName;
115 $form->_subName
= CRM_Utils_Request
::retrieve('subName', 'String', $form);
118 if ($form->_subName
== 'null') {
119 $form->_subName
= NULL;
123 $form->_groupCount
= $groupCount;
126 $form->_groupCount
= CRM_Utils_Request
::retrieve('cgcount', 'Positive', $form);
129 $form->assign('cgCount', $form->_groupCount
);
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);
137 $form->_entityId
= $entityID;
140 $form->_entityId
= CRM_Utils_Request
::retrieve('entityID', 'Positive', $form);
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;
149 $form->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $form);
152 $gid = (isset($form->_groupID
)) ?
$form->_groupID
: NULL;
153 $getCachedTree = isset($form->_getCachedTree
) ?
$form->_getCachedTree
: TRUE;
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
));
160 self
::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
164 * @param CRM_Core_Form $form
168 public static function setDefaultValues(&$form) {
170 CRM_Core_BAO_CustomGroup
::setDefaults($form->_groupTree
, $defaults, FALSE, FALSE, $form->get('action'));
175 * @param CRM_Core_Form $form
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
);
187 * @param $onlySubType
188 * @param $getCachedTree
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
;
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';
204 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($form->_type
,
217 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
218 $form->_customValueCount
= CRM_Core_BAO_CustomGroup
::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId
);
220 // we should use simplified formatted groupTree
221 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $form->_groupCount
, $form);
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];
228 return array($form, $groupTree);
231 $form->_groupTree
= $groupTree;
232 return array($form, $groupTree);