Advertisement
Guest User

Untitled

a guest
May 18th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. <?PHP
  2.  
  3. require_once('api/simpla.php');
  4.  
  5. class ProductsAdmin extends simpla
  6. {
  7. function fetch()
  8. {
  9.  
  10. $filter = array();
  11. $filter['page'] = max(1, $this->request->get('page', 'integer'));
  12.  
  13. $filter['limit'] = $this->settings->products_num_admin;
  14.  
  15. // Категории
  16. $categories = $this->categories->get_categories_tree();
  17. $this->design->assign('categories', $categories);
  18.  
  19. // Текущая категория
  20. $category_id = $this->request->get('category_id', 'integer');
  21. if($category_id && $category = $this->categories->get_category($category_id))
  22. $filter['category_id'] = $category->children;
  23.  
  24. // Бренды категории
  25. $brands = $this->brands->get_brands(array('category_id'=>$category_id));
  26. $this->design->assign('brands', $brands);
  27.  
  28. // Все бренды
  29. $all_brands = $this->brands->get_brands();
  30. $this->design->assign('all_brands', $all_brands);
  31.  
  32. // Текущий бренд
  33. $brand_id = $this->request->get('brand_id', 'integer');
  34. if($brand_id && $brand = $this->brands->get_brand($brand_id))
  35. $filter['brand_id'] = $brand->id;
  36.  
  37. // Текущий фильтр
  38. if($f = $this->request->get('filter', 'string'))
  39. {
  40. if($f == 'featured')
  41. $filter['featured'] = 1;
  42. elseif($f == 'discounted')
  43. $filter['discounted'] = 1;
  44. elseif($f == 'visible')
  45. $filter['visible'] = 1;
  46. elseif($f == 'hidden')
  47. $filter['visible'] = "00";
  48. $this->design->assign('filter', $f);
  49. }
  50.  
  51. // Поиск
  52. $keyword = $this->request->get('keyword');
  53. if(!empty($keyword))
  54. {
  55. $filter['keyword'] = $keyword;
  56. $this->design->assign('keyword', $keyword);
  57. }
  58.  
  59. // Обработка действий
  60. if($this->request->method('post'))
  61. {
  62. // Сохранение цен и наличия
  63. $prices = $this->request->post('price');
  64. $stocks = $this->request->post('stock');
  65.  
  66. foreach($prices as $id=>$price)
  67. {
  68. $stock = $stocks[$id];
  69. if($stock == '∞' || $stock == '')
  70. $stock = null;
  71.  
  72. $this->variants->update_variant($id, array('price'=>$price, 'stock'=>$stock));
  73. }
  74.  
  75. // Сортировка
  76. $positions = $this->request->post('positions');
  77. $ids = array_keys($positions);
  78. sort($positions);
  79. $positions = array_reverse($positions);
  80. foreach($positions as $i=>$position)
  81. $this->products->update_product($ids[$i], array('position'=>$position));
  82.  
  83.  
  84. // Действия с выбранными
  85. $ids = $this->request->post('check');
  86. if(!empty($ids))
  87. switch($this->request->post('action'))
  88. {
  89. case 'disable':
  90. {
  91. $this->products->update_product($ids, array('visible'=>0));
  92. break;
  93. }
  94. case 'enable':
  95. {
  96. $this->products->update_product($ids, array('visible'=>1));
  97. break;
  98. }
  99. case 'set_featured':
  100. {
  101. $this->products->update_product($ids, array('featured'=>1));
  102. break;
  103. }
  104. case 'unset_featured':
  105. {
  106. $this->products->update_product($ids, array('featured'=>0));
  107. break;
  108. }
  109. case 'delete':
  110. {
  111. foreach($ids as $id)
  112. $this->products->delete_product($id);
  113. break;
  114. }
  115. case 'duplicate':
  116. {
  117. foreach($ids as $id)
  118. $this->products->duplicate_product(intval($id));
  119. break;
  120. }
  121. case 'move_to_page':
  122. {
  123.  
  124. $target_page = $this->request->post('target_page', 'integer');
  125.  
  126. // Сразу потом откроем эту страницу
  127. $filter['page'] = $target_page;
  128.  
  129. // До какого товара перемещать
  130. $limit = $filter['limit']*($target_page-1);
  131. if($target_page > $this->request->get('page', 'integer'))
  132. $limit += count($ids)-1;
  133. else
  134. $ids = array_reverse($ids, true);
  135.  
  136.  
  137. $temp_filter = $filter;
  138. $temp_filter['page'] = $limit+1;
  139. $temp_filter['limit'] = 1;
  140. $target_product = array_pop($this->products->get_products($temp_filter));
  141. $target_position = $target_product->position;
  142.  
  143. // Если вылезли за последний товар - берем позицию последнего товара в качестве цели перемещения
  144. if($target_page > $this->request->get('page', 'integer') && !$target_position)
  145. {
  146. $query = $this->db->placehold("SELECT distinct p.position AS target FROM __products p LEFT JOIN __products_categories AS pc ON pc.product_id = p.id WHERE 1 $category_id_filter $brand_id_filter ORDER BY p.position DESC LIMIT 1", count($ids));
  147. $this->db->query($query);
  148. $target_position = $this->db->result('target');
  149. }
  150.  
  151. foreach($ids as $id)
  152. {
  153. $query = $this->db->placehold("SELECT position FROM __products WHERE id=? LIMIT 1", $id);
  154. $this->db->query($query);
  155. $initial_position = $this->db->result('position');
  156.  
  157. if($target_position > $initial_position)
  158. $query = $this->db->placehold(" UPDATE __products set position=position-1 WHERE position>? AND position<=?", $initial_position, $target_position);
  159. else
  160. $query = $this->db->placehold(" UPDATE __products set position=position+1 WHERE position<? AND position>=?", $initial_position, $target_position);
  161.  
  162. $this->db->query($query);
  163. $query = $this->db->placehold("UPDATE __products SET __products.position = ? WHERE __products.id = ?", $target_position, $id);
  164. $this->db->query($query);
  165. }
  166. break;
  167. }
  168. case 'move_to_category':
  169. {
  170. $category_id = $this->request->post('target_category', 'integer');
  171. $filter['page'] = 1;
  172. $category = $this->categories->get_category($category_id);
  173. $filter['category_id'] = $category->children;
  174.  
  175. foreach($ids as $id)
  176. {
  177. $query = $this->db->placehold("DELETE FROM __products_categories WHERE category_id=? AND product_id=? LIMIT 1", $category_id, $id);
  178. $this->db->query($query);
  179. $query = $this->db->placehold("UPDATE IGNORE __products_categories set category_id=? WHERE product_id=? ORDER BY position DESC LIMIT 1", $category_id, $id);
  180. $this->db->query($query);
  181. if($this->db->affected_rows() == 0)
  182. $query = $this->db->query("INSERT IGNORE INTO __products_categories set category_id=?, product_id=?", $category_id, $id);
  183.  
  184. }
  185. break;
  186. }
  187. case 'move_to_brand':
  188. {
  189. $brand_id = $this->request->post('target_brand', 'integer');
  190. $brand = $this->brands->get_brand($brand_id);
  191. $filter['page'] = 1;
  192. $filter['brand_id'] = $brand_id;
  193. $query = $this->db->placehold("UPDATE __products set brand_id=? WHERE id in (?@)", $brand_id, $ids);
  194. $this->db->query($query);
  195.  
  196. // Заново выберем бренды категории
  197. $brands = $this->brands->get_brands(array('category_id'=>$category_id));
  198. $this->design->assign('brands', $brands);
  199.  
  200. break;
  201. }
  202. }
  203. }
  204.  
  205. // Отображение
  206. if(isset($brand))
  207. $this->design->assign('brand', $brand);
  208. if(isset($category))
  209. $this->design->assign('category', $category);
  210.  
  211. $products_count = $this->products->count_products($filter);
  212.  
  213. $pages_count = ceil($products_count/$filter['limit']);
  214. $filter['page'] = min($filter['page'], $pages_count);
  215. $this->design->assign('products_count', $products_count);
  216. $this->design->assign('pages_count', $pages_count);
  217. $this->design->assign('current_page', $filter['page']);
  218.  
  219. $products = array();
  220. foreach($this->products->get_products($filter) as $p)
  221. $products[$p->id] = $p;
  222.  
  223.  
  224. if(!empty($products))
  225. {
  226.  
  227. // Товары
  228. $products_ids = array_keys($products);
  229. foreach($products as &$product)
  230. {
  231. $product->variants = array();
  232. $product->images = array();
  233. $product->properties = array();
  234. }
  235.  
  236. $variants = $this->variants->get_variants(array('product_id'=>$products_ids));
  237.  
  238.  
  239. foreach($variants as &$variant)
  240. {
  241. $products[$variant->product_id]->variants[] = $variant;
  242. }
  243.  
  244. $images = $this->products->get_images(array('product_id'=>$products_ids));
  245. foreach($images as $image)
  246. $products[$image->product_id]->images[$image->id] = $image;
  247. }
  248.  
  249. $this->design->assign('products', $products);
  250.  
  251. return $this->design->fetch('products.tpl');
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement