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