INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Contact / Form / RelatedContact.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 generic to all the contact types.
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44class CRM_Contact_Form_RelatedContact extends CRM_Core_Form {
45
46 /**
47 * The contact type of the form
48 *
49 * @var string
50 */
51 protected $_contactType;
52
53 /**
54 * The contact id, used when editing the form
55 *
56 * @var int
57 */
58 public $_contactId;
59
60 /**
100fef9d 61 * Form defaults
6a488035
TO
62 *
63 * @var array
64 */
65 protected $_defaults = array();
66
67 /**
100fef9d 68 * Build all the data structures needed to build the form
6a488035
TO
69 *
70 * @return void
6a488035 71 */
00be9182 72 public function preProcess() {
6a488035
TO
73 // reset action from the session
74 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
75 $this, FALSE, 'update'
76 );
77 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
78
79 $rcid = CRM_Utils_Request::retrieve('rcid', 'Positive', $this);
80 $rcid = $rcid ? "&id={$rcid}" : '';
81 $session = CRM_Core_Session::singleton();
82 $session->pushUserContext(CRM_Utils_System::url('civicrm/user', "reset=1{$rcid}"));
83
84 if ($this->_contactId) {
85 $contact = new CRM_Contact_DAO_Contact();
86 $contact->id = $this->_contactId;
87 if (!$contact->find(TRUE)) {
88 CRM_Core_Error::statusBounce(ts('contact does not exist: %1', array(1 => $this->_contactId)));
89 }
90 $this->_contactType = $contact->contact_type;
91
92 // check for permissions
93 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
94 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.'));
95 }
96
97 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_contactId);
98 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
99 }
100 else {
101 CRM_Core_Error::statusBounce(ts('Could not get a contact_id and/or contact_type'));
102 }
103 }
104
105 /**
c490a46a 106 * Set default values for the form. Note that in edit/view mode
6a488035
TO
107 * the default values are retrieved from the database
108 *
6a488035 109 *
355ba699 110 * @return void
6a488035 111 */
00be9182 112 public function setDefaultValues() {
6a488035
TO
113 return $this->_defaults;
114 }
115
116 /**
c490a46a 117 * Build the form object
6a488035 118 *
355ba699 119 * @return void
6a488035
TO
120 */
121 public function buildQuickForm() {
122 $params = array();
123 $params['id'] = $params['contact_id'] = $this->_contactId;
124 $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_defaults);
125
d5f1ee75
DG
126 $countryID = '';
127 $stateID = '';
128 if (!empty($this->_defaults['address'][1])) {
129 $countryID = CRM_Utils_Array::value('country_id',
130 $this->_defaults['address'][1]
131 );
132 $stateID = CRM_Utils_Array::value('state_province_id',
133 $this->_defaults['address'][1]
2efcf0c2 134 );
d5f1ee75 135 }
6a488035
TO
136 CRM_Contact_BAO_Contact_Utils::buildOnBehalfForm($this,
137 $this->_contactType,
138 $countryID,
139 $stateID,
0949913f 140 ts('Contact Information')
6a488035
TO
141 );
142
143 $this->addButtons(array(
144 array(
145 'type' => 'next',
146 'name' => ts('Save'),
147 'isDefault' => TRUE,
148 ),
149 array(
150 'type' => 'cancel',
151 'name' => ts('Cancel'),
152 ),
153 ));
154 }
155
156 /**
157 * Form submission of new/edit contact is processed.
158 *
6a488035 159 *
355ba699 160 * @return void
6a488035
TO
161 */
162 public function postProcess() {
163 // store the submitted values in an array
164 $params = $this->controller->exportValues($this->_name);
165
166 $locType = CRM_Core_BAO_LocationType::getDefault();
167 foreach (array(
168 'phone', 'email', 'address') as $locFld) {
169 if (!empty($this->_defaults[$locFld]) && $this->_defaults[$locFld][1]['location_type_id']) {
170 $params[$locFld][1]['is_primary'] = $this->_defaults[$locFld][1]['is_primary'];
171 $params[$locFld][1]['location_type_id'] = $this->_defaults[$locFld][1]['location_type_id'];
172 }
173 else {
174 $params[$locFld][1]['is_primary'] = 1;
175 $params[$locFld][1]['location_type_id'] = $locType->id;
176 }
177 }
178
179 $params['contact_type'] = $this->_contactType;
8394480e
BS
180 //CRM-14904
181 if ( isset($this->_defaults['contact_sub_type']) ) {
182 $params['contact_sub_type'] = $this->_defaults['contact_sub_type'];
183 }
6a488035
TO
184 $params['contact_id'] = $this->_contactId;
185
186 $contact = CRM_Contact_BAO_Contact::create($params, TRUE);
187
188 // set status message.
189 if ($this->_contactId) {
190 $message = ts('%1 has been updated.', array(1 => $contact->display_name));
191 }
192 else {
193 $message = ts('%1 has been created.', array(1 => $contact->display_name));
194 }
195 CRM_Core_Session::setStatus($message, ts('Contact Saved'), 'success');
196 }
197}