Advertisement
Guest User

Untitled

a guest
Aug 31st, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. <?php
  2.  
  3. if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest")
  4. exit ("Error.");
  5.  
  6. session_start();
  7. define('ROOT_DIR', substr(dirname(__FILE__), 0, -12));
  8. define('LK_DIR', substr(dirname(__FILE__), 0, -4));
  9. require_once(LK_DIR.'/view/View.php');
  10.  
  11. class Shop extends View{
  12.  
  13. public function fetch(){
  14. header("Content-type: text/html; charset=".$this->config->charset."");
  15.  
  16. if($_GET['mode'] == 'product')
  17. {
  18.  
  19. $ajax = '
  20.  
  21. <script type="text/javascript">
  22. $(\'form.variants\').submit(function(){
  23. id = $(this).find(\'input[name=id]\').val();
  24.  
  25. $.ajax({
  26. type: "POST",
  27. url: "'.$this->config->path.'ajax/Shop.php?mode=minicart",
  28. data: \'id=\'+id+\'\',
  29. success: function(html){
  30. $(\'#cart_informer\').html(html);
  31. }
  32.  
  33. });
  34.  
  35. return false;
  36.  
  37. });
  38.  
  39. $(\'form.formsearch\').submit(function(){
  40. key = $(this).find(\'input[name=keyword]\').val();
  41.  
  42. $(\'#content_lk\').text(\'Загрузка...\');
  43.  
  44. $.ajax({
  45. type: "GET",
  46. url: "'.$this->config->path.'ajax/Shop.php?mode=search",
  47. data: \'keyword=\'+key+\'\',
  48. success: function(html){
  49. $(\'#content_lk\').html(html);
  50. }
  51.  
  52. });
  53.  
  54. return false;
  55.  
  56. });
  57.  
  58. </script>
  59. ';
  60.  
  61. $cart = $this->mini_cart();
  62.  
  63. $this->db->query("SELECT * FROM __shop_googs WHERE id='".intval($_GET['id'])."'");
  64. $row = $this->db->get_row();
  65.  
  66. $server_i = $this->server_info($row['server']);
  67. $title = $this->title("Сервер &raquo; ".$server_i['name']." &raquo; Магазин &raquo; Товар &raquo; ".$row['name']."");
  68.  
  69. $this->design->load('shop_goog.html');
  70. $this->design->set('ajax', $ajax);
  71. $this->design->set('cart', $cart);
  72. $this->design->set('name', $row['name']);
  73. $this->design->set('text', $row['text']);
  74. $this->design->set('img', $row['img']);
  75. $this->design->set('sum', $row['sum']);
  76. $this->design->set('sum2', $row['sum']*64);
  77. $this->design->set('id', $row['id']);
  78. $this->design->set('server', $row['server']);
  79. $this->design->set('title', $title);
  80. return $this->design->result();
  81. }
  82. elseif($_GET['mode'] == 'minicart'){
  83.  
  84. if(isset($_POST['id']))
  85. {
  86. if($_SESSION['cart'] == '')
  87. {
  88. $_SESSION['cart'] = array($_POST['id'] => '1');
  89. } else {
  90. $NewCart = array();
  91. foreach($_SESSION['cart'] as $Id => $Value)
  92. {
  93. if($Id == (int)$_POST['id']) $Value = $Value+1;
  94. $NewCart[$Id] = $Value;
  95. }
  96. if(!$_SESSION['cart'][$_POST['id']]) $NewCart[$_POST['id']] = 1;
  97. $_SESSION['cart'] = $NewCart;
  98. }
  99. }
  100.  
  101. return $this->mini_cart();
  102.  
  103.  
  104. }
  105. elseif($_GET['mode'] == 'cart'){
  106.  
  107. if($_SESSION['cart'] != '')
  108. {
  109. foreach($_SESSION['cart'] as $Id => $Value)
  110. {
  111. $this->db->query("SELECT sum FROM __shop_googs WHERE id='".intval($Id)."'");
  112. $row99 = $this->db->get_row();
  113.  
  114. if(!$row99) unset($_SESSION['cart'][$Id]);
  115.  
  116. $CountGoogs += $Value;
  117. $SumGoogs += $Value*$row99['sum'];
  118. }
  119.  
  120. }
  121.  
  122. if($_GET['op'] == 'amounts'){
  123.  
  124. if(isset($_POST['amounts'])){
  125. $Amounts = $_POST['amounts'];
  126. foreach($Amounts as $VId => $NewA)
  127. {
  128. $_SESSION['cart'][intval($VId)] = intval($NewA);
  129. }
  130. }
  131. return header("Location: ".$this->config->path.'ajax/Shop.php?mode=cart');
  132.  
  133. }
  134. elseif($_GET['op'] == 'delete')
  135. {
  136. if($_GET['id'] != ''){
  137. unset($_SESSION['cart'][$_GET['id']]);
  138. if(count($_SESSION['cart']) == 0) unset($_SESSION['cart']);
  139. }
  140. return header("Location: ".$this->config->path.'ajax/Shop.php?mode=cart');
  141. }
  142. elseif($_GET['op'] == 'buy')
  143. {
  144. if($this->balance >= $SumGoogs)
  145. {
  146.  
  147.  
  148. foreach($_SESSION['cart'] as $id => $value){
  149.  
  150. $this->db->query("SELECT * FROM __shop_googs WHERE id='".intval($id)."'");
  151. $row = $this->db->get_row();
  152.  
  153. $this->db->query("INSERT INTO __history SET
  154. title='Покупка в магазине',
  155. text='Вы успешно приобрели товар ".$row['name']." за ".$row['sum']." рублей',
  156. sum='".$row['sum']."',
  157. date='".date('d.m.Y, H:i')."',
  158. type='shop',
  159. user='".$this->username."'
  160. ");
  161.  
  162. #Для серверов
  163.  
  164. $this->db->query("SELECT table_shop FROM __servers WHERE id='{$row['server']}'");
  165. $row_s = $this->db->get_row();
  166.  
  167. $item_code = $row['item_id']." ".$value;
  168.  
  169. $this->db->query("INSERT INTO __{$row_s['table_shop']} SET
  170. sid='{$row['id']}',
  171. username='".$this->username."',
  172. iid='{$row['item_id']}',
  173. item='{$item_code}',
  174. title='{$row['name']}',
  175. amount='".$value."'
  176.  
  177.  
  178. ");
  179.  
  180.  
  181. #Для серверов END
  182.  
  183.  
  184.  
  185. }
  186.  
  187. $this->update_balance($this->username, '-', $SumGoogs);
  188. unset($_SESSION['cart']);
  189.  
  190. return 'err2';
  191. } else return 'err1';
  192. }
  193. else{
  194.  
  195.  
  196. $submit_buy = '
  197. <a href=\'/giveitem.php\' class=\'buy_submit\'>Подтвердить покупку</a>
  198. ';
  199.  
  200. $ajax = '
  201.  
  202. <script type="text/javascript">
  203. $(\'form.formcart\').submit(function(){
  204.  
  205. $.ajax({
  206. type: "POST",
  207. url: "'.$this->config->path.'ajax/Shop.php?mode=cart&op=buy",
  208. data: \'buy=true\',
  209. success: function(html){
  210. if(html == \'err1\') {
  211. $(\'#error_lk\').html("Не хватает денег");
  212. $(\'#error_lk\').css({\'display\' : \'block\'});
  213. }
  214. else if(html == \'err2\'){
  215. $(\'#error_lk_ok\').html("Покупка успешно совершена! Ожидайте, предмет вот вот появятся у вас в инвентаре!");
  216. var delay = 1000;
  217. setTimeout("document.location.href=\'http://cabinet.ru/giveitem.php\'", delay);
  218. $(\'#error_lk_ok\').css({\'display\' : \'block\'});
  219. $(\'#content_lk\').text(\'Корзина пуста\');
  220. }
  221. else {
  222. alert(html);
  223. }
  224. }
  225.  
  226. });
  227.  
  228. return false;
  229.  
  230. });
  231.  
  232.  
  233. function amounts(id){
  234.  
  235. amounts_new = $(\'#amount\'+id+\'\').val();
  236.  
  237. $.ajax({
  238. type: "POST",
  239. url: "'.$this->config->path.'ajax/Shop.php?mode=cart&op=amounts",
  240. data: \'amounts[\'+id+\']=\'+amounts_new+\'\',
  241.  
  242. });
  243.  
  244. loadContent(\''.$this->config->path.'ajax/Shop.php?mode=cart\');
  245.  
  246. return false;
  247.  
  248. }
  249. </script>
  250. ';
  251.  
  252.  
  253. $googs = $this->max_cart();
  254.  
  255.  
  256.  
  257. $this->design->load('shop_cart.html');
  258. $this->design->set('googs_count', $CountGoogs);
  259. $this->design->set('itemnada', $myrow['item']);
  260. $this->design->set('submit', $submit_buy);
  261. $this->design->set('sum', $SumGoogs);
  262. $this->design->set('ajax', $ajax);
  263. $this->design->set('googs', $googs);
  264. $this->design->set_block('cart_true', $_SESSION['cart'] != '');
  265. $this->design->set_block('cart_false', $_SESSION['cart'] == '');
  266. return $this->design->result();
  267.  
  268. }
  269. }
  270. else {
  271.  
  272. $googs = $this->googs();
  273. $category = $this->category();
  274. $cart = $this->mini_cart();
  275.  
  276. $ajax = '
  277. <script type="text/javascript">
  278. $(\'form.variants\').submit(function(){
  279. id = $(this).find(\'input[name=id]\').val();
  280.  
  281. $.ajax({
  282. type: "POST",
  283. url: "'.$this->config->path.'ajax/Shop.php?mode=minicart",
  284. data: \'id=\'+id+\'\',
  285. success: function(html){
  286. $(\'#cart_informer\').html(html);
  287. }
  288.  
  289. });
  290.  
  291. return false;
  292.  
  293. });
  294.  
  295. $(\'form.formsearch\').submit(function(){
  296. key = $(this).find(\'input[name=keyword]\').val();
  297.  
  298. $(\'#content_lk\').text(\'Загрузка...\');
  299.  
  300. $.ajax({
  301. type: "GET",
  302. url: "'.$this->config->path.'ajax/Shop.php?mode=search",
  303. data: \'keyword=\'+key+\'\',
  304. success: function(html){
  305. $(\'#content_lk\').html(html);
  306. }
  307.  
  308. });
  309.  
  310. return false;
  311.  
  312. });
  313.  
  314. </script>
  315. ';
  316.  
  317. $this->design->load('shop.html');
  318. $this->design->set('googs', $googs['googs']);
  319. $this->design->set('navigation', $googs['nav']);
  320. $this->design->set('ajax', $ajax);
  321. $this->design->set('cart', $cart);
  322. $this->design->set('category', $category);
  323. $this->design->set('title', $googs['title']);
  324. return $this->design->result();
  325. }
  326. }
  327.  
  328. private function googs(){
  329. ob_start();
  330.  
  331. if(!$_SESSION['server']) { echo "<div id=\"error_lk\" style=\"display:block;\">Сервер не выбран</div>"; die();}
  332.  
  333. $s_info = $this->server_info($_SESSION['server']);
  334.  
  335. if(isset($_GET['cat_id']) and $_GET['mode'] == 'category')
  336. {
  337.  
  338. $name = $this->db->get_row($this->db->query("SELECT name FROM __shop_category WHERE id='".intval($_GET['cat_id'])."'"));
  339. $name = $name['name'];
  340.  
  341. $nav = $this->navigation("__shop_googs WHERE category='".intval($_GET['cat_id'])."' AND server='".intval($_SESSION['server'])."'", $_GET['page'], $this->config->path.'ajax/Shop.php?mode=category&cat_id='.intval($_GET['cat_id']).'&page=', $this->config->limit_googs);
  342. $this->db->query("SELECT * FROM __shop_googs WHERE category='".intval($_GET['cat_id'])."' AND server='".intval($_SESSION['server'])."' ORDER BY id DESC LIMIT {$nav['start']}, {$nav['num']}");
  343.  
  344. $title = $this->title('Сервер &raquo; '.$s_info['name'].' &raquo; Магазин &raquo; Категория &raquo; '.$name);
  345. $error = 'Товара нет';
  346. }
  347. elseif(isset($_GET['keyword']) and $_GET['mode'] == 'search'){
  348. $nav = $this->navigation("__shop_googs WHERE server='".intval($_SESSION['server'])."' AND name LIKE '%".$this->db->safesql($_GET['keyword'])."%'", $_GET['page'], $this->config->path.'ajax/Shop.php?mode=search&keyword='.$_GET['keyword'].'&page=', $this->config->limit_googs);
  349. $this->db->query("SELECT * FROM __shop_googs WHERE server='".intval($_SESSION['server'])."' AND name LIKE '%".$this->db->safesql($_GET['keyword'])."%' ORDER BY id DESC LIMIT {$nav['start']}, {$nav['num']}");
  350.  
  351. $title = $this->title('Сервер &raquo; '.$s_info['name'].' &raquo; Магазин &raquo; Пойск ');
  352. $error = 'По запросу '.$_GET['keyword'].' не чего не найдено';
  353. }
  354. else{
  355. $nav = $this->navigation("__shop_googs WHERE server='".intval($_SESSION['server'])."'", $_GET['page'], $this->config->path.'ajax/Shop.php?page=', $this->config->limit_googs);
  356. $this->db->query("SELECT * FROM __shop_googs WHERE server='".intval($_SESSION['server'])."' ORDER BY id DESC LIMIT {$nav['start']}, {$nav['num']}");
  357.  
  358. $title = $this->title('Сервер &raquo; '.$s_info['name'].' &raquo; Магазин &raquo; Категория &raquo; Без категорий');
  359. $error = 'Товара нет';
  360. }
  361.  
  362. if($this->db->num_rows() > 0){
  363. while($row = $this->db->get_row()){
  364.  
  365. if(isset($_GET['keyword'])) $name = str_replace($_GET['keyword'], '<span style="background-color:#6FB3E0;">'.$_GET['keyword'].'</span>', $row['name']);
  366. else $name = $row['name'];
  367.  
  368. $this->design->load('shop_googs.html');
  369. $this->design->set('name', $name);
  370. $this->design->set('img', $row['img']);
  371. $this->design->set('sum', $row['sum']);
  372. $this->design->set('sum2', $row['sum']*64);
  373. $this->design->set('id', $row['id']);
  374. $this->design->set('server', $row['server']);
  375. $this->design->set('url', $this->link('Shop', '?mode=product&id='.$row['id']));
  376. echo $this->design->result();
  377. }
  378. } else echo $error;
  379. $tmp = ob_get_clean();
  380. return array('nav' => $nav['nav'], 'googs' => $tmp, 'title' => $title);
  381. }
  382.  
  383. private function category(){
  384. ob_start();
  385. $this->db->query("SELECT * FROM __shop_category WHERE server='".intval($_SESSION['server'])."' ORDER BY list ASC");
  386. while($row = $this->db->get_row()){
  387. $this->design->load('shop_category.html');
  388. $this->design->set('name', $row['name']);
  389. $this->design->set('url', $this->link('Shop', '?mode=category&cat_id='.$row['id']));
  390. echo $this->design->result();
  391. }
  392. return ob_get_clean();
  393. }
  394.  
  395. private function mini_cart(){
  396.  
  397. if($_SESSION['cart'] != '')
  398. {
  399. foreach($_SESSION['cart'] as $Id => $Value)
  400. {
  401. $this->db->query("SELECT sum FROM __shop_googs WHERE id='".intval($Id)."'");
  402. $row = $this->db->get_row();
  403.  
  404. if(!$row) unset($_SESSION['cart'][$Id]);
  405.  
  406. $CountGoogs += $Value;
  407. $SumGoogs += $Value*$row['sum'];
  408. }
  409.  
  410. }
  411.  
  412. $this->design->load('shop_cart_mini.html');
  413. $this->design->set('googs', $CountGoogs);
  414. $this->design->set('sum_googs', $SumGoogs);
  415. $this->design->set('url', $this->link('Shop', '?mode=cart'));
  416. $this->design->set_block('cart_true', $_SESSION['cart'] != '');
  417. $this->design->set_block('cart_false', $_SESSION['cart'] == '');
  418. return $this->design->result();
  419. }
  420.  
  421. private function max_cart(){
  422. ob_start();
  423. foreach($_SESSION['cart'] as $id => $value){
  424. $this->db->query("SELECT id,name,sum,img FROM __shop_googs WHERE id='".intval($id)."'");
  425. $row = $this->db->get_row();
  426. $this->design->load('shop_cart_googs.html');
  427. $this->design->set('delete_url', $this->link('Shop', '?mode=cart&op=delete&id='.$row['id'].''));
  428. $this->design->set('id', $row['id']);
  429. $this->design->set('name', $row['name']);
  430. $this->design->set('img', $row['img']);
  431. $this->design->set('sum', $row['sum']);
  432. $this->design->set('all_sum', $row['sum']*$value);
  433. $this->design->set('kol-vo', $value);
  434. $this->design->set('url', $this->link('Shop', '?mode=product&id='.$row['id']));
  435. echo $this->design->result();
  436. }
  437. return ob_get_clean();
  438. }
  439.  
  440. }
  441.  
  442. $view = new Shop;
  443. print $view->fetch();
  444.  
  445. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement