CRM-15889 fix - Relationship enable/disable through API & the UI - does not take...
[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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 /**
fe482240 38 * The action links that we need to display for the browse screen.
6a488035
TO
39 *
40 * @var array
6a488035
TO
41 */
42 static $_links = NULL;
43
44 /**
fe482240 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 /**
fe482240 55 * View details of a relationship.
6a488035
TO
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 /**
fe482240 136 * called when action is browse.
6a488035 137 *
6a488035 138 */
00be9182 139 public function browse() {
40458f6c 140 // do nothing :) we are using datatable for rendering relationship selectors
6a488035
TO
141 }
142
143 /**
fe482240 144 * called when action is update or new.
6a488035 145 *
6a488035 146 */
00be9182 147 public function edit() {
6a488035
TO
148 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
149 $controller->setEmbedded(TRUE);
150
151 // set the userContext stack
152 $session = CRM_Core_Session::singleton();
153
154 // if this is called from case view, we need to redirect back to same page
155 if ($this->_caseId) {
156 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
157 }
158 else {
159 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
160 }
161
162 $session->pushUserContext($url);
163
164 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
353ffa53
TO
165 CRM_Core_DAO::$_nullObject
166 )
167 ) {
6a488035
TO
168 if ($this->_caseId) {
169 //create an activity for case role removal.CRM-4480
170 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
171 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
172 }
173
174 // delete relationship
175 CRM_Contact_BAO_Relationship::del($this->_id);
176
177 CRM_Utils_System::redirect($url);
178 }
179
180 $controller->set('contactId', $this->_contactId);
181 $controller->set('id', $this->_id);
182 $controller->process();
183 $controller->run();
184 }
185
00be9182 186 public function preProcess() {
6a488035
TO
187 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
188 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
189 $this->assign('contactId', $this->_contactId);
190
191 // check logged in url permission
192 CRM_Contact_Page_View::checkUserPermission($this);
193
6a488035
TO
194 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
195 $this->assign('action', $this->_action);
196 }
197
198 /**
dc195289 199 * the main function that is called when the page loads,
6a488035
TO
200 * it decides the which action has to be taken for the page.
201 *
76e7a76c 202 * @return null
6a488035 203 */
00be9182 204 public function run() {
6a488035
TO
205 $this->preProcess();
206
207 $this->setContext();
208
209 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
210
211 if ($this->_action & CRM_Core_Action::VIEW) {
212 $this->view();
213 }
214 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
215 $this->edit();
216 }
6a488035
TO
217
218 // if this is called from case view, suppress browse relationships form
babae7c3 219 else {
6a488035
TO
220 $this->browse();
221 }
222
223 return parent::run();
224 }
225
00be9182 226 public function setContext() {
6a488035
TO
227 $context = CRM_Utils_Request::retrieve('context', 'String',
228 $this, FALSE, 'search'
229 );
230
231 if ($context == 'dashboard') {
232 $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
233 $this, FALSE
234 );
235 $url = CRM_Utils_System::url('civicrm/user',
236 "reset=1&id={$cid}"
237 );
238 }
239 else {
240 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
241 }
242 $session = CRM_Core_Session::singleton();
243 $session->pushUserContext($url);
244 }
245
246 /**
fe482240 247 * called to delete the relationship of a contact.
6a488035 248 *
6a488035 249 */
00be9182 250 public function delete() {
6a488035
TO
251 // calls a function to delete relationship
252 CRM_Contact_BAO_Relationship::del($this->_id);
253 }
254
255 /**
fe482240 256 * Get action links.
6a488035 257 *
a6c01b45
CW
258 * @return array
259 * (reference) of action links
6a488035 260 */
00be9182 261 public static function &links() {
6a488035 262 if (!(self::$_links)) {
6a488035
TO
263 self::$_links = array(
264 CRM_Core_Action::VIEW => array(
265 'name' => ts('View'),
266 'url' => 'civicrm/contact/view/rel',
267 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
268 'title' => ts('View Relationship'),
269 ),
270 CRM_Core_Action::UPDATE => array(
271 'name' => ts('Edit'),
272 'url' => 'civicrm/contact/view/rel',
273 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
274 'title' => ts('Edit Relationship'),
275 ),
276 CRM_Core_Action::ENABLE => array(
277 'name' => ts('Enable'),
41b47b8a 278 'ref' => 'crm-enable-disable',
6a488035
TO
279 'title' => ts('Enable Relationship'),
280 ),
281 CRM_Core_Action::DISABLE => array(
282 'name' => ts('Disable'),
41b47b8a 283 'ref' => 'crm-enable-disable',
6a488035
TO
284 'title' => ts('Disable Relationship'),
285 ),
286 CRM_Core_Action::DELETE => array(
287 'name' => ts('Delete'),
288 'url' => 'civicrm/contact/view/rel',
289 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
6a488035
TO
290 'title' => ts('Delete Relationship'),
291 ),
292 // FIXME: Not sure what to put as the key.
293 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
294 CRM_Core_Action::NONE => array(
295 'name' => ts('Manage Case'),
296 'url' => 'civicrm/contact/view/case',
297 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
298 'title' => ts('Manage Case'),
299 ),
300 );
301 }
302 return self::$_links;
303 }
96025800 304
6a488035 305}