Merge pull request #4918 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Contact / Page / View / Relationship.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
36
37 /**
38 * The action links that we need to display for the browse screen
39 *
40 * @var array
6a488035
TO
41 */
42 static $_links = NULL;
43
44 /**
100fef9d 45 * Casid set if called from case context
6a488035
TO
46 *
47 * @var int
48 */
49 public $_caseId = NULL;
50
51 public $_permission = NULL;
52 public $_contactId = NULL;
53
54 /**
55 * View details of a relationship
56 *
57 * @return void
6a488035 58 */
00be9182 59 public function view() {
6a488035
TO
60 $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId, NULL, NULL, NULL, $this->_id);
61 //To check whether selected contact is a contact_id_a in
62 //relationship type 'a_b' in relationship table, if yes then
63 //revert the permissionship text in template
64 $relationship = new CRM_Contact_DAO_Relationship();
65 $relationship->id = $viewRelationship[$this->_id]['id'];
66
67 if ($relationship->find(TRUE)) {
68 if (($viewRelationship[$this->_id]['rtype'] == 'a_b') && ($this->_contactId == $relationship->contact_id_a)) {
69 $this->assign("is_contact_id_a", TRUE);
70 }
71 }
72 $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id'];
73 $this->assign('viewRelationship', $viewRelationship);
74
75 $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id');
76 $this->assign('isCurrentEmployer', FALSE);
05802b8e
DG
77
78 $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
a7ac519a 79
6a488035
TO
80 if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) {
81 $this->assign('isCurrentEmployer', TRUE);
82 }
05802b8e 83 elseif ($relType == $relTypes['Employee of']['id'] &&
6a488035
TO
84 ($viewRelationship[$this->_id]['cid'] == $employerId)
85 ) {
86 // make sure we are viewing employee of relationship
87 $this->assign('isCurrentEmployer', TRUE);
88 }
89
90 $viewNote = CRM_Core_BAO_Note::getNote($this->_id);
91 $this->assign('viewNote', $viewNote);
92
93 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', $this, $this->_id, 0, $relType);
94 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
95
96 $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->_id]);
97 // add viewed contribution to recent items list
98 $url = CRM_Utils_System::url('civicrm/contact/view/rel',
99 "action=view&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&context=home"
100 );
101
6a488035
TO
102 $session = CRM_Core_Session::singleton();
103 $recentOther = array();
104
105 if (($session->get('userID') == $this->_contactId) ||
106 CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)
107 ) {
108 $recentOther = array(
109 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
110 "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
111 ),
112 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
113 "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
114 ),
115 );
116 }
117
118 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
119 $this->assign('displayName', $displayName);
120 CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
121
122 $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')';
123
124 // add the recently viewed Relationship
125 CRM_Utils_Recent::add($title,
126 $url,
127 $viewRelationship[$this->_id]['id'],
128 'Relationship',
129 $this->_contactId,
130 NULL,
131 $recentOther
132 );
133 }
134
135 /**
dc195289 136 * called when action is browse
6a488035 137 *
76e7a76c 138 * @return null
6a488035 139 */
00be9182 140 public function browse() {
40458f6c 141 // do nothing :) we are using datatable for rendering relationship selectors
6a488035
TO
142 }
143
144 /**
dc195289 145 * called when action is update or new
6a488035 146 *
76e7a76c 147 * @return null
6a488035 148 */
00be9182 149 public function edit() {
6a488035
TO
150 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
151 $controller->setEmbedded(TRUE);
152
153 // set the userContext stack
154 $session = CRM_Core_Session::singleton();
155
156 // if this is called from case view, we need to redirect back to same page
157 if ($this->_caseId) {
158 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
159 }
160 else {
161 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
162 }
163
164 $session->pushUserContext($url);
165
166 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
353ffa53
TO
167 CRM_Core_DAO::$_nullObject
168 )
169 ) {
6a488035
TO
170 if ($this->_caseId) {
171 //create an activity for case role removal.CRM-4480
172 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
173 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
174 }
175
176 // delete relationship
177 CRM_Contact_BAO_Relationship::del($this->_id);
178
179 CRM_Utils_System::redirect($url);
180 }
181
182 $controller->set('contactId', $this->_contactId);
183 $controller->set('id', $this->_id);
184 $controller->process();
185 $controller->run();
186 }
187
00be9182 188 public function preProcess() {
6a488035
TO
189 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
190 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
191 $this->assign('contactId', $this->_contactId);
192
193 // check logged in url permission
194 CRM_Contact_Page_View::checkUserPermission($this);
195
6a488035
TO
196 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
197 $this->assign('action', $this->_action);
198 }
199
200 /**
dc195289 201 * the main function that is called when the page loads,
6a488035
TO
202 * it decides the which action has to be taken for the page.
203 *
76e7a76c 204 * @return null
6a488035 205 */
00be9182 206 public function run() {
6a488035
TO
207 $this->preProcess();
208
209 $this->setContext();
210
211 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
212
213 if ($this->_action & CRM_Core_Action::VIEW) {
214 $this->view();
215 }
216 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
217 $this->edit();
218 }
219 elseif ($this->_action & CRM_Core_Action::DISABLE) {
220 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::DISABLE);
221 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 0);
222 $session = CRM_Core_Session::singleton();
223 CRM_Utils_System::redirect($session->popUserContext());
224 }
225 elseif ($this->_action & CRM_Core_Action::ENABLE) {
226 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::ENABLE);
227 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 1);
228 $session = CRM_Core_Session::singleton();
229 CRM_Utils_System::redirect($session->popUserContext());
230 }
231
232 // if this is called from case view, suppress browse relationships form
babae7c3 233 else {
6a488035
TO
234 $this->browse();
235 }
236
237 return parent::run();
238 }
239
00be9182 240 public function setContext() {
6a488035
TO
241 $context = CRM_Utils_Request::retrieve('context', 'String',
242 $this, FALSE, 'search'
243 );
244
245 if ($context == 'dashboard') {
246 $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
247 $this, FALSE
248 );
249 $url = CRM_Utils_System::url('civicrm/user',
250 "reset=1&id={$cid}"
251 );
252 }
253 else {
254 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
255 }
256 $session = CRM_Core_Session::singleton();
257 $session->pushUserContext($url);
258 }
259
260 /**
dc195289 261 * called to delete the relationship of a contact
6a488035 262 *
76e7a76c 263 * @return null
6a488035 264 */
00be9182 265 public function delete() {
6a488035
TO
266 // calls a function to delete relationship
267 CRM_Contact_BAO_Relationship::del($this->_id);
268 }
269
270 /**
271 * Get action links
272 *
a6c01b45
CW
273 * @return array
274 * (reference) of action links
6a488035 275 */
00be9182 276 public static function &links() {
6a488035 277 if (!(self::$_links)) {
6a488035
TO
278 self::$_links = array(
279 CRM_Core_Action::VIEW => array(
280 'name' => ts('View'),
281 'url' => 'civicrm/contact/view/rel',
282 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
283 'title' => ts('View Relationship'),
284 ),
285 CRM_Core_Action::UPDATE => array(
286 'name' => ts('Edit'),
287 'url' => 'civicrm/contact/view/rel',
288 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
289 'title' => ts('Edit Relationship'),
290 ),
291 CRM_Core_Action::ENABLE => array(
292 'name' => ts('Enable'),
41b47b8a 293 'ref' => 'crm-enable-disable',
6a488035
TO
294 'title' => ts('Enable Relationship'),
295 ),
296 CRM_Core_Action::DISABLE => array(
297 'name' => ts('Disable'),
41b47b8a 298 'ref' => 'crm-enable-disable',
6a488035
TO
299 'title' => ts('Disable Relationship'),
300 ),
301 CRM_Core_Action::DELETE => array(
302 'name' => ts('Delete'),
303 'url' => 'civicrm/contact/view/rel',
304 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
6a488035
TO
305 'title' => ts('Delete Relationship'),
306 ),
307 // FIXME: Not sure what to put as the key.
308 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
309 CRM_Core_Action::NONE => array(
310 'name' => ts('Manage Case'),
311 'url' => 'civicrm/contact/view/case',
312 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
313 'title' => ts('Manage Case'),
314 ),
315 );
316 }
317 return self::$_links;
318 }
319}