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 = NULL;
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
88 public function getEntitySubTypeId() {
89 return $this->_entitySubTypeId
;
93 * Set the entity subtype ID being edited
97 public function setEntitySubTypeId($subTypeId) {
98 $this->_entitySubTypeId
= $subTypeId;
102 * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
104 public function addCustomDataToForm() {
105 if ($this->isSuppressCustomData()) {
108 $customisableEntities = CRM_Core_SelectValues
::customGroupExtends();
109 if (isset($customisableEntities[$this->getDefaultEntity()])) {
110 CRM_Custom_Form_CustomData
::addToForm($this, $this->getEntitySubTypeId());
115 * Build the form object.
117 public function buildQuickEntityForm() {
118 if ($this->isDeleteContext()) {
119 $this->buildDeleteForm();
122 $this->applyFilter('__ALL__', 'trim');
123 $this->addEntityFieldsToTemplate();
124 $this->assign('entityFields', $this->entityFields
);
125 $this->assign('entityID', $this->getEntityId());
126 $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
127 $this->assign('entityTable', CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($this->getDefaultEntity())));
128 $this->addCustomDataToForm();
129 $this->addFormButtons();
131 if ($this->isViewContext()) {
137 * Build the form for any deletion.
139 protected function buildDeleteForm() {
140 $this->assign('deleteMessage', $this->getDeleteMessage());
141 $this->addFormButtons();
145 * Add relevant buttons to the form.
147 protected function addFormButtons() {
148 if ($this->isViewContext() ||
$this->_action
& CRM_Core_Action
::PREVIEW
) {
152 'name' => ts('Done'),
161 'name' => $this->isDeleteContext() ?
ts('Delete') : ts('Save'),
166 'name' => ts('Cancel'),
173 * Get the defaults for the entity.
175 protected function getEntityDefaults() {
176 $defaults = $moneyFields = [];
178 if (!$this->isDeleteContext() &&
179 $this->getEntityId()) {
180 $params = ['id' => $this->getEntityId()];
181 $baoName = $this->_BAOName
;
182 $baoName::retrieve($params, $defaults);
184 foreach ($this->entityFields
as $entityFieldName => $fieldSpec) {
185 $value = CRM_Utils_Request
::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
186 if ($value !== FALSE && $value !== NULL) {
187 $defaults[$fieldSpec['name']] = $value;
189 // Store a list of fields with money formatters
190 if (CRM_Utils_Array
::value('formatter', $fieldSpec) == 'crmMoney') {
191 $moneyFields[] = $entityFieldName;
194 if (!empty($defaults['currency'])) {
195 // If we have a money formatter we need to pass the specified currency or it will render as the default
196 foreach ($moneyFields as $entityFieldName) {
197 $this->entityFields
[$entityFieldName]['formatterParam'] = $defaults['currency'];
201 // Assign again as we may have modified above
202 $this->assign('entityFields', $this->entityFields
);
207 * Get the validation rule to apply to a function.
209 * Alphanumeric is designed to always be safe & for now we just return
210 * that but in future we can use tighter rules for types like int, bool etc.
212 * @param string $fieldName
214 * @return string|int|bool
216 protected function getValidationTypeForField($fieldName) {
217 switch ($this->metadata
[$fieldName]['type']) {
218 case CRM_Utils_Type
::T_BOOLEAN
:
222 return 'Alphanumeric';
227 * Set translated fields.
229 * This function is called from the class constructor, allowing us to set
230 * fields on the class that can't be set as properties due to need for
231 * translation or other non-input specific handling.
233 protected function setTranslatedFields() {
234 $this->setEntityFields();
235 $this->setDeleteMessage();
236 $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']);
237 $this->metadata
= $metadata['values'];
238 foreach ($this->metadata
as $fieldName => $spec) {
239 if (isset($this->entityFields
[$fieldName])) {
240 if ($spec['localizable']) {
241 $this->entityFields
[$fieldName]['is_add_translate_dialog'] = TRUE;
243 if (empty($spec['html'])) {
244 $this->entityFields
[$fieldName]['not-auto-addable'] = TRUE;
251 * Add defined entity field to template.
253 protected function addEntityFieldsToTemplate() {
254 foreach ($this->getEntityFields() as $fieldSpec) {
255 if (empty($fieldSpec['not-auto-addable'])) {
256 $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array
::value('required', $fieldSpec), FALSE);
257 if (!empty($fieldSpec['is_freeze'])) {
265 * Is the form being used in the context of a deletion.
267 * (For some reason rather than having separate forms Civi overloads one form).
271 protected function isDeleteContext() {
272 return ($this->_action
& CRM_Core_Action
::DELETE
);
276 * Is the form being used in the context of a view.
280 protected function isViewContext() {
281 return ($this->_action
& CRM_Core_Action
::VIEW
);
284 protected function setEntityFieldsMetadata() {
285 foreach ($this->entityFields
as $field => &$props) {
286 if (!empty($props['not-auto-addable'])) {
287 // We can't load this field using metadata
290 if ($field != 'id' && $this->isDeleteContext()) {
291 // Delete forms don't generally present any fields to edit
295 if (empty($props['action'])) {
296 $props['action'] = $this->getApiAction();
298 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', $props);
299 $fieldSpec = $fieldSpec['values'];
300 if (!isset($props['description']) && isset($fieldSpec['description'])) {
301 $props['description'] = $fieldSpec['description'];