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