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
17 trait CRM_Core_Form_EntityFormTrait
{
20 * The entity subtype ID (eg. for Relationship / Activity)
24 protected $_entitySubTypeId;
27 * Get entity fields for the entity to be added to the form.
31 public function getEntityFields() {
32 return $this->entityFields
;
36 * Explicitly declare the form context.
38 public function getDefaultContext() {
43 * Get entity fields for the entity to be added to the form.
47 public function getDeleteMessage() {
48 return $this->deleteMessage
;
52 * Set the delete message.
54 * We do this from the constructor in order to do a translation.
56 public function setDeleteMessage() {
60 * Set entity fields to be assigned to the form.
62 protected function setEntityFields() {
66 * Get the entity id being edited.
70 public function getEntityId() {
75 * Should custom data be suppressed on this form.
79 protected function isSuppressCustomData() {
84 * Get the entity subtype ID being edited
90 public function getEntitySubTypeId($subTypeId) {
94 return $this->_entitySubTypeId
;
98 * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
100 public function addCustomDataToForm() {
101 if ($this->isSuppressCustomData()) {
104 $customisableEntities = CRM_Core_SelectValues
::customGroupExtends();
105 if (isset($customisableEntities[$this->getDefaultEntity()])) {
106 CRM_Custom_Form_CustomData
::addToForm($this);
111 * Build the form object.
113 public function buildQuickEntityForm() {
114 if ($this->isDeleteContext()) {
115 $this->buildDeleteForm();
118 $this->applyFilter('__ALL__', 'trim');
119 $this->addEntityFieldsToTemplate();
120 $this->assign('entityFields', $this->entityFields
);
121 $this->assign('entityID', $this->getEntityId());
122 $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
123 $this->assign('entityTable', CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($this->getDefaultEntity())));
124 $this->addCustomDataToForm();
125 $this->addFormButtons();
127 if ($this->isViewContext()) {
133 * Build the form for any deletion.
135 protected function buildDeleteForm() {
136 $this->assign('deleteMessage', $this->getDeleteMessage());
137 $this->addFormButtons();
141 * Add relevant buttons to the form.
143 protected function addFormButtons() {
144 if ($this->isViewContext() ||
$this->_action
& CRM_Core_Action
::PREVIEW
) {
148 'name' => ts('Done'),
157 'name' => $this->isDeleteContext() ?
ts('Delete') : ts('Save'),
162 'name' => ts('Cancel'),
169 * Get the defaults for the entity.
171 protected function getEntityDefaults() {
172 $defaults = $moneyFields = [];
174 if (!$this->isDeleteContext() &&
175 $this->getEntityId()) {
176 $params = ['id' => $this->getEntityId()];
177 $baoName = $this->_BAOName
;
178 $baoName::retrieve($params, $defaults);
180 foreach ($this->entityFields
as $entityFieldName => $fieldSpec) {
181 $value = CRM_Utils_Request
::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
182 if ($value !== FALSE && $value !== NULL) {
183 $defaults[$fieldSpec['name']] = $value;
185 // Store a list of fields with money formatters
186 if (CRM_Utils_Array
::value('formatter', $fieldSpec) == 'crmMoney') {
187 $moneyFields[] = $entityFieldName;
190 if (!empty($defaults['currency'])) {
191 // If we have a money formatter we need to pass the specified currency or it will render as the default
192 foreach ($moneyFields as $entityFieldName) {
193 $this->entityFields
[$entityFieldName]['formatterParam'] = $defaults['currency'];
197 // Assign again as we may have modified above
198 $this->assign('entityFields', $this->entityFields
);
203 * Get the validation rule to apply to a function.
205 * Alphanumeric is designed to always be safe & for now we just return
206 * that but in future we can use tighter rules for types like int, bool etc.
208 * @param string $fieldName
210 * @return string|int|bool
212 protected function getValidationTypeForField($fieldName) {
213 switch ($this->metadata
[$fieldName]['type']) {
214 case CRM_Utils_Type
::T_BOOLEAN
:
218 return 'Alphanumeric';
223 * Set translated fields.
225 * This function is called from the class constructor, allowing us to set
226 * fields on the class that can't be set as properties due to need for
227 * translation or other non-input specific handling.
229 protected function setTranslatedFields() {
230 $this->setEntityFields();
231 $this->setDeleteMessage();
232 $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']);
233 $this->metadata
= $metadata['values'];
234 foreach ($this->metadata
as $fieldName => $spec) {
235 if (isset($this->entityFields
[$fieldName])) {
236 if ($spec['localizable']) {
237 $this->entityFields
[$fieldName]['is_add_translate_dialog'] = TRUE;
239 if (empty($spec['html'])) {
240 $this->entityFields
[$fieldName]['not-auto-addable'] = TRUE;
247 * Add defined entity field to template.
249 protected function addEntityFieldsToTemplate() {
250 foreach ($this->getEntityFields() as $fieldSpec) {
251 if (empty($fieldSpec['not-auto-addable'])) {
252 $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array
::value('required', $fieldSpec), FALSE);
253 if (!empty($fieldSpec['is_freeze'])) {
261 * Is the form being used in the context of a deletion.
263 * (For some reason rather than having separate forms Civi overloads one form).
267 protected function isDeleteContext() {
268 return ($this->_action
& CRM_Core_Action
::DELETE
);
272 * Is the form being used in the context of a view.
276 protected function isViewContext() {
277 return ($this->_action
& CRM_Core_Action
::VIEW
);
280 protected function setEntityFieldsMetadata() {
281 foreach ($this->entityFields
as $field => &$props) {
282 if (!empty($props['not-auto-addable'])) {
283 // We can't load this field using metadata
286 if ($field != 'id' && $this->isDeleteContext()) {
287 // Delete forms don't generally present any fields to edit
291 if (empty($props['action'])) {
292 $props['action'] = $this->getApiAction();
294 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', $props);
295 $fieldSpec = $fieldSpec['values'];
296 if (!isset($props['description']) && isset($fieldSpec['description'])) {
297 $props['description'] = $fieldSpec['description'];