/** * 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(); } }
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; }