3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_Page_View_Relationship
extends CRM_Core_Page
{
19 use CRM_Core_Page_EntityPageTrait
;
22 * Casid set if called from case context.
26 public $_caseId = NULL;
31 public function setCaseId($caseId) {
32 $this->_caseId
= $caseId;
38 public function getCaseId() {
39 return $this->_caseId
;
43 * Explicitly declare the entity api name.
47 public function getDefaultEntity(): string {
48 return 'Relationship';
52 * Explicitly declare the form context.
56 public function getDefaultContext(): ?
string {
61 * View details of a relationship.
63 * @throws \CRM_Core_Exception
64 * @throws \CiviCRM_API3_Exception
66 public function view() {
67 $viewRelationship = CRM_Contact_BAO_Relationship
::getRelationship($this->getContactId(), NULL, NULL, NULL, $this->getEntityId());
68 //To check whether selected contact is a contact_id_a in
69 //relationship type 'a_b' in relationship table, if yes then
70 //reverse the text in the template
71 $relationship = new CRM_Contact_DAO_Relationship();
72 $relationship->id
= $viewRelationship[$this->getEntityId()]['id'];
74 if ($relationship->find(TRUE)) {
75 if (($viewRelationship[$this->getEntityId()]['rtype'] === 'a_b') && ($this->getContactId() == $relationship->contact_id_a
)) {
76 $this->assign('is_contact_id_a', TRUE);
79 $relType = $viewRelationship[$this->getEntityId()]['civicrm_relationship_type_id'];
80 $this->assign('viewRelationship', $viewRelationship);
82 $employerId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->getContactId(), 'employer_id');
83 $this->assign('isCurrentEmployer', FALSE);
85 $relTypes = CRM_Utils_Array
::index(['name_a_b'], CRM_Core_PseudoConstant
::relationshipType('name'));
87 if ($viewRelationship[$this->getEntityId()]['employer_id'] == $this->getContactId()) {
88 $this->assign('isCurrentEmployer', TRUE);
90 elseif ($relType == $relTypes['Employee of']['id'] &&
91 ($viewRelationship[$this->getEntityId()]['cid'] == $employerId)
93 // make sure we are viewing employee of relationship
94 $this->assign('isCurrentEmployer', TRUE);
97 $viewNote = CRM_Core_BAO_Note
::getNote($this->getEntityId());
98 $this->assign('viewNote', $viewNote);
100 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Relationship', NULL, $this->getEntityId(), 0, $relType,
101 NULL, TRUE, NULL, FALSE, CRM_Core_Permission
::VIEW
);
102 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->getEntityId());
104 $rType = CRM_Utils_Array
::value('rtype', $viewRelationship[$this->getEntityId()]);
105 // add viewed contribution to recent items list
106 $url = CRM_Utils_System
::url('civicrm/contact/view/rel',
107 "action=view&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&context=home"
110 $session = CRM_Core_Session
::singleton();
113 if (($session->get('userID') == $this->getContactId()) ||
114 CRM_Contact_BAO_Contact_Permission
::allow($this->getContactId(), CRM_Core_Permission
::EDIT
)
117 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/rel',
118 "action=update&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
120 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/rel',
121 "action=delete&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
126 $displayName = CRM_Contact_BAO_Contact
::displayName($this->getContactId());
127 $this->assign('displayName', $displayName);
128 CRM_Utils_System
::setTitle(ts('View Relationship for') . ' ' . $displayName);
130 $title = $displayName . ' (' . $viewRelationship[$this->getEntityId()]['relation'] . ' ' . CRM_Contact_BAO_Contact
::displayName($viewRelationship[$this->getEntityId()]['cid']) . ')';
132 // add the recently viewed Relationship
133 CRM_Utils_Recent
::add($title,
135 $viewRelationship[$this->getEntityId()]['id'],
137 $this->getContactId(),
144 * called when action is browse.
147 public function browse() {
148 // do nothing :) we are using datatable for rendering relationship selectors
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);
156 * called when action is update or new.
159 public function edit() {
160 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->getAction());
161 $controller->setEmbedded(TRUE);
163 // set the userContext stack
164 $session = CRM_Core_Session
::singleton();
166 // if this is called from case view, we need to redirect back to same page
167 if ($this->getCaseId()) {
168 $url = CRM_Utils_System
::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->getContactId()}&id={$this->getCaseId()}");
171 $url = CRM_Utils_System
::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->getContactId()}");
174 $session->pushUserContext($url);
176 if (CRM_Utils_Request
::retrieve('confirmed', 'Boolean')) {
177 if ($this->getCaseId()) {
178 //create an activity for case role removal.CRM-4480
179 CRM_Case_BAO_Case
::createCaseRoleActivity($this->getCaseId(), $this->getEntityId());
180 CRM_Core_Session
::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
183 // delete relationship
184 CRM_Contact_BAO_Relationship
::del($this->getEntityId());
186 CRM_Utils_System
::redirect($url);
189 $controller->set('contactId', $this->getContactId());
190 $controller->set('id', $this->getEntityId());
191 $controller->process();
196 * the main function that is called when the page loads,
197 * it decides the which action has to be taken for the page.
200 * @throws \CRM_Core_Exception
202 public function run() {
203 $this->assign('entityInClassFormat', 'relationship');
204 $this->preProcessQuickEntityPage();
208 $this->setCaseId(CRM_Utils_Request
::retrieve('caseID', 'Integer', $this));
210 if ($this->isViewContext()) {
213 elseif ($this->isEditContext() ||
$this->isDeleteContext()) {
217 // if this is called from case view, suppress browse relationships form
222 return parent
::run();
225 public function setContext() {
226 if ($this->getContext() === 'dashboard') {
227 $url = CRM_Utils_System
::url('civicrm/user', "reset=1&id={$this->getContactId()}");
230 $url = CRM_Utils_System
::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
232 $session = CRM_Core_Session
::singleton();
233 $session->pushUserContext($url);
237 * called to delete the relationship of a contact.
240 public function delete() {
241 // calls a function to delete relationship
242 CRM_Contact_BAO_Relationship
::del($this->getEntityId());
249 * (reference) of action links
251 public static function &links() {
252 if (!(self
::$_links)) {
254 CRM_Core_Action
::VIEW
=> [
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'),
260 CRM_Core_Action
::UPDATE
=> [
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'),
266 CRM_Core_Action
::ENABLE
=> [
267 'name' => ts('Enable'),
268 'ref' => 'crm-enable-disable',
269 'title' => ts('Enable Relationship'),
271 CRM_Core_Action
::DISABLE
=> [
272 'name' => ts('Disable'),
273 'ref' => 'crm-enable-disable',
274 'title' => ts('Disable Relationship'),
276 CRM_Core_Action
::DELETE
=> [
277 'name' => ts('Delete'),
278 'url' => 'civicrm/contact/view/rel',
279 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
280 'title' => ts('Delete Relationship'),
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.
284 CRM_Core_Action
::NONE
=> [
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'),
292 return self
::$_links;