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