3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class generates form components for Relationship Type.
37 class CRM_Admin_Form_RelationshipType
extends CRM_Admin_Form
{
40 * Explicitly declare the entity api name.
42 public function getDefaultEntity() {
43 return 'RelationshipType';
47 * Build the form object.
49 public function buildQuickForm() {
50 parent
::buildQuickForm();
51 $this->setPageTitle(ts('Relationship Type'));
53 if ($this->_action
& CRM_Core_Action
::DELETE
) {
57 $this->applyFilter('__ALL__', 'trim');
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')
64 $this->addRule('label_b_a', ts('Label already exists in Database.'),
65 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id
, 'label_b_a')
68 $this->addField('description');
70 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '__');
72 // add select for contact type
73 $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
75 '' => ts('All Contacts'),
78 $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
80 '' => ts('All Contacts'),
84 $this->addField('is_active');
86 //only selected field should be allow for edit, CRM-4888
88 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id
, 'is_reserved')
90 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) {
95 if ($this->_action
& CRM_Core_Action
::VIEW
) {
99 $this->assign('relationship_type_id', $this->_id
);
106 public function setDefaultValues() {
107 if ($this->_action
!= CRM_Core_Action
::DELETE
&&
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'];
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'];
126 return parent
::setDefaultValues();
131 * Process the form submission.
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');
139 // store the submitted values in an array
140 $params = $this->exportValues();
141 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
143 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
144 $params['id'] = $this->_id
;
147 $cTypeA = CRM_Utils_System
::explode('__',
148 $params['contact_types_a'],
151 $cTypeB = CRM_Utils_System
::explode('__',
152 $params['contact_types_b'],
156 $params['contact_type_a'] = $cTypeA[0];
157 $params['contact_type_b'] = $cTypeB[0];
159 $params['contact_sub_type_a'] = $cTypeA[1] ?
$cTypeA[1] : 'null';
160 $params['contact_sub_type_b'] = $cTypeB[1] ?
$cTypeB[1] : 'null';
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);
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']);
172 $result = civicrm_api3('RelationshipType', 'create', $params);
174 $this->ajaxResponse
['relationshipType'] = $result['values'];
176 CRM_Core_Session
::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');