Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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. | |
6a488035 TO |
24 | */ |
25 | class CRM_Contact_Form_RelatedContact extends CRM_Core_Form { | |
26 | ||
27 | /** | |
fe482240 | 28 | * The contact type of the form. |
6a488035 TO |
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 | ||
760ff443 | 41 | /** |
42 | * Explicitly declare the form context. | |
43 | */ | |
44 | public function getDefaultContext() { | |
45 | return 'create'; | |
46 | } | |
47 | ||
6a488035 | 48 | /** |
57507ae6 | 49 | * Build all the data structures needed to build the form. |
6a488035 | 50 | */ |
00be9182 | 51 | public function preProcess() { |
6a488035 TO |
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 | ||
353ffa53 TO |
58 | $rcid = CRM_Utils_Request::retrieve('rcid', 'Positive', $this); |
59 | $rcid = $rcid ? "&id={$rcid}" : ''; | |
6a488035 TO |
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)) { | |
be2fb01f | 67 | CRM_Core_Error::statusBounce(ts('contact does not exist: %1', [1 => $this->_contactId])); |
6a488035 TO |
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 | /** | |
5c9ff055 | 85 | * Set default values for the form. |
6a488035 | 86 | * |
5c9ff055 EM |
87 | * Note that in edit/view mode the default values are retrieved from the |
88 | * database | |
6a488035 | 89 | */ |
00be9182 | 90 | public function setDefaultValues() { |
6a488035 TO |
91 | return $this->_defaults; |
92 | } | |
93 | ||
94 | /** | |
ee0ce2ef | 95 | * Build the form object. |
6a488035 TO |
96 | */ |
97 | public function buildQuickForm() { | |
be2fb01f | 98 | $params = []; |
6a488035 | 99 | $params['id'] = $params['contact_id'] = $this->_contactId; |
353ffa53 | 100 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_defaults); |
6a488035 | 101 | |
d5f1ee75 DG |
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] | |
2efcf0c2 | 110 | ); |
d5f1ee75 | 111 | } |
6a488035 TO |
112 | CRM_Contact_BAO_Contact_Utils::buildOnBehalfForm($this, |
113 | $this->_contactType, | |
114 | $countryID, | |
115 | $stateID, | |
0949913f | 116 | ts('Contact Information') |
6a488035 TO |
117 | ); |
118 | ||
be2fb01f CW |
119 | $this->addButtons([ |
120 | [ | |
353ffa53 TO |
121 | 'type' => 'next', |
122 | 'name' => ts('Save'), | |
123 | 'isDefault' => TRUE, | |
be2fb01f CW |
124 | ], |
125 | [ | |
353ffa53 TO |
126 | 'type' => 'cancel', |
127 | 'name' => ts('Cancel'), | |
be2fb01f CW |
128 | ], |
129 | ]); | |
6a488035 TO |
130 | } |
131 | ||
132 | /** | |
133 | * Form submission of new/edit contact is processed. | |
6a488035 TO |
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(); | |
be2fb01f | 140 | foreach ([ |
69078420 SL |
141 | 'phone', |
142 | 'email', | |
143 | 'address', | |
144 | ] as $locFld) { | |
6a488035 TO |
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; | |
8394480e | 156 | //CRM-14904 |
481a74f4 | 157 | if (isset($this->_defaults['contact_sub_type'])) { |
8394480e BS |
158 | $params['contact_sub_type'] = $this->_defaults['contact_sub_type']; |
159 | } | |
6a488035 TO |
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) { | |
be2fb01f | 166 | $message = ts('%1 has been updated.', [1 => $contact->display_name]); |
6a488035 TO |
167 | } |
168 | else { | |
be2fb01f | 169 | $message = ts('%1 has been created.', [1 => $contact->display_name]); |
6a488035 TO |
170 | } |
171 | CRM_Core_Session::setStatus($message, ts('Contact Saved'), 'success'); | |
172 | } | |
96025800 | 173 | |
6a488035 | 174 | } |