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