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
34 trait CRM_Core_Form_EntityFormTrait
{
37 * Get entity fields for the entity to be added to the form.
41 public function getEntityFields() {
42 return $this->entityFields
;
46 * Explicitly declare the form context.
48 public function getDefaultContext() {
53 * Get entity fields for the entity to be added to the form.
57 public function getDeleteMessage() {
58 return $this->deleteMessage
;
62 * Get the entity id being edited.
66 public function getEntityId() {
70 * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
72 public function addCustomDataToForm() {
73 $customisableEntities = CRM_Core_SelectValues
::customGroupExtends();
74 if (isset($customisableEntities[$this->getDefaultEntity()])) {
75 CRM_Custom_Form_CustomData
::addToForm($this);
80 * Build the form object.
82 public function buildQuickEntityForm() {
83 if ($this->isDeleteContext()) {
84 $this->buildDeleteForm();
87 $this->applyFilter('__ALL__', 'trim');
88 $this->addEntityFieldsToTemplate();
89 $this->assign('entityFields', $this->entityFields
);
90 $this->assign('entityID', $this->getEntityId());
91 $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
92 $this->assign('entityTable', CRM_Core_DAO_AllCoreTables
::getTableForClass(CRM_Core_DAO_AllCoreTables
::getFullName($this->getDefaultEntity())));
93 $this->addCustomDataToForm();
94 $this->addFormButtons();
98 * Build the form for any deletion.
100 protected function buildDeleteForm() {
101 $this->assign('deleteMessage', $this->getDeleteMessage());
102 $this->addFormButtons();
106 * Add relevant buttons to the form.
108 protected function addFormButtons() {
109 if ($this->_action
& CRM_Core_Action
::VIEW ||
$this->_action
& CRM_Core_Action
::PREVIEW
) {
110 $this->addButtons(array(
113 'name' => ts('Done'),
120 $this->addButtons(array(
123 'name' => $this->_action
& CRM_Core_Action
::DELETE ?
ts('Delete') : ts('Save'),
128 'name' => ts('Cancel'),
136 * Get the defaults for the entity.
138 protected function getEntityDefaults() {
140 if ($this->_action
!= CRM_Core_Action
::DELETE
&&
143 $params = ['id' => $this->getEntityId()];
144 $baoName = $this->_BAOName
;
145 $baoName::retrieve($params, $defaults);
147 foreach ($this->entityFields
as $fieldSpec) {
148 $value = CRM_Utils_Request
::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
149 if ($value !== FALSE) {
150 $defaults[$fieldSpec['name']] = $value;
157 * Get the validation rule to apply to a function.
159 * Alphanumeric is designed to always be safe & for now we just return
160 * that but in future we can use tighter rules for types like int, bool etc.
162 * @param string $fieldName
164 * @return string|int|bool
166 protected function getValidationTypeForField($fieldName) {
167 switch ($this->metadata
[$fieldName]['type']) {
168 case CRM_Utils_Type
::T_BOOLEAN
:
172 return 'Alphanumeric';
177 * Set translated fields.
179 * This function is called from the class constructor, allowing us to set
180 * fields on the class that can't be set as properties due to need for
181 * translation or other non-input specific handling.
183 protected function setTranslatedFields() {
184 $this->setEntityFields();
185 $this->setDeleteMessage();
186 $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']);
187 $this->metadata
= $metadata['values'];
188 foreach ($this->metadata
as $fieldName => $spec) {
189 if (isset($this->entityFields
[$fieldName])) {
190 if ($spec['localizable']) {
191 $this->entityFields
[$fieldName]['is_add_translate_dialog'] = TRUE;
193 if (empty($spec['html'])) {
194 $this->entityFields
[$fieldName]['not-auto-addable'] = TRUE;
201 * Add defined entity field to template.
203 protected function addEntityFieldsToTemplate() {
204 foreach ($this->getEntityFields() as $fieldSpec) {
205 if (empty($fieldSpec['not-auto-addable'])) {
206 $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array
::value('required', $fieldSpec), FALSE);
207 if (!empty($fieldSpec['is_freeze'])) {
215 * Is the form being used in the context of a deletion.
217 * (For some reason rather than having separate forms Civi overloads one form).
221 protected function isDeleteContext() {
222 return ($this->_action
& CRM_Core_Action
::DELETE
);
225 protected function setEntityFieldsMetadata() {
226 foreach ($this->entityFields
as $field => &$props) {
227 if (!empty($props['not-auto-addable'])) {
228 // We can't load this field using metadata
232 if (empty($props['action'])) {
233 $props['action'] = $this->getApiAction();
235 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', $props);
236 $fieldSpec = $fieldSpec['values'];
237 if (!isset($props['description']) && isset($fieldSpec['description'])) {
238 $props['description'] = $fieldSpec['description'];