Merge pull request #3870 from colemanw/CRM-13863
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
43 * Function to build the form
44 *
355ba699 45 * @return void
6a488035
TO
46 * @access public
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50
51 if ($this->_action & CRM_Core_Action::DELETE) {
52
53 return;
54 }
55
56 $this->applyFilter('__ALL__', 'trim');
57
58 $this->add('text', 'label_a_b', ts('Relationship Label-A to B'),
59 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'label_a_b'), TRUE
60 );
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
65 $this->add('text', 'label_b_a', ts('Relationship Label-B to A'),
66 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'label_b_a')
67 );
68
69 $this->addRule('label_b_a', ts('Label already exists in Database.'),
70 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a')
71 );
72
73 $this->add('text', 'description', ts('Description'),
74 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_RelationshipType', 'description')
75 );
76
9deb4ed3 77 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
6a488035
TO
78
79 // add select for contact type
80 $contactTypeA = &$this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
81 array(
82 '' => ts('All Contacts')) + $contactTypes
83 );
84 $contactTypeB = &$this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
85 array(
86 '' => ts('All Contacts')) + $contactTypes
87 );
88
89 $isActive = &$this->add('checkbox', 'is_active', ts('Enabled?'));
90
91 //only selected field should be allow for edit, CRM-4888
92 if ($this->_id &&
93 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')
94 ) {
95 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field)$$field->freeze();
96 }
97
98 if ($this->_action & CRM_Core_Action::VIEW) {
99 $this->freeze();
6a488035
TO
100 }
101 }
102
e0ef6999
EM
103 /**
104 * @return array
105 */
6a488035
TO
106 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);
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
119 $defaults['contact_types_b'] = $defaults['contact_type_b'];
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 /**
131 * Function to process the form
132 *
133 * @access public
134 *
355ba699 135 * @return void
6a488035
TO
136 */
137 public function postProcess() {
138 if ($this->_action & CRM_Core_Action::DELETE) {
139 CRM_Contact_BAO_RelationshipType::del($this->_id);
140 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
141 }
142 else {
143 $params = array();
144 $ids = array();
145
146 // store the submitted values in an array
147 $params = $this->exportValues();
148 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
149
150 if ($this->_action & CRM_Core_Action::UPDATE) {
151 $ids['relationshipType'] = $this->_id;
152 }
153
9deb4ed3 154 $cTypeA = CRM_Utils_System::explode('__',
6a488035
TO
155 $params['contact_types_a'],
156 2
157 );
9deb4ed3 158 $cTypeB = CRM_Utils_System::explode('__',
6a488035
TO
159 $params['contact_types_b'],
160 2
161 );
162
163 $params['contact_type_a'] = $cTypeA[0];
164 $params['contact_type_b'] = $cTypeB[0];
165
166 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'NULL';
167 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'NULL';
168
169 CRM_Contact_BAO_RelationshipType::add($params, $ids);
170
171 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
172 }
173 }
174 //end of function
175}
176