| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 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', $this, $this->_id, 0, $relType); |
| 90 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree); |
| 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 | CRM_Core_DAO::$_nullObject |
| 162 | ) |
| 163 | ) { |
| 164 | if ($this->_caseId) { |
| 165 | //create an activity for case role removal.CRM-4480 |
| 166 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id); |
| 167 | CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success'); |
| 168 | } |
| 169 | |
| 170 | // delete relationship |
| 171 | CRM_Contact_BAO_Relationship::del($this->_id); |
| 172 | |
| 173 | CRM_Utils_System::redirect($url); |
| 174 | } |
| 175 | |
| 176 | $controller->set('contactId', $this->_contactId); |
| 177 | $controller->set('id', $this->_id); |
| 178 | $controller->process(); |
| 179 | $controller->run(); |
| 180 | } |
| 181 | |
| 182 | public function preProcess() { |
| 183 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 184 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); |
| 185 | $this->assign('contactId', $this->_contactId); |
| 186 | |
| 187 | // check logged in url permission |
| 188 | CRM_Contact_Page_View::checkUserPermission($this); |
| 189 | |
| 190 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 191 | $this->assign('action', $this->_action); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * the main function that is called when the page loads, |
| 196 | * it decides the which action has to be taken for the page. |
| 197 | * |
| 198 | * @return null |
| 199 | */ |
| 200 | public function run() { |
| 201 | $this->preProcess(); |
| 202 | |
| 203 | $this->setContext(); |
| 204 | |
| 205 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); |
| 206 | |
| 207 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 208 | $this->view(); |
| 209 | } |
| 210 | elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) { |
| 211 | $this->edit(); |
| 212 | } |
| 213 | |
| 214 | // if this is called from case view, suppress browse relationships form |
| 215 | else { |
| 216 | $this->browse(); |
| 217 | } |
| 218 | |
| 219 | return parent::run(); |
| 220 | } |
| 221 | |
| 222 | public function setContext() { |
| 223 | $context = CRM_Utils_Request::retrieve('context', 'String', |
| 224 | $this, FALSE, 'search' |
| 225 | ); |
| 226 | |
| 227 | if ($context == 'dashboard') { |
| 228 | $cid = CRM_Utils_Request::retrieve('cid', 'Integer', |
| 229 | $this, FALSE |
| 230 | ); |
| 231 | $url = CRM_Utils_System::url('civicrm/user', |
| 232 | "reset=1&id={$cid}" |
| 233 | ); |
| 234 | } |
| 235 | else { |
| 236 | $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel'); |
| 237 | } |
| 238 | $session = CRM_Core_Session::singleton(); |
| 239 | $session->pushUserContext($url); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * called to delete the relationship of a contact. |
| 244 | * |
| 245 | */ |
| 246 | public function delete() { |
| 247 | // calls a function to delete relationship |
| 248 | CRM_Contact_BAO_Relationship::del($this->_id); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get action links. |
| 253 | * |
| 254 | * @return array |
| 255 | * (reference) of action links |
| 256 | */ |
| 257 | public static function &links() { |
| 258 | if (!(self::$_links)) { |
| 259 | self::$_links = array( |
| 260 | CRM_Core_Action::VIEW => array( |
| 261 | 'name' => ts('View'), |
| 262 | 'url' => 'civicrm/contact/view/rel', |
| 263 | 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel', |
| 264 | 'title' => ts('View Relationship'), |
| 265 | ), |
| 266 | CRM_Core_Action::UPDATE => array( |
| 267 | 'name' => ts('Edit'), |
| 268 | 'url' => 'civicrm/contact/view/rel', |
| 269 | 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%', |
| 270 | 'title' => ts('Edit Relationship'), |
| 271 | ), |
| 272 | CRM_Core_Action::ENABLE => array( |
| 273 | 'name' => ts('Enable'), |
| 274 | 'ref' => 'crm-enable-disable', |
| 275 | 'title' => ts('Enable Relationship'), |
| 276 | ), |
| 277 | CRM_Core_Action::DISABLE => array( |
| 278 | 'name' => ts('Disable'), |
| 279 | 'ref' => 'crm-enable-disable', |
| 280 | 'title' => ts('Disable Relationship'), |
| 281 | ), |
| 282 | CRM_Core_Action::DELETE => array( |
| 283 | 'name' => ts('Delete'), |
| 284 | 'url' => 'civicrm/contact/view/rel', |
| 285 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%', |
| 286 | 'title' => ts('Delete Relationship'), |
| 287 | ), |
| 288 | // FIXME: Not sure what to put as the key. |
| 289 | // We want to use it differently later anyway (see CRM_Contact_BAO_Relationship::getRelationship). NONE should make it hidden by default. |
| 290 | CRM_Core_Action::NONE => array( |
| 291 | 'name' => ts('Manage Case'), |
| 292 | 'url' => 'civicrm/contact/view/case', |
| 293 | 'qs' => 'action=view&reset=1&cid=%%clientid%%&id=%%caseid%%', |
| 294 | 'title' => ts('Manage Case'), |
| 295 | ), |
| 296 | ); |
| 297 | } |
| 298 | return self::$_links; |
| 299 | } |
| 300 | |
| 301 | } |