/** * @param cbautoactionsActionTable $trigger * @param UserTable $user */ public function execute( $trigger, $user ) { global $_CB_framework; if ( ( ! $user->get( 'id' ) ) || ( ! $this->installed() ) ) { if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) { var_dump( CBTxt::T( 'AUTO_ACTION_CBSUBS_NOT_INSTALLED', ':: Action [action] :: CB Paid Subscriptions is not installed', array( '[action]' => (int) $trigger->get( 'id' ) ) ) ); } return; } foreach ( $trigger->getParams()->subTree( 'cbsubs' ) as $row ) { /** @var ParamsInterface $row */ $plans = $row->get( 'plans' ); if ( $plans ) { $plans = explode( '|*|', $plans ); cbArrayToInts( $plans ); $mode = $row->get( 'mode', 1, GetterInterface::INT ); $subscriptions = cbpaidSomethingMgr::getAllSomethingOfUser( $user, null ); $activePlans = array(); if ( $subscriptions ) foreach ( $subscriptions as $type ) { foreach ( array_keys( $type ) as $typeId ) { /** @var cbpaidSomething $subscription */ $subscription = $type[$typeId]; $subscriptionId = (int) $subscription->get( 'id' ); $subscriptionStatus = $subscription->get( 'status' ); $planId = (int) $subscription->get( 'plan_id' ); if ( in_array( $planId, $plans ) ) { switch ( $mode ) { case 2: if ( $subscriptionStatus != 'A' ) { $subscription->activate( $user, $_CB_framework->now(), true, 'R' ); } break; case 3: if ( $subscriptionStatus == 'A' ) { cbpaidControllerOrder::doUnsubscribeConfirm( $user, null, $planId, $subscriptionId ); } break; case 4: if ( $subscription->canDelete() ) { $subscription->revert( $user, 'Denied' ); $subscription->delete(); } break; case 1: default: if ( ( $subscriptionStatus == 'A' ) && ( ! in_array( $planId, $activePlans ) ) ) { $activePlans[] = $planId; } break; } } } } if ( $mode == 1 ) { $plansMgr = cbpaidPlansMgr::getInstance(); $postData = array(); $chosenPlans = array(); foreach ( $plans as $planId ) { if ( ! in_array( $planId, $activePlans ) ) { $chosenPlans[$planId] = $plansMgr->loadPlan( $planId ); } } if ( $chosenPlans ) { cbpaidControllerOrder::createSubscriptionsAndPayment( $user, $chosenPlans, $postData, null, null, 'A', null, 'U', 'free' ); } } } } }
/** * Gets all subscriptions of $user for $plans having $statuses * $plans and $statuses can be null or empty array() meaning that condition is ignored * * @param int $userId * @param int[]|null $plans * @param string[]|null $statuses * @return cbpaidSomething[] */ private function _getUsersSubscriptions( $userId, $plans, $statuses ) { $subsOfPlansStatus = array(); if ( $userId ) { // get list of plan_id of all active and inactive subscriptions: $user = CBuser::getUserDataInstance( $userId ); $subsByPlanType = cbpaidSomethingMgr::getAllSomethingOfUser( $user, null ); foreach ( $subsByPlanType as $subs ) { foreach ( $subs as $subscription ) { // $subscription = NEW cbpaidSomething(); if ( ( ( $plans == null ) || in_array( $subscription->plan_id, $plans ) ) && ( ( $statuses == null ) || in_array( $subscription->status, $statuses ) ) ) { $subsOfPlansStatus[] = $subscription; } } } } return $subsOfPlansStatus; }