| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 32 | */ |
| 33 | |
| 34 | trait CRM_Core_Form_EntityFormTrait { |
| 35 | /** |
| 36 | * Get entity fields for the entity to be added to the form. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | public function getEntityFields() { |
| 41 | return $this->entityFields; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Explicitly declare the form context. |
| 46 | */ |
| 47 | public function getDefaultContext() { |
| 48 | return 'create'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get entity fields for the entity to be added to the form. |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | public function getDeleteMessage() { |
| 57 | return $this->deleteMessage; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the entity id being edited. |
| 62 | * |
| 63 | * @return int|null |
| 64 | */ |
| 65 | public function getEntityId() { |
| 66 | return $this->_id; |
| 67 | } |
| 68 | /** |
| 69 | * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form. |
| 70 | */ |
| 71 | public function addCustomDataToForm() { |
| 72 | $customisableEntities = CRM_Core_SelectValues::customGroupExtends(); |
| 73 | if (isset($customisableEntities[$this->getDefaultEntity()])) { |
| 74 | CRM_Custom_Form_CustomData::addToForm($this); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Build the form object. |
| 80 | */ |
| 81 | public function buildQuickEntityForm() { |
| 82 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 83 | $this->buildDeleteForm(); |
| 84 | return; |
| 85 | } |
| 86 | $this->applyFilter('__ALL__', 'trim'); |
| 87 | $this->addEntityFieldsToTemplate(); |
| 88 | $this->assign('entityFields', $this->entityFields); |
| 89 | $this->assign('entityID', $this->getEntityId()); |
| 90 | $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity()))); |
| 91 | $this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity()))); |
| 92 | $this->addCustomDataToForm(); |
| 93 | $this->addFormButtons(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Build the form for any deletion. |
| 98 | */ |
| 99 | protected function buildDeleteForm() { |
| 100 | $this->assign('deleteMessage', $this->getDeleteMessage()); |
| 101 | $this->addFormButtons(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add relevant buttons to the form. |
| 106 | */ |
| 107 | protected function addFormButtons() { |
| 108 | if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { |
| 109 | $this->addButtons(array( |
| 110 | array( |
| 111 | 'type' => 'cancel', |
| 112 | 'name' => ts('Done'), |
| 113 | 'isDefault' => TRUE, |
| 114 | ), |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | else { |
| 119 | $this->addButtons(array( |
| 120 | array( |
| 121 | 'type' => 'next', |
| 122 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), |
| 123 | 'isDefault' => TRUE, |
| 124 | ), |
| 125 | array( |
| 126 | 'type' => 'cancel', |
| 127 | 'name' => ts('Cancel'), |
| 128 | ), |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Set translated fields. |
| 136 | * |
| 137 | * This function is called from the class constructor, allowing us to set |
| 138 | * fields on the class that can't be set as properties due to need for |
| 139 | * translation or other non-input specific handling. |
| 140 | */ |
| 141 | protected function setTranslatedFields() { |
| 142 | $this->setEntityFields(); |
| 143 | $this->setDeleteMessage(); |
| 144 | $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']); |
| 145 | $this->metadata = $metadata['values']; |
| 146 | foreach ($this->metadata as $fieldName => $spec) { |
| 147 | if (isset($this->entityFields[$fieldName])) { |
| 148 | if ($spec['localizable']) { |
| 149 | $this->entityFields[$fieldName]['is_add_translate_dialog'] = TRUE; |
| 150 | } |
| 151 | if (empty($spec['html'])) { |
| 152 | $this->entityFields[$fieldName]['not-auto-addable'] = TRUE; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Add defined entity field to template. |
| 160 | */ |
| 161 | protected function addEntityFieldsToTemplate() { |
| 162 | foreach ($this->getEntityFields() as $fieldSpec) { |
| 163 | if (empty($fieldSpec['not-auto-addable'])) { |
| 164 | $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec)); |
| 165 | if (!empty($fieldSpec['is_freeze'])) { |
| 166 | $element->freeze(); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |