8f9b8a32349368ec6c82acb9a12b3bf9a38bd0df
[civicrm-core.git] / CRM / Contact / Page / View / Relationship.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 */
61 public function view() {
62 $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId, NULL, NULL, NULL, $this->_id);
63 //To check whether selected contact is a contact_id_a in
64 //relationship type 'a_b' in relationship table, if yes then
65 //revert the permissionship text in template
66 $relationship = new CRM_Contact_DAO_Relationship();
67 $relationship->id = $viewRelationship[$this->_id]['id'];
68
69 if ($relationship->find(TRUE)) {
70 if (($viewRelationship[$this->_id]['rtype'] == 'a_b') && ($this->_contactId == $relationship->contact_id_a)) {
71 $this->assign("is_contact_id_a", TRUE);
72 }
73 }
74 $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id'];
75 $this->assign('viewRelationship', $viewRelationship);
76
77 $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id');
78 $this->assign('isCurrentEmployer', FALSE);
79
80 $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
81
82 if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) {
83 $this->assign('isCurrentEmployer', TRUE);
84 }
85 elseif ($relType == $relTypes['Employee of']['id'] &&
86 ($viewRelationship[$this->_id]['cid'] == $employerId)
87 ) {
88 // make sure we are viewing employee of relationship
89 $this->assign('isCurrentEmployer', TRUE);
90 }
91
92 $viewNote = CRM_Core_BAO_Note::getNote($this->_id);
93 $this->assign('viewNote', $viewNote);
94
95 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', $this, $this->_id, 0, $relType);
96 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
97
98 $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->_id]);
99 // add viewed contribution to recent items list
100 $url = CRM_Utils_System::url('civicrm/contact/view/rel',
101 "action=view&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&context=home"
102 );
103
104
105 $session = CRM_Core_Session::singleton();
106 $recentOther = array();
107
108 if (($session->get('userID') == $this->_contactId) ||
109 CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)
110 ) {
111 $recentOther = array(
112 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
113 "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
114 ),
115 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
116 "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
117 ),
118 );
119 }
120
121 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
122 $this->assign('displayName', $displayName);
123 CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
124
125 $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')';
126
127 // add the recently viewed Relationship
128 CRM_Utils_Recent::add($title,
129 $url,
130 $viewRelationship[$this->_id]['id'],
131 'Relationship',
132 $this->_contactId,
133 NULL,
134 $recentOther
135 );
136 }
137
138 /**
139 * This function is called when action is browse
140 *
141 * return null
142 */
143 public function browse() {
144 // do nothing :) we are using datatable for rendering relationship selectors
145 }
146
147 /**
148 * This function is called when action is update or new
149 *
150 * return null
151 */
152 public function edit() {
153 $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
154 $controller->setEmbedded(TRUE);
155
156 // set the userContext stack
157 $session = CRM_Core_Session::singleton();
158
159 // if this is called from case view, we need to redirect back to same page
160 if ($this->_caseId) {
161 $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
162 }
163 else {
164 $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
165 }
166
167 $session->pushUserContext($url);
168
169 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
170 CRM_Core_DAO::$_nullObject
171 )) {
172 if ($this->_caseId) {
173 //create an activity for case role removal.CRM-4480
174 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
175 CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
176 }
177
178 // delete relationship
179 CRM_Contact_BAO_Relationship::del($this->_id);
180
181 CRM_Utils_System::redirect($url);
182 }
183
184 $controller->set('contactId', $this->_contactId);
185 $controller->set('id', $this->_id);
186 $controller->process();
187 $controller->run();
188 }
189
190 public function preProcess() {
191 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
192 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
193 $this->assign('contactId', $this->_contactId);
194
195 // check logged in url permission
196 CRM_Contact_Page_View::checkUserPermission($this);
197
198 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
199 $this->assign('action', $this->_action);
200 }
201
202 /**
203 * This function is the main function that is called when the page loads,
204 * it decides the which action has to be taken for the page.
205 *
206 * return null
207 */
208 public function run() {
209 $this->preProcess();
210
211 $this->setContext();
212
213 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
214
215 if ($this->_action & CRM_Core_Action::VIEW) {
216 $this->view();
217 }
218 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
219 $this->edit();
220 }
221 elseif ($this->_action & CRM_Core_Action::DISABLE) {
222 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::DISABLE);
223 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 0);
224 $session = CRM_Core_Session::singleton();
225 CRM_Utils_System::redirect($session->popUserContext());
226 }
227 elseif ($this->_action & CRM_Core_Action::ENABLE) {
228 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_id, CRM_Core_Action::ENABLE);
229 CRM_Contact_BAO_Relationship::setIsActive($this->_id, 1);
230 $session = CRM_Core_Session::singleton();
231 CRM_Utils_System::redirect($session->popUserContext());
232 }
233
234 // if this is called from case view, suppress browse relationships form
235 else {
236 $this->browse();
237 }
238
239 return parent::run();
240 }
241
242 public function setContext() {
243 $context = CRM_Utils_Request::retrieve('context', 'String',
244 $this, FALSE, 'search'
245 );
246
247 if ($context == 'dashboard') {
248 $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
249 $this, FALSE
250 );
251 $url = CRM_Utils_System::url('civicrm/user',
252 "reset=1&id={$cid}"
253 );
254 }
255 else {
256 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
257 }
258 $session = CRM_Core_Session::singleton();
259 $session->pushUserContext($url);
260 }
261
262 /**
263 * This function is called to delete the relationship of a contact
264 *
265 * return null
266 */
267 public function delete() {
268 // calls a function to delete relationship
269 CRM_Contact_BAO_Relationship::del($this->_id);
270 }
271
272 /**
273 * Get action links
274 *
275 * @return array (reference) of action links
276 * @static
277 */
278 public static function &links() {
279 if (!(self::$_links)) {
280 self::$_links = array(
281 CRM_Core_Action::VIEW => array(
282 'name' => ts('View'),
283 'url' => 'civicrm/contact/view/rel',
284 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
285 'title' => ts('View Relationship'),
286 ),
287 CRM_Core_Action::UPDATE => array(
288 'name' => ts('Edit'),
289 'url' => 'civicrm/contact/view/rel',
290 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
291 'title' => ts('Edit Relationship'),
292 ),
293 CRM_Core_Action::ENABLE => array(
294 'name' => ts('Enable'),
295 'ref' => 'crm-enable-disable',
296 'title' => ts('Enable Relationship'),
297 ),
298 CRM_Core_Action::DISABLE => array(
299 'name' => ts('Disable'),
300 'ref' => 'crm-enable-disable',
301 'title' => ts('Disable Relationship'),
302 ),
303 CRM_Core_Action::DELETE => array(
304 'name' => ts('Delete'),
305 'url' => 'civicrm/contact/view/rel',
306 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
307 'title' => ts('Delete Relationship'),
308 ),
309 // FIXME: Not sure what to put as the key.
310 // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default.
311 CRM_Core_Action::NONE => array(
312 'name' => ts('Manage Case'),
313 'url' => 'civicrm/contact/view/case',
314 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%',
315 'title' => ts('Manage Case'),
316 ),
317 );
318 }
319 return self::$_links;
320 }
321 }