PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/awylie199/s5t
PHP | 209 lines | 188 code | 20 blank | 1 comment | 43 complexity | 0742df2ba8af685ca4a73e4cfe7c28cc 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 AjaxController extends Am_Mvc_Controller
  3. {
  4. public function preDispatch()
  5. {
  6. if (!$this->getRequest()->isXmlHttpRequest()) {
  7. throw new Am_Exception_InputError;
  8. }
  9. }
  10. function ajaxError($msg)
  11. {
  12. $this->_response->ajaxResponse(array('msg' => $msg));
  13. }
  14. function ajaxGetStates($vars)
  15. {
  16. return $this->_response->ajaxResponse($this->getDi()->stateTable->getOptions($vars['country']));
  17. }
  18. function ajaxCheckUniqLogin($vars)
  19. {
  20. $user_id = $this->getDi()->auth->getUserId();
  21. if (!$user_id) {
  22. $user_id = $this->getDi()->session->signup_member_id;
  23. }
  24. $login = $vars['login'];
  25. $msg = null;
  26. if (!$this->getDi()->userTable->checkUniqLogin($login, $user_id)) {
  27. $msg = ___('Username %s is already taken. Please choose another username', Am_Html::escape($login));
  28. }
  29. if (!$msg) {
  30. $msg = $this->getDi()->banTable->checkBan(array('login'=>$login));
  31. }
  32. return $this->_response->ajaxResponse($msg ? $msg : true);
  33. }
  34. function ajaxCheckUniqEmail($vars)
  35. {
  36. $user_id = $this->getDi()->auth->getUserId();
  37. if (!$user_id) {
  38. $user_id = $this->getDi()->session->signup_member_id;
  39. }
  40. $email = $vars['email'];
  41. $msg = null;
  42. if(isset($vars['_url'])) {
  43. $url = $vars['_url'];
  44. } else {
  45. $url = REL_ROOT_URL . '/member';
  46. }
  47. if (!$this->getDi()->userTable->checkUniqEmail($email, $user_id))
  48. $msg = ___('An account with the same email already exists.').'<br />'.
  49. ___('Please %slogin%s to your existing account.%sIf you have not completed payment, you will be able to complete it after login','<a href="' . Am_Html::escape($url) . '" class="ajax-link" title="' . Am_Html::escape($this->getDi()->config->get('site_title')) . '">','</a>','<br />');
  50. if (!$msg) {
  51. $msg = $this->getDi()->banTable->checkBan(array('email'=>$email));
  52. }
  53. if (!$msg && !Am_Validate::email($email)) {
  54. $msg = ___('Please enter valid Email');
  55. }
  56. return $this->_response->ajaxResponse($msg ? $msg : true);
  57. }
  58. function ajaxCheckCoupon($vars)
  59. {
  60. if (!$vars['coupon']) return $this->_response->ajaxResponse(true);
  61. $user_id = $this->getDi()->auth->getUserId();
  62. if (!$user_id)
  63. $user_id = $this->getDi()->session->signup_member_id;
  64. $coupon = $this->getDi()->couponTable->findFirstByCode($vars['coupon']);
  65. $msg = $coupon ? $coupon->validate($user_id) : ___('No coupons found with such coupon code');
  66. return $this->_response->ajaxResponse(is_null($msg) ? true : $msg);
  67. }
  68. function indexAction()
  69. {
  70. $vars = $this->_request->toArray();
  71. switch ($this->_request->getFiltered('do')){
  72. case 'get_states':
  73. $this->ajaxGetStates($vars);
  74. break;
  75. case 'check_uniq_login':
  76. $this->ajaxCheckUniqLogin($vars);
  77. break;
  78. case 'check_uniq_email':
  79. $this->ajaxCheckUniqEmail($vars);
  80. break;
  81. case 'check_coupon':
  82. $this->ajaxCheckCoupon($vars);
  83. break;
  84. default:
  85. $this->ajaxError('Unknown Request: ' . $vars['do']);
  86. }
  87. }
  88. function invoiceSummaryAction()
  89. {
  90. $invoice = $this->getDi()->invoiceRecord;
  91. $vars = $this->getRequest()->getParams();
  92. if(!$user = $this->getDi()->auth->getUser()) {
  93. $user = $this->getDi()->userRecord;
  94. $user->user_id = -1;
  95. }
  96. $user->toggleFrozen(true);
  97. if($vars['country']) {
  98. $user->country = $vars['country'];
  99. }
  100. if (isset($vars['tax_id'])) {
  101. $user->tax_id = $vars['tax_id'];
  102. }
  103. $invoice->setUser($user);
  104. $param = array();
  105. $page_current = $this->getRequest()->getParam('_save_');
  106. $vars_added = false;
  107. foreach ($_SESSION['amember']['_signup_container']['values'] as $page => $v) {
  108. if ($page == $page_current) {
  109. $v = array_merge($v, $vars);
  110. $vars_added = true;
  111. }
  112. $param = array_merge($param, $v);
  113. }
  114. $vars = $vars_added ? $param : array_merge($param, $vars);
  115. foreach ($vars as $k => $v) {
  116. if (strpos($k, 'product_id')===0) {
  117. foreach ((array)$vars[$k] as $key => $product_id) {
  118. if (substr($key, 0, 4) == '_qty') continue;
  119. @list($product_id, $plan_id, $qty) = explode('-', $product_id, 3);
  120. $qty_key = sprintf('_qty-%d-%d', $product_id, $plan_id);
  121. if (isset($vars[$k][$qty_key]))
  122. $qty = $vars[$k][$qty_key];
  123. $product_id = (int)$product_id;
  124. if (!$product_id) continue;
  125. $p = $this->getDi()->productTable->load($product_id);
  126. if ($plan_id > 0) $p->setBillingPlan(intval($plan_id));
  127. $qty = (int)$qty;
  128. if (!$p->getBillingPlan()->variable_qty || ($qty <= 0))
  129. $qty = 1;
  130. $plan_id = $p->getBillingPlan()->pk();
  131. $options = array();
  132. if (!empty($vars['productOption']["$product_id-$plan_id"])) {
  133. $options = $vars['productOption']["$product_id-$plan_id"][0];
  134. }
  135. $prOpt = $p->getOptions(true);
  136. foreach ($options as $opk => $opv) {
  137. $options[$opk] = array('value' => $opv, 'optionLabel' => $prOpt[$opk]->title,
  138. 'valueLabel' => $prOpt[$opk]->getOptionLabel($opv));
  139. }
  140. $invoice->add($p, $qty, $options);
  141. }
  142. }
  143. }
  144. if (!empty($vars['coupon'])) {
  145. $invoice->setCouponCode($vars['coupon']);
  146. $invoice->validateCoupon();
  147. }
  148. $this->_handleDonation($invoice, $vars);
  149. $invoice->calculate();
  150. $v = $this->getDi()->view;
  151. $v->invoice = $invoice;
  152. $v->display('_invoice-summary.phtml');
  153. }
  154. function _handleDonation(Invoice $invoice, $vars)
  155. {
  156. //we take into account only first period - it is just preview
  157. foreach ($invoice->getItems() as $item) {
  158. if ($item->item_type == 'product' && isset($vars['donation'][$item->item_id])) {
  159. if (!$vars['donation'][$item->item_id] && !$vars['donation_allow_free'][$item->item_id]) {
  160. $invoice->deleteItem($item);
  161. } else {
  162. $item->first_price = $vars['donation'][$item->item_id];
  163. $item->data()->set('orig_first_price', $item->first_price);
  164. }
  165. }
  166. }
  167. }
  168. function unsubscribedAction()
  169. {
  170. $v = $this->_request->getPost('unsubscribed');
  171. if (strlen($v) != 1)
  172. throw new Am_Exception_InputError("Wrong input");
  173. $v = ($v > 0) ? 1 : 0;
  174. if (($s = $this->getFiltered('s')) && ($e = $this->getParam('e')) &&
  175. Am_Mail::validateUnsubscribeLink($e, $s)) {
  176. $user = $this->getDi()->userTable->findFirstByEmail($e);
  177. } else {
  178. $user = $this->getDi()->user;
  179. }
  180. if (!$user)
  181. return $this->ajaxError(___('You must be logged-in to run this action'));
  182. if ($user->unsubscribed != $v) {
  183. $user->set('unsubscribed', $v)->update();
  184. $this->getDi()->hook->call(Am_Event::USER_UNSUBSCRIBED_CHANGED,
  185. array('user'=>$user, 'unsubscribed' => $v));
  186. }
  187. $this->_response->ajaxResponse(array('status' => 'OK', 'value' => $v));
  188. }
  189. }