| 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 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * This class generates form components for Payment-Instrument |
| 22 | */ |
| 23 | class CRM_Member_Form_MembershipView extends CRM_Core_Form { |
| 24 | |
| 25 | /** |
| 26 | * The action links that we need to display for the browse screen. |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | public static $_links = NULL; |
| 31 | |
| 32 | /** |
| 33 | * The id of the membership being viewed. |
| 34 | * |
| 35 | * @var int |
| 36 | */ |
| 37 | private $membershipID; |
| 38 | |
| 39 | /** |
| 40 | * Contact's ID. |
| 41 | * |
| 42 | * @var int |
| 43 | */ |
| 44 | private $contactID; |
| 45 | |
| 46 | /** |
| 47 | * Add context information at the end of a link. |
| 48 | * |
| 49 | * @return string |
| 50 | * extra query parameters |
| 51 | */ |
| 52 | public function addContext() { |
| 53 | $extra = ''; |
| 54 | foreach (['context', 'selectedChild'] as $arg) { |
| 55 | if ($value = CRM_Utils_Request::retrieve($arg, 'String', $this)) { |
| 56 | $extra .= "&{$arg}={$value}"; |
| 57 | } |
| 58 | } |
| 59 | return $extra; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get action Links. |
| 64 | * |
| 65 | * @return array |
| 66 | * (reference) of action links |
| 67 | */ |
| 68 | public function &links() { |
| 69 | if (!(self::$_links)) { |
| 70 | self::$_links = [ |
| 71 | CRM_Core_Action::DELETE => [ |
| 72 | 'name' => ts('Delete'), |
| 73 | 'url' => 'civicrm/contact/view/membership', |
| 74 | 'qs' => 'action=view&id=%%id%%&cid=%%cid%%&relAction=delete&mid=%%mid%%&reset=1' . $this->addContext(), |
| 75 | 'title' => ts('Cancel Related Membership'), |
| 76 | ], |
| 77 | CRM_Core_Action::ADD => [ |
| 78 | 'name' => ts('Create'), |
| 79 | 'url' => 'civicrm/contact/view/membership', |
| 80 | 'qs' => 'action=view&id=%%id%%&cid=%%cid%%&relAction=create&rid=%%rid%%&reset=1' . $this->addContext(), |
| 81 | 'title' => ts('Create Related Membership'), |
| 82 | ], |
| 83 | ]; |
| 84 | } |
| 85 | return self::$_links; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Perform create or delete action on related memberships. |
| 90 | * |
| 91 | * @param string $action |
| 92 | * Create or delete. |
| 93 | * @param array $owner |
| 94 | * Primary membership info (membership_id, contact_id, membership_type ...). |
| 95 | */ |
| 96 | public function relAction($action, $owner) { |
| 97 | switch ($action) { |
| 98 | case 'delete': |
| 99 | $id = CRM_Utils_Request::retrieve('mid', 'Positive', $this); |
| 100 | $relatedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 101 | $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($relatedContactId); |
| 102 | CRM_Member_BAO_Membership::del($id); |
| 103 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been deleted.', [1 => $relatedDisplayName]), |
| 104 | ts('Membership Deleted'), 'success'); |
| 105 | break; |
| 106 | |
| 107 | case 'create': |
| 108 | $params = [ |
| 109 | 'contact_id' => CRM_Utils_Request::retrieve('rid', 'Positive', $this), |
| 110 | 'membership_type_id' => $owner['membership_type_id'], |
| 111 | 'owner_membership_id' => $owner['id'], |
| 112 | 'join_date' => CRM_Utils_Date::processDate($owner['join_date'], NULL, TRUE, 'Ymd'), |
| 113 | 'start_date' => CRM_Utils_Date::processDate($owner['start_date'], NULL, TRUE, 'Ymd'), |
| 114 | 'end_date' => CRM_Utils_Date::processDate($owner['end_date'], NULL, TRUE, 'Ymd'), |
| 115 | 'source' => ts('Manual Assignment of Related Membership'), |
| 116 | 'is_test' => $owner['is_test'], |
| 117 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $owner), |
| 118 | 'status_id' => $owner['status_id'], |
| 119 | 'skipStatusCal' => TRUE, |
| 120 | 'createActivity' => TRUE, |
| 121 | ]; |
| 122 | // @todo stop passing $ids here (we are only doing so because of passbyreference) |
| 123 | $ids = []; |
| 124 | CRM_Member_BAO_Membership::create($params, $ids); |
| 125 | $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($params['contact_id']); |
| 126 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been created.', [1 => $relatedDisplayName]), |
| 127 | ts('Membership Added'), 'success'); |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | CRM_Core_Error::fatal(ts("Invalid action specified in URL")); |
| 132 | } |
| 133 | |
| 134 | // Redirect back to membership view page for the owner, without the relAction parameters |
| 135 | CRM_Utils_System::redirect( |
| 136 | CRM_Utils_System::url( |
| 137 | 'civicrm/contact/view/membership', |
| 138 | "action=view&reset=1&id={$owner['membership_id']}&cid={$owner['contact_id']}" . $this->addContext() |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set variables up before form is built. |
| 145 | * |
| 146 | * @return void |
| 147 | */ |
| 148 | public function preProcess() { |
| 149 | $values = []; |
| 150 | $this->membershipID = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 151 | $this->contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 152 | |
| 153 | // Make sure context is assigned to template for condition where we come here view civicrm/membership/view |
| 154 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
| 155 | $this->assign('context', $context); |
| 156 | |
| 157 | if ($this->membershipID) { |
| 158 | $params = ['id' => $this->membershipID]; |
| 159 | CRM_Member_BAO_Membership::retrieve($params, $values); |
| 160 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { |
| 161 | $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values['membership_type_id'], 'financial_type_id'); |
| 162 | $finType = CRM_Contribute_PseudoConstant::financialType($finTypeId); |
| 163 | if (!CRM_Core_Permission::check('view contributions of type ' . $finType)) { |
| 164 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | $this->assign('noACL', TRUE); |
| 169 | } |
| 170 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); |
| 171 | |
| 172 | // Do the action on related Membership if needed |
| 173 | $relAction = CRM_Utils_Request::retrieve('relAction', 'String', $this); |
| 174 | if ($relAction) { |
| 175 | $this->relAction($relAction, $values); |
| 176 | } |
| 177 | |
| 178 | // build associated contributions |
| 179 | $this->assign('accessContribution', FALSE); |
| 180 | if (CRM_Core_Permission::access('CiviContribute')) { |
| 181 | $this->assign('accessContribution', TRUE); |
| 182 | CRM_Member_Page_Tab::associatedContribution($values['contact_id'], $this->membershipID); |
| 183 | } |
| 184 | |
| 185 | //Provide information about membership source when it is the result of a relationship (CRM-1901) |
| 186 | $values['owner_membership_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', |
| 187 | $this->membershipID, |
| 188 | 'owner_membership_id' |
| 189 | ); |
| 190 | |
| 191 | if (isset($values['owner_membership_id'])) { |
| 192 | $values['owner_contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', |
| 193 | $values['owner_membership_id'], |
| 194 | 'contact_id', |
| 195 | 'id' |
| 196 | ); |
| 197 | |
| 198 | $values['owner_display_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 199 | $values['owner_contact_id'], |
| 200 | 'display_name', |
| 201 | 'id' |
| 202 | ); |
| 203 | |
| 204 | $direction = strrev($membershipType['relationship_direction']); |
| 205 | // To display relationship type in view membership page |
| 206 | $relTypeIds = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ",", $membershipType['relationship_type_id']); |
| 207 | $sql = " |
| 208 | SELECT relationship_type_id, |
| 209 | CASE |
| 210 | WHEN contact_id_a = {$values['owner_contact_id']} AND contact_id_b = {$values['contact_id']} THEN 'b_a' |
| 211 | WHEN contact_id_b = {$values['owner_contact_id']} AND contact_id_a = {$values['contact_id']} THEN 'a_b' |
| 212 | END AS 'relType' |
| 213 | FROM civicrm_relationship |
| 214 | WHERE relationship_type_id IN ($relTypeIds)"; |
| 215 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 216 | $values['relationship'] = NULL; |
| 217 | while ($dao->fetch()) { |
| 218 | $typeId = $dao->relationship_type_id; |
| 219 | $direction = $dao->relType; |
| 220 | if ($direction && $typeId) { |
| 221 | if ($values['relationship']) { |
| 222 | $values['relationship'] .= ','; |
| 223 | } |
| 224 | $values['relationship'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', |
| 225 | $typeId, |
| 226 | "name_$direction", |
| 227 | 'id' |
| 228 | ); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | $this->assign('has_related', FALSE); |
| 234 | // if membership can be granted, and we are the owner of the membership |
| 235 | if (!empty($membershipType['relationship_type_id']) && empty($values['owner_membership_id'])) { |
| 236 | // display related contacts/membership block |
| 237 | $this->assign('has_related', TRUE); |
| 238 | $this->assign('max_related', CRM_Utils_Array::value('max_related', $values, ts('Unlimited'))); |
| 239 | // split the relations in 2 arrays based on direction |
| 240 | $relTypeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); |
| 241 | $relDirection = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); |
| 242 | foreach ($relTypeId as $rid) { |
| 243 | $relTypeDir[substr($relDirection[0], 0, 1)][] = $rid; |
| 244 | } |
| 245 | // build query in 2 parts with a UNION if necessary |
| 246 | // _x and _y are replaced with _a and _b first, then vice-versa |
| 247 | // comment is a qualifier for the relationship - now just job_title |
| 248 | $select = " |
| 249 | SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, |
| 250 | rt.name_x_y as relation, r.start_date, r.end_date, |
| 251 | m.id as mid, ms.is_current_member, ms.label as status |
| 252 | FROM civicrm_relationship r |
| 253 | LEFT JOIN civicrm_relationship_type rt ON rt.id = r.relationship_type_id |
| 254 | LEFT JOIN civicrm_contact c ON c.id = r.contact_id_x |
| 255 | LEFT JOIN civicrm_membership m ON (m.owner_membership_id = {$values['id']} |
| 256 | AND m.contact_id = r.contact_id_x AND m.is_test = 0) |
| 257 | LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id |
| 258 | WHERE r.contact_id_y = {$values['contact_id']} AND r.is_active = 1 AND c.is_deleted = 0"; |
| 259 | $query = ''; |
| 260 | foreach (['a', 'b'] as $dir) { |
| 261 | if (isset($relTypeDir[$dir])) { |
| 262 | $query .= ($query ? ' UNION ' : '') |
| 263 | . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ? 'b' : 'a'), $select)) |
| 264 | . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')'; |
| 265 | } |
| 266 | } |
| 267 | $query .= " ORDER BY is_current_member DESC"; |
| 268 | $dao = CRM_Core_DAO::executeQuery($query); |
| 269 | $related = []; |
| 270 | $relatedRemaining = CRM_Utils_Array::value('max_related', $values, PHP_INT_MAX); |
| 271 | $rowElememts = [ |
| 272 | 'id', |
| 273 | 'cid', |
| 274 | 'name', |
| 275 | 'comment', |
| 276 | 'relation', |
| 277 | 'mid', |
| 278 | 'start_date', |
| 279 | 'end_date', |
| 280 | 'is_current_member', |
| 281 | 'status', |
| 282 | ]; |
| 283 | |
| 284 | while ($dao->fetch()) { |
| 285 | $row = []; |
| 286 | foreach ($rowElememts as $field) { |
| 287 | $row[$field] = $dao->$field; |
| 288 | } |
| 289 | if ($row['mid'] && ($row['is_current_member'] == 1)) { |
| 290 | $relatedRemaining--; |
| 291 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::DELETE, |
| 292 | [ |
| 293 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), |
| 294 | 'cid' => $row['cid'], |
| 295 | 'mid' => $row['mid'], |
| 296 | ], |
| 297 | ts('more'), |
| 298 | FALSE, |
| 299 | 'membership.relationship.action', |
| 300 | 'Relationship', |
| 301 | CRM_Utils_Request::retrieve('id', 'Positive', $this) |
| 302 | ); |
| 303 | } |
| 304 | else { |
| 305 | if ($relatedRemaining > 0) { |
| 306 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::ADD, |
| 307 | [ |
| 308 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), |
| 309 | 'cid' => $row['cid'], |
| 310 | 'rid' => $row['cid'], |
| 311 | ], |
| 312 | ts('more'), |
| 313 | FALSE, |
| 314 | 'membership.relationship.action', |
| 315 | 'Relationship', |
| 316 | CRM_Utils_Request::retrieve('id', 'Positive', $this) |
| 317 | ); |
| 318 | } |
| 319 | } |
| 320 | $related[] = $row; |
| 321 | } |
| 322 | $this->assign('related', $related); |
| 323 | if ($relatedRemaining <= 0) { |
| 324 | $this->assign('related_text', ts('None available')); |
| 325 | } |
| 326 | else { |
| 327 | if ($relatedRemaining < 100000) { |
| 328 | $this->assign('related_text', ts('%1 available', [1 => $relatedRemaining])); |
| 329 | } |
| 330 | else { |
| 331 | $this->assign('related_text', ts('Unlimited', [1 => $relatedRemaining])); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']); |
| 337 | $this->assign('displayName', $displayName); |
| 338 | |
| 339 | // Check if this is default domain contact CRM-10482 |
| 340 | if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) { |
| 341 | $displayName .= ' (' . ts('default organization') . ')'; |
| 342 | } |
| 343 | |
| 344 | // omitting contactImage from title for now since the summary overlay css doesn't work outside crm-container |
| 345 | CRM_Utils_System::setTitle(ts('View Membership for') . ' ' . $displayName); |
| 346 | |
| 347 | // add viewed membership to recent items list |
| 348 | $recentTitle = $displayName . ' - ' . ts('Membership Type:') . ' ' . $values['membership_type']; |
| 349 | $url = CRM_Utils_System::url('civicrm/contact/view/membership', |
| 350 | "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" |
| 351 | ); |
| 352 | |
| 353 | $recentOther = []; |
| 354 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::UPDATE)) { |
| 355 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', |
| 356 | "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" |
| 357 | ); |
| 358 | } |
| 359 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::DELETE)) { |
| 360 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', |
| 361 | "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" |
| 362 | ); |
| 363 | } |
| 364 | CRM_Utils_Recent::add($recentTitle, |
| 365 | $url, |
| 366 | $values['id'], |
| 367 | 'Membership', |
| 368 | $values['contact_id'], |
| 369 | NULL, |
| 370 | $recentOther |
| 371 | ); |
| 372 | |
| 373 | CRM_Member_Page_Tab::setContext($this, $values['contact_id']); |
| 374 | |
| 375 | $memType = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $this->membershipID, "membership_type_id"); |
| 376 | |
| 377 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->membershipID, 0, $memType); |
| 378 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->membershipID); |
| 379 | |
| 380 | $isRecur = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->membershipID, 'contribution_recur_id'); |
| 381 | |
| 382 | $autoRenew = $isRecur ? TRUE : FALSE; |
| 383 | } |
| 384 | |
| 385 | if (!empty($values['is_test'])) { |
| 386 | $values['membership_type'] = CRM_Core_TestEntity::appendTestText($values['membership_type']); |
| 387 | } |
| 388 | |
| 389 | $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->membershipID); |
| 390 | $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ? 'Yes' : 'No'; |
| 391 | |
| 392 | //do check for campaigns |
| 393 | if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) { |
| 394 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); |
| 395 | $values['campaign'] = $campaigns[$campaignId]; |
| 396 | } |
| 397 | |
| 398 | $this->assign($values); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Build the form object. |
| 403 | * |
| 404 | * @return void |
| 405 | */ |
| 406 | public function buildQuickForm() { |
| 407 | $this->addButtons([ |
| 408 | [ |
| 409 | 'type' => 'cancel', |
| 410 | 'name' => ts('Done'), |
| 411 | 'spacing' => ' ', |
| 412 | 'isDefault' => TRUE, |
| 413 | ], |
| 414 | ]); |
| 415 | } |
| 416 | |
| 417 | } |