| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 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 | /** |
| 35 | * This class generates form components for Relationship Type. |
| 36 | */ |
| 37 | class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { |
| 38 | |
| 39 | use CRM_Core_Form_EntityFormTrait; |
| 40 | |
| 41 | /** |
| 42 | * Fields for the entity to be assigned to the template. |
| 43 | * |
| 44 | * Fields may have keys |
| 45 | * - name (required to show in tpl from the array) |
| 46 | * - description (optional, will appear below the field) |
| 47 | * Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide) |
| 48 | * - not-auto-addable - this class will not attempt to add the field using addField. |
| 49 | * (this will be automatically set if the field does not have html in it's metadata |
| 50 | * or is not a core field on the form's entity). |
| 51 | * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']] |
| 52 | * - template - use a field specific template to render this field |
| 53 | * - required |
| 54 | * - is_freeze (field should be frozen). |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | protected $entityFields = []; |
| 59 | |
| 60 | /** |
| 61 | * Set entity fields to be assigned to the form. |
| 62 | */ |
| 63 | protected function setEntityFields() { |
| 64 | $this->entityFields = [ |
| 65 | 'label_a_b' => [ |
| 66 | 'name' => 'label_a_b', |
| 67 | 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."), |
| 68 | 'required' => TRUE, |
| 69 | ], |
| 70 | 'label_b_a' => [ |
| 71 | 'name' => 'label_b_a', |
| 72 | 'description' => ts("Label for the relationship from Contact B to Contact A. EXAMPLE: Contact B is 'Child of' Contact A. You may leave this blank for relationships where the name is the same in both directions (e.g. Spouse).") |
| 73 | ], |
| 74 | 'description' => [ |
| 75 | 'name' => 'description', |
| 76 | 'description' => '' |
| 77 | ], |
| 78 | 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE], |
| 79 | 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE], |
| 80 | 'is_active' => ['name' => 'is_active'], |
| 81 | ]; |
| 82 | |
| 83 | self::setEntityFieldsMetadata(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Deletion message to be assigned to the form. |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
| 91 | protected $deleteMessage; |
| 92 | |
| 93 | /** |
| 94 | * Explicitly declare the entity api name. |
| 95 | */ |
| 96 | public function getDefaultEntity() { |
| 97 | return 'RelationshipType'; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set the delete message. |
| 102 | * |
| 103 | * We do this from the constructor in order to do a translation. |
| 104 | */ |
| 105 | public function setDeleteMessage() { |
| 106 | $this->deleteMessage = ts('WARNING: Deleting this option will result in the loss of all Relationship records of this type.') . ts('This may mean the loss of a substantial amount of data, and the action cannot be undone.') . ts('Do you want to continue?'); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Build the form object. |
| 111 | */ |
| 112 | public function buildQuickForm() { |
| 113 | $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')); |
| 114 | $this->entityFields['is_active']['is_freeze'] = $isReserved; |
| 115 | |
| 116 | self::buildQuickEntityForm(); |
| 117 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $this->addRule('label_a_b', ts('Label already exists in Database.'), |
| 122 | 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b') |
| 123 | ); |
| 124 | $this->addRule('label_b_a', ts('Label already exists in Database.'), |
| 125 | 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a') |
| 126 | ); |
| 127 | |
| 128 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__'); |
| 129 | foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) { |
| 130 | $element = $this->add('select', $name, $label . ' ', |
| 131 | array( |
| 132 | '' => ts('All Contacts'), |
| 133 | ) + $contactTypes |
| 134 | ); |
| 135 | if ($isReserved) { |
| 136 | $element->freeze(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 141 | $this->freeze(); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @return array |
| 148 | */ |
| 149 | public function setDefaultValues() { |
| 150 | if ($this->_action != CRM_Core_Action::DELETE && |
| 151 | isset($this->_id) |
| 152 | ) { |
| 153 | $defaults = $params = array(); |
| 154 | $params = array('id' => $this->_id); |
| 155 | $baoName = $this->_BAOName; |
| 156 | $baoName::retrieve($params, $defaults); |
| 157 | $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults); |
| 158 | if (!empty($defaults['contact_sub_type_a'])) { |
| 159 | $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a']; |
| 160 | } |
| 161 | |
| 162 | $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults); |
| 163 | if (!empty($defaults['contact_sub_type_b'])) { |
| 164 | $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b']; |
| 165 | } |
| 166 | return $defaults; |
| 167 | } |
| 168 | else { |
| 169 | return parent::setDefaultValues(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Process the form submission. |
| 175 | */ |
| 176 | public function postProcess() { |
| 177 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 178 | CRM_Contact_BAO_RelationshipType::del($this->_id); |
| 179 | CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success'); |
| 180 | } |
| 181 | else { |
| 182 | // store the submitted values in an array |
| 183 | $params = $this->exportValues(); |
| 184 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 185 | |
| 186 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 187 | $params['id'] = $this->_id; |
| 188 | } |
| 189 | |
| 190 | $cTypeA = CRM_Utils_System::explode('__', |
| 191 | $params['contact_types_a'], |
| 192 | 2 |
| 193 | ); |
| 194 | $cTypeB = CRM_Utils_System::explode('__', |
| 195 | $params['contact_types_b'], |
| 196 | 2 |
| 197 | ); |
| 198 | |
| 199 | $params['contact_type_a'] = $cTypeA[0]; |
| 200 | $params['contact_type_b'] = $cTypeB[0]; |
| 201 | |
| 202 | $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null'; |
| 203 | $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null'; |
| 204 | |
| 205 | if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) { |
| 206 | $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $params); |
| 207 | } |
| 208 | |
| 209 | if (empty($params['id'])) { |
| 210 | // Set name on created but don't update on update as the machine name is not exposed. |
| 211 | $params['name_b_a'] = CRM_Utils_String::munge($params['label_b_a']); |
| 212 | $params['name_a_b'] = CRM_Utils_String::munge($params['label_a_b']); |
| 213 | } |
| 214 | |
| 215 | $result = civicrm_api3('RelationshipType', 'create', $params); |
| 216 | |
| 217 | $this->ajaxResponse['relationshipType'] = $result['values']; |
| 218 | |
| 219 | CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success'); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } |