/** * User wants to take a new subscription */ protected function displayNew() { $check = WpSubscription::canSubscribe($this->getUser()); if ($check !== true) { $this->action = self::ACTION_LIST; $this->msgKey = $check; $this->msgType = 'error'; $this->display(); return; } // load invitation using the hidden field // maybe there is a better way to do this? $request = $this->getRequest(); $invitation = null; if ($this->invitation != null) { $invitation = $this->invitation; } elseif ($request->getCheck('wpInvitation')) { $invitation = WpInvitation::newFromCode($request->getText('wpInvitation')); if ($invitation instanceof WpInvitation) { if (!$invitation->canBeUsed()) { $invitation = null; // can no longer be used } } } $formDescriptor = array(); if ($invitation instanceof WpInvitation) { $formDescriptor['UseInvitation'] = array('type' => 'text', 'section' => 'sub-new-section', 'label-message' => 'wp-inv-code-field', 'help-message' => 'wp-use-inv-help2', 'default' => $invitation->getCode(), 'disabled' => true); } $formDescriptor['Invitation'] = array('type' => 'hidden', 'label' => 'hidden', 'default' => $invitation != null ? $invitation->getCode() : ''); $formDescriptor['Plan'] = array('type' => 'select', 'section' => 'sub-new-section', 'label-message' => 'wp-planfield', 'help-message' => 'wp-planfield-help', 'validation-callback' => array($this, 'validateSubscribeNewPlanId'), 'options' => array()); $formDescriptor['Check'] = array('type' => 'check', 'section' => 'sub-new-section', 'label-message' => 'wp-checkfield', 'validation-callback' => array($this, 'validateSubscribeCheck'), 'required' => 'true'); $plans = WpPlan::factoryAvailableForFirstSubscription($invitation); foreach ($plans as $plan) { $wpp_name = $plan->getName(); $price = $plan->getPrice(); $formDescriptor['Plan']['options'][wfMessage('wp-plan-desc-short', wfMessage('wpp-' . $wpp_name)->text(), $price['amount'])->text()] = $plan->getId(); if ($this->planName == $wpp_name) { $formDescriptor['Plan']['default'] = $plan->getId(); } } $htmlForm = new HTMLFormS($formDescriptor); $htmlForm->addHeaderText(wfMessage('wp-sub-new-header')->parse()); $htmlForm->setMessagePrefix('wp'); $htmlForm->setTitle($this->getTitleFor(self::TITLE_NAME, self::ACTION_NEW)); $htmlForm->setSubmitCallback(array($this, 'processNew')); $htmlForm->setSubmitText(wfMessage('wp-plan-subscribe-go')->text()); if ($this->invitation != null) { // invitation code comes from another form, so only diplay form and do not process submission $htmlForm->setBlockSubmit(true); } else { // store invitation code if it has been loaded from the current form $this->invitation = $invitation; } if ($htmlForm->show()) { // form validated and processed OK switch ($this->just_subscribed->getTmrStatus()) { case 'PE': $this->getOutput()->redirect($this->getTitleFor('ElectronicPayment')->getLocalURL()); return; default: $this->msgType = 'success'; $this->msgKey = 'wp-new-success-ok'; break; } $this->action = self::ACTION_LIST; $this->display(); } }
/** * Returns available offers, taking account of the invitation. * @param WpInvitation $invitation Optional invitation * @return array array of WpPlans * @todo also add plan available using an invitation */ public static function factoryAvailableForFirstSubscription($invitation = null) { $offers = array(); if ($invitation instanceof WpInvitation) { $invitatonOffers = $invitation->getCategory()->getPlans(); foreach ($invitatonOffers as $offer) { $offer->cleverAppendToArray($offers); } } $nb_wikiplaces = 0; $nb_wikiplace_pages = 0; $diskspace = 0; $dbr = wfGetDB(DB_SLAVE); $now = $dbr->addQuotes(wfTimestamp(TS_DB)); $conds = $dbr->makeList(array("wpp_start_date <= {$now}", "wpp_end_date > {$now}", "wpp_invitation_only" => 0, "wpp_nb_wikiplaces >= {$nb_wikiplaces}", "wpp_nb_wikiplace_pages >= {$nb_wikiplace_pages}", "wpp_diskspace >= {$diskspace}"), LIST_AND); $result = $dbr->select('wp_plan', '*', $conds, __METHOD__); foreach ($result as $row) { $offer = self::constructFromDatabaseRow($row); $offer->cleverAppendToArray($offers); } $dbr->freeResult($result); return $offers; }
public function validateCode($code, $alldata) { if (!preg_match('/^[\\w_\\-\\?\\!\\.\\,]+$/', $code)) { return 'Error: Code should contain: alphanumeric chars _ - ? ! . , (space replaced by _)'; } $invitation = WpInvitation::newFromCode($code); if ($invitation instanceof WpInvitation) { return 'Error: This code already exists.'; } return true; }
/** * Subscribe to a plan (= no current active plan) * WARNING, you should ensure the user can subscribe before calling this: use canSubscribe() and canSubscribeTo() * @param User $use The user who buy the plan, and will use it * @param WpPlan $plan * @param WpInvitation $invitation Optional * @return WpSubscription the newly created subscription if ok, or null if an error occured (db error) */ public static function subscribe($user, $plan, $invitation = null) { if ($user === null || !$user instanceof User || $plan === null || !$plan instanceof WpPlan) { throw new MWException('Cannot subscribe, invalid argument.'); } $invitationId = 0; if ($invitation instanceof WpInvitation) { $invitationId = $invitation->getId(); } $user_id = $user->getId(); $db_master = $dbw = wfGetDB(DB_MASTER); // archive the current sub if necessary // not that even if this sub is active, it will be archived // so, be sure that you need to call this subscribe() ! $current_sub = self::newByUserId($user_id); $tmr = self::createTMR($user_id, $user->getEmail(), $plan, WP_SUBSCRIPTION_TMR_TYPE_NEW); // already paid, or waiting a payment ? switch ($tmr['tmr_status']) { case 'OK': // already paid by user $now = self::now(); $end = self::calculateEndDateFromStart($now, $plan->getPeriod()); $renewal_plan_id = $plan->getRenewalPlan($end)->getId(); if ($current_sub != null) { $current_sub->archive(); $current_sub->update($plan->getId(), $user_id, $tmr['tmr_id'], 'OK', $now, $end, true, $renewal_plan_id, false, $invitationId, $db_master); $sub = $current_sub; } else { $sub = self::create($plan->getId(), $user_id, $tmr['tmr_id'], 'OK', $now, $end, true, $renewal_plan_id, false, $invitationId, $db_master); } if ($sub == null) { return null; } self::addSubscribersGroupToUser($user); $sub->sendOnFirstActivation(); return $sub; case 'PE': // waiting payment if ($current_sub != null) { $current_sub->archive(); $current_sub->update($plan->getId(), $user_id, $tmr['tmr_id'], 'PE', null, null, false, WPP_ID_NORENEW, false, $invitationId, $db_master); $sub = $current_sub; } else { $sub = self::create($plan->getId(), $user_id, $tmr['tmr_id'], 'PE', null, null, false, WPP_ID_NORENEW, false, $invitationId, $db_master); } return $sub; } // if we arrive here, the payment status is unknown wfDebugLog('wikiplaces', 'WpSubscription::subscribe() ERROR the transaction status is not handled: "' . $tmr['tmr_status'] . '" of tmr_id[' . $tmr['tmr_id'] . ']'); return null; }