| 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(): string { |
| 48 | return 'Relationship'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Explicitly declare the form context. |
| 53 | * |
| 54 | * @return string|null |
| 55 | */ |
| 56 | public function getDefaultContext(): ?string { |
| 57 | return 'search'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * View details of a relationship. |
| 62 | * |
| 63 | * @throws \CRM_Core_Exception |
| 64 | * @throws \CiviCRM_API3_Exception |
| 65 | */ |
| 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']; |
| 73 | |
| 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); |
| 77 | } |
| 78 | } |
| 79 | $relType = $viewRelationship[$this->getEntityId()]['civicrm_relationship_type_id']; |
| 80 | $this->assign('viewRelationship', $viewRelationship); |
| 81 | |
| 82 | $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->getContactId(), 'employer_id'); |
| 83 | $this->assign('isCurrentEmployer', FALSE); |
| 84 | |
| 85 | $relTypes = CRM_Utils_Array::index(['name_a_b'], CRM_Core_PseudoConstant::relationshipType('name')); |
| 86 | |
| 87 | if ($viewRelationship[$this->getEntityId()]['employer_id'] == $this->getContactId()) { |
| 88 | $this->assign('isCurrentEmployer', TRUE); |
| 89 | } |
| 90 | elseif ($relType == $relTypes['Employee of']['id'] && |
| 91 | ($viewRelationship[$this->getEntityId()]['cid'] == $employerId) |
| 92 | ) { |
| 93 | // make sure we are viewing employee of relationship |
| 94 | $this->assign('isCurrentEmployer', TRUE); |
| 95 | } |
| 96 | |
| 97 | $viewNote = CRM_Core_BAO_Note::getNote($this->getEntityId()); |
| 98 | $this->assign('viewNote', $viewNote); |
| 99 | |
| 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()); |
| 103 | |
| 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" |
| 108 | ); |
| 109 | |
| 110 | $session = CRM_Core_Session::singleton(); |
| 111 | $recentOther = []; |
| 112 | |
| 113 | if (($session->get('userID') == $this->getContactId()) || |
| 114 | CRM_Contact_BAO_Contact_Permission::allow($this->getContactId(), CRM_Core_Permission::EDIT) |
| 115 | ) { |
| 116 | $recentOther = [ |
| 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" |
| 119 | ), |
| 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" |
| 122 | ), |
| 123 | ]; |
| 124 | } |
| 125 | |
| 126 | $displayName = CRM_Contact_BAO_Contact::displayName($this->getContactId()); |
| 127 | $this->assign('displayName', $displayName); |
| 128 | CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName); |
| 129 | |
| 130 | $title = $displayName . ' (' . $viewRelationship[$this->getEntityId()]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->getEntityId()]['cid']) . ')'; |
| 131 | |
| 132 | // add the recently viewed Relationship |
| 133 | CRM_Utils_Recent::add($title, |
| 134 | $url, |
| 135 | $viewRelationship[$this->getEntityId()]['id'], |
| 136 | 'Relationship', |
| 137 | $this->getContactId(), |
| 138 | NULL, |
| 139 | $recentOther |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * called when action is browse. |
| 145 | * |
| 146 | */ |
| 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); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * called when action is update or new. |
| 157 | * |
| 158 | */ |
| 159 | public function edit() { |
| 160 | $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->getAction()); |
| 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 |
| 167 | if ($this->getCaseId()) { |
| 168 | $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->getContactId()}&id={$this->getCaseId()}"); |
| 169 | } |
| 170 | else { |
| 171 | $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->getContactId()}"); |
| 172 | } |
| 173 | |
| 174 | $session->pushUserContext($url); |
| 175 | |
| 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'); |
| 181 | } |
| 182 | |
| 183 | // delete relationship |
| 184 | CRM_Contact_BAO_Relationship::del($this->getEntityId()); |
| 185 | |
| 186 | CRM_Utils_System::redirect($url); |
| 187 | } |
| 188 | |
| 189 | $controller->set('contactId', $this->getContactId()); |
| 190 | $controller->set('id', $this->getEntityId()); |
| 191 | $controller->process(); |
| 192 | $controller->run(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * the main function that is called when the page loads, |
| 197 | * it decides the which action has to be taken for the page. |
| 198 | * |
| 199 | * @return null |
| 200 | * @throws \CRM_Core_Exception |
| 201 | */ |
| 202 | public function run() { |
| 203 | $this->assign('entityInClassFormat', 'relationship'); |
| 204 | $this->preProcessQuickEntityPage(); |
| 205 | |
| 206 | $this->setContext(); |
| 207 | |
| 208 | $this->setCaseId(CRM_Utils_Request::retrieve('caseID', 'Integer', $this)); |
| 209 | |
| 210 | if ($this->isViewContext()) { |
| 211 | $this->view(); |
| 212 | } |
| 213 | elseif ($this->isEditContext() || $this->isDeleteContext()) { |
| 214 | $this->edit(); |
| 215 | } |
| 216 | |
| 217 | // if this is called from case view, suppress browse relationships form |
| 218 | else { |
| 219 | $this->browse(); |
| 220 | } |
| 221 | |
| 222 | return parent::run(); |
| 223 | } |
| 224 | |
| 225 | public function setContext() { |
| 226 | if ($this->getContext() === 'dashboard') { |
| 227 | $url = CRM_Utils_System::url('civicrm/user', "reset=1&id={$this->getContactId()}"); |
| 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 | /** |
| 237 | * called to delete the relationship of a contact. |
| 238 | * |
| 239 | */ |
| 240 | public function delete() { |
| 241 | // calls a function to delete relationship |
| 242 | CRM_Contact_BAO_Relationship::del($this->getEntityId()); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get action links. |
| 247 | * |
| 248 | * @return array |
| 249 | * (reference) of action links |
| 250 | */ |
| 251 | public static function &links() { |
| 252 | if (!(self::$_links)) { |
| 253 | 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'), |
| 259 | ], |
| 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'), |
| 265 | ], |
| 266 | CRM_Core_Action::ENABLE => [ |
| 267 | 'name' => ts('Enable'), |
| 268 | 'ref' => 'crm-enable-disable', |
| 269 | 'title' => ts('Enable Relationship'), |
| 270 | ], |
| 271 | CRM_Core_Action::DISABLE => [ |
| 272 | 'name' => ts('Disable'), |
| 273 | 'ref' => 'crm-enable-disable', |
| 274 | 'title' => ts('Disable Relationship'), |
| 275 | ], |
| 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'), |
| 281 | ], |
| 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'), |
| 289 | ], |
| 290 | ]; |
| 291 | } |
| 292 | return self::$_links; |
| 293 | } |
| 294 | |
| 295 | } |