/** * Creates a subscription object of the appropriate class to the product * * @return cbpaidUsersubscriptionRecord */ public function & newSubscription() { global $_CB_database; $sub = new cbpaidUsersubscriptionRecord( $_CB_database ); $sub->set( 'plan_id', $this->get( 'id' ) ); return $sub; }
/** * Checks for upgrade or renewal possibilities * * @param int $ui 1=frontend, 2=backend * @param int $user_id * @param int $now system unix time * @param cbpaidUsersubscriptionRecord[] $subscriptionsReturned RETURNED: current subscriptions * with ->status = 'A' for active ones and 'X' for expired ones. 'R' unpaid, 'C' cancelled. * @param array|null $plansToShowOnly array of specific plan numbers to show (so we add these plans if allowed and not spontaneous in frontend * @param int $subsAccess 0 has only read access, 1 has user access, 2 reserved for future Super-admin access * @param boolean $plansToShowOnlyDoIncludeChildren Include children with plansToShowOnly * @return cbPaidProduct[] upgrade possibilities including _renewalDiscount in plan's currency */ public function getUpgradeAndRenewalPossibilities($ui, $user_id, $now, &$subscriptionsReturned, $plansToShowOnly = null, $subsAccess = 1, $plansToShowOnlyDoIncludeChildren = false) { global $_CB_database, $_CB_framework; if (!isset($this->_upgradesCache[$user_id])) { $quantity = 1; //TBD later ! $paidUserExtension = cbpaidUserExtension::getInstance($user_id); $subscriptions = $paidUserExtension->getUserSubscriptions(null, true); $user = CBuser::getUserDataInstance((int) $user_id); $plansMgr = cbpaidPlansMgr::getInstance(); $plans = $plansMgr->loadPublishedPlans($user, true, 'any', null); //TBD LATER: upgrades limiting owners $params = cbpaidApp::settingsParams(); $enableFreeRegisteredUser = $params->get('enableFreeRegisteredUser', 1); $createAlsoFreeSubscriptions = $params->get('createAlsoFreeSubscriptions', 0); $noValidSubscriptionFound = true; $subscriptionsUpgradePlansIdsDiscount = array(); // array: [$k][$upgrade_plan->id]=discountedPrice where $l is index in $subscriptions $activeExclusiveSubChild = array(); // array: [$parentPlanId] = true $notProposedParents = array(); foreach (array_keys($subscriptions) as $k) { // for each user subscription: // 1. check if it's plan can be shown as an extra subscription possibility and/or upgrade, $subscriptions[$k]->checkRenewalUpgrade($ui, $user, $quantity, $now, $subsAccess); // 2. don't propose subscription which can not be shown to the user if ($subscriptions[$k]->_hideItsPlan && isset($plans[$subscriptions[$k]->plan_id])) { $plans[$subscriptions[$k]->plan_id]->_drawOnlyAsContainer = true; // $notProposedParents[$subscriptions[$k]->plan_id] = true; } if (($subscriptions[$k]->_hideThisSubscription || !$subscriptions[$k]->checkIfValid($now)) && (isset($plans[$subscriptions[$k]->plan_id]) && $plans[$subscriptions[$k]->plan_id]->get('multiple') == 0)) { foreach (array_keys($plans) as $pk) { // hidden or inactive subscription: do not display any of its children plans as upgrade possibility: if ($plans[$pk]->get('parent') == $subscriptions[$k]->plan_id) { $plans[$pk]->_drawOnlyAsContainer = true; $notProposedParents[$pk] = true; } } } if ($subscriptions[$k]->_hideThisSubscription) { unset($subscriptions[$k]); } elseif ($subscriptions[$k]->checkIfValid($now)) { // 3. all upgrade possibilities of this subscription $noValidSubscriptionFound = false; $subscriptionsUpgradePlansIdsDiscount[$k] = $subscriptions[$k]->_upgradePlansIdsDiscount; if ($subscriptions[$k]->getPlanAttribute('exclusive') == 1) { $activeExclusiveSubChild[$subscriptions[$k]->getPlanAttribute('parent')] = true; } } else { } } // add to each plan the subscriptions which can be upgraded: plan, subscription and price: foreach (array_keys($plans) as $pk) { foreach ($subscriptionsUpgradePlansIdsDiscount as $k => $upgradePlansDiscount) { foreach ($upgradePlansDiscount as $planId => $discountedPrice) { if ($plans[$pk]->get('id') == $planId) { $plans[$pk]->_subscriptionToUpdate = array($subscriptions[$k]->plan_id, $subscriptions[$k]->id); $plans[$pk]->_renewalDiscount = $discountedPrice; } } } } // finally remove all plans not allowed for upgrade and // also all exclusive plans which can't be upgraded to by no subscription // (already subscribed plans have already been removed by plan's _hideItsPlan instructions): // also memorize them as removed parent, so that children are not proposed either: foreach (array_keys($plans) as $pk) { $exclPlan = $plans[$pk]->get('exclusive'); $resultTexts = array(); // remove plans not listed by default and not specifically selected: if (!$plans[$pk]->isPlanAllowingUpgradesToThis($user_id, $resultTexts) || $plans[$pk]->get('propose_upgrade') != 1 && $ui != 2 && !($plansToShowOnly && (in_array($pk, $plansToShowOnly) || $plansToShowOnlyDoIncludeChildren && in_array($plans[$pk]->get('parent'), $plansToShowOnly))) || $exclPlan == 1 && $plans[$pk]->get('multiple') == 0 && isset($activeExclusiveSubChild[$plans[$pk]->get('parent')]) && $plans[$pk]->_subscriptionToUpdate === null) { // if ( $ui == 1 ) { // when we are in frontend: if (!(isset($plans[$pk]->_drawOnlyAsContainer) && $plans[$pk]->_drawOnlyAsContainer)) { $plans[$pk]->_drawOnlyAsContainer = true; $notProposedParents[$pk] = true; } } } // very finally remove also children of non-authorized parent plans: // second case is that parent plan isn't published: foreach (array_keys($plans) as $pk) { $parentPlanId = $plans[$pk]->get('parent'); if ($parentPlanId && (isset($notProposedParents[$parentPlanId]) || !isset($plans[$parentPlanId]))) { $plans[$pk]->_drawOnlyAsContainer = true; } } // If no sbscriptions at all or no active/registered ones, and the corresponding setting allows it: // Find the first free lifetime one with Registered level: if ((count($subscriptions) == 0 || $noValidSubscriptionFound) && $enableFreeRegisteredUser && !$createAlsoFreeSubscriptions) { $firstFreePlanId = null; $registeredUserGroup = $_CB_framework->getCfg('new_usertype'); foreach ($plans as $v) { if ($v->isLifetimeValidity() && $v->isFree() && in_array($v->get('usergroup'), array($registeredUserGroup, 0))) { if ($firstFreePlanId === null) { $firstFreePlanId = $v->get('id'); } break; } } if ($firstFreePlanId) { $freeSub = new cbpaidUsersubscriptionRecord($_CB_database); $freeSub->createSubscription($user_id, $plans[$firstFreePlanId], null, null, 'A', false); array_unshift($subscriptions, $freeSub); $plans[$firstFreePlanId]->_drawOnlyAsContainer = true; } } $this->_upgradesCache[$user_id] = array('subscriptions' => &$subscriptions, 'plans' => &$plans); } $subscriptionsReturned = $this->_upgradesCache[$user_id]['subscriptions']; return $this->_upgradesCache[$user_id]['plans']; }
/** * Shows an unsubscription confirmation form (frontend) * * @param UserTable $user * @param string $introText * @param int $planId * @param int $subscriptionId * @return string|null */ public function showUnsubscribeForm( &$user, $introText, $planId, $subscriptionId ) { global $_CB_database, $_CB_framework; $ui = $_CB_framework->getUi(); // get the most recent payment basket for that user and plan, and with that subscription if $subscriptionId != null: $subscription = new cbpaidUsersubscriptionRecord( $_CB_database ); $subscriptionLoaded = $subscription->load( (int) $subscriptionId ); if ( $subscriptionLoaded && ( $subscription->user_id == $user->id ) ) { $base_url = $this->getHttpsAbsURLwithParam( array( 'Itemid' => 0, 'user' => $user->id ), 'pluginclass' ); $return = '<form method="post" class="cbregUnsubscribeForm" action="'.$base_url.'">'; $return .= '<div id="cbregUnsubIntro">' . $introText . '</div>'; $return .= '<input type="hidden" name="user" value="'.$user->id.'" />'; $return .= '<input type="hidden" name="'.$this->_getPagingParamName("subscription[]").'" value="' . (int) $subscriptionId . '" />'; $return .= '<input type="hidden" name="'.$this->_getPagingParamName("plan[]").'" value="' . (int) $planId . '" />'; $return .= '<input type="hidden" name="'.$this->_getPagingParamName("act").'" value="confirm_unsub" />'; $return .= $this->_drawActionButton( $user, $ui, $subscription, $subscription->plan_id, CBPTXT::T("Yes: Unsubscribe"), 'confirm_unsubscribe', CBPTXT::T("This unsubscription is with immediate effect, without refund, and cannot be undone. Are you sure ?") ); $return .= $this->_drawActionButton( $user, $ui, $subscription, $subscription->plan_id, CBPTXT::T("No: Stay subscribed"), 'display_subscriptions' ); $return .= cbGetSpoofInputTag( 'plugin' ); $return .= '</form>'; } else { $params =& cbpaidApp::settingsParams(); $subTxt = CBPTXT::T( $params->get( 'subscription_name', 'subscription' ) ); $this->_setErrorMSG( sprintf( CBPTXT::T("No %s found"), $subTxt ) ); $return = null; } return $return; }