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