Commit | Line | Data |
---|---|---|
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 | */ | |
40 | class 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 | ||
103 | function setDefaultValues() { | |
104 | if ($this->_action != CRM_Core_Action::DELETE && | |
105 | isset($this->_id) | |
106 | ) { | |
107 | $defaults = $params = array(); | |
108 | $params = array('id' => $this->_id); | |
0e6e8724 DL |
109 | $baoName = $this->_BAOName; |
110 | $baoName::retrieve($params, $defaults); | |
6a488035 | 111 | $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults); |
a7488080 | 112 | if (!empty($defaults['contact_sub_type_a'])) { |
9deb4ed3 | 113 | $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a']; |
6a488035 TO |
114 | } |
115 | ||
116 | $defaults['contact_types_b'] = $defaults['contact_type_b']; | |
a7488080 | 117 | if (!empty($defaults['contact_sub_type_b'])) { |
9deb4ed3 | 118 | $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b']; |
6a488035 TO |
119 | } |
120 | return $defaults; | |
121 | } | |
122 | else { | |
123 | return parent::setDefaultValues(); | |
124 | } | |
125 | } | |
126 | ||
127 | /** | |
128 | * Function to process the form | |
129 | * | |
130 | * @access public | |
131 | * | |
355ba699 | 132 | * @return void |
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 { | |
140 | $params = array(); | |
141 | $ids = array(); | |
142 | ||
143 | // store the submitted values in an array | |
144 | $params = $this->exportValues(); | |
145 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
146 | ||
147 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
148 | $ids['relationshipType'] = $this->_id; | |
149 | } | |
150 | ||
9deb4ed3 | 151 | $cTypeA = CRM_Utils_System::explode('__', |
6a488035 TO |
152 | $params['contact_types_a'], |
153 | 2 | |
154 | ); | |
9deb4ed3 | 155 | $cTypeB = CRM_Utils_System::explode('__', |
6a488035 TO |
156 | $params['contact_types_b'], |
157 | 2 | |
158 | ); | |
159 | ||
160 | $params['contact_type_a'] = $cTypeA[0]; | |
161 | $params['contact_type_b'] = $cTypeB[0]; | |
162 | ||
163 | $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'NULL'; | |
164 | $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'NULL'; | |
165 | ||
166 | CRM_Contact_BAO_RelationshipType::add($params, $ids); | |
167 | ||
168 | CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success'); | |
169 | } | |
170 | } | |
171 | //end of function | |
172 | } | |
173 |