shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

reward.php (2236B)


      1 <?php
      2 class ControllerApiReward extends Controller {
      3 	public function index() {
      4 		$this->load->language('api/reward');
      5 
      6 		// Delete past reward in case there is an error
      7 		unset($this->session->data['reward']);
      8 
      9 		$json = array();
     10 
     11 		if (!isset($this->session->data['api_id'])) {
     12 			$json['error'] = $this->language->get('error_permission');
     13 		} else {
     14 			$points = $this->customer->getRewardPoints();
     15 
     16 			$points_total = 0;
     17 
     18 			foreach ($this->cart->getProducts() as $product) {
     19 				if ($product['points']) {
     20 					$points_total += $product['points'];
     21 				}
     22 			}
     23 
     24 			if (empty($this->request->post['reward'])) {
     25 				$json['error'] = $this->language->get('error_reward');
     26 			}
     27 
     28 			if ($this->request->post['reward'] > $points) {
     29 				$json['error'] = sprintf($this->language->get('error_points'), $this->request->post['reward']);
     30 			}
     31 
     32 			if ($this->request->post['reward'] > $points_total) {
     33 				$json['error'] = sprintf($this->language->get('error_maximum'), $points_total);
     34 			}
     35 
     36 			if (!$json) {
     37 				$this->session->data['reward'] = abs($this->request->post['reward']);
     38 
     39 				$json['success'] = $this->language->get('text_success');
     40 			}
     41 		}
     42 
     43 		$this->response->addHeader('Content-Type: application/json');
     44 		$this->response->setOutput(json_encode($json));
     45 	}
     46 
     47 	public function maximum() {
     48 		$this->load->language('api/reward');
     49 
     50 		$json = array();
     51 
     52 		if (!isset($this->session->data['api_id'])) {
     53 			$json['error'] = $this->language->get('error_permission');
     54 		} else {
     55 			$json['maximum'] = 0;
     56 
     57 			foreach ($this->cart->getProducts() as $product) {
     58 				if ($product['points']) {
     59 					$json['maximum'] += $product['points'];
     60 				}
     61 			}
     62 		}
     63 
     64 		$this->response->addHeader('Content-Type: application/json');
     65 		$this->response->setOutput(json_encode($json));
     66 	}
     67 
     68 	public function available() {
     69 		$this->load->language('api/reward');
     70 
     71 		$json = array();
     72 
     73 		if (!isset($this->session->data['api_id'])) {
     74 			$json['error'] = $this->language->get('error_permission');
     75 		} else {
     76 			$json['points'] = $this->customer->getRewardPoints();
     77 		}
     78 
     79 		$this->response->addHeader('Content-Type: application/json');
     80 		$this->response->setOutput(json_encode($json));
     81 	}
     82 }