Support custom fields CRM-12464
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 *
45 * @return None
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
77
78
79 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements();
80
81 // add select for contact type
82 $contactTypeA = &$this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
83 array(
84 '' => ts('All Contacts')) + $contactTypes
85 );
86 $contactTypeB = &$this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
87 array(
88 '' => ts('All Contacts')) + $contactTypes
89 );
90
91 $isActive = &$this->add('checkbox', 'is_active', ts('Enabled?'));
92
93 //only selected field should be allow for edit, CRM-4888
94 if ($this->_id &&
95 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')
96 ) {
97 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field)$$field->freeze();
98 }
99
100 if ($this->_action & CRM_Core_Action::VIEW) {
101 $this->freeze();
102 $url = CRM_Utils_System::url('civicrm/admin/reltype&reset=1');
103 $location = "window.location='$url'";
104 $this->addElement('button', 'done', ts('Done'), array('onclick' => $location));
105 }
106 }
107
108 function setDefaultValues() {
109 if ($this->_action != CRM_Core_Action::DELETE &&
110 isset($this->_id)
111 ) {
112 $defaults = $params = array();
113 $params = array('id' => $this->_id);
114 eval($this->_BAOName . '::retrieve( $params, $defaults );');
115 $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults);
116 if (CRM_Utils_Array::value('contact_sub_type_a', $defaults)) {
117 $defaults['contact_types_a'] .= CRM_Core_DAO::VALUE_SEPARATOR . $defaults['contact_sub_type_a'];
118 }
119
120 $defaults['contact_types_b'] = $defaults['contact_type_b'];
121 if (CRM_Utils_Array::value('contact_sub_type_b', $defaults)) {
122 $defaults['contact_types_b'] .= CRM_Core_DAO::VALUE_SEPARATOR . $defaults['contact_sub_type_b'];
123 }
124 return $defaults;
125 }
126 else {
127 return parent::setDefaultValues();
128 }
129 }
130
131 /**
132 * Function to process the form
133 *
134 * @access public
135 *
136 * @return None
137 */
138 public function postProcess() {
139 if ($this->_action & CRM_Core_Action::DELETE) {
140 CRM_Contact_BAO_RelationshipType::del($this->_id);
141 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
142 }
143 else {
144 $params = array();
145 $ids = array();
146
147 // store the submitted values in an array
148 $params = $this->exportValues();
149 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
150
151 if ($this->_action & CRM_Core_Action::UPDATE) {
152 $ids['relationshipType'] = $this->_id;
153 }
154
155 $cTypeA = CRM_Utils_System::explode(CRM_Core_DAO::VALUE_SEPARATOR,
156 $params['contact_types_a'],
157 2
158 );
159 $cTypeB = CRM_Utils_System::explode(CRM_Core_DAO::VALUE_SEPARATOR,
160 $params['contact_types_b'],
161 2
162 );
163
164 $params['contact_type_a'] = $cTypeA[0];
165 $params['contact_type_b'] = $cTypeB[0];
166
167 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'NULL';
168 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'NULL';
169
170 CRM_Contact_BAO_RelationshipType::add($params, $ids);
171
172 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
173 }
174 }
175 //end of function
176}
177