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