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
33 trait CRM_Core_Form_EntityFormTrait
{
36 * The entity subtype ID (eg. for Relationship / Activity)
40 protected $_entitySubTypeId;
43 * Get entity fields for the entity to be added to the form.
47 public function getEntityFields() {
48 return $this->entityFields
;
52 * Explicitly declare the form context.
54 public function getDefaultContext() {
59 * Get entity fields for the entity to be added to the form.
63 public function getDeleteMessage() {
64 return $this->deleteMessage
;
68 * Set the delete message.
70 * We do this from the constructor in order to do a translation.
72 public function setDeleteMessage() {
76 * Set entity fields to be assigned to the form.
78 protected function setEntityFields() {
82 * Get the entity id being edited.
86 public function getEntityId() {
91 * Should custom data be suppressed on this form.
95 protected function isSuppressCustomData() {
100 * Get the entity subtype ID being edited
106 public function getEntitySubTypeId($subTypeId) {
110 return $this->_entitySubTypeId
;
114 * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
116 public function addCustomDataToForm() {
117 if ($this->isSuppressCustomData()) {
120 $customisableEntities = CRM_Core_SelectValues
::customGroupExtends();
121 if (isset($customisableEntities[$this->getDefaultEntity()])) {
122 CRM_Custom_Form_CustomData
::addToForm($this);
127 * Build the form object.
129 public function buildQuickEntityForm() {
130 if ($this->isDeleteContext()) {
131 $this->buildDeleteForm();
134 $this->applyFilter('__ALL__', 'trim');
135 $this->addEntityFieldsToTemplate();
136 $this->assign('entityFields', $this->entityFields
);
137 $this->assign('entityID', $this->getEntityId());
138 $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
139 $this->assign('entityTable', CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($this->getDefaultEntity())));
140 $this->addCustomDataToForm();
141 $this->addFormButtons();
143 if ($this->isViewContext()) {
149 * Build the form for any deletion.
151 protected function buildDeleteForm() {
152 $this->assign('deleteMessage', $this->getDeleteMessage());
153 $this->addFormButtons();
157 * Add relevant buttons to the form.
159 protected function addFormButtons() {
160 if ($this->isViewContext() ||
$this->_action
& CRM_Core_Action
::PREVIEW
) {
164 'name' => ts('Done'),
173 'name' => $this->isDeleteContext() ?
ts('Delete') : ts('Save'),
178 'name' => ts('Cancel'),
185 * Get the defaults for the entity.
187 protected function getEntityDefaults() {
188 $defaults = $moneyFields = [];
190 if (!$this->isDeleteContext() &&
191 $this->getEntityId()) {
192 $params = ['id' => $this->getEntityId()];
193 $baoName = $this->_BAOName
;
194 $baoName::retrieve($params, $defaults);
196 foreach ($this->entityFields
as $entityFieldName => $fieldSpec) {
197 $value = CRM_Utils_Request
::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
198 if ($value !== FALSE && $value !== NULL) {
199 $defaults[$fieldSpec['name']] = $value;
201 // Store a list of fields with money formatters
202 if (CRM_Utils_Array
::value('formatter', $fieldSpec) == 'crmMoney') {
203 $moneyFields[] = $entityFieldName;
206 if (!empty($defaults['currency'])) {
207 // If we have a money formatter we need to pass the specified currency or it will render as the default
208 foreach ($moneyFields as $entityFieldName) {
209 $this->entityFields
[$entityFieldName]['formatterParam'] = $defaults['currency'];
213 // Assign again as we may have modified above
214 $this->assign('entityFields', $this->entityFields
);
219 * Get the validation rule to apply to a function.
221 * Alphanumeric is designed to always be safe & for now we just return
222 * that but in future we can use tighter rules for types like int, bool etc.
224 * @param string $fieldName
226 * @return string|int|bool
228 protected function getValidationTypeForField($fieldName) {
229 switch ($this->metadata
[$fieldName]['type']) {
230 case CRM_Utils_Type
::T_BOOLEAN
:
234 return 'Alphanumeric';
239 * Set translated fields.
241 * This function is called from the class constructor, allowing us to set
242 * fields on the class that can't be set as properties due to need for
243 * translation or other non-input specific handling.
245 protected function setTranslatedFields() {
246 $this->setEntityFields();
247 $this->setDeleteMessage();
248 $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']);
249 $this->metadata
= $metadata['values'];
250 foreach ($this->metadata
as $fieldName => $spec) {
251 if (isset($this->entityFields
[$fieldName])) {
252 if ($spec['localizable']) {
253 $this->entityFields
[$fieldName]['is_add_translate_dialog'] = TRUE;
255 if (empty($spec['html'])) {
256 $this->entityFields
[$fieldName]['not-auto-addable'] = TRUE;
263 * Add defined entity field to template.
265 protected function addEntityFieldsToTemplate() {
266 foreach ($this->getEntityFields() as $fieldSpec) {
267 if (empty($fieldSpec['not-auto-addable'])) {
268 $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array
::value('required', $fieldSpec), FALSE);
269 if (!empty($fieldSpec['is_freeze'])) {
277 * Is the form being used in the context of a deletion.
279 * (For some reason rather than having separate forms Civi overloads one form).
283 protected function isDeleteContext() {
284 return ($this->_action
& CRM_Core_Action
::DELETE
);
288 * Is the form being used in the context of a view.
292 protected function isViewContext() {
293 return ($this->_action
& CRM_Core_Action
::VIEW
);
296 protected function setEntityFieldsMetadata() {
297 foreach ($this->entityFields
as $field => &$props) {
298 if (!empty($props['not-auto-addable'])) {
299 // We can't load this field using metadata
302 if ($field != 'id' && $this->isDeleteContext()) {
303 // Delete forms don't generally present any fields to edit
307 if (empty($props['action'])) {
308 $props['action'] = $this->getApiAction();
310 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', $props);
311 $fieldSpec = $fieldSpec['values'];
312 if (!isset($props['description']) && isset($fieldSpec['description'])) {
313 $props['description'] = $fieldSpec['description'];