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