public function action($request) { $db = JFactory::getDBO(); $userflags = $request->metaUser->focusSubscription->getMIflags($request->plan->id, $this->id); $total_coupons = array(); $existing_coupons = array(); if (is_array($userflags)) { if (!empty($userflags['coupons'])) { $existing_coupons = explode(',', $userflags['coupons']); $total_coupons = array_merge($total_coupons, $existing_coupons); if (!empty($this->settings['inc_old_coupons'])) { foreach ($existing_coupons as $cid) { $ocph = new couponHandler(); $ocph->load($cid); $ocph->coupon->active = 1; $ocph->restrictions['max_reuse'] += $this->settings['inc_old_coupons']; $ocph->coupon->setParams($ocph->restrictions, 'restrictions'); $ocph->coupon->check(); $ocph->coupon->store(); } } } } $newcodes = array(); if (!empty($existing_coupons) && !empty($this->settings['always_new_coupons']) || empty($existing_coupons)) { if (!empty($this->settings['create_new_coupons']) && !empty($this->settings['master_coupon'])) { $cph = new CouponHandler(); $cph->load($this->settings['master_coupon']); if (is_object($cph->coupon)) { for ($i = 0; $i < $this->settings['create_new_coupons']; $i++) { $newcode = $cph->coupon->generateCouponCode(); $newcodes[] = $newcode; $cph->coupon->id = 0; $cph->coupon->coupon_code = $newcode; $cph->coupon->active = 1; $cph->restrictions['usecount'] = 0; if (!empty($this->settings['max_reuse'])) { $cph->restrictions['max_reuse'] = $this->settings['max_reuse']; } else { $cph->restrictions['max_reuse'] = 1; } if (!empty($this->settings['bind_subscription'])) { $cph->restrictions['depend_on_subscr_id'] = 1; $cph->restrictions['subscr_id_dependency'] = $request->metaUser->focusSubscription->id; } $cph->coupon->storeload(); if (!empty($this->settings['switch_type'])) { $cph->switchType(); } } if (!empty($this->settings['mail_out_coupons'])) { $this->mailOut($request, $newcodes); } } } } $total_coupons = array_merge($total_coupons, $newcodes); $newflags['coupons'] = implode(',', $total_coupons); $request->metaUser->focusSubscription->setMIflags($request->plan->id, $this->id, $newflags); return true; }
public function removeCoupon($coupon_code, $fromId = false) { if (empty($this->coupons)) { return null; } if ($fromId) { $coupon_code = couponHandler::codeFromId($coupon_code); $coupon_code = $coupon_code['coupon_code']; } if (!in_array($coupon_code, $this->coupons)) { return null; } foreach ($this->coupons as $id => $cc) { if ($cc == $coupon_code) { unset($this->coupons[$id]); } } $cph = new couponHandler(); $cph->load($coupon_code); if (!empty($cph->coupon->id)) { $cph->decrementCount($this); } if (empty($this->usage)) { return; } $usage = explode('.', $this->usage); // Update old notation if (!isset($usage[1])) { $temp = $usage[0]; $usage[0] = 'p'; $usage[1] = $temp; } switch (strtolower($usage[0])) { case 'c': case 'cart': $cart = new aecCart(); $cart->load($usage[1]); $cart->removeCoupon($coupon_code); $cart->storeload(); break; } }
public function save($coupon_code) { $new = 0; $type = $_POST['type']; $_POST['coupon_code'] = aecGetParam('coupon_code', 0, true, array('word', 'string', 'clear_nonalnum')); if ($_POST['coupon_code'] == '') { $this->setMessage(JText::_('AEC_MSG_NO_COUPON_CODE')); return; } $cph = new couponHandler(); if (!empty($_POST['id'])) { $cph->coupon = new Coupon($_POST['oldtype']); $cph->coupon->load($_POST['id']); if ($cph->coupon->id) { $cph->status = true; } } else { $cph->load($_POST['coupon_code']); } if (!$cph->status) { $cph->coupon = new Coupon($type); $cph->coupon->createNew($_POST['coupon_code']); $cph->status = true; $new = 1; } if ($cph->status) { if (!$new) { if ($cph->coupon->type != $_POST['type']) { $cph->switchType(); } } unset($_POST['type']); unset($_POST['oldtype']); unset($_POST['id']); $post = AECToolbox::cleanPOST($_POST, false); $cph->coupon->savePOSTsettings($post); $cph->coupon->storeload(); } else { $short = 'coupon store failure'; $event = 'When trying to store coupon'; $tags = 'coupon,loading,error'; $params = array(); $eventlog = new eventLog(); $eventlog->issue($short, $tags, $event, 128, $params); } $this->setMessage(JText::_('AEC_MSG_SUCESSFULLY_SAVED')); }
public function loadCoupon($coupon_code, $strict = true) { if (in_array($coupon_code, $this->delete_list) && $strict) { return false; } $cph = new couponHandler(); $cph->load($coupon_code); if (empty($cph->coupon->id)) { $this->setError(JText::_('COUPON_ERROR_INVALID')); $this->delete_list[] = $coupon_code; return false; } if ($cph->coupon->coupon_code !== $coupon_code) { $this->setError(JText::_('COUPON_ERROR_INVALID')); $this->delete_list[] = $coupon_code; return false; } if (!$cph->status) { $this->setError($cph->error); $this->delete_list[] = $coupon_code; return false; } if ($cph->coupon->restrictions['cart_multiple_items'] && !empty($cph->coupon->restrictions['cart_multiple_items_amount'])) { if (!array_key_exists($coupon_code, $this->max_amount_list)) { $this->coupons_list[] = array('coupon_code' => $coupon_code); $this->max_amount_list[$coupon_code] = $this->coupon->restrictions['cart_multiple_items_amount']; } if ($this->max_amount_list[$coupon_code]) { $this->max_amount_list[$coupon_code]--; } else { return false; } } else { $this->coupons_list[] = array('coupon_code' => $coupon_code); } $this->cph = $cph; return true; }