3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for custom data.
21 * It delegates the work to lower level subclasses and integrates the changes
22 * back in. It also uses a lot of functionality with the CRM API's, so any change
23 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
25 class CRM_Contact_Form_CustomData
extends CRM_Core_Form
{
28 * The table id, used when editing/creating custom data
35 * Entity type of the table id
39 protected $_entityType;
42 * Entity sub type of the table id
46 protected $_entitySubType;
53 //protected $_groupTree;
60 protected $_groupTitle;
63 * Array group display status.
67 protected $_groupCollapseDisplay;
76 public $_multiRecordDisplay;
81 * Pre processing work done here.
83 * Gets session variables for table name, id of entity in table, type of entity and stores them.
85 public function preProcess() {
86 $this->_cdType
= $_GET['type'] ??
NULL;
87 $this->assign('cdType', FALSE);
88 $this->_multiRecordDisplay
= CRM_Utils_Request
::retrieve('multiRecordDisplay', 'String', $this);
89 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
91 $this->assign('cdType', TRUE);
93 // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func
94 // this is due to some condition inside it which restricts it from saving in session
95 // so doing this for multi record edit action
96 $entityId = CRM_Utils_Request
::retrieve('entityID', 'Positive', $this);
97 if (!empty($entityId)) {
98 $subType = CRM_Contact_BAO_Contact
::getContactSubType($entityId, ',');
100 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
101 if ($this->_multiRecordDisplay
) {
102 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this);
103 $this->_tableID
= $this->_entityId
;
104 $this->_contactType
= CRM_Contact_BAO_Contact
::getContactType($this->_tableID
);
105 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $this);
106 $hasReachedMax = CRM_Core_BAO_CustomGroup
::hasReachedMaxLimit($this->_groupID
, $this->_tableID
);
107 if ($hasReachedMax && $mode === 'add') {
108 CRM_Core_Error
::statusBounce(ts('The maximum record limit is reached'));
110 $this->_copyValueId
= CRM_Utils_Request
::retrieve('copyValueId', 'Positive', $this);
112 $groupTitle = CRM_Core_BAO_CustomGroup
::getTitle($this->_groupID
);
115 CRM_Utils_System
::setTitle(ts('Add %1', [1 => $groupTitle]));
119 CRM_Utils_System
::setTitle(ts('Edit %1', [1 => $groupTitle]));
123 CRM_Utils_System
::setTitle(ts('Copy %1', [1 => $groupTitle]));
127 if (!empty($_POST['hidden_custom'])) {
128 $this->assign('postedInfo', TRUE);
133 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this, TRUE);
134 $this->_tableID
= CRM_Utils_Request
::retrieve('tableId', 'Positive', $this, TRUE);
136 $this->_contactType
= CRM_Contact_BAO_Contact
::getContactType($this->_tableID
);
137 $this->_contactSubType
= CRM_Contact_BAO_Contact
::getContactSubType($this->_tableID
, ',');
138 $this->assign('contact_type', $this->_contactType
);
139 $this->assign('contact_subtype', $this->_contactSubType
);
140 list($displayName, $contactImage) = CRM_Contact_BAO_Contact
::getDisplayAndImage($this->_tableID
);
141 CRM_Utils_System
::setTitle($displayName, $contactImage . ' ' . $displayName);
143 // when custom data is included in this page
144 if (!empty($_POST['hidden_custom'])) {
145 for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID
]; $i++
) {
146 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_contactSubType
, $i, $this->_contactType
, $this->_tableID
);
147 CRM_Custom_Form_CustomData
::buildQuickForm($this);
148 CRM_Custom_Form_CustomData
::setDefaultValues($this);
154 * Build the form object.
156 public function buildQuickForm() {
157 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
158 // buttons display for multi-valued fields to perform independednt actions
159 if ($this->_multiRecordDisplay
) {
160 $isMultiple = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
165 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay
);
166 $saveButtonName = $this->_copyValueId ?
ts('Save a Copy') : ts('Save');
170 'name' => $saveButtonName,
175 'name' => ts('Save and New'),
180 'name' => ts('Cancel'),
185 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
188 //need to assign custom data type and subtype to the template
189 $this->assign('entityID', $this->_tableID
);
190 $this->assign('groupID', $this->_groupID
);
192 // make this form an upload since we dont know if the custom data injected dynamically
193 // is of type file etc
197 'name' => ts('Save'),
202 'name' => ts('Cancel'),
208 * Set the default form values.
212 * the default array reference
214 public function setDefaultValues() {
215 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
216 if ($this->_copyValueId
) {
217 // cached tree is fetched
218 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($this->_type
,
227 CRM_Core_Permission
::EDIT
,
230 $valueIdDefaults = [];
231 $groupTreeValueId = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $this->_copyValueId
, $this);
232 CRM_Core_BAO_CustomGroup
::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
233 foreach ($valueIdDefaults as $valueIdElementName => $value) {
234 // build defaults for COPY action for new record saving
235 $valueIdElementNamePieces = explode('_', $valueIdElementName);
236 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
237 $elementName = implode('_', $valueIdElementNamePieces);
238 $customDefaultValue[$elementName] = $value;
242 $customDefaultValue = CRM_Custom_Form_CustomData
::setDefaultValues($this);
244 return $customDefaultValue;
247 if (empty($_POST['hidden_custom_group_count'])) {
248 // custom data building in edit mode (required to handle multi-value)
249 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($this->_contactType
, NULL, $this->_tableID
,
250 $this->_groupID
, $this->_contactSubType
252 $customValueCount = CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID
, NULL, NULL, $this->_tableID
);
255 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID
];
258 $this->assign('customValueCount', $customValueCount);
265 * Process the user submitted custom data values.
267 public function postProcess() {
268 // Get the form values and groupTree
270 $params = $this->controller
->exportValues($this->_name
);
272 CRM_Core_BAO_CustomValueTable
::postProcess($params,
277 $table = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID
, 'table_name');
278 $cgcount = CRM_Core_BAO_CustomGroup
::customGroupDataExistsForEntity($this->_tableID
, $table, TRUE);
280 $buttonName = $this->controller
->getButtonName();
281 if ($buttonName == $this->getButtonName('upload', 'new')) {
282 CRM_Core_Session
::singleton()
283 ->pushUserContext(CRM_Utils_System
::url('civicrm/contact/view/cd/edit', "reset=1&type={$this->_contactType}&groupID={$this->_groupID}&entityID={$this->_tableID}&cgcount={$cgcount}&multiRecordDisplay=single&mode=add"));
286 // Add entry in the log table
287 CRM_Core_BAO_Log
::register($this->_tableID
,
292 if (CRM_Core_Resources
::isAjaxMode()) {
293 $this->ajaxResponse +
= CRM_Contact_Form_Inline
::renderFooter($this->_tableID
);
296 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();