3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components generic to all the contact types.
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.
25 class CRM_Contact_Form_RelatedContact
extends CRM_Core_Form
{
28 * The contact type of the form.
32 protected $_contactType;
35 * The contact id, used when editing the form
42 * Explicitly declare the form context.
44 public function getDefaultContext() {
49 * Build all the data structures needed to build the form.
51 public function preProcess() {
52 // reset action from the session
53 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
54 $this, FALSE, 'update'
56 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
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}"));
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
]));
69 $this->_contactType
= $contact->contact_type
;
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.'));
76 list($displayName, $contactImage) = CRM_Contact_BAO_Contact
::getDisplayAndImage($this->_contactId
);
77 CRM_Utils_System
::setTitle($displayName, $contactImage . ' ' . $displayName);
80 CRM_Core_Error
::statusBounce(ts('Could not get a contact_id and/or contact_type'));
85 * Set default values for the form.
87 * Note that in edit/view mode the default values are retrieved from the
90 public function setDefaultValues() {
91 return $this->_defaults
;
95 * Build the form object.
97 public function buildQuickForm() {
99 $params['id'] = $params['contact_id'] = $this->_contactId
;
100 $contact = CRM_Contact_BAO_Contact
::retrieve($params, $this->_defaults
);
104 if (!empty($this->_defaults
['address'][1])) {
105 $countryID = CRM_Utils_Array
::value('country_id',
106 $this->_defaults
['address'][1]
108 $stateID = CRM_Utils_Array
::value('state_province_id',
109 $this->_defaults
['address'][1]
112 CRM_Contact_BAO_Contact_Utils
::buildOnBehalfForm($this,
116 ts('Contact Information')
122 'name' => ts('Save'),
127 'name' => ts('Cancel'),
133 * Form submission of new/edit contact is processed.
135 public function postProcess() {
136 // store the submitted values in an array
137 $params = $this->controller
->exportValues($this->_name
);
139 $locType = CRM_Core_BAO_LocationType
::getDefault();
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'];
150 $params[$locFld][1]['is_primary'] = 1;
151 $params[$locFld][1]['location_type_id'] = $locType->id
;
155 $params['contact_type'] = $this->_contactType
;
157 if (isset($this->_defaults
['contact_sub_type'])) {
158 $params['contact_sub_type'] = $this->_defaults
['contact_sub_type'];
160 $params['contact_id'] = $this->_contactId
;
162 $contact = CRM_Contact_BAO_Contact
::create($params, TRUE);
164 // set status message.
165 if ($this->_contactId
) {
166 $message = ts('%1 has been updated.', [1 => $contact->display_name
]);
169 $message = ts('%1 has been created.', [1 => $contact->display_name
]);
171 CRM_Core_Session
::setStatus($message, ts('Contact Saved'), 'success');