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