PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/s5-members/application/default/controllers/SignupController.php

https://bitbucket.org/awylie199/s5t
PHP | 364 lines | 311 code | 30 blank | 23 comment | 58 complexity | 229cf575a873d0fc6a5c1f698f23918e MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, LGPL-3.0, MIT, BSD-3-Clause
  1. <?php
  2. class SignupController extends Am_Mvc_Controller
  3. {
  4. /** @var Am_Form_Signup */
  5. protected $form;
  6. /** @var array */
  7. protected $vars;
  8. function init()
  9. {
  10. if (!class_exists('Am_Form_Brick', false)) {
  11. class_exists('Am_Form_Brick', true);
  12. Am_Di::getInstance()->hook->call(Am_Event::LOAD_BRICKS);
  13. }
  14. parent::init();
  15. }
  16. function loadForm()
  17. {
  18. if ($c = $this->getFiltered('c'))
  19. {
  20. if ($c == 'cart'){
  21. if ($this->_request->getParam('amember_redirect_url'))
  22. $this->getSession()->redirectUrl = $this->_request->getParam('amember_redirect_url');
  23. if($this->getDi()->auth->getUser() != null)
  24. {
  25. $url = $this->getSession()->redirectUrl;
  26. $this->getSession()->redirectUrl = '';
  27. $this->_redirect(urldecode($url));
  28. }
  29. else
  30. $this->record = $this->getDi()->savedFormTable->getByType(SavedForm::T_CART);
  31. }
  32. else
  33. $this->record = $this->getDi()->savedFormTable->findFirstBy(array(
  34. 'code' => $c,
  35. 'type' => SavedForm::T_SIGNUP,
  36. ));
  37. } else {
  38. $this->record = $this->getDi()->savedFormTable->getDefault
  39. (
  40. $this->getDi()->auth->getUserId() ?
  41. SavedForm::D_MEMBER : SavedForm::D_SIGNUP
  42. );
  43. }
  44. // call a hook to allow load another form
  45. $event = new Am_Event(Am_Event::LOAD_SIGNUP_FORM, array(
  46. 'request' => $this->_request,
  47. 'user' => $this->getDi()->auth->getUser(),
  48. ));
  49. $event->setReturn($this->record);
  50. $this->getDi()->hook->call($event);
  51. $this->record = $event->getReturn();
  52. if (!$this->record) {
  53. $this->getDi()->errorLogTable->log("Wrong signup form code - the form does not exists. Redirect Customer to default form. Referrer: " . $this->getRequest()->getHeader('REFERER'));
  54. $this->redirect('/signup', array('code'=>302));
  55. }
  56. /* @var $this->record SavedForm */
  57. if (!$this->record->isSignup())
  58. throw new Am_Exception_InputError("Wrong signup form loaded [$this->record->saved_form_id] - it is not a signup form!");
  59. if ($this->record->meta_title)
  60. $this->view->meta_title = $this->record->meta_title;
  61. if ($this->record->meta_keywords)
  62. $this->view->headMeta()->setName('keywords', $this->record->meta_keywords);
  63. if ($this->record->meta_description)
  64. $this->view->headMeta()->setName('description', $this->record->meta_description);
  65. if ($this->record->meta_robots)
  66. $this->view->headMeta()->setName('robots', $this->record->meta_robots);
  67. $this->view->code = $this->record->code;
  68. }
  69. function indexAction()
  70. {
  71. /*
  72. * First check user's login. user can be logged in plugin or user's login info can be in cookies.
  73. * Result does not matter here so skip it;
  74. *
  75. */
  76. if(!$this->getDi()->auth->getUserId() && $this->_request->isGet())
  77. $this->getDi()->auth->checkExternalLogin($this->_request);
  78. /*==TRIAL_SPLASH==*/
  79. if (!$this->getDi()->auth->getUserId() && $this->getDi()->config->get('signup_disable')) {
  80. $e = new Am_Exception_InputError(___('New Signups are Disabled'));
  81. $e->setLogError(false);
  82. throw $e;
  83. }
  84. $this->loadForm();
  85. $this->view->title = $this->record->title;
  86. $this->form = new Am_Form_Signup();
  87. $this->form->setParentController($this);
  88. $this->form->initFromSavedForm($this->record);
  89. try {
  90. $this->form->run();
  91. } catch (Am_Exception_QuietError $e){
  92. $e->setPublicTitle($this->record->title);
  93. throw $e;
  94. }
  95. }
  96. function display(Am_Form $form, $pageTitle)
  97. {
  98. $this->view->form = $form;
  99. $this->view->title = $this->record->title;
  100. if ($pageTitle) $this->view->title = $pageTitle;
  101. $this->view->display($this->record->tpl ? ('signup/' . basename($this->record->tpl)) : 'signup/signup.phtml');
  102. }
  103. function autoLoginIfNecessary()
  104. {
  105. if (($this->getConfig('auto_login_after_signup') || ($this->record->type == SavedForm::T_CART)) && $this->user->isApproved())
  106. {
  107. $this->user->refresh();
  108. $adapter = new Am_Auth_Adapter_User($this->user);
  109. $this->getDi()->auth->login($adapter, $this->getRequest()->getClientIp(), false);
  110. }
  111. }
  112. function process(array $vars, $name, HTML_QuickForm2_Controller_Page $page)
  113. {
  114. $this->getDi()->hook->call(Am_Event::SIGNUP_PAGE_BEFORE_PROCESS, array(
  115. 'vars' => $vars,
  116. 'savedForm' => $this->record
  117. ));
  118. $this->vars = $vars;
  119. // do actions here
  120. $this->user = $this->getDi()->auth->getUser();
  121. if ($this->getSession()->signup_member_id && $this->getSession()->signup_member_login)
  122. {
  123. $user = $this->getDi()->userTable->load((int)$this->getSession()->signup_member_id, false);
  124. if ($user && ((($this->getDi()->time - strtotime($user->added)) < 24*3600) && ($user->status == User::STATUS_PENDING)))
  125. {
  126. // prevent attacks as if someone has got ability to set signup_member_id to session
  127. if ($this->getSession()->signup_member_login == $user->login)
  128. {
  129. /// there is a potential problem
  130. /// because user password is not updated second time - @todo
  131. $this->user = $user;
  132. $this->autoLoginIfNecessary();
  133. } else
  134. {
  135. $this->getSession()->signup_member_id = null;
  136. $this->getSession()->signup_member_login = null;
  137. }
  138. } else {
  139. $this->getSession()->signup_member_id = null;
  140. }
  141. }
  142. $event = new Am_Event(Am_Event::SIGNUP_LOAD_USER, array(
  143. 'vars' => $vars,
  144. 'savedForm' => $this->record
  145. ));
  146. $event->setReturn($this->user);
  147. $this->getDi()->hook->call($event);
  148. $this->user = $event->getReturn();
  149. if (!$this->user)
  150. {
  151. $this->user = $this->getDi()->userRecord;
  152. $this->user->setForInsert($this->vars); // vars are filtered by the form !
  153. if (empty($this->user->login))
  154. $this->user->generateLogin();
  155. if (empty($this->vars['pass']))
  156. $this->user->generatePassword();
  157. else {
  158. $this->user->setPass($this->vars['pass']);
  159. }
  160. if (empty($this->user->lang))
  161. $this->user->lang = $this->getDi()->locale->getLanguage();
  162. $this->user->saved_form_id = $this->record->pk();
  163. $this->user->insert();
  164. $this->getSession()->signup_member_id = $this->user->pk();
  165. $this->getSession()->signup_member_login = $this->user->login;
  166. $this->autoLoginIfNecessary();
  167. // user inserted
  168. $this->getDi()->hook->call(Am_Event::SIGNUP_USER_ADDED, array(
  169. 'vars' => $this->vars,
  170. 'user' => $this->user,
  171. 'form' => $this->form,
  172. 'savedForm' => $this->record
  173. ));
  174. if ($this->getDi()->config->get('registration_mail'))
  175. $this->user->sendRegistrationEmail();
  176. if ($this->getDi()->config->get('registration_mail_admin'))
  177. $this->user->sendRegistrationToAdminEmail();
  178. if(!$this->user->isApproved())
  179. $this->user->sendNotApprovedEmail();
  180. } else {
  181. if ($this->record->isCart())
  182. {
  183. $url = $this->getSession()->redirectUrl;
  184. $this->getSession()->redirectUrl = '';
  185. $this->_redirect($url ? urldecode($url) : REL_ROOT_URL . '/cart', array('prependBase' => false));
  186. }
  187. unset($this->vars['pass']);
  188. unset($this->vars['login']);
  189. unset($this->vars['email']);
  190. $this->user->setForUpdate($this->vars)->update();
  191. // user updated
  192. $this->getDi()->hook->call(Am_Event::SIGNUP_USER_UPDATED, array(
  193. 'vars' => $this->vars,
  194. 'user' => $this->user,
  195. 'form' => $this->form,
  196. 'savedForm' => $this->record
  197. ));
  198. }
  199. // keep reference to e-mail confirmation link so it still working after signup
  200. if (!empty($this->vars['code']))
  201. {
  202. $this->getDi()->store->setBlob(Am_Form_Signup_Action_SendEmailCode::STORE_PREFIX . $this->vars['code'],
  203. $this->user->pk(), '+7 days');
  204. }
  205. if ($this->record->isCart())
  206. {
  207. $url = $this->getSession()->redirectUrl;
  208. $this->getSession()->redirectUrl = '';
  209. $this->_redirect($url ? urldecode($url) : REL_ROOT_URL . '/cart', array('prependBase' => false));
  210. return true;
  211. }
  212. /// now the ordering process
  213. $invoice = $this->getDi()->invoiceRecord;
  214. $invoice->saved_form_id = $this->record->pk();
  215. $this->getDi()->hook->call(Am_Event::INVOICE_SIGNUP, array(
  216. 'vars' => $this->vars,
  217. 'user' => $this->user,
  218. 'form' => $this->form,
  219. 'invoice' => $invoice,
  220. 'savedForm' => $this->record
  221. ));
  222. $invoice->setUser($this->user);
  223. foreach ($this->vars as $k => $v) {
  224. if (strpos($k, 'product_id')===0)
  225. foreach ((array)$this->vars[$k] as $product_id)
  226. {
  227. @list($product_id, $plan_id, $qty) = explode('-', $product_id, 3);
  228. $product_id = (int)$product_id;
  229. if (!$product_id) continue;
  230. $p = $this->getDi()->productTable->load($product_id);
  231. if ($plan_id > 0) $p->setBillingPlan(intval($plan_id));
  232. $qty = (int)$qty;
  233. if (!$p->getBillingPlan()->variable_qty || ($qty <= 0))
  234. $qty = 1;
  235. $options = array();
  236. if (!empty($this->vars['productOption']["$product_id-$plan_id"]))
  237. {
  238. $options = $this->vars['productOption']["$product_id-$plan_id"][0];
  239. }
  240. $prOpt = $p->getOptions(true);
  241. foreach ($options as $k => $v)
  242. {
  243. $options[$k] = array('value' => $v, 'optionLabel' => $prOpt[$k]->title,
  244. 'valueLabel' => $prOpt[$k]->getOptionLabel($v));
  245. }
  246. $invoice->add($p, $qty, $options);
  247. }
  248. }
  249. $event = new Am_Event(Am_Event::SIGNUP_INVOICE_ITEMS, array(
  250. 'vars' => $this->vars,
  251. 'form' => $this->form,
  252. 'invoice' => $invoice,
  253. 'savedForm' => $this->record
  254. ));
  255. $this->getDi()->hook->call($event);
  256. $invoice = $event->getReturn()?:$invoice;
  257. if (!$invoice->getItems()) {
  258. $this->form->getSessionContainer()->destroy();
  259. $this->_redirect('member');
  260. return true;
  261. }
  262. if (!empty($this->vars['coupon']))
  263. {
  264. $invoice->setCouponCode($this->vars['coupon']);
  265. $invoice->validateCoupon();
  266. }
  267. $invoice->calculate();
  268. $invoice->setPaysystem(isset($this->vars['paysys_id']) ? $this->vars['paysys_id'] : 'free');
  269. $err = $invoice->validate();
  270. if ($err)
  271. throw new Am_Exception_InputError($err[0]);
  272. if (!empty($this->vars['coupon']) &&
  273. !(float)$invoice->first_discount &&
  274. !(float)$invoice->second_discount) {
  275. $coupon = $this->getDi()->couponTable->findFirstByCode($this->vars['coupon']);
  276. $batch = $coupon->getBatch();
  277. if ($batch->discount > 0) {
  278. $page = $this->form->findPageByElementName('coupon');
  279. if (!$page) throw new Am_Exception_InternalError('Coupon brick is not found but coupon code presents in request');
  280. list($el) = $page->getForm()->getElementsByName('coupon');
  281. $el->setError(___('The coupon entered is not valid with any product(s) being purchased. No discount will be applied'));
  282. //now active datasource is datasource of current page
  283. //retrieve datasource for page with coupon element from
  284. //session and set it to form to populate it correctly
  285. $values = $page->getController()->getSessionContainer()->getValues($page->getForm()->getId());
  286. $page->getForm()->setDataSources(array(
  287. new HTML_QuickForm2_DataSource_Array($values)
  288. ));
  289. $page->handle('display');
  290. return false;
  291. }
  292. }
  293. $invoice->insert();
  294. $this->getDi()->hook->call(Am_Event::INVOICE_BEFORE_PAYMENT_SIGNUP, array(
  295. 'vars' => $this->vars,
  296. 'form' => $this->form,
  297. 'invoice' => $invoice,
  298. 'savedForm' => $this->record
  299. ));
  300. try {
  301. $payProcess = new Am_Paysystem_PayProcessMediator($this, $invoice);
  302. $result = $payProcess->process();
  303. } catch (Am_Exception_Redirect $e) {
  304. $this->form->getSessionContainer()->destroy();
  305. $invoice->refresh();
  306. if ($invoice->isCompleted())
  307. { // relogin customer if free subscription was ok
  308. $this->autoLoginIfNecessary();
  309. }
  310. throw $e;
  311. }
  312. // if we got back here, there was an error in payment!
  313. /** @todo offer payment method if previous failed */
  314. $page = $this->form->findPageByElementName('paysys_id');
  315. if (!$page) $page = $this->form->getFirstPage(); // just display first page
  316. foreach ($page->getForm()->getElementsByName('paysys_id') as $el)
  317. $el->setValue(null)->setError(current($result->getErrorMessages()));
  318. $page->handle('display');
  319. return false;
  320. }
  321. function getCurrentUrl()
  322. {
  323. $c = $this->getFiltered('c');
  324. return $this->_request->getScheme() . '://' .
  325. $this->_request->getHttpHost() .
  326. $this->_request->getBaseUrl() . '/' .
  327. $this->_request->getControllerName() .
  328. ($c ? "/$c" : '');
  329. }
  330. public function getForm()
  331. {
  332. return $this->form;
  333. }
  334. }