Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-21-20-13-45
[civicrm-core.git] / CRM / Contact / Page / View / Relationship.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
35 class 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);
80 if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) {
81 $this->assign('isCurrentEmployer', TRUE);
82 }
83 elseif ($relType == 4 &&
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
102
103 $session = CRM_Core_Session::singleton();
104 $recentOther = array();
105
106 if (($session->get('userID') == $this->_contactId) ||
107 CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)
108 ) {
109 $recentOther = array(
110 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
111 "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
112 ),
113 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
114 "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
115 ),
116 );
117 }
118
119 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
120 $this->assign('displayName', $displayName);
121 CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
122
123 $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')';
124
125 // add the recently viewed Relationship
126 CRM_Utils_Recent::add($title,
127 $url,
128 $viewRelationship[$this->_id]['id'],
129 'Relationship',
130 $this->_contactId,
131 NULL,
132 $recentOther
133 );
134 }
135
136 /**
137 * This function is called when action is browse
138 *
139 * return null
140 * @access public
141 */
142 function browse() {
143 $links = self::links();
144
145 //CRM-4418, handling edit and delete separately.
146 $permissions = array($this->_permission);
147 if ($this->_permission == CRM_Core_Permission::EDIT) {
148 //previously delete was subset of edit
149 //so for consistency lets grant delete also.
150 $permissions[] = CRM_Core_Permission::DELETE;
151 }
152 $mask = CRM_Core_Action::mask($permissions);
153
154 $currentRelationships = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId,
155 CRM_Contact_BAO_Relationship::CURRENT,
156 0, 0, 0,
157 $links, $mask
158 );
159
160 $inactiveRelationships = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId,
161 CRM_Contact_BAO_Relationship::INACTIVE,
162 0, 0, 0,
163 $links, $mask
164 );
165
166 $this->assign('currentRelationships', $currentRelationships);
167 // to show the 'Current Relationships' title and links only when viewed
168 // from relationship tab, not from dashboard
169 $this->assign('relationshipTabContext', TRUE);
170 $this->assign('inactiveRelationships', $inactiveRelationships);
171 }
172
173 /**
174 * This function is called when action is update or new
175 *
176 * return null
177 * @access public
178 */
179 function edit() {
180 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
181 $controller->setEmbedded(TRUE);
182
183 // set the userContext stack
184 $session = CRM_Core_Session::singleton();
185
186 // if this is called from case view, we need to redirect back to same page
187 if ($this->_caseId) {
188 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
189 }
190 else {
191 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
192 }
193
194 $session->pushUserContext($url);
195
196 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
197 CRM_Core_DAO::$_nullObject
198 )) {
199 if ($this->_caseId) {
200 //create an activity for case role removal.CRM-4480
201 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
202 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
203 }
204
205 // delete relationship
206 CRM_Contact_BAO_Relationship::del($this->_id);
207
208 CRM_Utils_System::redirect($url);
209 }
210
211 $controller->set('contactId', $this->_contactId);
212 $controller->set('id', $this->_id);
213 $controller->process();
214 $controller->run();
215 }
216
217 function preProcess() {
218 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
219 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
220 $this->assign('contactId', $this->_contactId);
221
222 // check logged in url permission
223 CRM_Contact_Page_View::checkUserPermission($this);
224
225 // set page title
226 CRM_Contact_Page_View::setTitle($this->_contactId);
227
228 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
229 $this->assign('action', $this->_action);
230 }
231
232 /**
233 * This function is the main function that is called when the page loads,
234 * it decides the which action has to be taken for the page.
235 *
236 * return null
237 * @access public
238 */
239 function run() {
240 $this->preProcess();
241
242 $this->setContext();
243
244 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
245
246 if ($this->_action & CRM_Core_Action::VIEW) {
247 $this->view();
248 }
249 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
250 $this->edit();
251 }
252 elseif ($this->_action & CRM_Core_Action::DISABLE) {
253 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::DISABLE);
254 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 0);
255 $session = CRM_Core_Session::singleton();
256 CRM_Utils_System::redirect($session->popUserContext());
257 }
258 elseif ($this->_action & CRM_Core_Action::ENABLE) {
259 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::ENABLE);
260 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 1);
261 $session = CRM_Core_Session::singleton();
262 CRM_Utils_System::redirect($session->popUserContext());
263 }
264
265 // if this is called from case view, suppress browse relationships form
266 if (!$this->_caseId) {
267 $this->browse();
268 }
269
270 return parent::run();
271 }
272
273 function setContext() {
274 $context = CRM_Utils_Request::retrieve('context', 'String',
275 $this, FALSE, 'search'
276 );
277
278 if ($context == 'dashboard') {
279 $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
280 $this, FALSE
281 );
282 $url = CRM_Utils_System::url('civicrm/user',
283 "reset=1&id={$cid}"
284 );
285 }
286 else {
287 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
288 }
289 $session = CRM_Core_Session::singleton();
290 $session->pushUserContext($url);
291 }
292
293 /**
294 * This function is called to delete the relationship of a contact
295 *
296 * return null
297 * @access public
298 */
299 function delete() {
300 // calls a function to delete relationship
301 CRM_Contact_BAO_Relationship::del($this->_id);
302 }
303
304 /**
305 * Get action links
306 *
307 * @return array (reference) of action links
308 * @static
309 */
310 static function &links() {
311 if (!(self::$_links)) {
312 $deleteExtra = ts('Are you sure you want to delete this relationship?');
313 $disableExtra = ts('Are you sure you want to disable this relationship?');
314 $enableExtra = ts('Are you sure you want to re-enable this relationship?');
315
316 self::$_links = array(
317 CRM_Core_Action::VIEW => array(
318 'name' => ts('View'),
319 'url' => 'civicrm/contact/view/rel',
320 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
321 'title' => ts('View Relationship'),
322 ),
323 CRM_Core_Action::UPDATE => array(
324 'name' => ts('Edit'),
325 'url' => 'civicrm/contact/view/rel',
326 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
327 'title' => ts('Edit Relationship'),
328 ),
329 CRM_Core_Action::ENABLE => array(
330 'name' => ts('Enable'),
331 'url' => 'civicrm/contact/view/rel',
332 'qs' => 'action=enable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
333 'extra' => 'onclick = "return confirm(\'' . $enableExtra . '\');"',
334 'title' => ts('Enable Relationship'),
335 ),
336 CRM_Core_Action::DISABLE => array(
337 'name' => ts('Disable'),
338 'url' => 'civicrm/contact/view/rel',
339 'qs' => 'action=disable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
340 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"',
341 'title' => ts('Disable Relationship'),
342 ),
343 CRM_Core_Action::DELETE => array(
344 'name' => ts('Delete'),
345 'url' => 'civicrm/contact/view/rel',
346 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
347 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
348 'title' => ts('Delete Relationship'),
349 ),
350 // FIXME: Not sure what to put as the key.
351 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
352 CRM_Core_Action::NONE => array(
353 'name' => ts('Manage Case'),
354 'url' => 'civicrm/contact/view/case',
355 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
356 'title' => ts('Manage Case'),
357 ),
358 );
359 }
360 return self::$_links;
361 }
362 }
363