Commit | Line | Data |
---|---|---|
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 | */ |
17 | class 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 | ||
317103ab CW |
97 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', NULL, $this->getEntityId(), 0, $relType, |
98 | NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW); | |
6921a820 | 99 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->getEntityId()); |
6a488035 | 100 | |
6921a820 | 101 | $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->getEntityId()]); |
6a488035 TO |
102 | // add viewed contribution to recent items list |
103 | $url = CRM_Utils_System::url('civicrm/contact/view/rel', | |
6921a820 | 104 | "action=view&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&context=home" |
6a488035 TO |
105 | ); |
106 | ||
6a488035 | 107 | $session = CRM_Core_Session::singleton(); |
affcc9d2 | 108 | $recentOther = []; |
6a488035 | 109 | |
6921a820 MWMC |
110 | if (($session->get('userID') == $this->getContactId()) || |
111 | CRM_Contact_BAO_Contact_Permission::allow($this->getContactId(), CRM_Core_Permission::EDIT) | |
6a488035 TO |
112 | ) { |
113 | $recentOther = array( | |
114 | 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', | |
6921a820 | 115 | "action=update&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home" |
6a488035 TO |
116 | ), |
117 | 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', | |
6921a820 | 118 | "action=delete&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home" |
6a488035 TO |
119 | ), |
120 | ); | |
121 | } | |
122 | ||
6921a820 | 123 | $displayName = CRM_Contact_BAO_Contact::displayName($this->getContactId()); |
6a488035 TO |
124 | $this->assign('displayName', $displayName); |
125 | CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName); | |
126 | ||
6921a820 | 127 | $title = $displayName . ' (' . $viewRelationship[$this->getEntityId()]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->getEntityId()]['cid']) . ')'; |
6a488035 TO |
128 | |
129 | // add the recently viewed Relationship | |
130 | CRM_Utils_Recent::add($title, | |
131 | $url, | |
6921a820 | 132 | $viewRelationship[$this->getEntityId()]['id'], |
6a488035 | 133 | 'Relationship', |
6921a820 | 134 | $this->getContactId(), |
6a488035 TO |
135 | NULL, |
136 | $recentOther | |
137 | ); | |
138 | } | |
139 | ||
140 | /** | |
fe482240 | 141 | * called when action is browse. |
6a488035 | 142 | * |
6a488035 | 143 | */ |
00be9182 | 144 | public function browse() { |
40458f6c | 145 | // do nothing :) we are using datatable for rendering relationship selectors |
16b745b0 MWMC |
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); | |
6a488035 TO |
150 | } |
151 | ||
152 | /** | |
fe482240 | 153 | * called when action is update or new. |
6a488035 | 154 | * |
6a488035 | 155 | */ |
00be9182 | 156 | public function edit() { |
6921a820 | 157 | $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->getAction()); |
6a488035 TO |
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 | |
6921a820 MWMC |
164 | if ($this->getCaseId()) { |
165 | $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->getContactId()}&id={$this->getCaseId()}"); | |
6a488035 TO |
166 | } |
167 | else { | |
6921a820 | 168 | $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->getContactId()}"); |
6a488035 TO |
169 | } |
170 | ||
171 | $session->pushUserContext($url); | |
172 | ||
1273d77c | 173 | if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) { |
6921a820 | 174 | if ($this->getCaseId()) { |
6a488035 | 175 | //create an activity for case role removal.CRM-4480 |
6921a820 | 176 | CRM_Case_BAO_Case::createCaseRoleActivity($this->getCaseId(), $this->getEntityId()); |
6a488035 TO |
177 | CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success'); |
178 | } | |
179 | ||
180 | // delete relationship | |
6921a820 | 181 | CRM_Contact_BAO_Relationship::del($this->getEntityId()); |
6a488035 TO |
182 | |
183 | CRM_Utils_System::redirect($url); | |
184 | } | |
185 | ||
6921a820 MWMC |
186 | $controller->set('contactId', $this->getContactId()); |
187 | $controller->set('id', $this->getEntityId()); | |
6a488035 TO |
188 | $controller->process(); |
189 | $controller->run(); | |
190 | } | |
191 | ||
6a488035 | 192 | /** |
dc195289 | 193 | * the main function that is called when the page loads, |
6a488035 TO |
194 | * it decides the which action has to be taken for the page. |
195 | * | |
76e7a76c | 196 | * @return null |
6921a820 | 197 | * @throws \CRM_Core_Exception |
6a488035 | 198 | */ |
00be9182 | 199 | public function run() { |
78e2927c | 200 | $this->assign('entityInClassFormat', 'relationship'); |
6921a820 | 201 | $this->preProcessQuickEntityPage(); |
6a488035 TO |
202 | |
203 | $this->setContext(); | |
204 | ||
6921a820 | 205 | $this->setCaseId(CRM_Utils_Request::retrieve('caseID', 'Integer', $this)); |
6a488035 | 206 | |
6921a820 | 207 | if ($this->isViewContext()) { |
6a488035 TO |
208 | $this->view(); |
209 | } | |
6921a820 | 210 | elseif ($this->isEditContext() || $this->isDeleteContext()) { |
6a488035 TO |
211 | $this->edit(); |
212 | } | |
6a488035 TO |
213 | |
214 | // if this is called from case view, suppress browse relationships form | |
babae7c3 | 215 | else { |
6a488035 TO |
216 | $this->browse(); |
217 | } | |
218 | ||
219 | return parent::run(); | |
220 | } | |
221 | ||
00be9182 | 222 | public function setContext() { |
6921a820 MWMC |
223 | if ($this->getContext() == 'dashboard') { |
224 | $url = CRM_Utils_System::url('civicrm/user', "reset=1&id={$this->getContactId()}"); | |
6a488035 TO |
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 | /** | |
fe482240 | 234 | * called to delete the relationship of a contact. |
6a488035 | 235 | * |
6a488035 | 236 | */ |
00be9182 | 237 | public function delete() { |
6a488035 | 238 | // calls a function to delete relationship |
6921a820 | 239 | CRM_Contact_BAO_Relationship::del($this->getEntityId()); |
6a488035 TO |
240 | } |
241 | ||
242 | /** | |
fe482240 | 243 | * Get action links. |
6a488035 | 244 | * |
a6c01b45 CW |
245 | * @return array |
246 | * (reference) of action links | |
6a488035 | 247 | */ |
00be9182 | 248 | public static function &links() { |
6a488035 | 249 | if (!(self::$_links)) { |
6a488035 TO |
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'), | |
41b47b8a | 265 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
266 | 'title' => ts('Enable Relationship'), |
267 | ), | |
268 | CRM_Core_Action::DISABLE => array( | |
269 | 'name' => ts('Disable'), | |
41b47b8a | 270 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
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%%', | |
6a488035 TO |
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 | } | |
96025800 | 291 | |
6a488035 | 292 | } |