/** * Draws the subscription for registrations and profile views * * @param string $plansTitle Title field of the plans (for validation texts) * @param string $selectionId html input tag attribute id='' field for the input * @param string $selectionName html input tag attribute name='' field for the input * @param string $selectionValue html input tag attribute value='' field for the input * @param string $insertBeforePrice HTML text to insert after description of this item but before price * @param string $insertAfterDescription HTML text to insert after this item as sub-items * @param boolean|int $selected TRUE if the item is selected, FALSE if not selected but radio/checkbox is visible, (int) 2 if selected but hidden input * @param string $reason Payment reason: 'N'=new subscription (default), 'R'=renewal, 'U'=update * @param boolean $displayDescription TRUE: display description also * @param boolean $displayPrice TRUE: display price/price selection also * @param int $user_id The user id for whom this plan is proposed * @return string */ public function drawProduct( $plansTitle, $selectionId, $selectionName, $selectionValue, $insertBeforePrice, $insertAfterDescription, $selected, /** @noinspection PhpUnusedParameterInspection */ $reason, $displayDescription, /** @noinspection PhpUnusedParameterInspection */ $displayPrice, $user_id ) { global $_CB_framework; $this->planstitle = $plansTitle; $this->_selectionId = ( $selectionId ? $this->_getPagingParamName( $selectionId ) : null ); $this->_selectionName = ( $selectionName ? $this->_getPagingParamName( $selectionName . '[selected][]' ) : null ); $this->_selectionValue = $selectionValue; $this->_insertBeforePrice = $insertBeforePrice; $this->_insertAfterDescription = $insertAfterDescription; $this->cssclass = trim( $this->_model->get( 'cssclass' ) ); $this->cssid = 'cbregProduct_' . $this->_model->get( 'id' ); $this->description = ( $displayDescription ? $this->_model->getPersonalized( 'description', $user_id, true ) : null ); $this->exclusive = $this->_model->get( 'exclusive' ); $hiddenSelected = ( $selected === 2 ); if ( $hiddenSelected ) { $selected = false; } $required = ( $this->exclusive && ( $_CB_framework->getUi() == 1 ) && ( $this->_model->get( 'parent' ) == 0 ) && ! $hiddenSelected ); if ( ( $this->_model->get( 'hidechildren' ) == 1 ) && ! $hiddenSelected ) { $this->cssclass .= ( $this->cssclass ? ' ' : '' ) . 'cbregDoHideChildren'; } if ( $hiddenSelected ) { $this->_control = 'hidden'; } elseif ( $this->exclusive ) { $this->_control = 'radio'; } else { $this->_control = 'checkbox'; } if ( $required && ! $hiddenSelected ) { $this->extrainputs = 'mosReq="1" mosLabel="' . htmlspecialchars( CBPTXT::T( $this->planstitle ) ) . '" class="required" '; } else { $this->extrainputs = ''; } if ( $this->cssclass ) { $this->cssclass = ' ' . trim( $this->cssclass ); } if ( $selected ) { $this->_checked = 'checked="checked" '; } else { $this->_checked = ''; } if ( $selectionId ) { $this->_tick = '<input type="' . $this->_control . '" name="' . $this->_selectionName . '" id="' . $this->_selectionId . '" value="' . $this->_selectionValue . '" ' . $this->_checked . $this->extrainputs . '/>'; } else { $this->_tick = ' '; } $htmlNameTranslated = $this->_model->getPersonalized( 'name', $user_id, true ); if ( $selectionId && ! $hiddenSelected ) { $this->_labelledName = '<label for="' . $this->_selectionId . '">' . $htmlNameTranslated . '</label>'; } else { $this->_labelledName = '<label>' . $htmlNameTranslated . '</label>'; } return 'override cbpaidProductView::draw for product item_type "' . htmlspecialchars( $this->_model->get( 'item_type' ) ) . '"'; }
/** * create a new subscription object and corresponding object in database * * @param int|null $user_id CB user id * @param cbpaidProduct $plan plan object of this subscription * @param string $status like status class variable * @param boolean $store true (default) if should be stored into db * @param int $subscriptionTime time of subscription */ protected function createMerchandiseRecord( $user_id, &$plan, $status = 'R', $store = true, $subscriptionTime = null ) { global $_CB_framework; if ( $subscriptionTime === null ) { $subscriptionTime = $_CB_framework->now(); } $this->reset(); $this->user_id = $user_id; $this->plan_id = $plan->get( 'id' ); $this->payment_date = date( 'Y-m-d H:i:s', $subscriptionTime ); $this->getCurrencyAmount( $plan ); $this->status = $status; if ( is_object( $plan->_integrations ) ) { $this->integrations = $plan->_integrations->asJson(); } else { $this->integrations = ''; } if ( $store ) { $this->ip_addresses = cbpaidRequest::getIPlist(); $this->historySetMessage( $this->recordName() . ' record created' ); $this->store(); } $this->_plan = $plan; }
/** * create a new subscription object and corresponding object in database * * @param int|null $user_id CB user id * @param cbpaidProduct $plan plan object of this subscription * @param string $status like status class variable * @param boolean $store true (default) if should be stored into db * @param int $subscriptionTime time of subscription */ protected function createMerchandiseRecord($user_id, $plan, $status = 'R', $store = true, $subscriptionTime = null) { if ($subscriptionTime === null) { $subscriptionTime = cbpaidTimes::getInstance()->startTime(); } $this->reset(); $this->user_id = $user_id; $this->plan_id = $plan->get('id'); $this->payment_date = $this->_db->getUtcDateTime($subscriptionTime); $this->getCurrencyAmount($plan); $this->status = $status; if (is_object($plan->_integrations)) { $this->integrations = $plan->_integrations->asJson(); } else { $this->integrations = ''; } if ($store) { $this->ip_addresses = cbpaidRequest::getIPlist(); $this->historySetMessage($this->recordName() . ' record created'); $this->store(); } $this->_plan = $plan; }
/** * create a new (or find an existing) subscription object and corresponding object in database and links to subscription to be replaced. * * @param UserTable $user CB base user object * @param cbpaidProduct $plan payment plan object of this subscription * @param array $replacesSubscriptionId array( planId, subscriptionId ) or NULL of subscription to replace * @param array $existingSubscriptionId array( planId, subscriptionId ) or NULL of existing subscription * @param string $status [Optional default='R'] 'N' = new, 'R' = renewal * @param boolean $store [Optional default=true] if object to be stored into database * @param int $subscriptionTime [Optional default=time function] * @param string $reason payment reason: 'N'=new subscription (default), 'R'=renewal, 'U'=update * @param array $parentSubId parent subscription's id, if this subscription depends of a parent subscription * @return float remaing value (in plan's currency) of existing plan */ public function createOrLoadReplacementSubscription( &$user, &$plan, $replacesSubscriptionId = null, $existingSubscriptionId = null, $status = 'R', $store = true, $subscriptionTime = null, $reason = 'N', $parentSubId = null ) { global $_CB_framework, $_CB_database; if ( $parentSubId ) { $this->parent_plan = $parentSubId[0]; $this->parent_subscription = $parentSubId[1]; } if ( $subscriptionTime === null ) { $subscriptionTime = $_CB_framework->now(); } if ( $existingSubscriptionId === null ) { // first tries to find if there is already an existing one, otherwise creates a new one: if ( $replacesSubscriptionId ) { if ( ! $this->loadThisMatching( array( 'replaces_subscription' => (int) $replacesSubscriptionId[1], 'replaces_plan' => (int) $replacesSubscriptionId[0], 'plan_id' => (int) $plan->get( 'id' ) ) ) ) { $this->createSubscription( $user->id, $plan, $replacesSubscriptionId, $parentSubId, $status, $store, $subscriptionTime ); } else { /* only single pair of replace(subscription+plan) allowed ! 'user_id' => (int) $user->id, 'plan_id' => (int) $plan->get( 'id' ) : */ if ( $this->user_id != $user->id ) { trigger_error( sprintf( 'Subscription::Loaded subscription id %d for different user (sub-user: %d, user: %d).', $this->id, $this->user_id, $user->id ), E_USER_WARNING ); } } } else { if ( $plan->get( 'multiple' ) || ! $this->loadThisMatchingInt( 'user_id', (int) $user->id, 'plan_id', (int) $plan->get( 'id' ) ) ) { $this->createSubscription( $user->id, $plan, $replacesSubscriptionId, $parentSubId, $status, $store, $subscriptionTime ); } } } else { if ( $plan->get( 'id' ) == $existingSubscriptionId[0] ) { if ( ! $this->load( (int) $existingSubscriptionId[1] ) ) { trigger_error( sprintf( 'Subscription::createOrLoadReplacementSubscription: subscription id %d load error: %s', $existingSubscriptionId[1], htmlspecialchars($_CB_database->getErrorMsg()) ), E_USER_NOTICE ); $result = $this->loadThisMatching( array( 'plan_id' => (int) $plan->id, 'user_id' => (int) $user->id, 'status' => 'I' ), array( 'subscription_date' => 'DESC' ) ); if ( ! $result ) { trigger_error( sprintf( 'Subscription::createOrLoadReplacementSubscription: subscription with plan id %d user_id %d not found either', $plan->id, $user->id ), E_USER_NOTICE ); $this->createSubscription( $user->id, $plan, $replacesSubscriptionId, $parentSubId, $status, $store, $subscriptionTime ); } } else { if ( $this->replaces_subscription ) { $replacesSubscriptionId = array( $this->replaces_plan, $this->replaces_subscription ); } } } else { trigger_error( sprintf( 'Subscription::createOrLoadReplacementSubscription: existingSubscription id %d / Plan id %d function param does not match plan param id %d error:', $existingSubscriptionId[0], $existingSubscriptionId[1], $plan->get( 'id' ) ), E_USER_ERROR ); exit; } } $remainingValue = 0; if ( $replacesSubscriptionId != null ) { if ( $this->getPlan() ) { $remainingValue = $this->getRemainingValueOfReplacedSubscription( $this->getPlan()->currency(), $replacesSubscriptionId[0], $replacesSubscriptionId[1], $subscriptionTime ); } } return $remainingValue; }
/** * Creates a something of a plan * * @param cbpaidProduct $plan * @param UserTable $user * @param string $state * @param int $subscriptionTime */ protected function createSomething($plan, $user, $state, $subscriptionTime) { global $_CB_database; $replacesSubId = null; $reason = 'N'; if ($state == 'X' && $plan->get('item_type') != 'usersubscription') { // safeguard for donations and merchandises: they do not expire!, so set them as cencelled if import is as expired: $state = 'C'; } /* if ( $plan->get( 'exclusive' ) && ( $plan->get( 'item_type' ) == 'usersubscription' ) && ( $state == 'A' ) ) { $incompatible = false; $paidUserExtension = cbpaidUserExtension::getInstance( $user->id ); $subscriptions = $paidUserExtension->getUserSubscriptions( null, false ); foreach ( $subscriptions as $s ) { if ( ( $s->parent_plan == $plan->get( 'parent' ) ) && $s->checkIfValid() ) { $sPlan = $s->getPlan(); if ( $sPlan->get( 'exclusive' ) && ( $sPlan->get( 'item_type' ) == 'usersubscription' ) ) { // This other exclusive user subscription with same parent plan is active: then it is an upgrade from that subscription $replacesSubId = array( $s->plan_id, $s->id ); $reason = 'U'; break; } } } if ( $incompatible ) { continue; } } */ $parentSubId = null; if ($plan->get('parent')) { $plansMgr = cbpaidPlansMgr::getInstance(); $parentPlan = $plansMgr->loadPlan($plan->get('parent')); $parentSub = $parentPlan->loadLatestSomethingOfUser($user->id, null); if ($parentSub) { $parentSubId = array($parentSub->plan_id, $parentSub->id); } } $postdata = array(); $price = null; $recurringPrice = null; $subscription = $plan->createProductThing($user, $postdata, $reason, $state == 'A' ? 'I' : $state, $replacesSubId, null, $subscriptionTime, $price, $recurringPrice, $parentSubId); if ($state == 'A') { $subscription->activate($user, $subscriptionTime); } elseif ($state == 'X') { /** @var cbpaidUsersubscriptionRecord $subscription */ if (is_callable(array($subscription, 'computeExpiryTimeIfActivatedNow'))) { // Sets expiry_date for expired user subscriptions: $expiry = $subscription->computeExpiryTimeIfActivatedNow($subscriptionTime); $subscription->expiry_date = $expiry === null ? $_CB_database->getNullDate() : $_CB_database->getUtcDateTime($expiry); if (!$subscription->store()) { trigger_error('subscription store error:' . htmlspecialchars($_CB_database->getErrorMsg()), E_USER_ERROR); } } } }
/** * create a new (or find an existing) subscription object and corresponding object in database and links to subscription to be replaced. * * @param UserTable $user CB user object * @param cbpaidProduct $plan payment plan object of this subscription * @param array $replacesSubscriptionId array( planId, subscriptionId ) or NULL * @param array $existingSubscriptionId array( planId, subscriptionId ) or NULL * @param string $status [Optional default='R'] 'N' = new, 'R' = renewal * @param boolean $store [Optional default=true] if object to be stored into database * @param int $subscriptionTime [Optional default=time function] * @param string $reason payment reason: 'N'=new subscription (default), 'R'=renewal, 'U'=update * @param array $parentSubId parent plan and subscription's id, if this subscription depends of a parent subscription * @return float remaing value (in plan's currency) of existing plan */ public function createOrLoadReplacementSubscription( &$user, &$plan, /** @noinspection PhpUnusedParameterInspection */ $replacesSubscriptionId = null, /** @noinspection PhpUnusedParameterInspection */ $existingSubscriptionId = null, /** @noinspection PhpUnusedParameterInspection */ $status = 'R', /** @noinspection PhpUnusedParameterInspection */ $store = true, /** @noinspection PhpUnusedParameterInspection */ $subscriptionTime = null, /** @noinspection PhpUnusedParameterInspection */ $reason='N', $parentSubId = null ) { $this->reset(); $this->user_id = $user->id; $this->plan_id = $plan->get( 'id' ); if ( $parentSubId ) { $this->parent_plan = $parentSubId[0]; $this->parent_subscription = $parentSubId[1]; } if ( is_object( $plan->_integrations ) ) { $this->integrations = $plan->_integrations->asJson(); } else { $this->integrations = ''; } return 0.0; // override ! }