Merge in 5.26
[civicrm-core.git] / CRM / Contact / Form / Inline.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 * Parent class for inline contact forms.
20 */
21 abstract class CRM_Contact_Form_Inline extends CRM_Core_Form {
22
23 /**
24 * Id of the contact that is being edited
25 * @var int
26 */
27 public $_contactId;
28
29 /**
30 * Type of contact being edited
31 * @var string
32 */
33 public $_contactType;
34
35 /**
36 * Sub type of contact being edited
37 * @var string
38 */
39 public $_contactSubType;
40
41 /**
42 * Explicitly declare the form context.
43 */
44 public function getDefaultContext() {
45 return 'create';
46 }
47
48 /**
49 * Explicitly declare the entity api name.
50 */
51 public function getDefaultEntity() {
52 return 'Contact';
53 }
54
55 /**
56 * Common preprocess: fetch contact ID and contact type
57 */
58 public function preProcess() {
59 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
60 $this->assign('contactId', $this->_contactId);
61
62 // get contact type and subtype
63 if (empty($this->_contactType)) {
64 $contactTypeInfo = CRM_Contact_BAO_Contact::getContactTypes($this->_contactId);
65 $this->_contactType = $contactTypeInfo[0];
66
67 // check if subtype is set
68 if (isset($contactTypeInfo[1])) {
69 // unset contact type which is 0th element
70 unset($contactTypeInfo[0]);
71 $this->_contactSubType = $contactTypeInfo;
72 }
73 }
74
75 $this->assign('contactType', $this->_contactType);
76
77 $this->setAction(CRM_Core_Action::UPDATE);
78 }
79
80 /**
81 * Common form elements.
82 */
83 public function buildQuickForm() {
84 CRM_Contact_Form_Inline_Lock::buildQuickForm($this, $this->_contactId);
85
86 $buttons = [
87 [
88 'type' => 'upload',
89 'name' => ts('Save'),
90 'isDefault' => TRUE,
91 ],
92 [
93 'type' => 'cancel',
94 'name' => ts('Cancel'),
95 ],
96 ];
97 $this->addButtons($buttons);
98 }
99
100 /**
101 * Override default cancel action.
102 */
103 public function cancelAction() {
104 $response = ['status' => 'cancel'];
105 CRM_Utils_JSON::output($response);
106 }
107
108 /**
109 * Set defaults for the form.
110 *
111 * @return array
112 */
113 public function setDefaultValues() {
114 $defaults = $params = [];
115 $params['id'] = $this->_contactId;
116
117 CRM_Contact_BAO_Contact::getValues($params, $defaults);
118
119 return $defaults;
120 }
121
122 /**
123 * Add entry to log table.
124 */
125 protected function log() {
126 CRM_Core_BAO_Log::register($this->_contactId,
127 'civicrm_contact',
128 $this->_contactId
129 );
130 }
131
132 /**
133 * Common function for all inline contact edit forms.
134 *
135 * Prepares ajaxResponse
136 */
137 protected function response() {
138 $this->ajaxResponse = array_merge(
139 self::renderFooter($this->_contactId),
140 $this->ajaxResponse,
141 CRM_Contact_Form_Inline_Lock::getResponse($this->_contactId)
142 );
143 // Note: Post hooks will be called by CRM_Core_Form::mainProcess
144 }
145
146 /**
147 * Render change log footer markup for a contact and supply count.
148 *
149 * Needed for refreshing the contact summary screen
150 *
151 * @param int $cid
152 * @param bool $includeCount
153 * @return array
154 */
155 public static function renderFooter($cid, $includeCount = TRUE) {
156 // Load change log footer from template.
157 $smarty = CRM_Core_Smarty::singleton();
158 $smarty->assign('contactId', $cid);
159 $smarty->assign('external_identifier', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'external_identifier'));
160 $smarty->assign('lastModified', CRM_Core_BAO_Log::lastModified($cid, 'civicrm_contact'));
161 $viewOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
162 'contact_view_options', TRUE
163 );
164 $smarty->assign('changeLog', $viewOptions['log']);
165 $ret = ['markup' => $smarty->fetch('CRM/common/contactFooter.tpl')];
166 if ($includeCount) {
167 $ret['count'] = CRM_Contact_BAO_Contact::getCountComponent('log', $cid);
168 }
169 return ['changeLog' => $ret];
170 }
171
172 }