Merge pull request #18907 from alifrumin/2139
[civicrm-core.git] / CRM / Contact / Form / RelatedContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components generic to all the contact types.
20 *
21 * It delegates the work to lower level subclasses and integrates the changes
22 * back in. It also uses a lot of functionality with the CRM API's, so any change
23 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
24 */
25 class CRM_Contact_Form_RelatedContact extends CRM_Core_Form {
26
27 /**
28 * The contact type of the form.
29 *
30 * @var string
31 */
32 protected $_contactType;
33
34 /**
35 * The contact id, used when editing the form
36 *
37 * @var int
38 */
39 public $_contactId;
40
41 /**
42 * Explicitly declare the form context.
43 */
44 public function getDefaultContext() {
45 return 'create';
46 }
47
48 /**
49 * Build all the data structures needed to build the form.
50 */
51 public function preProcess() {
52 // reset action from the session
53 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
54 $this, FALSE, 'update'
55 );
56 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
57
58 $rcid = CRM_Utils_Request::retrieve('rcid', 'Positive', $this);
59 $rcid = $rcid ? "&id={$rcid}" : '';
60 $session = CRM_Core_Session::singleton();
61 $session->pushUserContext(CRM_Utils_System::url('civicrm/user', "reset=1{$rcid}"));
62
63 if ($this->_contactId) {
64 $contact = new CRM_Contact_DAO_Contact();
65 $contact->id = $this->_contactId;
66 if (!$contact->find(TRUE)) {
67 CRM_Core_Error::statusBounce(ts('contact does not exist: %1', [1 => $this->_contactId]));
68 }
69 $this->_contactType = $contact->contact_type;
70
71 // check for permissions
72 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
73 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.'));
74 }
75
76 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_contactId);
77 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
78 }
79 else {
80 CRM_Core_Error::statusBounce(ts('Could not get a contact_id and/or contact_type'));
81 }
82 }
83
84 /**
85 * Set default values for the form.
86 *
87 * Note that in edit/view mode the default values are retrieved from the
88 * database
89 */
90 public function setDefaultValues() {
91 return $this->_defaults;
92 }
93
94 /**
95 * Build the form object.
96 */
97 public function buildQuickForm() {
98 $params = [];
99 $params['id'] = $params['contact_id'] = $this->_contactId;
100 $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_defaults);
101
102 $countryID = '';
103 $stateID = '';
104 if (!empty($this->_defaults['address'][1])) {
105 $countryID = CRM_Utils_Array::value('country_id',
106 $this->_defaults['address'][1]
107 );
108 $stateID = CRM_Utils_Array::value('state_province_id',
109 $this->_defaults['address'][1]
110 );
111 }
112 CRM_Contact_BAO_Contact_Utils::buildOnBehalfForm($this,
113 $this->_contactType,
114 $countryID,
115 $stateID,
116 ts('Contact Information')
117 );
118
119 $this->addButtons([
120 [
121 'type' => 'next',
122 'name' => ts('Save'),
123 'isDefault' => TRUE,
124 ],
125 [
126 'type' => 'cancel',
127 'name' => ts('Cancel'),
128 ],
129 ]);
130 }
131
132 /**
133 * Form submission of new/edit contact is processed.
134 */
135 public function postProcess() {
136 // store the submitted values in an array
137 $params = $this->controller->exportValues($this->_name);
138
139 $locType = CRM_Core_BAO_LocationType::getDefault();
140 foreach ([
141 'phone',
142 'email',
143 'address',
144 ] as $locFld) {
145 if (!empty($this->_defaults[$locFld]) && $this->_defaults[$locFld][1]['location_type_id']) {
146 $params[$locFld][1]['is_primary'] = $this->_defaults[$locFld][1]['is_primary'];
147 $params[$locFld][1]['location_type_id'] = $this->_defaults[$locFld][1]['location_type_id'];
148 }
149 else {
150 $params[$locFld][1]['is_primary'] = 1;
151 $params[$locFld][1]['location_type_id'] = $locType->id;
152 }
153 }
154
155 $params['contact_type'] = $this->_contactType;
156 //CRM-14904
157 if (isset($this->_defaults['contact_sub_type'])) {
158 $params['contact_sub_type'] = $this->_defaults['contact_sub_type'];
159 }
160 $params['contact_id'] = $this->_contactId;
161
162 $contact = CRM_Contact_BAO_Contact::create($params, TRUE);
163
164 // set status message.
165 if ($this->_contactId) {
166 $message = ts('%1 has been updated.', [1 => $contact->display_name]);
167 }
168 else {
169 $message = ts('%1 has been created.', [1 => $contact->display_name]);
170 }
171 CRM_Core_Session::setStatus($message, ts('Contact Saved'), 'success');
172 }
173
174 }