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