Merge pull request #11962 from compucorp/55-hide-adding-option-value-for-locked-groups
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components for Relationship Type.
6a488035
TO
36 */
37class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
38
a9999eb6 39 /**
40 * Explicitly declare the entity api name.
41 */
42 public function getDefaultEntity() {
43 return 'RelationshipType';
44 }
45
6a488035 46 /**
eceb18cc 47 * Build the form object.
6a488035
TO
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
e2046b33 51 $this->setPageTitle(ts('Relationship Type'));
6a488035
TO
52
53 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
54 return;
55 }
56
57 $this->applyFilter('__ALL__', 'trim');
58
a9999eb6 59 $this->addField('label_a_b');
60 $this->addField('label_b_a');
6a488035
TO
61 $this->addRule('label_a_b', ts('Label already exists in Database.'),
62 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b')
63 );
6a488035
TO
64 $this->addRule('label_b_a', ts('Label already exists in Database.'),
65 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a')
66 );
67
a9999eb6 68 $this->addField('description');
6a488035 69
9deb4ed3 70 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
6a488035
TO
71
72 // add select for contact type
a9999eb6 73 $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
6a488035 74 array(
3bdca100 75 '' => ts('All Contacts'),
353ffa53 76 ) + $contactTypes
6a488035 77 );
a9999eb6 78 $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
6a488035 79 array(
3bdca100 80 '' => ts('All Contacts'),
353ffa53 81 ) + $contactTypes
6a488035
TO
82 );
83
a9999eb6 84 $this->addField('is_active');
6a488035
TO
85
86 //only selected field should be allow for edit, CRM-4888
87 if ($this->_id &&
88 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')
89 ) {
83ba323f
TO
90 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) {
91 $$field->freeze();
02fc859b 92 }
6a488035
TO
93 }
94
95 if ($this->_action & CRM_Core_Action::VIEW) {
96 $this->freeze();
6a488035 97 }
f2f99298
SV
98
99 $this->assign('relationship_type_id', $this->_id);
100
6a488035
TO
101 }
102
e0ef6999
EM
103 /**
104 * @return array
105 */
00be9182 106 public function setDefaultValues() {
6a488035
TO
107 if ($this->_action != CRM_Core_Action::DELETE &&
108 isset($this->_id)
109 ) {
110 $defaults = $params = array();
111 $params = array('id' => $this->_id);
0e6e8724
DL
112 $baoName = $this->_BAOName;
113 $baoName::retrieve($params, $defaults);
6a488035 114 $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults);
a7488080 115 if (!empty($defaults['contact_sub_type_a'])) {
9deb4ed3 116 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
6a488035
TO
117 }
118
18cb89ba 119 $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults);
a7488080 120 if (!empty($defaults['contact_sub_type_b'])) {
9deb4ed3 121 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
6a488035
TO
122 }
123 return $defaults;
124 }
125 else {
126 return parent::setDefaultValues();
127 }
128 }
129
130 /**
eceb18cc 131 * Process the form submission.
6a488035
TO
132 */
133 public function postProcess() {
134 if ($this->_action & CRM_Core_Action::DELETE) {
135 CRM_Contact_BAO_RelationshipType::del($this->_id);
136 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
137 }
138 else {
6a488035
TO
139 // store the submitted values in an array
140 $params = $this->exportValues();
141 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
142
143 if ($this->_action & CRM_Core_Action::UPDATE) {
780df37b 144 $params['id'] = $this->_id;
6a488035
TO
145 }
146
9deb4ed3 147 $cTypeA = CRM_Utils_System::explode('__',
6a488035
TO
148 $params['contact_types_a'],
149 2
150 );
9deb4ed3 151 $cTypeB = CRM_Utils_System::explode('__',
6a488035
TO
152 $params['contact_types_b'],
153 2
154 );
155
156 $params['contact_type_a'] = $cTypeA[0];
157 $params['contact_type_b'] = $cTypeB[0];
158
a9999eb6 159 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null';
160 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null';
6a488035 161
780df37b
CW
162 if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) {
163 $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $params);
164 }
165
a9999eb6 166 if (empty($params['id'])) {
167 // Set name on created but don't update on update as the machine name is not exposed.
168 $params['name_b_a'] = CRM_Utils_String::munge($params['label_b_a']);
169 $params['name_a_b'] = CRM_Utils_String::munge($params['label_a_b']);
170 }
171
172 $result = civicrm_api3('RelationshipType', 'create', $params);
4324b8d7 173
a9999eb6 174 $this->ajaxResponse['relationshipType'] = $result['values'];
6a488035
TO
175
176 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
177 }
178 }
e2046b33 179
6a488035 180}