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;
56 * Which blocks should we show and hide.
58 * @var CRM_Core_ShowHideBlocks
67 protected $_groupTitle;
70 * Array group display status.
74 protected $_groupCollapseDisplay;
83 public $_multiRecordDisplay;
88 * Pre processing work done here.
90 * Gets session variables for table name, id of entity in table, type of entity and stores them.
92 public function preProcess() {
93 $this->_cdType
= $_GET['type'] ??
NULL;
94 $this->assign('cdType', FALSE);
95 $this->_multiRecordDisplay
= CRM_Utils_Request
::retrieve('multiRecordDisplay', 'String', $this);
96 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
98 $this->assign('cdType', TRUE);
100 // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func
101 // this is due to some condition inside it which restricts it from saving in session
102 // so doing this for multi record edit action
103 $entityId = CRM_Utils_Request
::retrieve('entityID', 'Positive', $this);
104 if (!empty($entityId)) {
105 $subType = CRM_Contact_BAO_Contact
::getContactSubType($entityId, ',');
107 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
108 if ($this->_multiRecordDisplay
) {
109 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this);
110 $this->_tableID
= $this->_entityId
;
111 $this->_contactType
= CRM_Contact_BAO_Contact
::getContactType($this->_tableID
);
112 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $this);
113 $hasReachedMax = CRM_Core_BAO_CustomGroup
::hasReachedMaxLimit($this->_groupID
, $this->_tableID
);
114 if ($hasReachedMax && $mode == 'add') {
115 CRM_Core_Error
::statusBounce(ts('The maximum record limit is reached'));
117 $this->_copyValueId
= CRM_Utils_Request
::retrieve('copyValueId', 'Positive', $this);
119 $groupTitle = CRM_Core_BAO_CustomGroup
::getTitle($this->_groupID
);
120 $mode = CRM_Utils_Request
::retrieve('mode', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'GET');
121 $mode = ucfirst($mode);
122 CRM_Utils_System
::setTitle(ts('%1 %2 Record', [1 => $mode, 2 => $groupTitle]));
124 if (!empty($_POST['hidden_custom'])) {
125 $this->assign('postedInfo', TRUE);
130 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this, TRUE);
131 $this->_tableID
= CRM_Utils_Request
::retrieve('tableId', 'Positive', $this, TRUE);
133 $this->_contactType
= CRM_Contact_BAO_Contact
::getContactType($this->_tableID
);
134 $this->_contactSubType
= CRM_Contact_BAO_Contact
::getContactSubType($this->_tableID
, ',');
135 $this->assign('contact_type', $this->_contactType
);
136 $this->assign('contact_subtype', $this->_contactSubType
);
137 list($displayName, $contactImage) = CRM_Contact_BAO_Contact
::getDisplayAndImage($this->_tableID
);
138 CRM_Utils_System
::setTitle($displayName, $contactImage . ' ' . $displayName);
140 // when custom data is included in this page
141 if (!empty($_POST['hidden_custom'])) {
142 for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID
]; $i++
) {
143 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_contactSubType
, $i, $this->_contactType
, $this->_tableID
);
144 CRM_Custom_Form_CustomData
::buildQuickForm($this);
145 CRM_Custom_Form_CustomData
::setDefaultValues($this);
151 * Build the form object.
153 public function buildQuickForm() {
154 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
155 // buttons display for multi-valued fields to perform independednt actions
156 if ($this->_multiRecordDisplay
) {
157 $isMultiple = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
162 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay
);
163 $saveButtonName = $this->_copyValueId ?
ts('Save a Copy') : ts('Save');
167 'name' => $saveButtonName,
172 'name' => ts('Save and New'),
177 'name' => ts('Cancel'),
182 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
185 //need to assign custom data type and subtype to the template
186 $this->assign('entityID', $this->_tableID
);
187 $this->assign('groupID', $this->_groupID
);
189 // make this form an upload since we dont know if the custom data injected dynamically
190 // is of type file etc
194 'name' => ts('Save'),
199 'name' => ts('Cancel'),
205 * Set the default form values.
209 * the default array reference
211 public function setDefaultValues() {
212 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
213 if ($this->_copyValueId
) {
214 // cached tree is fetched
215 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($this->_type
,
227 $valueIdDefaults = [];
228 $groupTreeValueId = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $this->_copyValueId
, $this);
229 CRM_Core_BAO_CustomGroup
::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
230 $tableId = $groupTreeValueId[$this->_groupID
]['table_id'];
231 foreach ($valueIdDefaults as $valueIdElementName => $value) {
232 // build defaults for COPY action for new record saving
233 $valueIdElementNamePieces = explode('_', $valueIdElementName);
234 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
235 $elementName = implode('_', $valueIdElementNamePieces);
236 $customDefaultValue[$elementName] = $value;
240 $customDefaultValue = CRM_Custom_Form_CustomData
::setDefaultValues($this);
242 return $customDefaultValue;
245 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($this->_contactType
,
249 $this->_contactSubType
252 if (empty($_POST['hidden_custom_group_count'])) {
253 // custom data building in edit mode (required to handle multi-value)
254 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($this->_contactType
, NULL, $this->_tableID
,
255 $this->_groupID
, $this->_contactSubType
257 $customValueCount = CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID
, NULL, NULL, $this->_tableID
);
260 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID
];
263 $this->assign('customValueCount', $customValueCount);
270 * Process the user submitted custom data values.
272 public function postProcess() {
273 // Get the form values and groupTree
275 $params = $this->controller
->exportValues($this->_name
);
277 CRM_Core_BAO_CustomValueTable
::postProcess($params,
282 $table = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID
, 'table_name');
283 $cgcount = CRM_Core_BAO_CustomGroup
::customGroupDataExistsForEntity($this->_tableID
, $table, TRUE);
285 $buttonName = $this->controller
->getButtonName();
286 if ($buttonName == $this->getButtonName('upload', 'new')) {
287 CRM_Core_Session
::singleton()
288 ->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"));
291 // Add entry in the log table
292 CRM_Core_BAO_Log
::register($this->_tableID
,
297 if (CRM_Core_Resources
::isAjaxMode()) {
298 $this->ajaxResponse +
= CRM_Contact_Form_Inline
::renderFooter($this->_tableID
);
301 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();