Merge pull request #11737 from aydun/CRM-21816-relative-dates-in-search
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
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 /**
40 * Explicitly declare the entity api name.
41 */
42 public function getDefaultEntity() {
43 return 'RelationshipType';
44 }
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
51 $this->setPageTitle(ts('Relationship Type'));
52
53 if ($this->_action & CRM_Core_Action::DELETE) {
54 return;
55 }
56
57 $this->applyFilter('__ALL__', 'trim');
58
59 $this->addField('label_a_b');
60 $this->addField('label_b_a');
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 );
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
68 $this->addField('description');
69
70 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
71
72 // add select for contact type
73 $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
74 array(
75 '' => ts('All Contacts'),
76 ) + $contactTypes
77 );
78 $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
79 array(
80 '' => ts('All Contacts'),
81 ) + $contactTypes
82 );
83
84 $this->addField('is_active');
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 ) {
90 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) {
91 $$field->freeze();
92 }
93 }
94
95 if ($this->_action & CRM_Core_Action::VIEW) {
96 $this->freeze();
97 }
98
99 $this->assign('relationship_type_id', $this->_id);
100
101 }
102
103 /**
104 * @return array
105 */
106 public function setDefaultValues() {
107 if ($this->_action != CRM_Core_Action::DELETE &&
108 isset($this->_id)
109 ) {
110 $defaults = $params = array();
111 $params = array('id' => $this->_id);
112 $baoName = $this->_BAOName;
113 $baoName::retrieve($params, $defaults);
114 $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults);
115 if (!empty($defaults['contact_sub_type_a'])) {
116 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
117 }
118
119 $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults);
120 if (!empty($defaults['contact_sub_type_b'])) {
121 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
122 }
123 return $defaults;
124 }
125 else {
126 return parent::setDefaultValues();
127 }
128 }
129
130 /**
131 * Process the form submission.
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 {
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) {
144 $params['id'] = $this->_id;
145 }
146
147 $cTypeA = CRM_Utils_System::explode('__',
148 $params['contact_types_a'],
149 2
150 );
151 $cTypeB = CRM_Utils_System::explode('__',
152 $params['contact_types_b'],
153 2
154 );
155
156 $params['contact_type_a'] = $cTypeA[0];
157 $params['contact_type_b'] = $cTypeB[0];
158
159 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null';
160 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null';
161
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
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);
173
174 $this->ajaxResponse['relationshipType'] = $result['values'];
175
176 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
177 }
178 }
179
180 }