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 * Get the entity subtype ID being edited
97 public function getEntitySubTypeId($subTypeId) {
101 return $this->_entitySubTypeId
;
105 * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
107 public function addCustomDataToForm() {
108 $customisableEntities = CRM_Core_SelectValues
::customGroupExtends();
109 if (isset($customisableEntities[$this->getDefaultEntity()])) {
110 CRM_Custom_Form_CustomData
::addToForm($this);
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'];