Merge pull request #22639 from civicrm/5.46
[civicrm-core.git] / CRM / Contact / Page / View / Relationship.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 class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
18
19 use CRM_Core_Page_EntityPageTrait;
20
21 /**
22 * Casid set if called from case context.
23 *
24 * @var int
25 */
26 public $_caseId = NULL;
27
28 /**
29 * @param int $caseId
30 */
31 public function setCaseId($caseId) {
32 $this->_caseId = $caseId;
33 }
34
35 /**
36 * @return int
37 */
38 public function getCaseId() {
39 return $this->_caseId;
40 }
41
42 /**
43 * Explicitly declare the entity api name.
44 *
45 * @return string
46 */
47 public function getDefaultEntity() {
48 return 'Relationship';
49 }
50
51 /**
52 * Explicitly declare the form context.
53 *
54 * @return string|null
55 */
56 public function getDefaultContext() {
57 return 'search';
58 }
59
60 /**
61 * View details of a relationship.
62 */
63 public function view() {
64 $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->getContactId(), NULL, NULL, NULL, $this->getEntityId());
65 //To check whether selected contact is a contact_id_a in
66 //relationship type 'a_b' in relationship table, if yes then
67 //revert the permissionship text in template
68 $relationship = new CRM_Contact_DAO_Relationship();
69 $relationship->id = $viewRelationship[$this->getEntityId()]['id'];
70
71 if ($relationship->find(TRUE)) {
72 if (($viewRelationship[$this->getEntityId()]['rtype'] == 'a_b') && ($this->getContactId() == $relationship->contact_id_a)) {
73 $this->assign("is_contact_id_a", TRUE);
74 }
75 }
76 $relType = $viewRelationship[$this->getEntityId()]['civicrm_relationship_type_id'];
77 $this->assign('viewRelationship', $viewRelationship);
78
79 $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->getContactId(), 'employer_id');
80 $this->assign('isCurrentEmployer', FALSE);
81
82 $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
83
84 if ($viewRelationship[$this->getEntityId()]['employer_id'] == $this->getContactId()) {
85 $this->assign('isCurrentEmployer', TRUE);
86 }
87 elseif ($relType == $relTypes['Employee of']['id'] &&
88 ($viewRelationship[$this->getEntityId()]['cid'] == $employerId)
89 ) {
90 // make sure we are viewing employee of relationship
91 $this->assign('isCurrentEmployer', TRUE);
92 }
93
94 $viewNote = CRM_Core_BAO_Note::getNote($this->getEntityId());
95 $this->assign('viewNote', $viewNote);
96
97 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', NULL, $this->getEntityId(), 0, $relType,
98 NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
99 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->getEntityId());
100
101 $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->getEntityId()]);
102 // add viewed contribution to recent items list
103 $url = CRM_Utils_System::url('civicrm/contact/view/rel',
104 "action=view&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&context=home"
105 );
106
107 $session = CRM_Core_Session::singleton();
108 $recentOther = [];
109
110 if (($session->get('userID') == $this->getContactId()) ||
111 CRM_Contact_BAO_Contact_Permission::allow($this->getContactId(), CRM_Core_Permission::EDIT)
112 ) {
113 $recentOther = array(
114 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
115 "action=update&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
116 ),
117 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
118 "action=delete&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
119 ),
120 );
121 }
122
123 $displayName = CRM_Contact_BAO_Contact::displayName($this->getContactId());
124 $this->assign('displayName', $displayName);
125 CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
126
127 $title = $displayName . ' (' . $viewRelationship[$this->getEntityId()]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->getEntityId()]['cid']) . ')';
128
129 // add the recently viewed Relationship
130 CRM_Utils_Recent::add($title,
131 $url,
132 $viewRelationship[$this->getEntityId()]['id'],
133 'Relationship',
134 $this->getContactId(),
135 NULL,
136 $recentOther
137 );
138 }
139
140 /**
141 * called when action is browse.
142 *
143 */
144 public function browse() {
145 // do nothing :) we are using datatable for rendering relationship selectors
146 $columnHeaders = CRM_Contact_BAO_Relationship::getColumnHeaders();
147 $contactRelationships = $selector = NULL;
148 CRM_Utils_Hook::searchColumns('relationship.columns', $columnHeaders, $contactRelationships, $selector);
149 $this->assign('columnHeaders', $columnHeaders);
150 }
151
152 /**
153 * called when action is update or new.
154 *
155 */
156 public function edit() {
157 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->getAction());
158 $controller->setEmbedded(TRUE);
159
160 // set the userContext stack
161 $session = CRM_Core_Session::singleton();
162
163 // if this is called from case view, we need to redirect back to same page
164 if ($this->getCaseId()) {
165 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->getContactId()}&id={$this->getCaseId()}");
166 }
167 else {
168 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->getContactId()}");
169 }
170
171 $session->pushUserContext($url);
172
173 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
174 if ($this->getCaseId()) {
175 //create an activity for case role removal.CRM-4480
176 CRM_Case_BAO_Case::createCaseRoleActivity($this->getCaseId(), $this->getEntityId());
177 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
178 }
179
180 // delete relationship
181 CRM_Contact_BAO_Relationship::del($this->getEntityId());
182
183 CRM_Utils_System::redirect($url);
184 }
185
186 $controller->set('contactId', $this->getContactId());
187 $controller->set('id', $this->getEntityId());
188 $controller->process();
189 $controller->run();
190 }
191
192 /**
193 * the main function that is called when the page loads,
194 * it decides the which action has to be taken for the page.
195 *
196 * @return null
197 * @throws \CRM_Core_Exception
198 */
199 public function run() {
200 $this->assign('entityInClassFormat', 'relationship');
201 $this->preProcessQuickEntityPage();
202
203 $this->setContext();
204
205 $this->setCaseId(CRM_Utils_Request::retrieve('caseID', 'Integer', $this));
206
207 if ($this->isViewContext()) {
208 $this->view();
209 }
210 elseif ($this->isEditContext() || $this->isDeleteContext()) {
211 $this->edit();
212 }
213
214 // if this is called from case view, suppress browse relationships form
215 else {
216 $this->browse();
217 }
218
219 return parent::run();
220 }
221
222 public function setContext() {
223 if ($this->getContext() == 'dashboard') {
224 $url = CRM_Utils_System::url('civicrm/user', "reset=1&id={$this->getContactId()}");
225 }
226 else {
227 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
228 }
229 $session = CRM_Core_Session::singleton();
230 $session->pushUserContext($url);
231 }
232
233 /**
234 * called to delete the relationship of a contact.
235 *
236 */
237 public function delete() {
238 // calls a function to delete relationship
239 CRM_Contact_BAO_Relationship::del($this->getEntityId());
240 }
241
242 /**
243 * Get action links.
244 *
245 * @return array
246 * (reference) of action links
247 */
248 public static function &links() {
249 if (!(self::$_links)) {
250 self::$_links = array(
251 CRM_Core_Action::VIEW => array(
252 'name' => ts('View'),
253 'url' => 'civicrm/contact/view/rel',
254 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
255 'title' => ts('View Relationship'),
256 ),
257 CRM_Core_Action::UPDATE => array(
258 'name' => ts('Edit'),
259 'url' => 'civicrm/contact/view/rel',
260 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
261 'title' => ts('Edit Relationship'),
262 ),
263 CRM_Core_Action::ENABLE => array(
264 'name' => ts('Enable'),
265 'ref' => 'crm-enable-disable',
266 'title' => ts('Enable Relationship'),
267 ),
268 CRM_Core_Action::DISABLE => array(
269 'name' => ts('Disable'),
270 'ref' => 'crm-enable-disable',
271 'title' => ts('Disable Relationship'),
272 ),
273 CRM_Core_Action::DELETE => array(
274 'name' => ts('Delete'),
275 'url' => 'civicrm/contact/view/rel',
276 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
277 'title' => ts('Delete Relationship'),
278 ),
279 // FIXME: Not sure what to put as the key.
280 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
281 CRM_Core_Action::NONE => array(
282 'name' => ts('Manage Case'),
283 'url' => 'civicrm/contact/view/case',
284 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
285 'title' => ts('Manage Case'),
286 ),
287 );
288 }
289 return self::$_links;
290 }
291
292 }