Exemple #1
0
 public function action_view($id2, $id)
 {
     if (!is_numeric($id)) {
         Message::set(Message::ERROR, 'Invalid ID');
         $this->request->redirect('zone');
     }
     $item = Model_Shop::get_one_item($this->shop->id, $id);
     $this->title = $item->name;
     $this->item = $item;
     $post = Validate::factory($_POST)->filter(TRUE, 'trim')->rule('amount', 'digit')->callback('amount', array($this, 'shop_got_item'));
     if ($post->check()) {
         $item2 = Model_User::get_item($this->user->id, $id);
         // User got the item in his relation table.
         if ($item2) {
             DB::update('user_items')->set(array('amount' => new Database_Expression('amount + ' . $post['amount'])))->where('user_id', '=', $this->user->id)->and_where('item_id', '=', $id)->execute();
         } else {
             DB::insert('user_items', array('user_id', 'item_id', 'amount'))->values(array($this->user->id, $id, $post['amount']))->execute();
         }
         DB::update('shop_items')->set(array('amount' => new Database_Expression('amount - ' . $post['amount'])))->where('shop_id', '=', $this->shop_id)->and_where('item_id', '=', $id)->execute();
         $item->amount = $item->amount - $post['amount'];
         Message::set(Message::SUCCESS, 'You bought ' . $post['amount'] . ' ' . $item->name);
     } else {
         if ($post->errors()) {
             Message::set(Message::ERROR, $post->errors('shop'));
         }
     }
     $this->template->content = View::factory('shop/view')->set('shop', $this->shop)->set('item', $item);
 }
Exemple #2
0
 /**
  * add_relation 
  * @return type
  */
 public static function add_relation()
 {
     try {
         if (!self::validation_add_relation()) {
             return self::error();
         }
         # lat&lng -> geohash
         $geohash = Util_Geohash::encode(Input::post('lat'), Input::post('lng'));
         # transaction
         DB::start_transaction();
         # shop_id指定
         if (is_null(Input::post('shop_id'))) {
             # new shop
             $shop_id = Model_Shop::add(Input::post('shop_name'));
         } else {
             $data = Model_Shop::get_by_pk("shop", Input::post('shop_id'));
             if (!$data) {
                 throw new Exception('shop_id ' . Input::post('shop_id') . " is not exsits.");
             }
             $shop_id = $data['shop_id'];
         }
         # new shop geo add
         if (is_null(Input::post('shop_id'))) {
             if (!self::add($shop_id, Input::post('lat'), Input::post('lng'), $geohash)) {
                 throw new Exception("insert geo fail.");
             }
         }
         # fileupload & setting
         self::$file_name = self::file_upload($shop_id);
         self::$file_path = self::UPLOAD_DIR . Input::post('shop_id') . DS . self::$file_name;
         if (!self::$file_name) {
             throw new Exception('file upload fail.');
         }
         # image resize
         # todo
         # image add
         if (!Model_Image::add($shop_id, Input::post('user_id'), self::$file_name)) {
             throw new Exception("insert image fail.");
         }
         # commit
         DB::commit_transaction();
         # success
         $data = ['status' => CREATED];
     } catch (Exception $ex) {
         # 画像ファイルが存在すれば削除
         if (is_file(self::$file_path)) {
             unlink(self::$file_path);
         }
         # rollback
         DB::rollback_transaction();
         $data = ['status' => DATABASE_ERROR, 'message' => '[database error]insert table fail.'];
         Log::error($ex);
     }
     return $data;
 }
 public function CheckInventory($data)
 {
     $updated_data = array();
     foreach ($data as $row => $val) {
         if ($val == 0) {
             $shop = new Model_Shop();
             $shop->removefromCart($row);
         } else {
             $check = $this->getData("Store", " ProductId =" . $row);
             if ($check[0]['Quantity'] < $val) {
                 $updated_data[$row] = 1;
                 setcookie("cart_item_qty[" . $row . "]", 1, time() + 3600, "/");
             } else {
                 $updated_data[$row] = $val;
                 setcookie("cart_item_qty[" . $row . "]", $val, time() + 3600, "/");
             }
         }
     }
     return $updated_data;
 }
 public function staticsAction()
 {
     $this->_helper->layout()->setLayout('static');
     //user
     $authsession = new Zend_Session_Namespace('authsession');
     //cart
     $shop = new Model_Shop();
     $tmp_items = isset($_COOKIE['cart_items']) ? $_COOKIE['cart_items'] : array();
     $listCartItems = $shop->listCartItems($tmp_items);
     $items = array();
     $item_amount_total = 0;
     if ($listCartItems > 0) {
         foreach ($listCartItems as $item) {
             $cart_item_qty = isset($_COOKIE['cart_item_qty']) && isset($_COOKIE['cart_item_qty'][$item['ProductID']]) ? $_COOKIE['cart_item_qty'][$item['ProductID']] : 1;
             if ($cart_item_qty >= 6) {
                 $item_total = $item['DiscPrice'] * $cart_item_qty;
             } else {
                 $item_total = $item['Price'] * $cart_item_qty;
             }
             $items[] = $item_total;
         }
         $item_amount_total = number_format(array_sum($items), 2);
     }
     $item_count = isset($items) ? count($items) : '0';
     //final data
     $user = isset($authsession->logged_user) ? $authsession->logged_user : '******';
     $user = isset($authsession->logged_admin) ? $authsession->logged_admin : $user;
     $statics = array("logged_user" => $user, "cart_item_count" => $item_count, "cart_items" => $items, "cart_total" => $item_amount_total, "moolah" => 1);
     $this->view->data = json_encode($statics);
 }