Merge pull request #2061 from civicrm/4.3
[civicrm-core.git] / CRM / Contact / Page / View / Relationship.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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
41 * @static
42 */
43 static $_links = NULL;
44
45 /**
46 * casid set if called from case context
47 *
48 * @var int
49 */
50 public $_caseId = NULL;
51
52 public $_permission = NULL;
53 public $_contactId = NULL;
54
55 /**
56 * View details of a relationship
57 *
58 * @return void
59 *
60 * @access public
61 */
62 function view() {
63 $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId, NULL, NULL, NULL, $this->_id);
64 //To check whether selected contact is a contact_id_a in
65 //relationship type 'a_b' in relationship table, if yes then
66 //revert the permissionship text in template
67 $relationship = new CRM_Contact_DAO_Relationship();
68 $relationship->id = $viewRelationship[$this->_id]['id'];
69
70 if ($relationship->find(TRUE)) {
71 if (($viewRelationship[$this->_id]['rtype'] == 'a_b') && ($this->_contactId == $relationship->contact_id_a)) {
72 $this->assign("is_contact_id_a", TRUE);
73 }
74 }
75 $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id'];
76 $this->assign('viewRelationship', $viewRelationship);
77
78 $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id');
79 $this->assign('isCurrentEmployer', FALSE);
05802b8e
DG
80
81 $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
82
6a488035
TO
83 if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) {
84 $this->assign('isCurrentEmployer', TRUE);
85 }
05802b8e 86 elseif ($relType == $relTypes['Employee of']['id'] &&
6a488035
TO
87 ($viewRelationship[$this->_id]['cid'] == $employerId)
88 ) {
89 // make sure we are viewing employee of relationship
90 $this->assign('isCurrentEmployer', TRUE);
91 }
92
93 $viewNote = CRM_Core_BAO_Note::getNote($this->_id);
94 $this->assign('viewNote', $viewNote);
95
96 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', $this, $this->_id, 0, $relType);
97 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
98
99 $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->_id]);
100 // add viewed contribution to recent items list
101 $url = CRM_Utils_System::url('civicrm/contact/view/rel',
102 "action=view&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&context=home"
103 );
104
105
106 $session = CRM_Core_Session::singleton();
107 $recentOther = array();
108
109 if (($session->get('userID') == $this->_contactId) ||
110 CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)
111 ) {
112 $recentOther = array(
113 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
114 "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
115 ),
116 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
117 "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
118 ),
119 );
120 }
121
122 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
123 $this->assign('displayName', $displayName);
124 CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
125
126 $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')';
127
128 // add the recently viewed Relationship
129 CRM_Utils_Recent::add($title,
130 $url,
131 $viewRelationship[$this->_id]['id'],
132 'Relationship',
133 $this->_contactId,
134 NULL,
135 $recentOther
136 );
137 }
138
139 /**
140 * This function is called when action is browse
141 *
142 * return null
143 * @access public
144 */
145 function browse() {
146 $links = self::links();
147
148 //CRM-4418, handling edit and delete separately.
149 $permissions = array($this->_permission);
150 if ($this->_permission == CRM_Core_Permission::EDIT) {
151 //previously delete was subset of edit
152 //so for consistency lets grant delete also.
153 $permissions[] = CRM_Core_Permission::DELETE;
154 }
155 $mask = CRM_Core_Action::mask($permissions);
156
157 $currentRelationships = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId,
158 CRM_Contact_BAO_Relationship::CURRENT,
159 0, 0, 0,
160 $links, $mask
161 );
162
163 $inactiveRelationships = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId,
164 CRM_Contact_BAO_Relationship::INACTIVE,
165 0, 0, 0,
166 $links, $mask
167 );
168
169 $this->assign('currentRelationships', $currentRelationships);
170 // to show the 'Current Relationships' title and links only when viewed
171 // from relationship tab, not from dashboard
172 $this->assign('relationshipTabContext', TRUE);
173 $this->assign('inactiveRelationships', $inactiveRelationships);
174 }
175
176 /**
177 * This function is called when action is update or new
178 *
179 * return null
180 * @access public
181 */
182 function edit() {
183 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
184 $controller->setEmbedded(TRUE);
185
186 // set the userContext stack
187 $session = CRM_Core_Session::singleton();
188
189 // if this is called from case view, we need to redirect back to same page
190 if ($this->_caseId) {
191 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
192 }
193 else {
194 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
195 }
196
197 $session->pushUserContext($url);
198
199 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
200 CRM_Core_DAO::$_nullObject
201 )) {
202 if ($this->_caseId) {
203 //create an activity for case role removal.CRM-4480
204 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
205 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
206 }
207
208 // delete relationship
209 CRM_Contact_BAO_Relationship::del($this->_id);
210
211 CRM_Utils_System::redirect($url);
212 }
213
214 $controller->set('contactId', $this->_contactId);
215 $controller->set('id', $this->_id);
216 $controller->process();
217 $controller->run();
218 }
219
220 function preProcess() {
221 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
222 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
223 $this->assign('contactId', $this->_contactId);
224
225 // check logged in url permission
226 CRM_Contact_Page_View::checkUserPermission($this);
227
228 // set page title
229 CRM_Contact_Page_View::setTitle($this->_contactId);
230
231 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
232 $this->assign('action', $this->_action);
233 }
234
235 /**
236 * This function is the main function that is called when the page loads,
237 * it decides the which action has to be taken for the page.
238 *
239 * return null
240 * @access public
241 */
242 function run() {
243 $this->preProcess();
244
245 $this->setContext();
246
247 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
248
249 if ($this->_action & CRM_Core_Action::VIEW) {
250 $this->view();
251 }
252 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
253 $this->edit();
254 }
255 elseif ($this->_action & CRM_Core_Action::DISABLE) {
256 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::DISABLE);
257 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 0);
258 $session = CRM_Core_Session::singleton();
259 CRM_Utils_System::redirect($session->popUserContext());
260 }
261 elseif ($this->_action & CRM_Core_Action::ENABLE) {
262 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::ENABLE);
263 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 1);
264 $session = CRM_Core_Session::singleton();
265 CRM_Utils_System::redirect($session->popUserContext());
266 }
267
268 // if this is called from case view, suppress browse relationships form
269 if (!$this->_caseId) {
270 $this->browse();
271 }
272
273 return parent::run();
274 }
275
276 function setContext() {
277 $context = CRM_Utils_Request::retrieve('context', 'String',
278 $this, FALSE, 'search'
279 );
280
281 if ($context == 'dashboard') {
282 $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
283 $this, FALSE
284 );
285 $url = CRM_Utils_System::url('civicrm/user',
286 "reset=1&id={$cid}"
287 );
288 }
289 else {
290 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
291 }
292 $session = CRM_Core_Session::singleton();
293 $session->pushUserContext($url);
294 }
295
296 /**
297 * This function is called to delete the relationship of a contact
298 *
299 * return null
300 * @access public
301 */
302 function delete() {
303 // calls a function to delete relationship
304 CRM_Contact_BAO_Relationship::del($this->_id);
305 }
306
307 /**
308 * Get action links
309 *
310 * @return array (reference) of action links
311 * @static
312 */
313 static function &links() {
314 if (!(self::$_links)) {
315 $deleteExtra = ts('Are you sure you want to delete this relationship?');
316 $disableExtra = ts('Are you sure you want to disable this relationship?');
317 $enableExtra = ts('Are you sure you want to re-enable this relationship?');
318
319 self::$_links = array(
320 CRM_Core_Action::VIEW => array(
321 'name' => ts('View'),
322 'url' => 'civicrm/contact/view/rel',
323 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
324 'title' => ts('View Relationship'),
325 ),
326 CRM_Core_Action::UPDATE => array(
327 'name' => ts('Edit'),
328 'url' => 'civicrm/contact/view/rel',
329 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
330 'title' => ts('Edit Relationship'),
331 ),
332 CRM_Core_Action::ENABLE => array(
333 'name' => ts('Enable'),
334 'url' => 'civicrm/contact/view/rel',
335 'qs' => 'action=enable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
336 'extra' => 'onclick = "return confirm(\'' . $enableExtra . '\');"',
337 'title' => ts('Enable Relationship'),
338 ),
339 CRM_Core_Action::DISABLE => array(
340 'name' => ts('Disable'),
341 'url' => 'civicrm/contact/view/rel',
342 'qs' => 'action=disable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
343 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"',
344 'title' => ts('Disable Relationship'),
345 ),
346 CRM_Core_Action::DELETE => array(
347 'name' => ts('Delete'),
348 'url' => 'civicrm/contact/view/rel',
349 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
350 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
351 'title' => ts('Delete Relationship'),
352 ),
353 // FIXME: Not sure what to put as the key.
354 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
355 CRM_Core_Action::NONE => array(
356 'name' => ts('Manage Case'),
357 'url' => 'civicrm/contact/view/case',
358 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
359 'title' => ts('Manage Case'),
360 ),
361 );
362 }
363 return self::$_links;
364 }
365}
366